Class: Tools::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/tools/registry.rb

Overview

Manages tool registration, schema export, and dispatch. Tools are registered by class and looked up by name at execution time. An optional context hash is passed to each tool’s constructor, allowing shared dependencies (e.g. a ShellSession) to reach tools that need them.

Examples:

registry = Tools::Registry.new(context: {shell_session: my_shell})
registry.register(Tools::Bash)
registry.execute("bash", {"command" => "ls"})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context: {}) ⇒ Registry

Returns a new instance of Registry.

Parameters:

  • context (Hash) (defaults to: {})

    keyword arguments forwarded to every tool constructor



20
21
22
23
# File 'lib/tools/registry.rb', line 20

def initialize(context: {})
  @tools = {}
  @context = context
end

Instance Attribute Details

#toolsHash{String => Class} (readonly)

Returns registered tool classes keyed by name.

Returns:

  • (Hash{String => Class})

    registered tool classes keyed by name



17
18
19
# File 'lib/tools/registry.rb', line 17

def tools
  @tools
end

Instance Method Details

#any?Boolean

Returns whether any tools are registered.

Returns:

  • (Boolean)

    whether any tools are registered



56
57
58
# File 'lib/tools/registry.rb', line 56

def any?
  @tools.any?
end

#execute(name, input) ⇒ String, Hash

Instantiate and execute a tool by name. The registry’s context is forwarded to the tool constructor as keyword arguments.

Parameters:

  • name (String)

    registered tool name

  • input (Hash)

    tool input parameters

Returns:

  • (String, Hash)

    tool execution result

Raises:



44
45
46
47
# File 'lib/tools/registry.rb', line 44

def execute(name, input)
  tool_class = @tools.fetch(name) { raise UnknownToolError, "Unknown tool: #{name}" }
  tool_class.new(**@context).execute(input)
end

#register(tool_class) ⇒ void

This method returns an undefined value.

Register a tool class. The class must respond to .tool_name.

Parameters:

  • tool_class (Class<Tools::Base>)

    the tool class to register



28
29
30
# File 'lib/tools/registry.rb', line 28

def register(tool_class)
  @tools[tool_class.tool_name] = tool_class
end

#registered?(name) ⇒ Boolean

Returns whether a tool with the given name is registered.

Parameters:

  • name (String)

    tool name to check

Returns:

  • (Boolean)

    whether a tool with the given name is registered



51
52
53
# File 'lib/tools/registry.rb', line 51

def registered?(name)
  @tools.key?(name)
end

#schemasArray<Hash>

Returns schema array for the Anthropic tools API parameter.

Returns:

  • (Array<Hash>)

    schema array for the Anthropic tools API parameter



33
34
35
# File 'lib/tools/registry.rb', line 33

def schemas
  @tools.values.map(&:schema)
end