Skip to content

Commit 60e8389

Browse files
committed
test: cover interrupt exit for all transports
1 parent 308f466 commit 60e8389

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

‎tests/server/mcpserver/test_server.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dataclasses import dataclass
66
from pathlib import Path
77
from types import SimpleNamespace
8-
from typing import Annotated, Any
8+
from typing import Annotated, Any, Literal
99
from unittest.mock import AsyncMock, MagicMock, patch
1010

1111
import anyio
@@ -111,10 +111,24 @@ def test_dependencies(self):
111111
mcp_no_deps = MCPServer("test")
112112
assert mcp_no_deps.dependencies == []
113113

114-
def test_stdio_keyboard_interrupt_exits_cleanly(self):
114+
@pytest.mark.parametrize("transport", ["stdio", "sse", "streamable-http"])
115+
def test_keyboard_interrupt_exits_cleanly(self, transport: Literal["stdio", "sse", "streamable-http"]):
115116
mcp = MCPServer("test")
116117

117118
with patch("mcp.server.mcpserver.server.anyio.run", side_effect=KeyboardInterrupt) as run:
119+
mcp.run(transport)
120+
121+
assert run.call_count == 1
122+
if transport == "stdio":
123+
run.assert_called_once_with(mcp.run_stdio_async)
124+
125+
def test_run_propagates_non_interrupt_errors(self):
126+
mcp = MCPServer("test")
127+
128+
with (
129+
patch("mcp.server.mcpserver.server.anyio.run", side_effect=RuntimeError("boom")) as run,
130+
pytest.raises(RuntimeError, match="boom"),
131+
):
118132
mcp.run("stdio")
119133

120134
run.assert_called_once_with(mcp.run_stdio_async)

0 commit comments

Comments
 (0)