Class: LLM::Tool

Inherits:
Object
  • Object
show all
Extended by:
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

Class Method Details

.clear_registry!void

This method returns an undefined value.

Clear the registry



78
79
80
81
82
83
# File 'lib/llm/tool.rb', line 78

def self.clear_registry!
  lock do
    @registry.clear
    nil
  end
end

.description(desc = nil) ⇒ String

Returns (or sets) the tool description

Parameters:

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

    The tool description

Returns:

  • (String)


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

def self.description(desc = nil)
  lock do
    desc ? function.description(desc) : function.description
  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.



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

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

.inherited(tool) ⇒ void

This method returns an undefined value.

Registers the tool as a function when inherited

Parameters:

  • klass (Class)

    The subclass



109
110
111
112
113
114
115
# File 'lib/llm/tool.rb', line 109

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

.lockObject

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.



157
158
159
# File 'lib/llm/tool.rb', line 157

def self.lock(&)
  @__monitor.synchronize(&)
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



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

def self.mcp(mcp, tool)
  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
  unregister(klass)
end

.mcp?Boolean

Returns true if the tool is an MCP tool

Returns:

  • (Boolean)


165
166
167
# File 'lib/llm/tool.rb', line 165

def self.mcp?
  false
end

.name(name = nil) ⇒ String

Returns (or sets) the tool name

Parameters:

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

    The tool name

Returns:

  • (String)


121
122
123
124
125
# File 'lib/llm/tool.rb', line 121

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

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

Returns (or sets) tool parameters

Yield Parameters:

  • schema (LLM::Schema)

    The schema object to define parameters

Returns:



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

def self.params(&)
  lock do
    function.tap { _1.params(&) }
  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.

Register a tool in the registry

Parameters:



89
90
91
92
93
# File 'lib/llm/tool.rb', line 89

def self.register(tool)
  lock do
    @registry << tool
  end
end

.registryArray<LLM::Tool>

Note:

This method excludes tools who haven’t defined a name as well as tools defined via MCP.

Returns all subclasses of LLM::Tool

Returns:



68
69
70
71
72
# File 'lib/llm/tool.rb', line 68

def self.registry
  lock do
    @registry.select(&:name)
  end
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:



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

def self.unregister(tool)
  lock do
    @registry.delete(tool)
  end
end