You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/client.md
+137Lines changed: 137 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -215,6 +215,143 @@ The `get_display_name()` function implements the proper precedence rules for dis
215
215
216
216
This ensures your client UI shows the most user-friendly names that servers provide.
217
217
218
+
## SSE Transport (Legacy)
219
+
220
+
For servers that only support the older SSE transport, use the `sse_client()` context manager from `mcp.client.sse`:
221
+
222
+
```python
223
+
import asyncio
224
+
225
+
from mcp import ClientSession
226
+
from mcp.client.sse import sse_client
227
+
228
+
229
+
asyncdefmain():
230
+
# Connect to an SSE server
231
+
asyncwith sse_client(
232
+
url="http://localhost:8000/sse",
233
+
headers={"Authorization": "Bearer token"},
234
+
timeout=5,
235
+
sse_read_timeout=300, # 5 minutes
236
+
) as (read_stream, write_stream):
237
+
asyncwith ClientSession(read_stream, write_stream) as session:
238
+
await session.initialize()
239
+
tools =await session.list_tools()
240
+
print(f"Available tools: {[t.name for t in tools.tools]}")
241
+
242
+
243
+
if__name__=="__main__":
244
+
asyncio.run(main())
245
+
```
246
+
247
+
The `sse_client()` accepts:
248
+
249
+
-`url` - The SSE endpoint URL
250
+
-`headers` - Optional HTTP headers
251
+
-`timeout` - HTTP timeout for regular operations (default: 5 seconds)
252
+
-`sse_read_timeout` - Timeout for SSE read operations (default: 5 minutes)
253
+
-`auth` - Optional `httpx.Auth` handler
254
+
-`httpx_client_factory` - Optional factory for customizing the HTTP client
255
+
256
+
Note: SSE transport is legacy. Prefer [Streamable HTTP transport](https://modelcontextprotocol.io/specification/2025-03-26/basic/transports#streamable-http) for new implementations.
257
+
258
+
## Roots
259
+
260
+
### Listing Roots
261
+
262
+
To allow the server to query the client's root URIs, provide a `list_roots_callback` when creating the `ClientSession`. The callback receives a `RequestContext[ClientSession, Any]` and returns a `ListRootsResult`:
263
+
264
+
```python
265
+
import asyncio
266
+
267
+
from mcp import ClientSession, StdioServerParameters, types
# Log messages from the server will be passed to handle_log
342
+
```
343
+
344
+
### Setting Server Log Level
345
+
346
+
Request the server to change its minimum logging level:
347
+
348
+
```python
349
+
# Ask the server to only send warning-level and above
350
+
await session.set_logging_level("warning")
351
+
```
352
+
353
+
The `set_logging_level()` method sends a `logging/setLevel` request. The server decides how to handle this (see [Setting Log Level](server.md#setting-log-level) for the server side).
354
+
218
355
## OAuth Authentication for Clients
219
356
220
357
For OAuth 2.1 client authentication, see [Authorization](authorization.md).
Ping requests are always allowed, even before initialization is complete.
53
+
54
+
## Cancellation
55
+
56
+
Either side can cancel a previously-issued request by sending a `CancelledNotification`. This is useful for long-running operations:
57
+
58
+
```python
59
+
from mcp.types import CancelledNotification, CancelledNotificationParams
60
+
61
+
# Build a cancellation notification for a specific request
62
+
notification = CancelledNotification(
63
+
params=CancelledNotificationParams(
64
+
requestId="request-123",
65
+
reason="User cancelled the operation",
66
+
),
67
+
)
68
+
```
69
+
70
+
The `CancelledNotificationParams` fields:
71
+
72
+
-`requestId` - The ID of the request to cancel (optional in the type, but should be provided)
73
+
-`reason` - A human-readable reason for cancellation (optional)
74
+
75
+
Both `ClientNotification` and `ServerNotification` include `CancelledNotification` as a valid notification type.
76
+
77
+
## Capability Negotiation
78
+
79
+
During initialization, the client and server exchange their supported capabilities. The Python SDK handles this automatically based on the callbacks you provide.
80
+
81
+
### Client-Side Auto-Declaration
82
+
83
+
The `ClientSession` automatically declares capabilities based on which callbacks are provided:
84
+
85
+
```python
86
+
from mcp import ClientSession, types
87
+
from mcp.shared.context import RequestContext
88
+
89
+
90
+
# Providing a sampling_callback automatically declares sampling capability
1. The client sends `LATEST_PROTOCOL_VERSION` as its requested version
157
+
2. The server checks if that version is in its `SUPPORTED_PROTOCOL_VERSIONS`
158
+
3. If supported, the server echoes it back; otherwise it responds with its own `LATEST_PROTOCOL_VERSION`
159
+
4. The client verifies the server's chosen version is in its own `SUPPORTED_PROTOCOL_VERSIONS`
160
+
5. If the versions are incompatible, the client raises a `RuntimeError`
161
+
162
+
## JSON Schema Generation
163
+
164
+
The SDK automatically generates [JSON Schema (2020-12)](https://json-schema.org/specification) from Python type annotations via Pydantic. This is used for tool input schemas, structured output schemas, and elicitation schemas:
"""Search with auto-generated JSON schema for the input."""
183
+
returnf"Searching for: {params.query}"
184
+
```
185
+
186
+
The tool's `inputSchema` is automatically derived from the function's parameter type annotations. Pydantic models, `TypedDict`, dataclasses, and primitive types are all supported. The generated schema includes field descriptions, defaults, types, and validation constraints.
0 commit comments