fix:streamablehttp_client to raise McpError when receiving unexpected content types instead of just printing error messages.#1382 #1506
+97
−1
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