From 691c33870ca173f68513701c3d882c0e1518db1a Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Fri, 20 Nov 2026 13:07:57 +0900 Subject: [PATCH] Pass the access token to a handler the legacy `input_required` shim re-runs ## Motivation and Context When a handler on the handshake lifecycle returns `input_required`, the legacy shim sends the embedded requests to the client and runs the handler again with the answers. The re-run was dispatched without the access token of the request, so `server_context.auth_info` was `nil` on every round after the first: a tool that calls `server_context.require_scopes!` was answered `insufficient_scope` after the user had replied, and a handler that reads the caller's identity lost it. `tools/call`, `prompts/get` and `resources/read` were all affected. Every round belongs to the one authenticated request, so the shim now threads `auth_info` through to the re-run. ## How Has This Been Tested? `test/mcp/server_input_required_legacy_shim_test.rb`; both new tests fail on the previous library, where the second run sees no token. ## Breaking Changes None. --- lib/mcp/server.rb | 15 +++--- .../server_input_required_legacy_shim_test.rb | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/lib/mcp/server.rb b/lib/mcp/server.rb index 4fc37f7b..8a4fef01 100644 --- a/lib/mcp/server.rb +++ b/lib/mcp/server.rb @@ -865,6 +865,7 @@ def handle_request(request, method, session: nil, related_request_id: nil, auth_ session: session, related_request_id: related_request_id, cancellation: cancellation, + auth_info: auth_info, ) else serialize_input_required_result(result, instrumentation_data: instrumentation_data, envelope: envelope, request: params, method: method) @@ -1048,7 +1049,7 @@ def serialize_input_required_result(result, instrumentation_data:, envelope:, re # re-runs with `inputResponses`/`requestState` merged into the original params - the same deterministic replay # contract the modern client driver follows. The `requestState` round-trips in-process as the raw value # the handler wrote; `RequestStateSecurity` sealing is wire hardening and does not apply. - def run_legacy_input_required_shim(result, instrumentation_data:, method:, params:, session:, related_request_id:, cancellation:) + def run_legacy_input_required_shim(result, instrumentation_data:, method:, params:, session:, related_request_id:, cancellation:, auth_info:) rounds = 0 loop do @@ -1084,6 +1085,7 @@ def run_legacy_input_required_shim(result, instrumentation_data:, method:, param session: session, related_request_id: related_request_id, cancellation: cancellation, + auth_info: auth_info, ) return result unless result.is_a?(InputRequiredResult) @@ -1113,15 +1115,16 @@ def legacy_leg_params(entry) end # Re-runs the handler of one of the three MRTR-capable methods for - # the legacy shim. Legacy wire, so no envelope is threaded. - def redispatch_mrtr_method(method, params, instrumentation_data:, session:, related_request_id:, cancellation:) + # the legacy shim. Legacy wire, so no envelope is threaded. The access token is threaded: every round belongs to + # the one authenticated request, so the handler sees the same `auth_info` on each. + def redispatch_mrtr_method(method, params, instrumentation_data:, session:, related_request_id:, cancellation:, auth_info:) case method when Methods::TOOLS_CALL - call_tool(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation) + call_tool(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info) when Methods::PROMPTS_GET - get_prompt(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation) + get_prompt(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info) when Methods::RESOURCES_READ - contents = read_resource_contents(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation) + contents = read_resource_contents(params, instrumentation_data: instrumentation_data, session: session, related_request_id: related_request_id, cancellation: cancellation, auth_info: auth_info) contents.is_a?(InputRequiredResult) ? contents : build_read_resource_result(contents) else raise RequestHandlerError.new( diff --git a/test/mcp/server_input_required_legacy_shim_test.rb b/test/mcp/server_input_required_legacy_shim_test.rb index ce0f4b4b..a6092a65 100644 --- a/test/mcp/server_input_required_legacy_shim_test.rb +++ b/test/mcp/server_input_required_legacy_shim_test.rb @@ -315,6 +315,57 @@ def send_notification(method, params = nil, session_id: nil, related_request_id: assert_empty transport.sent end + test "a re-run handler sees the access token of the request, as its first run did" do + token = Server::OAuth::AccessToken.new(token: "t", subject: "alice", scopes: ["mcp:tools"]) + subjects = [] + tool = Tool.define(name: "greeter") do |server_context:| + subjects << server_context.auth_info&.subject + server_context.require_scopes!("mcp:tools") + if server_context.input_response("who") + Tool::Response.new([{ type: "text", text: "done" }]) + else + Server::InputRequiredResult.new( + input_requests: { "who" => { method: Methods::ELICITATION_CREATE, params: { message: "Who?" } } }, + ) + end + end + session = legacy_session(tools: [tool], transport: AnsweringTransport.new) + + response = session.handle(tool_call_request, auth_info: token) + + assert_equal "done", response.dig(:result, :content, 0, :text) + assert_equal ["alice", "alice"], subjects + end + + test "re-run prompt and resource handlers see the access token of the request" do + token = Server::OAuth::AccessToken.new(token: "t", subject: "alice") + subjects = [] + ask = ->(server_context) do + subjects << server_context.auth_info&.subject + next if server_context.input_response("who") + + Server::InputRequiredResult.new( + input_requests: { "who" => { method: Methods::ELICITATION_CREATE, params: { message: "Who?" } } }, + ) + end + prompt = Prompt.define(name: "asker") do |_args, server_context:| + ask.call(server_context) || Prompt::Result.new(messages: []) + end + server = Server.new(name: "shim_test", prompts: [prompt], input_required_legacy_shim: true) + server.resources_read_handler do |params, server_context:| + ask.call(server_context) || [{ uri: params[:uri], text: "read" }] + end + session = ServerSession.new(server: server, transport: AnsweringTransport.new, session_id: "legacy") + session.store_client_info(client: { name: "legacy" }, capabilities: { elicitation: { form: {} } }) + + prompt_response = session.handle({ jsonrpc: "2.0", id: 1, method: Methods::PROMPTS_GET, params: { name: "asker" } }, auth_info: token) + read_response = session.handle({ jsonrpc: "2.0", id: 2, method: Methods::RESOURCES_READ, params: { uri: "file:///a" } }, auth_info: token) + + assert_equal [], prompt_response.dig(:result, :messages) + assert_equal "read", read_response.dig(:result, :contents, 0, :text) + assert_equal ["alice"] * 4, subjects + end + private def legacy_session(tools:, transport:, capabilities: { elicitation: { form: {} } },