From a26e1dc25351c547a2d89e57ee075b5db17722a4 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Mon, 14 Jul 2025 10:02:33 -0400 Subject: [PATCH] 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. --- mcp/streamable.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mcp/streamable.go b/mcp/streamable.go index 11d70a3..d371c87 100644 --- a/mcp/streamable.go +++ b/mcp/streamable.go @@ -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: + } } }()