mcp: preserve array response for single-request legacy batches
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
- Start a stateful
StreamableHTTPHandlerwithJSONResponse: true. - POST this request with the normal JSON and event-stream
Acceptvalues:
[{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26","capabilities":{},"clientInfo":{"name":"test","version":"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.
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.