Skip to content

Parse SSE responses in HTTP client via event_stream_parser#322

Open
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
atesgoral:ag/client-sse-parser
Open

Parse SSE responses in HTTP client via event_stream_parser#322
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
atesgoral:ag/client-sse-parser

Conversation

@atesgoral
Copy link
Copy Markdown
Contributor

Summary

First slice of #321 (Streamable HTTP client support), factored out for easier review.

The Streamable HTTP spec allows servers to respond to a POST with either application/json or text/event-stream. MCP::Client::HTTP currently rejects the latter. This PR adds SSE parsing via the optional event_stream_parser gem — users add it to their Gemfile only when their server uses SSE, matching the existing pattern for faraday.

send_request now dispatches on the response Content-Type, scans the event stream for the first JSON-RPC response message (skipping comments, notifications, and non-response events), and returns it.

This PR intentionally does not include other Streamable HTTP features — those will land as follow-up stacked PRs:

  • 202 Accepted handling for async-delivered responses
  • Mcp-Session-Id header tracking / SessionExpiredError on 404
  • Client#connect handshake helper + protocol_version tracking
  • Client#close / DELETE session termination
  • Example + docs rewrite

Splitting it this way keeps each PR self-contained and shippable: after this merges, the client correctly handles both JSON and SSE responses from stateless servers.

Changes

  • lib/mcp/client/http.rb: replace validate_response_content_type! with a parse_response_body dispatch on Content-Type; add parse_sse_response and require_event_stream_parser!
  • Gemfile: add event_stream_parser to the test group
  • docs/building-clients.md: mention event_stream_parser as an optional dependency for SSE responses
  • test/mcp/client/http_test.rb: add tests for SSE happy path, SSE error response, SSE stream with no response message, and LoadError when the gem is missing; convert the old "non-JSON response" test to use text/html (since text/event-stream is now valid)

Test plan

  • rake test passes (737 runs, 1847 assertions)
  • rake rubocop shows no new offenses on changed files
  • New tests cover SSE happy path, SSE error responses, empty SSE streams, and missing event_stream_parser gem

🤖 Generated with Claude Code

The Streamable HTTP spec allows servers to respond with either
`application/json` or `text/event-stream`. The client previously
rejected the latter. Add SSE parsing via the optional
`event_stream_parser` gem (users add it to their Gemfile when their
server uses SSE), dispatch on the response Content-Type, and scan the
event stream for the first JSON-RPC response message.

No other Streamable HTTP features (202 handling, session IDs,
`connect`, DELETE termination) are included here; those will land in
follow-up PRs.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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