Drop RootModel from JSONRPCMessage (#1908)

This commit is contained in:
Marcelo Trylesinski 2026-01-19 14:04:15 +01:00 • committed by GitHub
commit f4672c5084
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 314 additions and 476 deletions

View file

@ -18,7 +18,6 @@ from mcp.types import (
EmptyResult,
ErrorData,
JSONRPCError,
JSONRPCMessage,
JSONRPCRequest,
JSONRPCResponse,
TextContent,
@ -130,7 +129,7 @@ async def test_response_id_type_mismatch_string_to_int():
"""Receive a request and respond with a string ID instead of integer."""
message = await server_read.receive()
assert isinstance(message, SessionMessage)
root = message.message.root
root = message.message
assert isinstance(root, JSONRPCRequest)
# Get the original request ID (which is an integer)
request_id = root.id
@ -142,7 +141,7 @@ async def test_response_id_type_mismatch_string_to_int():
id=str(request_id), # Convert to string to simulate mismatch
result={},
)
await server_write.send(SessionMessage(message=JSONRPCMessage(response)))
await server_write.send(SessionMessage(message=response))
async def make_request(client_session: ClientSession):
nonlocal result_holder
@ -185,7 +184,7 @@ async def test_error_response_id_type_mismatch_string_to_int():
"""Receive a request and respond with an error using a string ID."""
message = await server_read.receive()
assert isinstance(message, SessionMessage)
root = message.message.root
root = message.message
assert isinstance(root, JSONRPCRequest)
request_id = root.id
assert isinstance(request_id, int)
@ -196,7 +195,7 @@ async def test_error_response_id_type_mismatch_string_to_int():
id=str(request_id), # Convert to string to simulate mismatch
error=ErrorData(code=-32600, message="Test error"),
)
await server_write.send(SessionMessage(message=JSONRPCMessage(error_response)))
await server_write.send(SessionMessage(message=error_response))
async def make_request(client_session: ClientSession):
nonlocal error_holder
@ -247,7 +246,7 @@ async def test_response_id_non_numeric_string_no_match():
id="not_a_number", # Non-numeric string
result={},
)
await server_write.send(SessionMessage(message=JSONRPCMessage(response)))
await server_write.send(SessionMessage(message=response))
async def make_request(client_session: ClientSession):
try:

View file

@ -503,12 +503,12 @@ def test_sse_message_id_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")))
msg = types.JSONRPCRequest.model_validate_json(json_message)
assert msg == snapshot(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)))
msg = types.JSONRPCRequest.model_validate_json(json_message)
assert msg == snapshot(types.JSONRPCRequest(method="ping", jsonrpc="2.0", id=123))
@pytest.mark.parametrize(
@ -601,5 +601,5 @@ async def test_sse_client_handles_empty_keepalive_pings() -> None:
msg = await read_stream.receive()
# If we get here without error, the empty message was skipped successfully
assert not isinstance(msg, Exception)
assert isinstance(msg.message.root, types.JSONRPCResponse)
assert msg.message.root.id == 1
assert isinstance(msg.message, types.JSONRPCResponse)
assert msg.message.id == 1

View file

@ -49,7 +49,6 @@ from mcp.shared.message import ClientMessageMetadata, ServerMessageMetadata, Ses
from mcp.shared.session import RequestResponder
from mcp.types import (
InitializeResult,
JSONRPCMessage,
JSONRPCRequest,
TextContent,
TextResourceContents,
@ -1859,7 +1858,7 @@ async def test_close_sse_stream_callback_not_provided_for_old_protocol_version()
)
# Create a mock message and request
mock_message = JSONRPCMessage(root=JSONRPCRequest(jsonrpc="2.0", id="test-1", method="tools/list"))
mock_message = JSONRPCRequest(jsonrpc="2.0", id="test-1", method="tools/list")
mock_request = MagicMock()
# Call _create_session_message with OLD protocol version