Revert "refactor: reorganize message handling for better type safety and clar…" (#282)
Some checks failed
Main branch checks / checks (push) Has been cancelled

This reverts commit 9d0f2daddb.
This commit is contained in:
Marcelo Trylesinski 2025-03-14 10:50:46 +01:00 • committed by GitHub
commit 7196604468
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,7 +4,7 @@ import anyio
import pytest
from mcp.server.stdio import stdio_server
from mcp.types import JSONRPCMessage, JSONRPCRequest, JSONRPCResponse, MessageFrame
from mcp.types import JSONRPCMessage, JSONRPCRequest, JSONRPCResponse
@pytest.mark.anyio
@ -13,8 +13,8 @@ async def test_stdio_server():
stdout = io.StringIO()
messages = [
JSONRPCRequest(jsonrpc="2.0", id=1, method="ping"),
JSONRPCResponse(jsonrpc="2.0", id=2, result={}),
JSONRPCMessage(root=JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")),
JSONRPCMessage(root=JSONRPCResponse(jsonrpc="2.0", id=2, result={})),
]
for message in messages:
@ -35,29 +35,17 @@ async def test_stdio_server():
# Verify received messages
assert len(received_messages) == 2
assert isinstance(received_messages[0].message, JSONRPCMessage)
assert isinstance(received_messages[0].message.root, JSONRPCRequest)
assert received_messages[0].message.root.id == 1
assert received_messages[0].message.root.method == "ping"
assert isinstance(received_messages[1].message, JSONRPCMessage)
assert isinstance(received_messages[1].message.root, JSONRPCResponse)
assert received_messages[1].message.root.id == 2
assert received_messages[0] == JSONRPCMessage(
root=JSONRPCRequest(jsonrpc="2.0", id=1, method="ping")
)
assert received_messages[1] == JSONRPCMessage(
root=JSONRPCResponse(jsonrpc="2.0", id=2, result={})
)
# Test sending responses from the server
responses = [
MessageFrame(
message=JSONRPCMessage(
root=JSONRPCRequest(jsonrpc="2.0", id=3, method="ping")
),
raw=None,
),
MessageFrame(
message=JSONRPCMessage(
root=JSONRPCResponse(jsonrpc="2.0", id=4, result={})
),
raw=None,
),
JSONRPCMessage(root=JSONRPCRequest(jsonrpc="2.0", id=3, method="ping")),
JSONRPCMessage(root=JSONRPCResponse(jsonrpc="2.0", id=4, result={})),
]
async with write_stream:
@ -68,10 +56,13 @@ async def test_stdio_server():
output_lines = stdout.readlines()
assert len(output_lines) == 2
# Parse and verify the JSON responses directly
request_json = JSONRPCRequest.model_validate_json(output_lines[0].strip())
response_json = JSONRPCResponse.model_validate_json(output_lines[1].strip())
assert request_json.id == 3
assert request_json.method == "ping"
assert response_json.id == 4
received_responses = [
JSONRPCMessage.model_validate_json(line.strip()) for line in output_lines
]
assert len(received_responses) == 2
assert received_responses[0] == JSONRPCMessage(
root=JSONRPCRequest(jsonrpc="2.0", id=3, method="ping")
)
assert received_responses[1] == JSONRPCMessage(
root=JSONRPCResponse(jsonrpc="2.0", id=4, result={})
)