mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 06:54:18 +01:00
* refactor: improve typing with memory stream type aliases Move memory stream type definitions to models.py and use them throughout the codebase for better type safety and maintainability. GitHub-Issue:#201 * refactor: move streams to ParsedMessage * refactor: update test files to use ParsedMessage Updates test files to work with the ParsedMessage stream type aliases and fixes a line length issue in test_201_client_hangs_on_logging.py. Github-Issue:#201 * refactor: rename ParsedMessage to MessageFrame for clarity 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * refactor: move MessageFrame class to types.py for better code organization 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * fix pyright * refactor: update websocket client to use MessageFrame Modified the websocket client to work with the new MessageFrame type, preserving raw message text and properly extracting the root JSON-RPC message when sending. Github-Issue:#204 * fix: use NoneType instead of None for type parameters in MessageFrame 🤖 Generated with [Claude Code](https://claude.ai/code) * refactor: rename root to message
104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
import anyio
|
|
import pytest
|
|
|
|
from mcp.server.lowlevel import NotificationOptions, Server
|
|
from mcp.server.models import InitializationOptions
|
|
from mcp.types import (
|
|
LATEST_PROTOCOL_VERSION,
|
|
ClientCapabilities,
|
|
Implementation,
|
|
InitializeRequestParams,
|
|
JSONRPCMessage,
|
|
JSONRPCNotification,
|
|
JSONRPCRequest,
|
|
MessageFrame,
|
|
NotificationParams,
|
|
)
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_request_id_match() -> None:
|
|
"""Test that the server preserves request IDs in responses."""
|
|
server = Server("test")
|
|
custom_request_id = "test-123"
|
|
|
|
# Create memory streams for communication
|
|
client_writer, client_reader = anyio.create_memory_object_stream(1)
|
|
server_writer, server_reader = anyio.create_memory_object_stream(1)
|
|
|
|
# Server task to process the request
|
|
async def run_server():
|
|
async with client_reader, server_writer:
|
|
await server.run(
|
|
client_reader,
|
|
server_writer,
|
|
InitializationOptions(
|
|
server_name="test",
|
|
server_version="1.0.0",
|
|
capabilities=server.get_capabilities(
|
|
notification_options=NotificationOptions(),
|
|
experimental_capabilities={},
|
|
),
|
|
),
|
|
raise_exceptions=True,
|
|
)
|
|
|
|
# Start server task
|
|
async with (
|
|
anyio.create_task_group() as tg,
|
|
client_writer,
|
|
client_reader,
|
|
server_writer,
|
|
server_reader,
|
|
):
|
|
tg.start_soon(run_server)
|
|
|
|
# Send initialize request
|
|
init_req = JSONRPCRequest(
|
|
id="init-1",
|
|
method="initialize",
|
|
params=InitializeRequestParams(
|
|
protocolVersion=LATEST_PROTOCOL_VERSION,
|
|
capabilities=ClientCapabilities(),
|
|
clientInfo=Implementation(name="test-client", version="1.0.0"),
|
|
).model_dump(by_alias=True, exclude_none=True),
|
|
jsonrpc="2.0",
|
|
)
|
|
|
|
await client_writer.send(
|
|
MessageFrame(message=JSONRPCMessage(root=init_req), raw=None)
|
|
)
|
|
await server_reader.receive() # Get init response but don't need to check it
|
|
|
|
# Send initialized notification
|
|
initialized_notification = JSONRPCNotification(
|
|
method="notifications/initialized",
|
|
params=NotificationParams().model_dump(by_alias=True, exclude_none=True),
|
|
jsonrpc="2.0",
|
|
)
|
|
await client_writer.send(
|
|
MessageFrame(
|
|
message=JSONRPCMessage(root=initialized_notification), raw=None
|
|
)
|
|
)
|
|
|
|
# Send ping request with custom ID
|
|
ping_request = JSONRPCRequest(
|
|
id=custom_request_id, method="ping", params={}, jsonrpc="2.0"
|
|
)
|
|
|
|
await client_writer.send(
|
|
MessageFrame(message=JSONRPCMessage(root=ping_request), raw=None)
|
|
)
|
|
|
|
# Read response
|
|
response = await server_reader.receive()
|
|
|
|
# Verify response ID matches request ID
|
|
assert (
|
|
response.message.root.id == custom_request_id
|
|
), "Response ID should match request ID"
|
|
|
|
# Cancel server task
|
|
tg.cancel_scope.cancel()
|