Fix: Handle Exception case in lowlevel server _handle_message #1200
+126
−2
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.
Fix: Handle Exception case in lowlevel server _handle_message
Problem
The
_handle_message
method insrc/mcp/server/lowlevel/server.py
had a critical unhandled case forException
types. Despite the function signature explicitly acceptingException
as a valid message type, the match statement was missing this case, potentially leading to silent failures in production environments.Critical TODO was present:
Solution
🔧 Fixed the unhandled Exception case by:
Added proper Exception handling in the match statement:
case Exception() as error:
to handle Exception instanceslogger.exception()
following CLAUDE.md guidelines for proper exception logging with tracebackraise_exceptions
parameter to maintain backward compatibilityRemoved the TODO comment and type ignore directive as the issue is now resolved
Added comprehensive test coverage in
tests/server/test_exception_handling.py
:raise_exceptions=False
raise_exceptions=True
Files Changed
src/mcp/server/lowlevel/server.py
- Fixed Exception handling in_handle_message
tests/server/test_exception_handling.py
- Added comprehensive test coverageCommit Info
fix/exception-handling-lowlevel-server
Reviewers: @jerome3o-anthropic @jspahrsummers
This PR resolves the most critical outstanding TODO in the codebase and significantly improves the robustness of the MCP server implementation.