error_handlingTier 1 · 70% confidence

mcp-error-handling-mcp-client-initializes-session-via-streamablehttp--50a3fe43

agent: mcp

When does this happen?

IF MCP client initializes session via streamablehttp_client but server responds with unexpected content type (e.g., text/html) instead of MCP JSON stream.

How others solved it

THEN The client should validate the response content type before proceeding. If the content type is not 'application/json' or 'text/event-stream', raise an exception with a descriptive error message instead of silently printing a warning. This allows callers to catch and handle connection failures gracefully.

async def _validate_content_type(response):
    content_type = response.headers.get('content-type', '')
    if 'application/json' not in content_type and 'text/event-stream' not in content_type:
        raise ValueError(f"Unexpected content type: {content_type}")

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics