|
5 | 5 | import anyio |
6 | 6 | import pytest |
7 | 7 |
|
| 8 | +from mcp.server import Server, ServerRequestContext |
8 | 9 | from mcp.server.stdio import stdio_server |
9 | 10 | from mcp.shared.message import SessionMessage |
10 | | -from mcp.types import JSONRPCMessage, JSONRPCRequest, JSONRPCResponse, jsonrpc_message_adapter |
| 11 | +from mcp.types import ( |
| 12 | + LATEST_PROTOCOL_VERSION, |
| 13 | + CallToolRequestParams, |
| 14 | + CallToolResult, |
| 15 | + ClientCapabilities, |
| 16 | + Implementation, |
| 17 | + InitializeRequestParams, |
| 18 | + JSONRPCError, |
| 19 | + JSONRPCMessage, |
| 20 | + JSONRPCNotification, |
| 21 | + JSONRPCRequest, |
| 22 | + JSONRPCResponse, |
| 23 | + ListToolsResult, |
| 24 | + PaginatedRequestParams, |
| 25 | + TextContent, |
| 26 | + Tool, |
| 27 | + jsonrpc_message_adapter, |
| 28 | +) |
11 | 29 |
|
12 | 30 |
|
13 | 31 | @pytest.mark.anyio |
@@ -92,3 +110,79 @@ async def test_stdio_server_invalid_utf8(monkeypatch: pytest.MonkeyPatch): |
92 | 110 | second = await read_stream.receive() |
93 | 111 | assert isinstance(second, SessionMessage) |
94 | 112 | assert second.message == valid |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.anyio |
| 116 | +async def test_stdio_server_drains_in_flight_responses_on_stdin_eof(): |
| 117 | + """When stdin reaches EOF (e.g., bash-redirected input), already-received |
| 118 | + requests must still be able to emit their responses on stdout.""" |
| 119 | + stdin = io.StringIO() |
| 120 | + stdout = io.StringIO() |
| 121 | + |
| 122 | + tool_started_count = 0 |
| 123 | + both_tools_started = anyio.Event() |
| 124 | + allow_tools_to_finish = anyio.Event() |
| 125 | + |
| 126 | + async def handle_list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult: |
| 127 | + return ListToolsResult(tools=[Tool(name="slow", description="test", input_schema={})]) |
| 128 | + |
| 129 | + async def handle_call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult: |
| 130 | + nonlocal tool_started_count |
| 131 | + tool_started_count += 1 |
| 132 | + if tool_started_count == 2: |
| 133 | + both_tools_started.set() |
| 134 | + await allow_tools_to_finish.wait() |
| 135 | + return CallToolResult(content=[TextContent(type="text", text="ok")]) |
| 136 | + |
| 137 | + server = Server("test", on_list_tools=handle_list_tools, on_call_tool=handle_call_tool) |
| 138 | + |
| 139 | + init_req = JSONRPCRequest( |
| 140 | + jsonrpc="2.0", |
| 141 | + id=0, |
| 142 | + method="initialize", |
| 143 | + params=InitializeRequestParams( |
| 144 | + protocol_version=LATEST_PROTOCOL_VERSION, |
| 145 | + capabilities=ClientCapabilities(), |
| 146 | + client_info=Implementation(name="test", version="1.0"), |
| 147 | + ).model_dump(by_alias=True, mode="json", exclude_none=True), |
| 148 | + ) |
| 149 | + initialized = JSONRPCNotification(jsonrpc="2.0", method="notifications/initialized") |
| 150 | + call_1 = JSONRPCRequest( |
| 151 | + jsonrpc="2.0", |
| 152 | + id=1, |
| 153 | + method="tools/call", |
| 154 | + params=CallToolRequestParams(name="slow", arguments={}).model_dump(by_alias=True, mode="json"), |
| 155 | + ) |
| 156 | + call_2 = JSONRPCRequest( |
| 157 | + jsonrpc="2.0", |
| 158 | + id=2, |
| 159 | + method="tools/call", |
| 160 | + params=CallToolRequestParams(name="slow", arguments={}).model_dump(by_alias=True, mode="json"), |
| 161 | + ) |
| 162 | + |
| 163 | + for message in (init_req, initialized, call_1, call_2): |
| 164 | + stdin.write(message.model_dump_json(by_alias=True, exclude_none=True) + "\n") |
| 165 | + stdin.seek(0) |
| 166 | + |
| 167 | + async with stdio_server(stdin=anyio.AsyncFile(stdin), stdout=anyio.AsyncFile(stdout)) as ( |
| 168 | + read_stream, |
| 169 | + write_stream, |
| 170 | + ): |
| 171 | + with anyio.fail_after(5): |
| 172 | + async with anyio.create_task_group() as tg: |
| 173 | + tg.start_soon(server.run, read_stream, write_stream, server.create_initialization_options()) |
| 174 | + await both_tools_started.wait() |
| 175 | + allow_tools_to_finish.set() |
| 176 | + |
| 177 | + stdout.seek(0) |
| 178 | + ids: set[int | str] = set() |
| 179 | + for line in stdout.readlines(): |
| 180 | + line = line.strip() |
| 181 | + if not line: |
| 182 | + continue |
| 183 | + message = jsonrpc_message_adapter.validate_json(line) |
| 184 | + if isinstance(message, JSONRPCResponse | JSONRPCError): |
| 185 | + assert message.id is not None |
| 186 | + ids.add(message.id) |
| 187 | + assert 1 in ids |
| 188 | + assert 2 in ids |
0 commit comments