Skip to content

Commit 6abe5fa

Browse files
committed
fix: keep HTTP client handler exception details local
1 parent 48c0cc9 commit 6abe5fa

3 files changed

Lines changed: 29 additions & 11 deletions

File tree

‎docs/_client/transports.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ since servers deliver requests that are not tied to a client request on that str
184184
a JSON-RPC `-32601` (method not found) error. To handle methods other than `elicitation/create`, register directly on the transport with
185185
`http_transport.on_server_request("method/name") { |params| ... }`.
186186

187+
If a handler raises an unexpected exception, the HTTP transport answers with JSON-RPC error `-32603`, `Internal error`.
188+
The exception class and message are written to local stderr, not sent to the server.
189+
A `MCP::Client::ServerRequestError` keeps its chosen code and message in the response.
190+
187191
On a [modern](/client/lifecycle/) connection servers cannot send requests at all; the same
188192
registered handlers instead drive the requests embedded in `input_required` results,
189193
as documented on [Multi Round-Trip Requests](/client/mrtr/).

‎lib/mcp/client/http.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1196,12 +1196,13 @@ def dispatch_server_request(message)
11961196
error: { code: e.code, message: e.message },
11971197
}
11981198
rescue StandardError => e
1199+
warn("MCP::Client::HTTP: error handling #{message["method"]} request: #{e.class}: #{e.message}")
11991200
{
12001201
jsonrpc: JsonRpcHandler::Version::V2_0,
12011202
id: message["id"],
12021203
error: {
12031204
code: JsonRpcHandler::ErrorCode::INTERNAL_ERROR,
1204-
message: "Internal error handling #{message["method"]} request: #{e.message}",
1205+
message: "Internal error",
12051206
},
12061207
}
12071208
end

‎test/mcp/client/http_test.rb‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,20 +1688,33 @@ def test_send_request_answers_raising_handler_with_internal_error
16881688
expected_error_response = {
16891689
jsonrpc: "2.0",
16901690
id: 9,
1691-
error: { code: -32603, message: "Internal error handling elicitation/create request: boom" },
1691+
error: { code: -32603, message: "Internal error" },
16921692
}
1693-
error_stub = stub_request(:post, url).with(
1694-
body: expected_error_response.to_json,
1695-
).to_return(
1696-
status: 202, body: "",
1697-
)
1693+
posted_response = nil
1694+
error_stub = stub_request(:post, url).with { |http_request|
1695+
JSON.parse(http_request.body)["id"] == 9
1696+
}.to_return do |http_request|
1697+
posted_response = JSON.parse(http_request.body, symbolize_names: true)
1698+
{ status: 202, body: "" }
1699+
end
16981700

1699-
client.on_server_request("elicitation/create") { raise "boom" }
1701+
handler_error = Errno::ENOENT.new("/private/client/ui-config.yml")
1702+
client.on_server_request("elicitation/create") { raise handler_error }
17001703

1701-
response = client.send_request(request: request)
1704+
stdout, stderr = capture_io do
1705+
response = client.send_request(request: request)
17021706

1703-
assert_equal({ "content" => [] }, response["result"])
1704-
assert_requested(error_stub)
1707+
assert_equal({ "content" => [] }, response["result"])
1708+
assert_requested(error_stub)
1709+
end
1710+
1711+
assert_equal(expected_error_response, posted_response)
1712+
assert_empty(stdout)
1713+
assert_equal(
1714+
"MCP::Client::HTTP: error handling elicitation/create request: " \
1715+
"#{handler_error.class}: #{handler_error.message}\n",
1716+
stderr,
1717+
)
17051718
end
17061719

17071720
def test_send_request_answers_server_request_error_with_its_code

0 commit comments

Comments
 (0)