File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -293,7 +293,10 @@ def run(
293293
294294 match transport :
295295 case "stdio" :
296- anyio .run (self .run_stdio_async )
296+ try :
297+ anyio .run (self .run_stdio_async )
298+ except KeyboardInterrupt :
299+ return
297300 case "sse" : # pragma: no cover
298301 anyio .run (lambda : self .run_sse_async (** kwargs ))
299302 case "streamable-http" : # pragma: no cover
Original file line number Diff line number Diff line change @@ -74,6 +74,17 @@ def test_dependencies(self):
7474 mcp_no_deps = MCPServer ("test" )
7575 assert mcp_no_deps .dependencies == []
7676
77+ def test_run_stdio_exits_cleanly_on_keyboard_interrupt (self , monkeypatch : pytest .MonkeyPatch ):
78+ mcp = MCPServer ("test" )
79+
80+ def raise_keyboard_interrupt (func : Any ) -> None :
81+ assert func == mcp .run_stdio_async
82+ raise KeyboardInterrupt
83+
84+ monkeypatch .setattr ("mcp.server.mcpserver.server.anyio.run" , raise_keyboard_interrupt )
85+
86+ mcp .run ("stdio" )
87+
7788 async def test_sse_app_returns_starlette_app (self ):
7889 """Test that sse_app returns a Starlette application with correct routes."""
7990 mcp = MCPServer ("test" )
You can’t perform that action at this time.
0 commit comments