fix: catch KeyboardInterrupt in run() for clean Ctrl+C exit#2943
Open
kaXianc2-gom wants to merge 1 commit into
Open
fix: catch KeyboardInterrupt in run() for clean Ctrl+C exit#2943kaXianc2-gom wants to merge 1 commit into
kaXianc2-gom wants to merge 1 commit into
Conversation
When running a stdio server from the terminal, Ctrl+C previously produced a noisy multi-frame traceback through anyio.run() → asyncio.runners → anyio.streams.memory.receive. This change catches KeyboardInterrupt at the top-level run() method so the server exits quietly, matching user expectations for a terminal application. Closes modelcontextprotocol#2663 Co-Authored-By: Claude <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AI Assistance Disclosure
This contribution was developed with AI assistance (Claude / Anthropic Claude Code).
Summary
Fix #2663: When running a stdio server from the terminal and pressing Ctrl+C, the server previously produced a noisy multi-frame traceback through
anyio.run()→asyncio.runners→anyio.streams.memory.receive. This change wraps theanyio.run()call inMCPServer.run()with atry/except KeyboardInterruptso the server exits cleanly.Changes
src/mcp/server/mcpserver/server.py: +7 lines (try/except + comment), −5 lines (indent shift)tests/server/test_stdio.py: +14 lines (1 new test)Verification Process
try/except KeyboardInterruptinrun()anyio.runto raiseKeyboardInterruptTest Results
Cross-Validation
Closes #2663