Skip to content

Conversation

@hassan123789
Copy link
Contributor

## Summary

Fix `SSEClientTransport.Connect` to check HTTP status code before attempting to parse SSE events.

## Problem

Previously, authentication failures (401), authorization errors (403), and other HTTP errors would result in a confusing error message:

missing endpoint: first event is "", want "endpoint"


This happened because the SSE event parser received an error response body (e.g., JSON error from the server) instead of a valid SSE stream.

## Solution

Add an HTTP status code check immediately after `httpClient.Do(req)` and before attempting to parse SSE events. Non-2xx responses now return a clear error:

failed to connect: Unauthorized


This is consistent with the error handling in `StreamableClientTransport`, where HTTP errors are checked before processing the response.

## Changes

- **mcp/sse.go**: Added status code check in `SSEClientTransport.Connect`
- **mcp/sse_test.go**: Added `TestSSEClientTransport_HTTPErrors` with test cases for 401, 403, 404, and 500

## Testing

All existing tests pass, plus new test coverage for HTTP error scenarios.

## Note

The issue author mentioned interest in typed errors for programmatic error handling. This could be addressed in a follow-up PR if desired.

Fixes #714

Fix SSEClientTransport.Connect to check HTTP status code before
attempting to parse SSE events. Previously, authentication failures
(401), authorization errors (403), and other HTTP errors would result
in a confusing missing endpoint error because the SSE event parser
received an error response body instead of a valid SSE stream.

Now SSEClientTransport reports HTTP errors consistently, returning
errors like failed to connect: Unauthorized instead of the confusing
missing endpoint error.

Fixes modelcontextprotocol#714
Copy link
Contributor

@findleyr findleyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@findleyr findleyr merged commit 27b3354 into modelcontextprotocol:main Dec 31, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants