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":
anyio.run(lambda: self.run_sse_async(**kwargs))
case "streamable-http": # pragma: no branch
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
12 changes: 11 additions & 1 deletion tests/server/mcpserver/test_server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
from pathlib import Path
from typing import Any
from typing import Any, Literal, cast
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
Expand Down Expand Up @@ -73,6 +73,16 @@ def test_dependencies(self):
mcp_no_deps = MCPServer("test")
assert mcp_no_deps.dependencies == []

@pytest.mark.parametrize("transport", ["stdio", "sse", "streamable-http"])
def test_run_suppresses_keyboard_interrupt(self, transport: str):
mcp = MCPServer("test")
transport_name = cast(Literal["stdio", "sse", "streamable-http"], transport)

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

run.assert_called_once()

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