Skip to content

Commit fbfa594

Browse files
committed
Add docs
1 parent d6d3809 commit fbfa594

File tree

4 files changed

+351
-1
lines changed

4 files changed

+351
-1
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- [Advanced Usage](#advanced-usage)
4242
- [Low-Level Server](#low-level-server)
4343
- [Writing MCP Clients](#writing-mcp-clients)
44+
- [API for Client Configuration](#api-for-client-configuration)
4445
- [MCP Primitives](#mcp-primitives)
4546
- [Server Capabilities](#server-capabilities)
4647
- [Documentation](#documentation)
@@ -862,6 +863,11 @@ async def main():
862863

863864
For a complete working example, see [`examples/clients/simple-auth-client/`](examples/clients/simple-auth-client/).
864865

866+
### API for Client Configuration
867+
868+
The MCP Python SDK provides an API for client configuration. This lets client applications easily load MCP servers from configuration files in a variety of formats and with some useful, built-in features.
869+
870+
See the [Client Configuration Guide](docs/client-configuration.md) for complete details.
865871

866872
### MCP Primitives
867873

docs/client-configuration.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
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+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ site_url: https://modelcontextprotocol.github.io/python-sdk
1212

1313
nav:
1414
- Home: index.md
15+
- Client Configuration: client-configuration.md
1516
- API Reference: api.md
1617

1718
theme:

src/mcp/client/config/mcp_servers_config.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,47 @@
1-
"""Configuration management for MCP servers."""
1+
"""Configuration management for MCP servers.
2+
3+
This module provides comprehensive configuration management for MCP (Model
4+
Context Protocol) servers, supporting multiple file formats and advanced
5+
features:
6+
7+
Features:
8+
9+
- Multiple file formats: JSON, JSONC (JSON with comments), and YAML (.yaml/.yml)
10+
11+
- Automatic server type inference based on configuration fields
12+
13+
- Input variable substitution with ${input:key} syntax and validation of
14+
required inputs, borrowed from VS Code
15+
16+
- Support for both 'mcpServers' and 'servers' (VS Code) field names
17+
18+
Supported Server Types:
19+
20+
- streamable_http: HTTP-based servers using streamable transport
21+
22+
- stdio: Servers that communicate via standard input/output
23+
24+
- sse: Server-Sent Events based servers
25+
26+
Example usage:
27+
28+
# Load basic configuration
29+
config = MCPServersConfig.from_file("config.json")
30+
31+
# Load YAML with input substitution
32+
config = MCPServersConfig.from_file(
33+
"config.yaml",
34+
inputs={"api-key": "secret", "host": "api.example.com"}
35+
)
36+
37+
# Validate inputs
38+
missing = config.validate_inputs(provided_inputs)
39+
if missing:
40+
raise ValueError(f"Missing inputs: {missing}")
41+
42+
Dependencies:
43+
- PyYAML: Required for YAML file support (install with 'mcp[yaml]')
44+
"""
245

346
# stdlib imports
447
import json

0 commit comments

Comments
 (0)