Skip to content

Fix JSON array response handling in StreamableHTTP client transport #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

notsobad
Copy link

Fix JSON array response handling in StreamableHTTP client transport

Motivation and Context

The StreamableHTTP client transport's _handle_json_response method was not properly handling JSON array responses from servers. When a server returns multiple JSONRPCMessage objects as an array (e.g., batch responses), the current implementation would fail to parse them correctly. This issue was discovered when testing with servers that return array responses for certain operations like tools/list.

How Has This Been Tested?

  • Tested with servers returning single JSONRPCMessage responses (existing functionality)
  • Tested with servers returning JSON array responses containing multiple JSONRPCMessage objects
  • Verified that both response types are correctly parsed and processed
  • Confirmed that protocol version extraction still works correctly for initialization responses

Breaking Changes

None. This is a backward-compatible fix that maintains existing functionality while adding support for array responses.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The fix refactors the JSON parsing logic to:

  1. First parse the raw JSON content to determine its structure
  2. Handle both single object and array responses appropriately
  3. Maintain proper error handling for malformed responses
  4. Preserve existing functionality for protocol version extraction and message processing

This change ensures compatibility with MCP servers that may return batch responses or multiple messages in a single HTTP response, which is a valid pattern in JSON-RPC 2.0 specifications.

@notsobad
Copy link
Author

https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/client/streamableHttp.ts#L473

The TypeScript SDK can handle this situation correctly.

@notsobad notsobad changed the title fix json response Fix JSON array response handling in StreamableHTTP client transport Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant