Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/mcp/server/mcpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,10 @@ def run(

match transport:
case "stdio":
anyio.run(self.run_stdio_async)
try:
anyio.run(self.run_stdio_async)
except KeyboardInterrupt:
return
case "sse": # pragma: no cover
anyio.run(lambda: self.run_sse_async(**kwargs))
case "streamable-http": # pragma: no cover
Expand Down
11 changes: 11 additions & 0 deletions tests/server/mcpserver/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ def test_dependencies(self):
mcp_no_deps = MCPServer("test")
assert mcp_no_deps.dependencies == []

def test_run_stdio_exits_cleanly_on_keyboard_interrupt(self, monkeypatch: pytest.MonkeyPatch):
mcp = MCPServer("test")

def raise_keyboard_interrupt(func: Any) -> None:
assert func == mcp.run_stdio_async
raise KeyboardInterrupt

monkeypatch.setattr("mcp.server.mcpserver.server.anyio.run", raise_keyboard_interrupt)

mcp.run("stdio")

async def test_sse_app_returns_starlette_app(self):
"""Test that sse_app returns a Starlette application with correct routes."""
mcp = MCPServer("test")
Expand Down
Loading