Skip to content

Commit 304fed1

Browse files
committed
mcp: support application/json in streamable client
The client handles POST responses with content type application/json. TODO: tests
1 parent e8c6e03 commit 304fed1

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

mcp/streamable.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,8 @@ func (s *streamableClientConn) Write(ctx context.Context, msg jsonrpc.Message) e
800800
return nil
801801
}
802802

803+
// postMessage POSTs msg to the server and reads the response.
804+
// It returns the session ID from the response.
803805
func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string, msg jsonrpc.Message) (string, error) {
804806
data, err := jsonrpc2.EncodeMessage(msg)
805807
if err != nil {
@@ -836,8 +838,17 @@ func (s *streamableClientConn) postMessage(ctx context.Context, sessionID string
836838
// Section 2.1: The SSE stream is initiated after a POST.
837839
go s.handleSSE(resp)
838840
case "application/json":
839-
// TODO: read the body and send to s.incoming (in a select that also receives from s.done).
841+
body, err := io.ReadAll(resp.Body)
840842
resp.Body.Close()
843+
if err != nil {
844+
return "", err
845+
}
846+
select {
847+
case s.incoming <- body:
848+
case <-s.done:
849+
// The connection was closed by the client; exit gracefully.
850+
return sessionID, nil
851+
}
841852
return "", fmt.Errorf("streamable HTTP client does not yet support raw JSON responses")
842853
default:
843854
resp.Body.Close()

mcp/streamable_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ func TestClientReplay(t *testing.T) {
158158
client := NewClient(testImpl, &ClientOptions{
159159
ProgressNotificationHandler: func(ctx context.Context, cc *ClientSession, params *ProgressNotificationParams) {
160160
notifications <- params.Message
161-
}})
161+
},
162+
})
162163
clientSession, err := client.Connect(ctx, NewStreamableClientTransport(proxy.URL, nil))
163164
if err != nil {
164165
t.Fatalf("client.Connect() failed: %v", err)

0 commit comments

Comments
 (0)