Skip to content

Commit 09c89ae

Browse files
committed
Parse HTTP client response bodies without Faraday's JSON middleware
## Motivation and Context json 3.0, released on 2026-09-07, accepts the options of `JSON.parse` as keywords only, and Faraday's json response middleware, through 2.14.3, hands them over as a positional Hash, which Ruby 3 no longer converts. With both installed, every JSON body the client receives fails inside that middleware, and `MCP::Client::HTTP` reports each request as an internal error. Faraday's main branch passes the options as keywords, but no release carries that yet. The client already parses streamed bodies and buffered SSE bodies itself; only a JSON body delivered whole by an adapter without streaming support still went through the middleware. The client now parses that body too and no longer registers the middleware, so every body takes `parse_json_buffer` and a malformed one answers `:parse_error` whichever way it arrived. A JSON middleware added through the Faraday customizer block still works, since a body it has parsed already is used as is. ## How Has This Been Tested? A new test drives a buffered JSON response through a stand-in `JSON.parse` with the json 3.0 signature, options as keywords only, so on Ruby 3 it failed with the middleware registered whatever json version is installed and passes now. The existing buffered-body test keeps covering the plain path. The full suite passes on Ruby 3.0.7 with json 3.0.1 and faraday 2.14.3, where five client tests failed before. ## Breaking Changes None. A malformed JSON body delivered by an adapter without streaming support now raises `RequestHandlerError` with `error_type: :parse_error`, as the streamed path already did, instead of `:internal_error` wrapping a `Faraday::ParsingError`.
1 parent 44af58c commit 09c89ae

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎lib/mcp/client/http.rb‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,8 @@ def client
723723
require_faraday!
724724
@client ||= Faraday.new(url) do |faraday|
725725
faraday.request(:json)
726-
faraday.response(:json)
726+
# The client parses response bodies itself (`resolve_response_body`), so streamed and buffered responses
727+
# take the same path; Faraday's json response middleware is deliberately left out.
727728
faraday.response(:raise_error)
728729

729730
faraday.headers["Accept"] = ACCEPT_HEADER
@@ -1074,8 +1075,8 @@ def resolve_response_body(stream, response, method, params)
10741075
elsif content_type&.include?("application/json")
10751076
return parse_json_buffer(stream.buffer, method, params) unless stream.buffer.empty?
10761077

1077-
# Adapters without `on_data` support deliver the body via `response.body`,
1078-
# already parsed by the json response middleware.
1078+
# Adapters without `on_data` support deliver the body via `response.body`; a JSON middleware
1079+
# added by the Faraday customizer may have parsed it already.
10791080
response.body.is_a?(String) ? parse_json_buffer(response.body, method, params) : response.body
10801081
else
10811082
raise RequestHandlerError.new(

‎test/mcp/client/http_test.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,26 @@ def test_send_request_parses_json_response_when_adapter_does_not_stream
940940
assert_equal({ "result" => { "tools" => [] } }, response)
941941
end
942942

943+
def test_send_request_parses_a_json_body_with_a_parser_taking_keyword_options_only
944+
# json 3.0 accepts parser options as keywords only, and Faraday's json response middleware
945+
# passes them as a positional Hash, which Ruby 3 no longer converts. The stand-in parser has
946+
# the json 3.0 signature, so the body must reach `JSON.parse` through the client's own call.
947+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
948+
stub.post("/") do
949+
[200, { "Content-Type" => "application/json" }, { result: { tools: [] } }.to_json]
950+
end
951+
end
952+
client = HTTP.new(url: url) { |faraday| faraday.adapter(:test, stubs) }
953+
parse = JSON.method(:parse)
954+
keyword_options_only_parse = ->(source, **options) { parse.call(source, **options) }
955+
956+
response = JSON.stub(:parse, keyword_options_only_parse) do
957+
client.send_request(request: { jsonrpc: "2.0", id: "test_id", method: "tools/list" })
958+
end
959+
960+
assert_equal({ "result" => { "tools" => [] } }, response)
961+
end
962+
943963
def test_send_request_mirrors_x_mcp_header_params_into_mcp_param_headers
944964
# SEP-2243: on a modern connection, `tools/list` teaches the transport the `x-mcp-header`
945965
# declarations, and the following `tools/call` mirrors the annotated arguments into

0 commit comments

Comments
 (0)