Skip to content

Commit 8c3649b

Browse files
committed
add new tool
1 parent 254927f commit 8c3649b

File tree

3 files changed

+228
-117
lines changed

3 files changed

+228
-117
lines changed

src/McpServer/README.md

Lines changed: 89 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,102 @@
11
# PowerToys Model Context Protocol Server
22

3-
This module hosts a standalone Model Context Protocol (MCP) server that exposes PowerToys functionality to MCP-compliant AI agents. The first release focuses on Awake, enabling agents to query status and toggle keep-awake modes without going through the PowerToys Runner process. The server is designed to grow over time with additional modules.
3+
This module hosts a standalone Model Context Protocol (MCP) server that exposes PowerToys functionality to MCP-compliant AI agents. The server is built as a .NET 9 console application that implements the MCP specification using the official ModelContextProtocol SDK.
44

5-
## Capabilities
5+
## Project Structure
66

7-
| Tool name | Description | Module |
8-
|-----------|-------------|--------|
9-
| `awake_status` | Returns the current Awake configuration (mode, timers, display policy). | Awake |
10-
| `awake_set_mode` | Switches Awake between passive, indefinite, timed, or expirable modes using the PowerToys settings pipeline. | Awake |
7+
- **Program.cs**: Main entry point that configures the MCP server with stdio transport
8+
- **Tools/AwakeTools.cs**: Implementation of Awake-related MCP tools
9+
- **PowerToys.McpServer.csproj**: .NET 9 project configuration with MCP dependencies
1110

12-
## Running the server
11+
## Dependencies
1312

14-
The executable lives next to other module binaries (for example, `PowerToys.McpServer.exe` under the architecture-specific output folder). The server communicates over standard input/output using MCP framing (`Content-Length` header followed by JSON). A minimal session looks like:
13+
- **Microsoft.Extensions.Hosting**: For hosting infrastructure and dependency injection
14+
- **ModelContextProtocol**: Official MCP SDK for .NET
15+
- **PowerToys Settings Library**: Integration with PowerToys settings system
16+
- **ManagedCommon**: PowerToys logging and utilities
1517

16-
1. Client sends `initialize` request.
17-
2. Client calls `tools/list` to discover available tools.
18-
3. Client invokes `tools/call` with the desired tool name and arguments.
18+
## Available Tools
19+
20+
| Tool Name | Description | Parameters | Module |
21+
|-----------|-------------|------------|--------|
22+
| `GetAwakeStatus` | Returns the current Awake configuration (mode, timers, display policy) | None | Awake |
23+
| `SetAwakePassive` | Set Awake to passive mode (allow system to sleep normally) | None | Awake |
24+
| `SetAwakeIndefinite` | Set Awake to indefinite mode (keep system awake until manually changed) | `keepDisplayOn` (bool), `force` (bool) | Awake |
25+
| `SetAwakeTimed` | Set Awake to timed mode (keep system awake for a specific duration) | `durationSeconds` (int), `keepDisplayOn` (bool), `force` (bool) | Awake |
26+
27+
## Building and Running
28+
29+
### Prerequisites
30+
- .NET 9 SDK
31+
- Visual Studio 2022 (recommended) or VS Code with C# extension
32+
33+
### Build
34+
```bash
35+
# From PowerToys root directory
36+
msbuild src/McpServer/PowerToys.McpServer.csproj /p:Platform=x64 /p:Configuration=Debug
37+
38+
# Or using dotnet CLI
39+
cd src/McpServer
40+
dotnet build -c Debug
41+
```
42+
43+
### Run the Server
44+
The executable is built to `x64\Debug\PowerToys.McpServer.exe`. The server communicates over standard input/output using MCP framing (`Content-Length` header followed by JSON).
45+
46+
**Example MCP Client Session:**
47+
1. Client sends `initialize` request with MCP version and capabilities
48+
2. Client calls `tools/list` to discover available PowerToys tools
49+
3. Client invokes `tools/call` with the desired tool name and arguments
50+
4. Server responds with tool execution results or errors
1951

2052
The server will remain active until the process is terminated or a `shutdown` request is received.
2153

22-
## Adding new module tools
54+
### Logging
55+
- Application logs are written to `%LOCALAPPDATA%\Microsoft\PowerToys\McpServer\Logs\`
56+
- MCP protocol logs are sent to stderr (required by MCP specification)
57+
58+
## Architecture
59+
60+
The server uses the official ModelContextProtocol .NET SDK and follows these patterns:
61+
62+
- **Tool Discovery**: Tools are automatically discovered using `WithToolsFromAssembly()`
63+
- **Tool Attributes**: Methods marked with `[McpServerTool]` and `[Description]` are exposed as MCP tools
64+
- **Parameter Binding**: Method parameters are automatically bound from MCP tool call arguments
65+
- **Error Handling**: Exceptions are caught and returned as MCP error responses
66+
- **Settings Integration**: Uses PowerToys settings system for configuration persistence
67+
68+
## Adding New Module Tools
69+
70+
1. Create a new static class in the `Tools/` directory (e.g., `FancyZonesTools.cs`)
71+
2. Mark the class with `[McpServerToolType]` attribute
72+
3. Implement static methods with `[McpServerTool]` and `[Description]` attributes:
73+
```csharp
74+
[McpServerToolType]
75+
public static class MyModuleTools
76+
{
77+
[McpServerTool]
78+
[Description("Description of what this tool does")]
79+
public static JsonObject MyTool(
80+
[Description("Parameter description")] string parameter)
81+
{
82+
// Implementation here
83+
return new JsonObject();
84+
}
85+
}
86+
```
87+
4. Follow existing patterns in `AwakeTools.cs` for:
88+
- Settings integration using `SettingsUtils`
89+
- Logging using `Logger.LogInfo/LogError`
90+
- Error handling and response formatting
91+
- PowerToys process detection and module status checks
92+
93+
## Integration with PowerToys
94+
95+
The MCP server integrates with PowerToys through:
2396

24-
1. Implement an `IMcpModule` that exposes one or more `IMcpTool` instances.
25-
2. Register the module in `Program.cs` via `ModuleCatalog.RegisterModule`.
26-
3. Use `Awake` implementations as reference for schema design, telemetry, and settings integration.
27-
4. Update documentation and packaging (signing lists, installer termination lists, verification scripts) if new executables are introduced.
97+
- **Settings System**: Uses the same settings files as the main PowerToys application
98+
- **Process Management**: Detects and interacts with running PowerToys processes
99+
- **Module Status**: Checks if specific PowerToys modules are enabled
100+
- **Logging**: Uses PowerToys logging infrastructure for troubleshooting
28101

29102
Refer to the PowerToys developer documentation for build and packaging instructions.

0 commit comments

Comments
 (0)