Skip to content

Commit 42163b0

Browse files
committed
test: request with bad method crashes task
1 parent f2f4dbd commit 42163b0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from collections.abc import AsyncGenerator
2+
3+
import pytest
4+
from anyio.streams.memory import MemoryObjectSendStream
5+
6+
from mcp.shared.memory import create_client_server_memory_streams
7+
from mcp.shared.message import SessionMessage
8+
from mcp.shared.session import BaseSession
9+
from mcp.types import ClientNotification, ClientRequest, JSONRPCMessage, JSONRPCRequest
10+
11+
12+
@pytest.fixture
13+
async def client_write() -> (
14+
AsyncGenerator[MemoryObjectSendStream[SessionMessage], None]
15+
):
16+
"""A stream that allows to write to a running session."""
17+
async with create_client_server_memory_streams() as (
18+
(_, client_write),
19+
(server_read, server_write),
20+
):
21+
async with BaseSession(
22+
read_stream=server_read,
23+
write_stream=server_write,
24+
receive_request_type=ClientRequest,
25+
receive_notification_type=ClientNotification,
26+
) as _:
27+
yield client_write
28+
29+
30+
@pytest.mark.anyio
31+
async def test_session_does_not_raise_error_with_bad_input(
32+
client_write: MemoryObjectSendStream[SessionMessage],
33+
):
34+
# Given a running session
35+
36+
# When the client sends a bad request to the session
37+
request = JSONRPCRequest(jsonrpc="2.0", id=1, method="bad_method", params=None)
38+
message = SessionMessage(message=JSONRPCMessage(root=request))
39+
await client_write.send(message)
40+
41+
# Then the session can still be talked to
42+
await client_write.send(message)

0 commit comments

Comments
 (0)