Skip to content

Commit db7d89e

Browse files
committed
test: cover MCPServer stdio run paths
1 parent ec89e98 commit db7d89e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/server/mcpserver/test_server.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
from contextlib import asynccontextmanager
23
from pathlib import Path
34
from typing import Any
45
from unittest.mock import AsyncMock, MagicMock, patch
@@ -75,6 +76,44 @@ def test_dependencies(self):
7576
mcp_no_deps = MCPServer("test")
7677
assert mcp_no_deps.dependencies == []
7778

79+
def test_run_dispatches_to_stdio(self, monkeypatch: pytest.MonkeyPatch):
80+
mcp = MCPServer("test")
81+
captured: dict[str, Any] = {}
82+
83+
def fake_anyio_run(fn: Any, *args: Any, **kwargs: Any) -> None:
84+
captured["fn"] = fn
85+
captured["args"] = args
86+
captured["kwargs"] = kwargs
87+
88+
monkeypatch.setattr(MCPServer.run.__globals__["anyio"], "run", fake_anyio_run)
89+
90+
mcp.run()
91+
92+
assert captured["fn"] == mcp.run_stdio_async
93+
assert captured["args"] == ()
94+
assert captured["kwargs"] == {}
95+
96+
@pytest.mark.anyio
97+
async def test_run_stdio_async_uses_stdio_server(self, monkeypatch: pytest.MonkeyPatch):
98+
mcp = MCPServer("test")
99+
read_stream = object()
100+
write_stream = object()
101+
init_options = object()
102+
lowlevel_run = AsyncMock()
103+
104+
mcp._lowlevel_server.run = lowlevel_run # type: ignore[method-assign]
105+
monkeypatch.setattr(mcp._lowlevel_server, "create_initialization_options", lambda: init_options)
106+
107+
@asynccontextmanager
108+
async def fake_stdio_server():
109+
yield read_stream, write_stream
110+
111+
monkeypatch.setitem(MCPServer.run_stdio_async.__globals__, "stdio_server", fake_stdio_server)
112+
113+
await mcp.run_stdio_async()
114+
115+
lowlevel_run.assert_awaited_once_with(read_stream, write_stream, init_options)
116+
78117
async def test_sse_app_returns_starlette_app(self):
79118
"""Test that sse_app returns a Starlette application with correct routes."""
80119
mcp = MCPServer("test")

0 commit comments

Comments
 (0)