Skip to content

Commit 9d12f51

Browse files
authored
Merge pull request #600 from koic/accept_any_2xx_acknowledgement_of_a_notification
Accept any 2xx acknowledgement of a notification in the HTTP client
2 parents 2527cba + 243c19a commit 9d12f51

2 files changed

Lines changed: 89 additions & 4 deletions

File tree

‎lib/mcp/client/http.rb‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def send_request(request:)
400400
)
401401
end
402402

403+
request_id = request[:id] || request["id"]
403404
method = request[:method] || request["method"]
404405
params = request[:params] || request["params"]
405406
oauth_retried = false
@@ -427,11 +428,11 @@ def send_request(request:)
427428
nil
428429
end
429430

430-
body = resolve_response_body(stream, response, method, params)
431+
body = resolve_response_body(stream, response, request_id, method, params)
431432

432433
# Every way a response arrives (JSON body, SSE event, resumed stream) ends here. Checked before
433434
# anything is learned from the body, and only for a request: a notification awaits no response.
434-
reject_invalid_response!(body, method, params) if request[:id] || request["id"]
435+
reject_invalid_response!(body, method, params) if request_id
435436

436437
capture_session_info(method, response, body) if response
437438
capture_mcp_param_declarations(method, params, body)
@@ -1106,8 +1107,11 @@ def require_faraday!
11061107
end
11071108

11081109
# Determines the logical JSON-RPC body of the exchange after the POST stream finished
1109-
# (or was aborted because the response already arrived).
1110-
def resolve_response_body(stream, response, method, params)
1110+
# (or was aborted because the response already arrived). A notification (no `request_id`) awaits
1111+
# no response, so any success status acknowledges it, as it does for the TypeScript and Python clients,
1112+
# which check the content type of a response only for a request; the spec has the server answer
1113+
# a notification with 202, but a server answering with another 2xx is accepted as well.
1114+
def resolve_response_body(stream, response, request_id, method, params)
11111115
return stream.response if stream.response
11121116

11131117
# 202 Accepted is the server's ACK for a JSON-RPC notification or response; no body is expected.
@@ -1125,6 +1129,9 @@ def resolve_response_body(stream, response, method, params)
11251129
return stream.response if stream.response
11261130
end
11271131

1132+
# A stream that ends without a response has acknowledged a notification; there is nothing to resume for.
1133+
return if request_id.nil? && response.success?
1134+
11281135
# SEP-1699: a graceful close after a priming event (an event with an id) but before
11291136
# the response means "reconnect via GET to resume".
11301137
return await_response_after_disconnect(stream, method, params) if stream.primed?
@@ -1141,6 +1148,8 @@ def resolve_response_body(stream, response, method, params)
11411148
# added by the Faraday customizer may have parsed it already.
11421149
response.body.is_a?(String) ? parse_json_buffer(response.body, method, params) : response.body
11431150
else
1151+
return if request_id.nil? && response.success?
1152+
11441153
raise RequestHandlerError.new(
11451154
"Unsupported Content-Type: #{content_type.inspect}. Expected application/json or text/event-stream.",
11461155
{ method: method, params: params },

‎test/mcp/client/http_test.rb‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,6 +2739,82 @@ def test_send_request_does_not_check_the_body_answering_a_notification
27392739
assert_empty(response)
27402740
end
27412741

2742+
def test_connect_succeeds_when_the_initialized_notification_is_acknowledged_with_any_2xx
2743+
# The spec has the server answer a notification with 202 and no body; the TypeScript and Python
2744+
# clients accept any 2xx, whatever the body, and some hosted servers answer an empty 200.
2745+
acknowledgements = [
2746+
{ status: 200, headers: {}, body: "" },
2747+
{ status: 204, headers: {}, body: "" },
2748+
{ status: 200, headers: { "Content-Type" => "text/plain" }, body: "OK" },
2749+
]
2750+
acknowledgements.each do |acknowledgement|
2751+
WebMock.reset!
2752+
2753+
stub_request(:post, url).with { |req| JSON.parse(req.body)["method"] == "initialize" }.to_return do |req|
2754+
{
2755+
status: 200,
2756+
headers: { "Content-Type" => "application/json", "Mcp-Session-Id" => "session-abc" },
2757+
body: { jsonrpc: "2.0", id: JSON.parse(req.body)["id"], result: { protocolVersion: "2025-11-25" } }.to_json,
2758+
}
2759+
end
2760+
2761+
stub_request(:post, url).with { |req|
2762+
JSON.parse(req.body)["method"] == "notifications/initialized"
2763+
}.to_return(**acknowledgement)
2764+
2765+
client = HTTP.new(url: url)
2766+
client.connect(mode: :legacy)
2767+
2768+
assert_predicate(client, :connected?, acknowledgement.inspect)
2769+
assert_equal("session-abc", client.session_id)
2770+
end
2771+
end
2772+
2773+
def test_send_request_returns_nil_for_a_notification_answered_with_an_sse_stream_carrying_no_response
2774+
# A primed stream that closes without a response would make a request reconnect; a notification has
2775+
# no response to resume for, so the stream is its acknowledgement.
2776+
stub_request(:post, url).to_return(
2777+
status: 200,
2778+
headers: { "Content-Type" => "text/event-stream" },
2779+
body: "id: event-1\nretry: 100\ndata:\n\n",
2780+
)
2781+
2782+
response = client.send_request(request: { jsonrpc: "2.0", method: "notifications/initialized" })
2783+
2784+
assert_nil(response)
2785+
assert_not_requested(:get, url)
2786+
end
2787+
2788+
def test_send_request_still_raises_for_a_notification_answered_with_a_3xx
2789+
# Only a success status acknowledges a notification; a redirect, which the transport does not follow,
2790+
# keeps its handling, on both the body-less and the SSE path.
2791+
answers = [
2792+
{ status: 302, headers: { "Location" => "https://example.com/elsewhere" }, body: "", error_type: :unsupported_media_type },
2793+
{ status: 302, headers: { "Content-Type" => "text/event-stream" }, body: "data:\n\n", error_type: :parse_error },
2794+
]
2795+
answers.each do |answer|
2796+
stub_request(:post, url).to_return(status: answer[:status], headers: answer[:headers], body: answer[:body])
2797+
2798+
error = assert_raises(RequestHandlerError, answer.inspect) do
2799+
client.send_request(request: { jsonrpc: "2.0", method: "notifications/initialized" })
2800+
end
2801+
2802+
assert_equal(answer[:error_type], error.error_type)
2803+
end
2804+
end
2805+
2806+
def test_send_request_raises_error_for_a_request_answered_without_a_json_or_sse_body
2807+
[{ status: 200, headers: {}, body: "" }, { status: 204, headers: {}, body: "" }].each do |answer|
2808+
stub_request(:post, url).to_return(**answer)
2809+
2810+
error = assert_raises(RequestHandlerError, answer.inspect) do
2811+
client.send_request(request: { jsonrpc: "2.0", id: "test_id", method: "tools/list" })
2812+
end
2813+
2814+
assert_equal(:unsupported_media_type, error.error_type)
2815+
end
2816+
end
2817+
27422818
def test_send_request_raises_error_for_an_sse_response_that_is_not_json_rpc_2_0
27432819
# The first response-shaped event settles the exchange, so the valid one after it changes nothing.
27442820
sse_body = <<~SSE

0 commit comments

Comments
 (0)