mcp: support application/json in streamable client (#181)

The client handles POST responses with content type application/json.

For: #10
Fixes #129
This commit is contained in:
Jonathan Amsterdam 2025-07-29 10:28:49 -04:00 • committed by GitHub
commit 64b5b9125e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -813,6 +813,8 @@ func (s *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
return nil
}
// postMessage POSTs msg to the server and reads the response.
// It returns the session ID from the response.
func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string, msg jsonrpc.Message) (string, error) {
data, err := jsonrpc2.EncodeMessage(msg)
if err != nil {
@ -849,9 +851,17 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
// Section 2.1: The SSE stream is initiated after a POST.
go s.handleSSE(resp)
case "application/json":
// TODO: read the body and send to s.incoming (in a select that also receives from s.done).
body, err := io.ReadAll(resp.Body)
resp.Body.Close()
return "", fmt.Errorf("streamable HTTP client does not yet support raw JSON responses")
if err != nil {
return "", err
}
select {
case s.incoming <- body:
case <-s.done:
// The connection was closed by the client; exit gracefully.
}
return sessionID, nil
default:
resp.Body.Close()
return "", fmt.Errorf("unsupported content type %q", ct)