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

Methods included from Param

param

Methods included from Function::Registry

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

Class Method Details

.mcp?Boolean

Returns true if the tool is an MCP tool

Returns:

  • (Boolean)


171
172
173
# File 'lib/llm/tool.rb', line 171

def self.mcp?
  false
end

.clear_registry!void

This method returns an undefined value.

Clear the registry



74
75
76
77
78
79
# File 'lib/llm/tool.rb', line 74

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:



85
86
87
88
# File 'lib/llm/tool.rb', line 85

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:



94
95
96
97
98
99
100
# File 'lib/llm/tool.rb', line 94

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



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

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

.name(name = nil) ⇒ String

Returns (or sets) the tool name

Parameters:

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

    The tool name

Returns:

  • (String)


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

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:



127
128
129
# File 'lib/llm/tool.rb', line 127

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:



136
137
138
# File 'lib/llm/tool.rb', line 136

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)


144
145
146
147
148
# File 'lib/llm/tool.rb', line 144

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:



154
155
156
157
158
# File 'lib/llm/tool.rb', line 154

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.



162
163
164
165
166
# File 'lib/llm/tool.rb', line 162

def self.function
  lock do
    @function ||= LLM::Function.new(nil)
  end
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
62
63
64
65
66
67
68
69
# File 'lib/llm/tool.rb', line 42

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

      define_singleton_method(:inspect) do
        "<LLM::Tool:0x#{object_id.to_s(16)} 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
    @mcp = false
    register(klass)
    klass
  ensure
    @mcp = false
  end
end