Class: RubyLLM::MCP::Native::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/mcp/native/response_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinator) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



9
10
11
# File 'lib/ruby_llm/mcp/native/response_handler.rb', line 9

def initialize(coordinator)
  @coordinator = coordinator
end

Instance Attribute Details

#coordinatorObject (readonly)

Returns the value of attribute coordinator.



7
8
9
# File 'lib/ruby_llm/mcp/native/response_handler.rb', line 7

def coordinator
  @coordinator
end

Instance Method Details

#execute(result) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_llm/mcp/native/response_handler.rb', line 13

def execute(result)
  operation = CancellableOperation.new(result.id)
  coordinator.register_in_flight_request(result.id, operation)
  is_deferred = false

  begin
    # Execute in a separate thread that can be terminated on cancellation
    operation.execute do
      handled, deferred = dispatch_request(result)
      is_deferred = deferred
      handled
    end
  rescue Errors::RequestCancelled => e
    RubyLLM::MCP.logger.info("Request #{result.id} was cancelled: #{e.message}")
    # Don't send response - cancellation means result is unused
    # Clean up if this was a deferred elicitation
    Handlers::ElicitationRegistry.remove(result.id) if is_deferred
    true
  ensure
    # Only unregister if not deferred (async operations stay registered)
    coordinator.unregister_in_flight_request(result.id) unless is_deferred
  end
end