|
8 | 8 | import anyio |
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from mcp.server import Server, ServerRequestContext |
11 | 12 | from mcp.server.mcpserver import MCPServer |
12 | 13 | from mcp.server.stdio import stdio_server |
13 | 14 | from mcp.shared.message import SessionMessage |
14 | | -from mcp.types import JSONRPCMessage, JSONRPCRequest, JSONRPCResponse, jsonrpc_message_adapter |
| 15 | +from mcp.types import ( |
| 16 | + LATEST_PROTOCOL_VERSION, |
| 17 | + CallToolRequestParams, |
| 18 | + CallToolResult, |
| 19 | + ClientCapabilities, |
| 20 | + Implementation, |
| 21 | + InitializeRequestParams, |
| 22 | + JSONRPCError, |
| 23 | + JSONRPCMessage, |
| 24 | + JSONRPCNotification, |
| 25 | + JSONRPCRequest, |
| 26 | + JSONRPCResponse, |
| 27 | + ListToolsResult, |
| 28 | + PaginatedRequestParams, |
| 29 | + TextContent, |
| 30 | + Tool, |
| 31 | + jsonrpc_message_adapter, |
| 32 | +) |
15 | 33 |
|
16 | 34 |
|
17 | 35 | @pytest.mark.anyio |
@@ -169,3 +187,79 @@ async def lifespan(server: MCPServer) -> AsyncIterator[None]: |
169 | 187 | assert events == ["setup", "cleanup"] |
170 | 188 | response = jsonrpc_message_adapter.validate_json(captured.getvalue().decode().strip()) |
171 | 189 | assert response == JSONRPCResponse(jsonrpc="2.0", id=1, result={}) |
| 190 | + |
| 191 | + |
| 192 | +@pytest.mark.anyio |
| 193 | +async def test_stdio_server_drains_in_flight_responses_on_stdin_eof(): |
| 194 | + """When stdin reaches EOF (e.g., bash-redirected input), already-received |
| 195 | + requests must still be able to emit their responses on stdout.""" |
| 196 | + stdin = io.StringIO() |
| 197 | + stdout = io.StringIO() |
| 198 | + |
| 199 | + tool_started_count = 0 |
| 200 | + both_tools_started = anyio.Event() |
| 201 | + allow_tools_to_finish = anyio.Event() |
| 202 | + |
| 203 | + async def handle_list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult: |
| 204 | + return ListToolsResult(tools=[Tool(name="slow", description="test", input_schema={})]) |
| 205 | + |
| 206 | + async def handle_call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult: |
| 207 | + nonlocal tool_started_count |
| 208 | + tool_started_count += 1 |
| 209 | + if tool_started_count == 2: |
| 210 | + both_tools_started.set() |
| 211 | + await allow_tools_to_finish.wait() |
| 212 | + return CallToolResult(content=[TextContent(type="text", text="ok")]) |
| 213 | + |
| 214 | + server = Server("test", on_list_tools=handle_list_tools, on_call_tool=handle_call_tool) |
| 215 | + |
| 216 | + init_req = JSONRPCRequest( |
| 217 | + jsonrpc="2.0", |
| 218 | + id=0, |
| 219 | + method="initialize", |
| 220 | + params=InitializeRequestParams( |
| 221 | + protocol_version=LATEST_PROTOCOL_VERSION, |
| 222 | + capabilities=ClientCapabilities(), |
| 223 | + client_info=Implementation(name="test", version="1.0"), |
| 224 | + ).model_dump(by_alias=True, mode="json", exclude_none=True), |
| 225 | + ) |
| 226 | + initialized = JSONRPCNotification(jsonrpc="2.0", method="notifications/initialized") |
| 227 | + call_1 = JSONRPCRequest( |
| 228 | + jsonrpc="2.0", |
| 229 | + id=1, |
| 230 | + method="tools/call", |
| 231 | + params=CallToolRequestParams(name="slow", arguments={}).model_dump(by_alias=True, mode="json"), |
| 232 | + ) |
| 233 | + call_2 = JSONRPCRequest( |
| 234 | + jsonrpc="2.0", |
| 235 | + id=2, |
| 236 | + method="tools/call", |
| 237 | + params=CallToolRequestParams(name="slow", arguments={}).model_dump(by_alias=True, mode="json"), |
| 238 | + ) |
| 239 | + |
| 240 | + for message in (init_req, initialized, call_1, call_2): |
| 241 | + stdin.write(message.model_dump_json(by_alias=True, exclude_none=True) + "\n") |
| 242 | + stdin.seek(0) |
| 243 | + |
| 244 | + async with stdio_server(stdin=anyio.AsyncFile(stdin), stdout=anyio.AsyncFile(stdout)) as ( |
| 245 | + read_stream, |
| 246 | + write_stream, |
| 247 | + ): |
| 248 | + with anyio.fail_after(5): |
| 249 | + async with anyio.create_task_group() as tg: |
| 250 | + tg.start_soon(server.run, read_stream, write_stream, server.create_initialization_options()) |
| 251 | + await both_tools_started.wait() |
| 252 | + allow_tools_to_finish.set() |
| 253 | + |
| 254 | + stdout.seek(0) |
| 255 | + ids: set[int | str] = set() |
| 256 | + for line in stdout.readlines(): |
| 257 | + line = line.strip() |
| 258 | + if not line: |
| 259 | + continue |
| 260 | + message = jsonrpc_message_adapter.validate_json(line) |
| 261 | + if isinstance(message, JSONRPCResponse | JSONRPCError): |
| 262 | + assert message.id is not None |
| 263 | + ids.add(message.id) |
| 264 | + assert 1 in ids |
| 265 | + assert 2 in ids |
0 commit comments