FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix: exit cleanly on stdio KeyboardInterrupt by he-yufeng · Pull Request #2671 · modelcontextprotocol/python-sdk · GitHub

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion src/mcp/server/mcpserver/server.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL