Skip to content

Iris Tools Package

The tools package provides the tool registry, middleware system, and helper types for building tool-augmented AI applications. As of v0.11.0+, it includes a comprehensive middleware system for logging, caching, rate limiting, retries, and circuit breakers.

type Tool interface {
Name() string // Unique identifier
Description() string // Human-readable description
Schema() ToolSchema // JSON Schema for parameters
Call(ctx context.Context, args json.RawMessage) (any, error) // Execution
}

Thread-safe tool registry for managing and executing tools:

func NewRegistry(opts ...RegistryOption) *Registry
func WithRegistryMiddleware(middlewares ...Middleware) RegistryOption
func (r *Registry) Register(t Tool) error
func (r *Registry) RegisterWithMiddleware(t Tool, middlewares ...Middleware) error
func (r *Registry) Get(name string) (Tool, bool)
func (r *Registry) List() []Tool
func (r *Registry) Execute(ctx context.Context, name string, args json.RawMessage) (any, error)

Middleware wraps tool execution to add cross-cutting concerns:

type ToolCallFunc func(ctx context.Context, args json.RawMessage) (any, error)
type Middleware func(next ToolCallFunc) ToolCallFunc
func Chain(middlewares ...Middleware) Middleware
func ApplyMiddleware(tool Tool, middlewares ...Middleware) Tool
func ForTools(toolNames []string, middleware Middleware) Middleware
func ExceptTools(toolNames []string, middleware Middleware) Middleware
MiddlewareFunctionDescription
LoggingWithLogging(logger)Log tool calls with timing
Detailed LoggingWithDetailedLogging(logger)Log with arguments (dev only)
TimeoutWithTimeout(duration)Enforce execution time limit
Rate LimitWithRateLimit(ratePerSecond)Token bucket rate limiting
CacheWithCache(cache, ttl)Cache idempotent tool results
ValidationWithBasicValidation()Validate JSON arguments
MetricsWithMetrics(collector)Collect execution metrics
RetryWithRetry(config)Retry transient failures
Circuit BreakerWithCircuitBreaker(config)Prevent cascading failures
type RetryConfig struct {
MaxAttempts int
InitialWait time.Duration
MaxWait time.Duration
Multiplier float64
Retryable func(err error) bool
}
type CircuitBreakerConfig struct {
FailureThreshold int // Failures before opening
SuccessThreshold int // Successes to close from half-open
OpenDuration time.Duration // Time to stay open
}
func DefaultRetryConfig() RetryConfig
func DefaultCircuitBreakerConfig() CircuitBreakerConfig

Share data between middleware:

type ToolContext struct {
ToolName string
CallID string
Iteration int
Schema ToolSchema
Metadata map[string]any
}
func ContextWithToolContext(ctx context.Context, tc *ToolContext) context.Context
func ToolContextFromContext(ctx context.Context) *ToolContext
// Create middleware chain
middleware := tools.Chain(
tools.WithLogging(log.Default()),
tools.WithTimeout(30 * time.Second),
tools.WithRetry(tools.DefaultRetryConfig()),
)
// Create registry with middleware
registry := tools.NewRegistry(
tools.WithRegistryMiddleware(middleware),
)
// Register tools
registry.Register(weatherTool)
registry.Register(searchTool)
// Use with ChatBuilder
resp, err := client.Chat("gpt-4o").
User("What's the weather in SF?").
Tools(registry.List()...).
GetResponse(ctx)