ClaudeAgentSDK.Tool.Registry (claude_agent_sdk v0.6.9)
View SourceGenServer that manages tool registration and execution for SDK MCP servers.
The Registry maintains a mapping of tool names to their metadata and execution modules, enabling:
- Tool registration at server startup
- Tool lookup by name
- Tool execution dispatch
- Concurrent access to tool definitions
Usage
{:ok, pid} = Registry.start_link([])
tool = %{
name: :calculator,
description: "Calculate",
input_schema: %{type: "object"},
module: MyTools.Calculator
}
:ok = Registry.register_tool(pid, tool)
{:ok, result} = Registry.execute_tool(pid, :calculator, %{"expression" => "2+2"})
Summary
Functions
Returns a specification to start this module under a supervisor.
Executes a tool with given input.
Gets a tool by name from the registry.
Lists all registered tools.
Registers a tool with the registry.
Starts the registry GenServer.
Types
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec execute_tool(GenServer.server(), atom(), map()) :: {:ok, map()} | {:error, term()}
Executes a tool with given input.
Parameters
registry- Registry PID or namename- Tool name (atom)input- Input map matching tool's input_schema
Returns
{:ok, result}- Tool executed successfully{:error, reason}- Execution failed
Examples
{:ok, result} = Registry.execute_tool(pid, :add, %{"a" => 5, "b" => 3})
@spec get_tool(GenServer.server(), atom()) :: {:ok, tool_metadata()} | {:error, :not_found}
Gets a tool by name from the registry.
Parameters
registry- Registry PID or namename- Tool name (atom)
Returns
{:ok, tool}- Tool found{:error, :not_found}- Tool not found
Examples
{:ok, tool} = Registry.get_tool(pid, :calculator)
@spec list_tools(GenServer.server()) :: {:ok, [tool_metadata()]}
Lists all registered tools.
Parameters
registry- Registry PID or name
Returns
{:ok, tools} where tools is a list of tool metadata maps.
Examples
{:ok, tools} = Registry.list_tools(pid)
Enum.each(tools, fn tool -> IO.puts(tool.name) end)
@spec register_tool(GenServer.server(), tool_metadata()) :: :ok | {:error, :already_registered}
Registers a tool with the registry.
Parameters
registry- Registry PID or nametool- Tool metadata map
Returns
:ok- Successfully registered{:error, :already_registered}- Tool already exists
Examples
tool = %{
name: :add,
description: "Add numbers",
input_schema: %{type: "object"},
module: MyTools.Add
}
:ok = Registry.register_tool(pid, tool)
@spec start_link(keyword()) :: GenServer.on_start()
Starts the registry GenServer.
Options
:name- Optional name for the registry
Examples
{:ok, pid} = Registry.start_link([])
{:ok, pid} = Registry.start_link(name: :my_registry)