Skip to content

Commit 16a85b7

Browse files
committed
Bound the client-controlled limit in the extension examples
A request-sized parameter in example code should demonstrate input bounding: Field(ge=, le=) rejects an absurd limit before the handler allocates anything for it.
1 parent efd373d commit 16a85b7

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎docs/advanced/extensions.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,14 @@ async def main() -> None:
9191
An extension can register **new request methods**: its own verbs, served next to the
9292
spec's:
9393

94-
```python title="server.py" hl_lines="13-19 23 32-40"
94+
```python title="server.py" hl_lines="14-20 24 33-41"
9595
--8<-- "docs_src/extensions/tutorial002.py"
9696
```
9797

9898
* `SearchParams` subclasses `RequestParams`, so the 2026 `_meta` envelope parses
99-
uniformly and your handler gets validated params, never a raw dict.
99+
uniformly and your handler gets validated params, never a raw dict. Bound what
100+
the client controls: `Field(ge=1, le=100)` rejects an absurd `limit` before
101+
your code allocates anything for it.
100102
* `require_client_extension(ctx, EXTENSION_ID)` is the gate: a client that did not
101103
declare the extension gets the `-32021` (missing required client capability) error,
102104
with the machine-readable `requiredCapabilities` payload the spec asks for.

‎docs_src/extensions/tutorial002.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Any
33

44
import mcp_types as types
5+
from pydantic import Field
56

67
from mcp.server.context import ServerRequestContext
78
from mcp.server.extension import Extension, MethodBinding
@@ -12,7 +13,7 @@
1213

1314
class SearchParams(types.RequestParams):
1415
query: str
15-
limit: int = 10
16+
limit: int = Field(default=10, ge=1, le=100)
1617

1718

1819
class SearchResult(types.Result):

‎examples/stories/extensions/server.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import Any
1111

1212
import mcp_types as types
13+
from pydantic import Field
1314

1415
from mcp.server.context import ServerRequestContext
1516
from mcp.server.extension import Extension, MethodBinding, ToolBinding
@@ -23,7 +24,7 @@ class SearchParams(types.RequestParams):
2324
"""Subclass `RequestParams` so `_meta` (and the 2026 envelope keys) parse uniformly."""
2425

2526
query: str
26-
limit: int = 3
27+
limit: int = Field(default=3, ge=1, le=25)
2728

2829

2930
class SearchResult(types.Result):

0 commit comments

Comments
 (0)