Fix: Handle Exception case in lowlevel server _handle_message#1200
Closed
samsonkolge wants to merge 2 commits intomodelcontextprotocol:mainfrom
Closed
Fix: Handle Exception case in lowlevel server _handle_message#1200samsonkolge wants to merge 2 commits intomodelcontextprotocol:mainfrom
samsonkolge wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
- Add missing Exception case in _handle_message match statement - Use logger.exception() following CLAUDE.md guidelines - Respect raise_exceptions parameter for backward compatibility - Add comprehensive tests for exception handling scenarios - Remove TODO comment as issue is resolved This fixes a critical gap where Exception instances passed to _handle_message were not properly handled, potentially causing silent failures in production.
Contributor
|
Closing this PR as this is a duplicate of #786 |
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.
Fix: Handle Exception case in lowlevel server _handle_message
Problem
The
_handle_messagemethod insrc/mcp/server/lowlevel/server.pyhad a critical unhandled case forExceptiontypes. Despite the function signature explicitly acceptingExceptionas 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_exceptionsparameter 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=Falseraise_exceptions=TrueFiles Changed
src/mcp/server/lowlevel/server.py- Fixed Exception handling in_handle_messagetests/server/test_exception_handling.py- Added comprehensive test coverageCommit Info
fix/exception-handling-lowlevel-serverReviewers: @jerome3o-anthropic @jspahrsummers
This PR resolves the most critical outstanding TODO in the codebase and significantly improves the robustness of the MCP server implementation.