Skip to content

Cancelling during an input_required round still runs the remaining input handlers #727

Backlog

Description

@yorzi

Describe the bug

MCP::Client#drive_input_required (lib/mcp/client.rb:822) checks the caller's cancellation: token only when it sends a request. Between requests it does not look at the token:

  1. When an input_required result embeds several requests and the token is cancelled while the first handler runs, fulfill_input_requests (:876) still calls the remaining handlers. These are on_elicitation, on_sampling and on_roots handlers, so a user who cancelled can still be prompted, or a model called.
  2. A requestState-only result is waited out with sleep(backoff) (:852), which cancellation does not interrupt.

CancelledError is raised in both cases, but only once the handlers or the wait are done.

To Reproduce

Save as repro.rb in the repository root and run bundle exec ruby -Ilib repro.rb on main (4962426):

require "mcp"
require "mcp/client"

transport = Object.new
def transport.send_notification(notification:); end
def transport.send_request(request:)
  ask = ->(message) { { "method" => "elicitation/create", "params" => { "message" => message } } }
  { "result" => { "resultType" => "input_required", "inputRequests" => { "a" => ask.call("first"), "b" => ask.call("second") } } }
end

cancellation = MCP::Cancellation.new
client = MCP::Client.new(transport: transport)
client.on_elicitation do |params|
  puts "handler: #{params["message"]}"
  cancellation.cancel(reason: "user stopped")
  { action: "decline" }
end

begin
  client.call_tool(name: "my_tool", cancellation: cancellation)
rescue MCP::CancelledError => e
  puts "#{e.class}: #{e.reason}"
end

Expected behavior

The handler that is running may finish. No further handler is called, and a backoff wait ends as soon as the token is cancelled. docs/_client/cancellation.md says the calling thread is woken with MCP::CancelledError.

Logs

handler: first
handler: second
MCP::CancelledError: user stopped

Ruby 4.0.9.

Additional context

#707 fixed cancellation in the server's legacy input_required shim, which is a separate path.

I have a fix with tests and will open a PR for it.

Activity

  1. added
    bug
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions