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
17 changes: 10 additions & 7 deletions src/mcp/server/mcpserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,16 @@ def run(
if transport not in TRANSPORTS.__args__: # type: ignore # pragma: no cover
raise ValueError(f"Unknown transport: {transport}")

match transport:
case "stdio":
anyio.run(self.run_stdio_async)
case "sse": # pragma: no cover
anyio.run(lambda: self.run_sse_async(**kwargs))
case "streamable-http": # pragma: no cover
anyio.run(lambda: self.run_streamable_http_async(**kwargs))
try:
match transport:
case "stdio":
anyio.run(self.run_stdio_async)
case "sse": # pragma: no cover
anyio.run(lambda: self.run_sse_async(**kwargs))
case "streamable-http": # pragma: no cover
anyio.run(lambda: self.run_streamable_http_async(**kwargs))
except KeyboardInterrupt:
return

async def _handle_list_tools(
self, ctx: ServerRequestContext[LifespanResultT], params: PaginatedRequestParams | None
Expand Down
8 changes: 8 additions & 0 deletions tests/server/mcpserver/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def test_dependencies(self):
mcp_no_deps = MCPServer("test")
assert mcp_no_deps.dependencies == []

def test_stdio_keyboard_interrupt_exits_cleanly(self):
mcp = MCPServer("test")

with patch("mcp.server.mcpserver.server.anyio.run", side_effect=KeyboardInterrupt) as run:
mcp.run("stdio")

run.assert_called_once_with(mcp.run_stdio_async)

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