|
1 | 1 | import base64 |
| 2 | +from contextlib import asynccontextmanager |
2 | 3 | from pathlib import Path |
3 | 4 | from typing import Any |
4 | 5 | from unittest.mock import AsyncMock, MagicMock, patch |
@@ -75,6 +76,44 @@ def test_dependencies(self): |
75 | 76 | mcp_no_deps = MCPServer("test") |
76 | 77 | assert mcp_no_deps.dependencies == [] |
77 | 78 |
|
| 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 | + |
78 | 117 | async def test_sse_app_returns_starlette_app(self): |
79 | 118 | """Test that sse_app returns a Starlette application with correct routes.""" |
80 | 119 | mcp = MCPServer("test") |
|
0 commit comments