-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_run_stdio_custom_streams.py
More file actions
49 lines (37 loc) · 1.49 KB
/
test_run_stdio_custom_streams.py
File metadata and controls
49 lines (37 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""MCPServer.run_stdio_async forwards optional stdin/stdout to stdio_server."""
from __future__ import annotations
import io
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from typing import Any
from unittest.mock import AsyncMock
import anyio
import pytest
from mcp.server.mcpserver import MCPServer
@pytest.mark.anyio
async def test_run_stdio_async_passes_streams_to_stdio_server(monkeypatch: pytest.MonkeyPatch) -> None:
captured: dict[str, object] = {}
@asynccontextmanager
async def spy_stdio_server(
stdin: anyio.AsyncFile[str] | None = None,
stdout: anyio.AsyncFile[str] | None = None,
) -> AsyncIterator[tuple[AsyncMock, AsyncMock]]:
captured["stdin"] = stdin
captured["stdout"] = stdout
read_stream = AsyncMock()
write_stream = AsyncMock()
yield read_stream, write_stream
async def noop_run(*_args: Any, **_kwargs: Any) -> None:
return None
monkeypatch.setattr("mcp.server.mcpserver.server.stdio_server", spy_stdio_server)
server = MCPServer("test-stdio-spy")
monkeypatch.setattr(server._lowlevel_server, "run", noop_run)
monkeypatch.setattr(server._lowlevel_server, "create_initialization_options", lambda: object())
sin = io.StringIO()
sout = io.StringIO()
await server.run_stdio_async(
stdin=anyio.AsyncFile(sin),
stdout=anyio.AsyncFile(sout),
)
assert captured["stdin"] is not None
assert captured["stdout"] is not None