Parse SSE responses in HTTP client via event_stream_parser#322
Open
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
Parse SSE responses in HTTP client via event_stream_parser#322atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
atesgoral wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/jsonortext/event-stream.MCP::Client::HTTPcurrently rejects the latter. This PR adds SSE parsing via the optionalevent_stream_parsergem — users add it to their Gemfile only when their server uses SSE, matching the existing pattern forfaraday.send_requestnow dispatches on the responseContent-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:
Mcp-Session-Idheader tracking /SessionExpiredErroron 404Client#connecthandshake helper +protocol_versiontrackingClient#close/ DELETE session terminationSplitting 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: replacevalidate_response_content_type!with aparse_response_bodydispatch onContent-Type; addparse_sse_responseandrequire_event_stream_parser!Gemfile: addevent_stream_parserto the test groupdocs/building-clients.md: mentionevent_stream_parseras an optional dependency for SSE responsestest/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 usetext/html(sincetext/event-streamis now valid)Test plan
rake testpasses (737 runs, 1847 assertions)rake rubocopshows no new offenses on changed filesevent_stream_parsergem🤖 Generated with Claude Code