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



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

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)


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

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.



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

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



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

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.



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

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



41
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 41

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)


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

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)


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

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:



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

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:



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

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:



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

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:



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

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