Make sure RequestId is not coerced as int (#1178)
This commit is contained in:
parent
0b1b52ba45
commit
7570ba30cf
2 changed files with 12 additions and 3 deletions
|
|
@ -36,7 +36,7 @@ DEFAULT_NEGOTIATED_VERSION = "2025-03-26"
|
||||||
ProgressToken = str | int
|
ProgressToken = str | int
|
||||||
Cursor = str
|
Cursor = str
|
||||||
Role = Literal["user", "assistant"]
|
Role = Literal["user", "assistant"]
|
||||||
RequestId = Annotated[int | str, Field(union_mode="left_to_right")]
|
RequestId = Annotated[int, Field(strict=True)] | str
|
||||||
AnyFunction: TypeAlias = Callable[..., Any]
|
AnyFunction: TypeAlias = Callable[..., Any]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -466,12 +466,21 @@ async def test_request_context_isolation(context_server: None, server_url: str)
|
||||||
|
|
||||||
|
|
||||||
def test_sse_message_id_coercion():
|
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.
|
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}'
|
json_message = '{"jsonrpc": "2.0", "id": "123", "method": "ping", "params": null}'
|
||||||
msg = types.JSONRPCMessage.model_validate_json(json_message)
|
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)))
|
assert msg == snapshot(types.JSONRPCMessage(root=types.JSONRPCRequest(method="ping", jsonrpc="2.0", id=123)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue