fix:streamablehttp_client to raise McpError when receiving unexpected content types instead of just printing error messages.#1382#1506
Open
Wyn1996 wants to merge 8 commits intomodelcontextprotocol:mainfrom
Conversation
…ype so ClientSession receives McpError; keep fallback exception if no request id; add request id plumbing
- Test verifies that when server returns non-MCP content type (HTML), client raises McpError instead of just printing - Handles nested ExceptionGroup structure from task groups - Confirms the fix works end-to-end
- Sort imports alphabetically (asyncio before threading) - Add proper ExceptionGroup import with fallback for older Python versions - Remove unnecessary f-string prefix - All linting checks now pass
- Add type ignore comments for ExceptionGroup compatibility issues - Add proper type annotations for find_mcp_error function - All pre-commit checks now pass including pyright type checking
- CI pyright version updated and now reports ExceptionGroup as unknown import - Add type ignore comment to both import paths for compatibility - Resolves CI type checking errors
- Add more detailed description of what the test verifies - Clarify that it tests HTML response handling - Trigger CI checks for PR validation
- Remove trailing whitespace that was causing pre-commit formatting issues - Ensure clean formatting for CI checks
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 streamablehttp_client to raise McpError when receiving unexpected content types instead of just printing error messages.#1382
#1382
Motivation and Context
Previously, when connecting to a server that doesn't respond with the expected MCP JSON stream (e.g., HTML error pages, misconfigured endpoints), the streamablehttp_client would only print an "Unexpected content type" error message without raising an exception. This made it difficult for applications to handle cases where the server is not reachable or not serving MCP correctly, as no exception was raised for proper error handling.
How Has This Been Tested?
Added a comprehensive test case test_client_unexpected_content_type_raises_mcp_error that simulates a server returning HTML content
Verified that the client now properly raises McpError wrapped in ExceptionGroup when encountering unexpected content types
All existing tests continue to pass
Linting and formatting checks pass
Breaking Changes
This is a breaking change for applications that were relying on the previous behavior where no exception was raised for unexpected content types. Applications will now need to handle McpError exceptions when connecting to non-MCP servers.
Types of changes
Checklist
Additional context
Technical Implementation:
Modified _handle_unexpected_content_type method in src/mcp/client/streamable_http.py to construct and send a JSONRPCError with error code 32600
The error is properly associated with the originating request ID when available
Added proper ExceptionGroup import with fallback for Python version compatibility
The fix ensures that ClientSession.initialize() can catch and handle these errors appropriately
Test Coverage:
Created a realistic test scenario using a Starlette server that returns HTML instead of MCP JSON
Test verifies that the client raises McpError containing the unexpected content type information
Handles the nested ExceptionGroup structure that occurs due to anyio's task group error handling
Error Handling:
When a request ID is available, sends a proper JSON-RPC error response
When no request ID is available, sends a generic ValueError
Maintains backward compatibility for the error message format while adding proper exception handling