Implementation:Ollama Ollama XTools Registry
| Knowledge Sources | |
|---|---|
| Domains | Agent Loop, Tool Execution |
| Last Updated | 2025-02-15 00:00 GMT |
Overview
Manages the collection of available tools for the Ollama agent loop, providing registration, lookup, execution, and schema generation.
Description
The Registry struct holds a map of Tool implementations keyed by name. Provides CRUD operations (Register, Unregister, Get, Has) and bulk operations (Tools for API schema generation, Execute for running tool calls, Names/Count). DefaultRegistry creates a registry with built-in tools, controllable via environment variables (OLLAMA_AGENT_DISABLE_BASH). The Tool interface requires Name, Description, Schema, and Execute methods.
Usage
Created at the start of each agent session and passed to the chat loop. Tools can be dynamically added or removed during a session.
Code Reference
Source Location
- Repository: Ollama
- File: x/tools/registry.go
- Lines: 1-131
Signature
type Tool interface {
Name() string
Description() string
Schema() api.ToolFunction
Execute(args map[string]any) (string, error)
}
type Registry struct {
tools map[string]Tool
}
func NewRegistry() *Registry
func DefaultRegistry() *Registry
func (r *Registry) Register(tool Tool)
func (r *Registry) Unregister(name string)
func (r *Registry) Get(name string) (Tool, bool)
func (r *Registry) Has(name string) bool
func (r *Registry) Tools() api.Tools
func (r *Registry) Execute(call api.ToolCall) (string, error)
func (r *Registry) Names() []string
func (r *Registry) Count() int
Import
import "github.com/ollama/ollama/x/tools"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tool | Tool | Yes | Tool implementation to register |
| call | api.ToolCall | Yes | Tool call from LLM to execute |
Outputs
| Name | Type | Description |
|---|---|---|
| result | string | Tool execution output |
| error | error | Non-nil if tool is unknown or execution fails |
Usage Examples
registry := tools.DefaultRegistry()
// Check available tools
fmt.Println(registry.Names()) // ["bash"]
// Get tool schemas for LLM
apiTools := registry.Tools()
// Execute a tool call
result, err := registry.Execute(api.ToolCall{
Function: api.ToolCallFunction{
Name: "bash",
Arguments: api.ToolCallFunctionArguments{"command": "echo hello"},
},
})