Class: RubyLLM::MCP::Handlers::HumanInTheLoopHandler

Inherits:
Object
  • Object
show all
Includes:
Concerns::ApprovalActions, Concerns::AsyncExecution, Concerns::ErrorHandling, Concerns::GuardChecks, Concerns::Lifecycle, Concerns::Logging, Concerns::Options, Concerns::RegistryIntegration, Concerns::Timeouts, Concerns::ToolFiltering
Defined in:
lib/ruby_llm/mcp/handlers/human_in_the_loop_handler.rb

Overview

Base class for human-in-the-loop approval handlers Provides access to tool details, guards, and async support

Examples:

Basic approval handler

class MyApprovalHandler < RubyLLM::MCP::Handlers::HumanInTheLoopHandler
  def execute
    if safe_tool?(tool_name)
      approve
    else
      deny("Tool requires approval")
    end
  end

  private

  def safe_tool?(name)
    ["read_file", "list_files"].include?(name)
  end
end

Approval handler with guards and filtering

class SecureApprovalHandler < RubyLLM::MCP::Handlers::HumanInTheLoopHandler
  allow_tools "read_file", "list_files"
  deny_tools "rm", "delete_all"

  guard :check_tool_safety

  def execute
    return deny("Tool denied") if tool_denied?
    approve
  end

  private

  def check_tool_safety
    return true if tool_allowed?
    "Tool not in safe list"
  end
end

Async approval handler

class AsyncApprovalHandler < RubyLLM::MCP::Handlers::HumanInTheLoopHandler
  async_execution timeout: 300

  on_timeout :handle_timeout_event

  def execute
    notify_user(tool_name, parameters)
    defer # Returns { status: :deferred, timeout: 300 }
  end

  private

  def handle_timeout_event
    deny("User did not respond in time")
  end
end

Instance Attribute Summary collapse

Attributes included from Concerns::ApprovalActions

#approval_id, #parameters, #tool_name

Attributes included from Concerns::ToolFiltering

#tool_name

Attributes included from Concerns::Options

#options

Instance Method Summary collapse

Methods included from Concerns::ToolFiltering

included

Methods included from Concerns::GuardChecks

#call, included

Methods included from Concerns::Timeouts

#handle_timeout, included

Methods included from Concerns::AsyncExecution

#async?, included, #timeout

Methods included from Concerns::ErrorHandling

#call

Methods included from Concerns::Lifecycle

#call, #execute, included

Methods included from Concerns::Options

included

Constructor Details

#initialize(tool_name:, parameters:, approval_id:, coordinator:, **options) ⇒ HumanInTheLoopHandler

Initialize human-in-the-loop handler

Parameters:

  • tool_name (String)

    the tool name

  • parameters (Hash)

    the tool parameters

  • approval_id (String)

    unique identifier for this approval

  • coordinator (Object)

    the coordinator managing the request

  • options (Hash)

    handler-specific options



83
84
85
86
87
88
89
# File 'lib/ruby_llm/mcp/handlers/human_in_the_loop_handler.rb', line 83

def initialize(tool_name:, parameters:, approval_id:, coordinator:, **options)
  @tool_name = tool_name
  @parameters = parameters
  @approval_id = approval_id
  @coordinator = coordinator
  super(**options)
end

Instance Attribute Details

#coordinatorObject (readonly)

Returns the value of attribute coordinator.



75
76
77
# File 'lib/ruby_llm/mcp/handlers/human_in_the_loop_handler.rb', line 75

def coordinator
  @coordinator
end