Class: Agents::Tool

Inherits:
RubyLLM::Tool
  • Object
show all
Defined in:
lib/agents/tool.rb

Direct Known Subclasses

AgentTool, HandoffTool

Instance Method Summary collapse

Instance Method Details

#execute(tool_context, **params) ⇒ String

Execute the tool with context injection. This method is called by the runner and handles the thread-safe execution pattern by passing all state through parameters.

Parameters:

  • tool_context (Agents::ToolContext)

    The execution context containing shared state and usage tracking

  • params (Hash)

    Tool-specific parameters as defined by the tool’s param declarations

Returns:

  • (String)

    The tool’s result



48
49
50
# File 'lib/agents/tool.rb', line 48

def execute(tool_context, **params)
  perform(tool_context, **params)
end

#perform(tool_context, **params) ⇒ String

Perform the tool’s action. Subclasses must implement this method. This is where the actual tool logic lives. The method receives all execution state through parameters, ensuring thread safety.

Examples:

Implementing perform in a subclass

class SearchTool < Agents::Tool
  def perform(tool_context, query:, max_results: 10)
    api_key = tool_context.context[:search_api_key]
    results = SearchAPI.search(query, api_key: api_key, limit: max_results)
    results.map(&:title).join("\n")
  end
end

Parameters:

  • tool_context (Agents::ToolContext)

    The execution context

  • params (Hash)

    Tool-specific parameters

Returns:

  • (String)

    The tool’s result

Raises:

  • (NotImplementedError)

    If not implemented by subclass



68
69
70
# File 'lib/agents/tool.rb', line 68

def perform(tool_context, **params)
  raise NotImplementedError, "Tools must implement #perform(tool_context, **params)"
end