Skip to content

Make sure RequestId is not coerced as int #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mcp/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ProgressToken = str | int
Cursor = str
Role = Literal["user", "assistant"]
RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
RequestId = Annotated[int, Field(strict=True)] | str
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just makes sure that only int that is an int is actually an int. 👀

i.e. a string that looks like an int doesn't become an int.

AnyFunction: TypeAlias = Callable[..., Any]


Expand Down Expand Up @@ -849,7 +849,7 @@ class Tool(BaseMetadata):
"""A JSON Schema object defining the expected parameters for the tool."""
outputSchema: dict[str, Any] | None = None
"""
An optional JSON Schema object defining the structure of the tool's output
An optional JSON Schema object defining the structure of the tool's output
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow there are empty spaces? This shouldn't happen.

returned in the structuredContent field of a CallToolResult.
"""
annotations: ToolAnnotations | None = None
Expand Down
11 changes: 10 additions & 1 deletion tests/shared/test_sse.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,12 +466,21 @@ async def test_request_context_isolation(context_server: None, server_url: str)


def test_sse_message_id_coercion():
"""Test that string message IDs that look like integers are parsed as integers.
"""Previously, the `RequestId` would coerce a string that looked like an integer into an integer.

See <https://github.com/modelcontextprotocol/python-sdk/pull/851> for more details.

As per the JSON-RPC 2.0 specification, the id in the response object needs to be the same type as the id in the
request object. In other words, we can't perform the coercion.

See <https://www.jsonrpc.org/specification#response_object> for more details.
"""
json_message = '{"jsonrpc": "2.0", "id": "123", "method": "ping", "params": null}'
msg = types.JSONRPCMessage.model_validate_json(json_message)
assert msg == snapshot(types.JSONRPCMessage(root=types.JSONRPCRequest(method="ping", jsonrpc="2.0", id="123")))

json_message = '{"jsonrpc": "2.0", "id": 123, "method": "ping", "params": null}'
msg = types.JSONRPCMessage.model_validate_json(json_message)
assert msg == snapshot(types.JSONRPCMessage(root=types.JSONRPCRequest(method="ping", jsonrpc="2.0", id=123)))


Expand Down
Loading