|
| 1 | +# MCP Client Configuration (NEW) |
| 2 | + |
| 3 | +This guide covers how to configure MCP servers for client applications, |
| 4 | +such as Claude Desktop, Cursor, and VS Code. |
| 5 | + |
| 6 | +## Configuration File Formats |
| 7 | + |
| 8 | +MCP supports multiple configuration file formats for maximum flexibility. |
| 9 | + |
| 10 | +### JSON Configuration |
| 11 | + |
| 12 | +```json |
| 13 | +{ |
| 14 | + "mcpServers": { |
| 15 | + "time": { |
| 16 | + "command": "uvx", |
| 17 | + "args": ["mcp-server-time"] |
| 18 | + }, |
| 19 | + "filesystem": { |
| 20 | + "command": "npx", |
| 21 | + "args": ["-y", "@modelcontextprotocol/server-filesystem", "/Users/username/Desktop"] |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +This is a typical JSON configuration file for an MCP server in that it has |
| 28 | +`command` and `args` (as a list) fields. |
| 29 | + |
| 30 | +Also supported is to specify the entire command in the `command` field (easier |
| 31 | +to write and read). In this case, the library will automatically split the |
| 32 | +command into `command` and `args` fields internally. |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "mcpServers": { |
| 37 | + "time": { |
| 38 | + "command": "uvx mcp-server-time" |
| 39 | + }, |
| 40 | + "filesystem": { |
| 41 | + "command": "npx -y @modelcontextprotocol/server-filesystem /Users/username/Desktop" |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +JSON is the most commonly used format for MCP servers, but it has some |
| 48 | +limitations, which is why subsequent sections cover other formats, such as JSONC |
| 49 | +and YAML. |
| 50 | + |
| 51 | +### JSON with Comments (JSONC) |
| 52 | + |
| 53 | +For better maintainability, MCP supports JSON files with `//` comments: |
| 54 | + |
| 55 | +```jsonc |
| 56 | +{ |
| 57 | + "mcpServers": { |
| 58 | + // Can get current time in various timezones |
| 59 | + "time": { |
| 60 | + "command": "uvx mcp-server-time" |
| 61 | + }, |
| 62 | + |
| 63 | + // Can get the contents of the user's desktop |
| 64 | + "filesystem": { |
| 65 | + "command": "npx -y @modelcontextprotocol/server-filesystem /Users/username/Desktop" |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### YAML Configuration |
| 72 | + |
| 73 | +YAML configuration files are supported for improved readability: |
| 74 | + |
| 75 | +```yaml |
| 76 | +mcpServers: |
| 77 | + # Can get current time in various timezones |
| 78 | + time: |
| 79 | + command: uvx mcp-server-time |
| 80 | + |
| 81 | + # Can get the contents of the user's desktop |
| 82 | + filesystem: |
| 83 | + command: npx -y @modelcontextprotocol/server-filesystem /Users/username/Desktop |
| 84 | +``` |
| 85 | +
|
| 86 | +**Installation**: YAML support requires the optional dependency: |
| 87 | +
|
| 88 | +```bash |
| 89 | +pip install "mcp[yaml]" |
| 90 | +``` |
| 91 | + |
| 92 | +## Server Types and Auto-Detection |
| 93 | + |
| 94 | +MCP automatically infers server types based on configuration fields when the |
| 95 | +`type` field is omitted: |
| 96 | + |
| 97 | +### Stdio Servers |
| 98 | + |
| 99 | +Servers with a `command` field are automatically detected as `stdio` type: |
| 100 | + |
| 101 | +```yaml |
| 102 | +mcpServers: |
| 103 | + python-server: |
| 104 | + command: python -m my_server |
| 105 | + # type: stdio (auto-inferred) |
| 106 | +``` |
| 107 | + |
| 108 | +### Streamable HTTP Servers |
| 109 | + |
| 110 | +Servers with a `url` field (without SSE keywords) are detected as `streamable_http` type: |
| 111 | + |
| 112 | +```yaml |
| 113 | +mcpServers: |
| 114 | + api-server: |
| 115 | + url: https://api.example.com/mcp |
| 116 | + # type: streamable_http (auto-inferred) |
| 117 | +``` |
| 118 | + |
| 119 | +### SSE Servers |
| 120 | + |
| 121 | +Servers with a `url` field containing "sse" in the URL, name, or description are detected as `sse` type: |
| 122 | + |
| 123 | +```yaml |
| 124 | +mcpServers: |
| 125 | + sse-server: |
| 126 | + url: https://api.example.com/sse |
| 127 | + # type: sse (auto-inferred due to "sse" in URL) |
| 128 | + |
| 129 | + event-server: |
| 130 | + url: https://api.example.com/events |
| 131 | + description: "SSE-based event server" |
| 132 | + # type: sse (auto-inferred due to "SSE" in description) |
| 133 | +``` |
| 134 | + |
| 135 | +## Input Variables and Substitution |
| 136 | + |
| 137 | +MCP supports dynamic configuration using input variables, which is a feature |
| 138 | +that VS Code supports. This works in both JSON and YAML configurations. |
| 139 | + |
| 140 | +### Defining Inputs |
| 141 | + |
| 142 | +```yaml |
| 143 | +inputs: |
| 144 | + - id: api-key |
| 145 | + type: promptString |
| 146 | + description: "Your API key" |
| 147 | + password: true |
| 148 | + - id: server-host |
| 149 | + type: promptString |
| 150 | + description: "Server hostname" |
| 151 | + |
| 152 | +mcpServers: |
| 153 | + dynamic-server: |
| 154 | + url: https://${input:server-host}/mcp |
| 155 | + headers: |
| 156 | + Authorization: Bearer ${input:api-key} |
| 157 | +``` |
| 158 | +
|
| 159 | +### Using Inputs |
| 160 | +
|
| 161 | +When loading the configuration, provide input values: |
| 162 | +
|
| 163 | +```python |
| 164 | +from mcp.client.config.mcp_servers_config import MCPServersConfig |
| 165 | + |
| 166 | +# Load with input substitution |
| 167 | +config = MCPServersConfig.from_file( |
| 168 | + "config.yaml", |
| 169 | + inputs={ |
| 170 | + "api-key": "secret-key-123", |
| 171 | + "server-host": "api.example.com" |
| 172 | + } |
| 173 | +) |
| 174 | +``` |
| 175 | + |
| 176 | +### Input Validation |
| 177 | + |
| 178 | +MCP validates that all required inputs are provided: |
| 179 | + |
| 180 | +```python |
| 181 | +# Check required inputs |
| 182 | +required_inputs = config.get_required_inputs() |
| 183 | +print(f"Required inputs: {required_inputs}") |
| 184 | + |
| 185 | +# Validate provided inputs |
| 186 | +missing_inputs = config.validate_inputs(provided_inputs) |
| 187 | +if missing_inputs: |
| 188 | + print(f"Missing required inputs: {missing_inputs}") |
| 189 | +``` |
| 190 | + |
| 191 | +## Configuration Schema |
| 192 | + |
| 193 | +### Server Configuration Base Fields |
| 194 | + |
| 195 | +All server types support these common fields: |
| 196 | + |
| 197 | +- `name` (string, optional): Display name for the server |
| 198 | +- `description` (string, optional): Server description |
| 199 | +- `isActive` (boolean, default: true): Whether the server is active |
| 200 | + |
| 201 | +### Stdio Server Configuration |
| 202 | + |
| 203 | +```yaml |
| 204 | +mcpServers: |
| 205 | + stdio-server: |
| 206 | + type: stdio # Optional if 'command' is present |
| 207 | + command: python -m my_server |
| 208 | + args: # Optional additional arguments |
| 209 | + - --debug |
| 210 | + - --port=8080 |
| 211 | + env: # Optional environment variables |
| 212 | + DEBUG: "true" |
| 213 | + API_KEY: secret123 |
| 214 | +``` |
| 215 | +
|
| 216 | +### Streamable HTTP Server Configuration |
| 217 | +
|
| 218 | +```yaml |
| 219 | +mcpServers: |
| 220 | + http-server: |
| 221 | + type: streamable_http # Optional if 'url' is present |
| 222 | + url: https://api.example.com/mcp |
| 223 | + headers: # Optional HTTP headers |
| 224 | + Authorization: Bearer token123 |
| 225 | + X-Custom-Header: value |
| 226 | +``` |
| 227 | +
|
| 228 | +### SSE Server Configuration |
| 229 | +
|
| 230 | +```yaml |
| 231 | +mcpServers: |
| 232 | + sse-server: |
| 233 | + type: sse |
| 234 | + url: https://api.example.com/sse |
| 235 | + headers: # Optional HTTP headers |
| 236 | + Authorization: Bearer token123 |
| 237 | +``` |
| 238 | +
|
| 239 | +## Field Aliases |
| 240 | +
|
| 241 | +MCP supports both traditional and modern field names: |
| 242 | +
|
| 243 | +- `mcpServers` (most common) or `servers` (VS Code) |
| 244 | + |
| 245 | +```yaml |
| 246 | +# More common format |
| 247 | +mcpServers: |
| 248 | + my-server: |
| 249 | + command: python -m server |
| 250 | +
|
| 251 | +# VS Code format (equivalent) |
| 252 | +servers: |
| 253 | + my-server: |
| 254 | + command: python -m server |
| 255 | +``` |
| 256 | + |
| 257 | +## Loading Configuration Files |
| 258 | + |
| 259 | +```python |
| 260 | +from mcp.client.config.mcp_servers_config import MCPServersConfig |
| 261 | +from pathlib import Path |
| 262 | +
|
| 263 | +# Load JSON |
| 264 | +config = MCPServersConfig.from_file("config.json") |
| 265 | +
|
| 266 | +# Load YAML (auto-detected by extension) |
| 267 | +config = MCPServersConfig.from_file("config.yaml") |
| 268 | +
|
| 269 | +# Force YAML parsing |
| 270 | +config = MCPServersConfig.from_file("config.json", use_pyyaml=True) |
| 271 | +
|
| 272 | +# Load with input substitution |
| 273 | +config = MCPServersConfig.from_file( |
| 274 | + "config.yaml", |
| 275 | + inputs={"api-key": "secret"} |
| 276 | +) |
| 277 | +``` |
| 278 | + |
| 279 | +## Error Handling |
| 280 | + |
| 281 | +### Missing YAML Dependency |
| 282 | + |
| 283 | +```python |
| 284 | +try: |
| 285 | + config = MCPServersConfig.from_file("config.yaml") |
| 286 | +except ImportError as e: |
| 287 | + print("Install YAML support: pip install 'mcp[yaml]'") |
| 288 | +``` |
| 289 | + |
| 290 | +### Missing Input Values |
| 291 | + |
| 292 | +```python |
| 293 | +try: |
| 294 | + config = MCPServersConfig.from_file("config.yaml", inputs={}) |
| 295 | +except ValueError as e: |
| 296 | + print(f"Configuration error: {e}") |
| 297 | + # Error: Missing required input values: |
| 298 | + # - api-key: Your API key |
| 299 | + # - server-host: Server hostname |
| 300 | +``` |
0 commit comments