Skip to content

Conversation

@Wyn1996
Copy link

@Wyn1996 Wyn1996 commented Oct 22, 2025

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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

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

…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
@Wyn1996 Wyn1996 changed the title Feat/wyn fix:streamablehttp_client to raise McpError when receiving unexpected content types instead of just printing error messages.#1382 Oct 22, 2025
Wyn1996 and others added 4 commits October 22, 2025 20:06
- 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
@maxisbey maxisbey added bug Something isn't working needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention labels Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working needs maintainer action Potentially serious issue - needs proactive fix and maintainer attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants