Class: RubyLLM::MCP::Handlers::SamplingHandler

Inherits:
Object
  • Object
show all
Includes:
Concerns::ErrorHandling, Concerns::GuardChecks, Concerns::Lifecycle, Concerns::Logging, Concerns::ModelFiltering, Concerns::Options, Concerns::SamplingActions
Defined in:
lib/ruby_llm/mcp/handlers/sampling_handler.rb

Overview

Base class for sampling request handlers Provides access to sample object, guards, and helper methods

Examples:

Basic sampling handler

class MySamplingHandler < RubyLLM::MCP::Handlers::SamplingHandler
  def execute
    response = default_chat_completion("gpt-4")
    accept(response)
  end
end

Sampling handler with guards

class GuardedSamplingHandler < RubyLLM::MCP::Handlers::SamplingHandler
  allow_models "gpt-4", "claude-3-opus"

  guard :check_model
  guard :check_token_limit

  def execute
    response = default_chat_completion(sample.model)
    accept(response)
  end

  private

  def check_model
    return true if model_allowed?(sample.model)
    "Model not allowed"
  end

  def check_token_limit
    return true if sample.max_tokens <= 4000
    "Too many tokens"
  end
end

Instance Attribute Summary collapse

Attributes included from Concerns::SamplingActions

#sample

Attributes included from Concerns::Options

#options

Instance Method Summary collapse

Methods included from Concerns::ModelFiltering

included

Methods included from Concerns::GuardChecks

#call, included

Methods included from Concerns::ErrorHandling

#call

Methods included from Concerns::Lifecycle

#call, #execute, included

Methods included from Concerns::Options

included

Constructor Details

#initialize(sample:, coordinator:, **options) ⇒ SamplingHandler

Initialize sampling handler

Parameters:

  • sample (RubyLLM::MCP::Sample)

    the sampling request

  • coordinator (Object)

    the coordinator managing the request

  • options (Hash)

    handler-specific options



56
57
58
59
60
# File 'lib/ruby_llm/mcp/handlers/sampling_handler.rb', line 56

def initialize(sample:, coordinator:, **options)
  @sample = sample
  @coordinator = coordinator
  super(**options)
end

Instance Attribute Details

#coordinatorObject (readonly)

Returns the value of attribute coordinator.



50
51
52
# File 'lib/ruby_llm/mcp/handlers/sampling_handler.rb', line 50

def coordinator
  @coordinator
end