Skip to content

Commit a26e1dc

Browse files
authored
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 #129.
1 parent b780f1b commit a26e1dc

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

mcp/streamable.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,8 +645,7 @@ type streamableClientConn struct {
645645
mu sync.Mutex
646646
protocolVersion string
647647
_sessionID string
648-
// bodies map[*http.Response]io.Closer
649-
err error
648+
err error
650649
}
651650

652651
func (c *streamableClientConn) setProtocolVersion(s string) {
@@ -741,10 +740,16 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
741740
}
742741

743742
sessionID = resp.Header.Get(sessionIDHeader)
744-
if resp.Header.Get("Content-Type") == "text/event-stream" {
743+
switch ct := resp.Header.Get("Content-Type"); ct {
744+
case "text/event-stream":
745745
go s.handleSSE(resp)
746-
} else {
746+
case "application/json":
747+
// TODO: read the body and send to s.incoming (in a select that also receives from s.done).
748+
resp.Body.Close()
749+
return "", fmt.Errorf("streamable HTTP client does not yet support raw JSON responses")
750+
default:
747751
resp.Body.Close()
752+
return "", fmt.Errorf("unsupported content type %q", ct)
748753
}
749754
return sessionID, nil
750755
}
@@ -760,7 +765,11 @@ func (s *streamableClientConn) handleSSE(resp *http.Response) {
760765
// TODO: surface this error; possibly break the stream
761766
return
762767
}
763-
s.incoming <- evt.data
768+
select {
769+
case <-s.done:
770+
return
771+
case s.incoming <- evt.data:
772+
}
764773
}
765774
}()
766775

0 commit comments

Comments
 (0)