Skip to content

Commit a1d330d

Browse files
authored
Proper handle extra data in MCP objects (#1937)
1 parent 5301298 commit a1d330d

File tree

4 files changed

+42
-155
lines changed

4 files changed

+42
-155
lines changed

docs/migration.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,30 @@ await client.read_resource(str(my_any_url))
321321

322322
<!-- Add deprecations below -->
323323

324+
## Bug Fixes
325+
326+
### Extra fields no longer allowed on top-level MCP types
327+
328+
MCP protocol types no longer accept arbitrary extra fields at the top level. This matches the MCP specification which only allows extra fields within `_meta` objects, not on the types themselves.
329+
330+
```python
331+
# This will now raise a validation error
332+
from mcp.types import CallToolRequestParams
333+
334+
params = CallToolRequestParams(
335+
name="my_tool",
336+
arguments={},
337+
unknown_field="value", # ValidationError: extra fields not permitted
338+
)
339+
340+
# Extra fields are still allowed in _meta
341+
params = CallToolRequestParams(
342+
name="my_tool",
343+
arguments={},
344+
_meta={"progressToken": "tok", "customField": "value"}, # OK
345+
)
346+
```
347+
324348
## New Features
325349

326350
### `streamable_http_app()` available on lowlevel Server

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ dependencies = [
2626
"anyio>=4.5",
2727
"httpx>=0.27.1",
2828
"httpx-sse>=0.4",
29-
"pydantic>=2.12.0; python_version >= '3.14'",
30-
"pydantic>=2.11.0; python_version < '3.14'",
29+
"pydantic>=2.12.0",
3130
"starlette>=0.48.0; python_version >= '3.14'",
3231
"starlette>=0.27; python_version < '3.14'",
3332
"python-multipart>=0.0.9",

src/mcp/types/_types.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030

3131

3232
class MCPModel(BaseModel):
33-
"""Base class for all MCP protocol types. Allows extra fields for forward compatibility."""
33+
"""Base class for all MCP protocol types."""
3434

35-
# TODO(Marcelo): The extra="allow" should be only on specific types e.g. `Meta`, not on the base class.
36-
model_config = ConfigDict(extra="allow", alias_generator=to_camel, populate_by_name=True)
35+
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)
3736

3837

3938
Meta: TypeAlias = dict[str, Any]
@@ -472,10 +471,13 @@ class GetTaskPayloadRequest(Request[GetTaskPayloadRequestParams, Literal["tasks/
472471

473472
class GetTaskPayloadResult(Result):
474473
"""The response to a tasks/result request.
474+
475475
The structure matches the result type of the original request.
476476
For example, a tools/call task would return the CallToolResult structure.
477477
"""
478478

479+
model_config = ConfigDict(extra="allow", alias_generator=to_camel, populate_by_name=True)
480+
479481

480482
class CancelTaskRequestParams(RequestParams):
481483
task_id: str

0 commit comments

Comments
 (0)