go-sdk icon indicating copy to clipboard operation
go-sdk copied to clipboard

mcp: preserve array response for single-request legacy batches

Open theOxifier opened this issue 3 months ago • 1 comments

Description

When StreamableHTTPOptions.JSONResponse is enabled, a legacy JSON-RPC batch containing one request is accepted for protocol revisions before 2025-06-18, but the response is serialized as a bare JSON-RPC object rather than a one-element response array.

JSON-RPC 2.0 requires the response to a batch request to be an array, including when the batch contains a single call. This is observable for an initialization batch: the server creates a session and emits Mcp-Session-Id, but strict middleware or clients can reject the response shape.

Reproduction

  1. Start a stateful StreamableHTTPHandler with JSONResponse: true.
  2. POST this request with the normal JSON and event-stream Accept values:
[{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"1"}}}]
  1. Observe a successful bare object response:
{"jsonrpc":"2.0","id":1,"result":{...}}

Expected:

[{"jsonrpc":"2.0","id":1,"result":{...}}]

Cause

streamableServerConn.servePOST records the calls but not whether the incoming payload was a batch. stream.deliverLocked unwraps any response collection of length one.

Proposed fix

Preserve the incoming isBatch value on the logical stream and only unwrap one response when the original request was not a batch. Add a regression for a one-request 2025-03-26 initialization batch with JSON responses enabled.

The behavior is present in v1.7.0 and current main as of 2026-08-30.

theOxifier avatar Aug 30 '26 10:08 theOxifier

Hi, I would like to work on this issue.

I plan to preserve the original batch flag, keep single-request batch responses as JSON arrays, and add a regression test for the legacy initialization batch case.

wmh123wmh1 avatar Aug 31 '26 09:08 wmh123wmh1