Class: LLM::Tool

Inherits:
Object
  • Object
show all
Extended by:
Function::Registry, Param
Defined in:
lib/llm/tool.rb,
lib/llm/tool/param.rb

Overview

The LLM::Tool class represents a local tool that can be called by an LLM. Under the hood, it is a wrapper around LLM::Function but allows the definition of a function (also known as a tool) as a class.

Examples:

class System < LLM::Tool
  name "system"
  description "Runs system commands"
  params do |schema|
    schema.object(command: schema.string.required)
  end

  def call(command:)
    {success: Kernel.system(command)}
  end
end

Defined Under Namespace

Modules: Param

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Param

param, required

Methods included from Function::Registry

clear_registry!, find_by_name, lock, register, registry, registry_key, tool_name, unregister

Class Method Details

.a2a(a2a, skill) ⇒ Class<LLM::Tool>

Returns a subclass of LLM::Tool

Parameters:

Returns:

  • (Class<LLM::Tool>)

    Returns a subclass of LLM::Tool



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/llm/tool.rb', line 70

def self.a2a(a2a, skill)
  name = skill.name.gsub(" ", "-")
  Class.new(LLM::Tool) do
    name(name)
    description(skill.description)
    parameter :input, String, "The input string"
    required %i[input]

    define_singleton_method(:inspect) do
      "<#{LLM::Utils.object_id(self)} name=#{name} (a2a)>"
    end
    singleton_class.alias_method :to_s, :inspect

    define_singleton_method(:a2a?) do
      true
    end

    define_method(:call) do |input:|
      res = a2a.send_message(input)
      {task: res}
    end
  end
end

.clear_registry!void

This method returns an undefined value.

Clear the registry



97
98
99
100
101
102
# File 'lib/llm/tool.rb', line 97

def self.clear_registry!
  lock do
    @__registry.each_value { LLM::Function.unregister(_1.function) }
    super
  end
end

.register(tool) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Registers a tool and its function.

Parameters:



108
109
110
111
# File 'lib/llm/tool.rb', line 108

def self.register(tool)
  super
  LLM::Function.register(tool.function)
end

.unregister(tool) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Unregister a tool from the registry

Parameters:



117
118
119
120
121
122
123
# File 'lib/llm/tool.rb', line 117

def self.unregister(tool)
  lock do
    LLM::Function.unregister(tool.function)
    super
    tool
  end
end

.inherited(tool) ⇒ void

This method returns an undefined value.

Registers the tool as a function when inherited

Parameters:

  • klass (Class)

    The subclass



129
130
131
132
133
134
135
# File 'lib/llm/tool.rb', line 129

def self.inherited(tool)
  LLM.lock(:inherited) do
    tool.instance_eval { @__monitor ||= Monitor.new }
    tool.function.define(tool)
    LLM::Tool.register(tool) unless tool.mcp? || tool.a2a?
  end
end

.name(name = nil) ⇒ String

Returns (or sets) the tool name

Parameters:

  • name (String, nil) (defaults to: nil)

    The tool name

Returns:

  • (String)


141
142
143
144
145
# File 'lib/llm/tool.rb', line 141

def self.name(name = nil)
  lock do
    name ? function.name(name) : function.name
  end
end

.registryArray<Class<LLM::Tool>>

Returns all registered tool classes with definitions.

Returns:



150
151
152
# File 'lib/llm/tool.rb', line 150

def self.registry
  super.select(&:name)
end

.find_by_name!(name) ⇒ Class<LLM::Tool>

Finds a registered tool by name.

Parameters:

  • name (String)

Returns:

Raises:



159
160
161
# File 'lib/llm/tool.rb', line 159

def self.find_by_name!(name)
  find_by_name(name) || raise(LLM::NoSuchToolError, "no such tool #{name.inspect}")
end

.description(desc = nil) ⇒ String

Returns (or sets) the tool description

Parameters:

  • desc (String, nil) (defaults to: nil)

    The tool description

Returns:

  • (String)


167
168
169
170
171
# File 'lib/llm/tool.rb', line 167

def self.description(desc = nil)
  lock do
    desc ? function.description(desc) : function.description
  end
end

.params {|schema| ... } ⇒ LLM::Schema

Returns (or sets) tool parameters

Yield Parameters:

  • schema (LLM::Schema)

    The schema object to define parameters

Returns:



177
178
179
180
181
# File 'lib/llm/tool.rb', line 177

def self.params(&)
  lock do
    function.tap { _1.params(&) }
  end
end

.functionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



185
186
187
188
189
# File 'lib/llm/tool.rb', line 185

def self.function
  lock do
    @function ||= LLM::Function.new(nil)
  end
end

.mcp?Boolean

Returns true if the tool is an MCP tool

Returns:

  • (Boolean)


194
195
196
# File 'lib/llm/tool.rb', line 194

def self.mcp?
  false
end

.a2a?Boolean

Returns true if the tool is an A2A tool

Returns:

  • (Boolean)


201
202
203
# File 'lib/llm/tool.rb', line 201

def self.a2a?
  false
end

.mcp(mcp, tool) ⇒ Class<LLM::Tool>

Returns a subclass of LLM::Tool

Parameters:

  • mcp (LLM::MCP)

    The MCP client that will execute the tool call

  • tool (Hash)

    A tool (as a raw Hash)

Returns:

  • (Class<LLM::Tool>)

    Returns a subclass of LLM::Tool



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/llm/tool.rb', line 42

def self.mcp(mcp, tool)
  Class.new(LLM::Tool) do
    name tool["name"]
    description tool["description"]
    params { tool["inputSchema"] || {type: "object", properties: {}} }

    define_singleton_method(:inspect) do
      "<#{LLM::Utils.object_id(self)} name=#{tool["name"]} (mcp)>"
    end
    singleton_class.alias_method :to_s, :inspect

    define_singleton_method(:mcp?) do
      true
    end

    define_method(:call) do |**args|
      mcp.call_tool(tool["name"], args)
    end
  end
end

Instance Method Details

#on_cancelnil

Called when an in-flight tool run is cancelled.

Returns:

  • (nil)


229
230
231
# File 'lib/llm/tool.rb', line 229

def on_cancel
  on_interrupt
end

#functionLLM::Function

Returns a function bound to this tool instance.

Returns:



208
209
210
# File 'lib/llm/tool.rb', line 208

def function
  @function ||= self.class.function.dup.tap { _1.define(self) }
end

#mcp?Boolean

Returns true if the tool is an MCP tool

Returns:

  • (Boolean)


215
216
217
# File 'lib/llm/tool.rb', line 215

def mcp?
  self.class.mcp?
end

#on_interruptnil

Called when an in-flight tool run is interrupted. Tools can override this to implement cooperative cleanup.

Returns:

  • (nil)


223
224
# File 'lib/llm/tool.rb', line 223

def on_interrupt
end