docs: add snippet verification for docs/ pages (#2115) · modelcontextprotocol/python-sdk@1ef124e · GitHub
Skip to content

Commit 1ef124e

Browse files
docs: add snippet verification for docs/ pages (#2115)
1 parent cfbbd7d commit 1ef124e

26 files changed

Lines changed: 446 additions & 43 deletions

‎.github/workflows/shared.yml‎

Lines changed: 3 additions & 3 deletions

‎.pre-commit-config.yaml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ repos:
5555
language: system
5656
files: ^(pyproject\.toml|uv\.lock)$
5757
pass_filenames: false
58-
- id: readme-snippets
59-
name: Check README snippets are up to date
60-
entry: uv run --frozen python scripts/update_readme_snippets.py --check
58+
- id: doc-snippets
59+
name: Check doc snippets are up to date
60+
entry: uv run --frozen python scripts/update_doc_snippets.py --check
6161
language: system
62-
files: ^(README\.md|examples/.*\.py|scripts/update_readme_snippets\.py)$
62+
files: ^(README\.md|docs/.*\.md|examples/.*\.py|scripts/update_doc_snippets\.py)$
6363
pass_filenames: false

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ if __name__ == "__main__":
131131
mcp.run(transport="streamable-http")
132132
```
133133

134-
_Full example: [examples/snippets/servers/fastmcp_quickstart.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/servers/fastmcp_quickstart.py)_
134+
_Full example: [examples/snippets/servers/fastmcp_quickstart.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/fastmcp_quickstart.py)_
135135
<!-- /snippet-source -->
136136

137137
You can install this server in [Claude Code](https://docs.claude.com/en/docs/claude-code/mcp) and interact with it right away. First, run the server:

‎docs/client.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ For OAuth 2.1 client authentication, see [Authorization](authorization.md#client
225225

226226
Clients can provide a `list_roots_callback` so that servers can discover the client's workspace roots (directories, project folders, etc.):
227227

228+
<!-- snippet-source examples/snippets/clients/roots_example.py -->
228229
```python
229230
from mcp import ClientSession, types
230231
from mcp.shared.context import RequestContext
@@ -250,6 +251,9 @@ session = ClientSession(
250251
)
251252
```
252253

254+
_Full example: [examples/snippets/clients/roots_example.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/clients/roots_example.py)_
255+
<!-- /snippet-source -->
256+
253257
When a `list_roots_callback` is provided, the client automatically declares the `roots` capability (with `listChanged=True`) during initialization.
254258

255259
### Roots Change Notifications
@@ -265,6 +269,7 @@ await session.send_roots_list_changed()
265269

266270
For servers that use the older SSE transport, use `sse_client()` from `mcp.client.sse`:
267271

272+
<!-- snippet-source examples/snippets/clients/sse_client.py -->
268273
```python
269274
import asyncio
270275

@@ -284,6 +289,9 @@ async def main():
284289
asyncio.run(main())
285290
```
286291

292+
_Full example: [examples/snippets/clients/sse_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/clients/sse_client.py)_
293+
<!-- /snippet-source -->
294+
287295
The `sse_client()` function accepts optional `headers`, `timeout`, `sse_read_timeout`, and `auth` parameters. The SSE transport is considered legacy; prefer [Streamable HTTP](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports#streamable-http) for new servers.
288296

289297
## Ping
@@ -302,6 +310,7 @@ result = await session.send_ping()
302310

303311
Pass a `logging_callback` to receive log messages from the server:
304312

313+
<!-- snippet-source examples/snippets/clients/logging_client.py -->
305314
```python
306315
from mcp import ClientSession, types
307316

@@ -318,6 +327,9 @@ session = ClientSession(
318327
)
319328
```
320329

330+
_Full example: [examples/snippets/clients/logging_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/clients/logging_client.py)_
331+
<!-- /snippet-source -->
332+
321333
### Setting the Server Log Level
322334

323335
Request that the server change its minimum log level:

‎docs/protocol.md‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,29 @@ Both return an `EmptyResult` on success. If the remote side does not respond wit
4242

4343
Either side can cancel a previously-issued request by sending a `CancelledNotification`:
4444

45+
<!-- snippet-source examples/snippets/clients/cancellation.py -->
4546
```python
4647
import mcp.types as types
47-
48-
# Send a cancellation notification
49-
await session.send_notification(
50-
types.ClientNotification(
51-
types.CancelledNotification(
52-
params=types.CancelledNotificationParams(
53-
requestId="request-id-to-cancel",
54-
reason="User navigated away",
48+
from mcp import ClientSession
49+
50+
51+
async def cancel_request(session: ClientSession) -> None:
52+
"""Send a cancellation notification for a previously-issued request."""
53+
await session.send_notification(
54+
types.ClientNotification(
55+
types.CancelledNotification(
56+
params=types.CancelledNotificationParams(
57+
requestId="request-id-to-cancel",
58+
reason="User navigated away",
59+
)
5560
)
5661
)
5762
)
58-
)
5963
```
6064

65+
_Full example: [examples/snippets/clients/cancellation.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/clients/cancellation.py)_
66+
<!-- /snippet-source -->
67+
6168
The `CancelledNotificationParams` fields:
6269

6370
- `requestId` (optional): The ID of the request to cancel. Required for non-task cancellations.
@@ -106,6 +113,7 @@ During initialization, the client sends `LATEST_PROTOCOL_VERSION`. If the server
106113

107114
MCP uses [JSON Schema 2020-12](https://json-schema.org/draft/2020-12) for tool input schemas, output schemas, and elicitation schemas. When using Pydantic models, schemas are generated automatically via `model_json_schema()`:
108115

116+
<!-- snippet-source examples/snippets/servers/json_schema_example.py -->
109117
```python
110118
from pydantic import BaseModel, Field
111119

@@ -132,6 +140,9 @@ schema = SearchParams.model_json_schema()
132140
# }
133141
```
134142

143+
_Full example: [examples/snippets/servers/json_schema_example.py](https://github.com/modelcontextprotocol/python-sdk/blob/v1.x/examples/snippets/servers/json_schema_example.py)_
144+
<!-- /snippet-source -->
145+
135146
For FastMCP tools, input schemas are derived automatically from function signatures. For structured output, the output schema is derived from the return type annotation.
136147

137148
## Pagination

‎docs/server.md‎

Lines changed: 56 additions & 4 deletions

0 commit comments

Comments
 (0)