-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathtest_192_request_id.py
88 lines (74 loc) · 2.88 KB
/
test_192_request_id.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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,
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:
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(JSONRPCMessage(root=init_req))
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(JSONRPCMessage(root=initialized_notification))
# Send ping request with custom ID
ping_request = JSONRPCRequest(
id=custom_request_id, method="ping", params={}, jsonrpc="2.0"
)
await client_writer.send(JSONRPCMessage(root=ping_request))
# Read response
response = await server_reader.receive()
# Verify response ID matches request ID
assert (
response.root.id == custom_request_id
), "Response ID should match request ID"
# Cancel server task
tg.cancel_scope.cancel()