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:
parent
b780f1b3e1
commit
a26e1dc253
1 changed files with 14 additions and 5 deletions
|
|
@ -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:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue