mcp: fix SSEClientTransport to report HTTP errors properly (#740)

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

Fixes #714
This commit is contained in:
ZAIN ULHASSAN 2026-01-01 01:16:20 +09:00 • committed by GitHub
commit 27b3354f80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -351,6 +351,14 @@ func (c *SSEClientTransport) Connect(ctx context.Context) (Connection, error) {
return nil, err
}
// Check HTTP status code before attempting to parse SSE events.
// This ensures proper error reporting for authentication failures (401),
// authorization failures (403), and other HTTP errors.
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
resp.Body.Close()
return nil, fmt.Errorf("failed to connect: %s", http.StatusText(resp.StatusCode))
}
msgEndpoint, err := func() (*url.URL, error) {
var evt Event
for evt, err = range scanEvents(resp.Body) {