mcp: fail when streamable http client encounters unsupported content (#130)

The streamable HTTP client is not fully implemented, but did not fail
with meaningful error messages when encountering unsupported content
types from other servers. This may be straightforward to fix, but for
now at least provide a meaningful error message.

For modelcontextprotocol/go-sdk#129.
This commit is contained in:
Robert Findley 2025-07-14 10:02:33 -04:00 • committed by GitHub
commit a26e1dc253
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -645,8 +645,7 @@ type streamableClientConn struct {
mu sync.Mutex
protocolVersion string
_sessionID string
// bodies map[*http.Response]io.Closer
err error
err error
}
func (c *streamableClientConn) setProtocolVersion(s string) {
@ -741,10 +740,16 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
}
sessionID = resp.Header.Get(sessionIDHeader)
if resp.Header.Get("Content-Type") == "text/event-stream" {
switch ct := resp.Header.Get("Content-Type"); ct {
case "text/event-stream":
go s.handleSSE(resp)
} else {
case "application/json":
// TODO: read the body and send to s.incoming (in a select that also receives from s.done).
resp.Body.Close()
return "", fmt.Errorf("streamable HTTP client does not yet support raw JSON responses")
default:
resp.Body.Close()
return "", fmt.Errorf("unsupported content type %q", ct)
}
return sessionID, nil
}
@ -760,7 +765,11 @@ func (s *streamableClientConn) handleSSE(resp *http.Response) {
// TODO: surface this error; possibly break the stream
return
}
s.incoming <- evt.data
select {
case <-s.done:
return
case s.incoming <- evt.data:
}
}
}()