tool

package
v1.17.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 31, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package tool defines interfaces for tools that agents can invoke.

Tools are capabilities that allow agents to perform specific actions, such as searching the web, executing code, or calling external APIs.

Tool Interface Hierarchy

The tool system uses a layered interface design inspired by ADK-Go but enhanced with Hector's streaming capabilities:

Tool (base)
  ├── CallableTool       - Simple synchronous execution (ADK-Go compatible)
  ├── StreamingTool      - Real-time incremental output (Hector extension)
  ├── IsLongRunning()    - Async operations (returns job ID, polls for completion)
  └── RequiresApproval() - HITL pattern (human approval before execution)

Execution Patterns

1. **Simple Tool** (CallableTool):

  • Executes synchronously
  • Returns final result
  • ADK-Go compatible

2. **Streaming Tool** (StreamingTool):

  • Yields incremental chunks during execution
  • Great for: command output, sub-agent responses, progress updates
  • Maps to A2A `artifact-update` with `append: true`

3. **HITL Tool** (RequiresApproval = true):

  • Pauses execution before running
  • Task transitions to `input_required` state
  • Human approves or denies, task resumes or stops
  • Maps to A2A `status-update` with `state: input_required`

4. **Async Tool** (IsLongRunning = true):

  • Returns immediately with job ID
  • Polls for completion (not yet implemented)
  • No human intervention needed

Creating Tools

Use the provided constructors for different tool types:

// Simple function tool (ADK-Go style)
tool := functiontool.New(myFunc)

// Streaming command tool (Hector extension)
tool := commandtool.New(commandtool.Config{...})

// MCP toolset (lazy connection)
toolset := mcptoolset.New(mcpConfig)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallableTool

type CallableTool interface {
	Tool

	// Call executes the tool with the given arguments.
	// Returns the result as a map and any error that occurred.
	// This is a blocking call that waits for completion.
	Call(ctx Context, args map[string]any) (map[string]any, error)

	// Schema returns the JSON schema for the tool's parameters.
	// Returns nil if the tool takes no parameters.
	Schema() map[string]any
}

CallableTool extends Tool with synchronous execution capability. This is the ADK-Go compatible interface - simple and straightforward.

type CancellableTool

type CancellableTool interface {
	Tool

	// Cancel requests graceful termination of the tool execution
	// identified by callID. Returns true if cancellation was initiated.
	// The tool should clean up resources and return promptly.
	Cancel(callID string) bool

	// SupportsCancellation returns true if this tool type supports cancellation.
	// Tools may return false if current state prevents cancellation.
	SupportsCancellation() bool
}

CancellableTool extends Tool with explicit cancellation capability. Tools implementing this interface can be individually cancelled during execution while the agent continues processing other work.

This is complementary to context cancellation:

  • Context cancellation: propagates from HTTP request, cascades to all operations
  • CancellableTool: targeted cancellation of specific tool execution by callID

Use cases:

  • Cancelling a long-running command without aborting the entire agent
  • Stopping a sub-agent call mid-execution
  • Interrupting MCP tool calls

type Context

type Context interface {
	agent.CallbackContext

	// FunctionCallID returns the unique ID of this tool invocation.
	FunctionCallID() string

	// Actions returns the event actions to modify state or request transfers.
	Actions() *agent.EventActions

	// SearchMemory searches the agent's memory for relevant information.
	SearchMemory(ctx context.Context, query string) (*agent.MemorySearchResponse, error)

	// Task returns the parent task for cascade cancellation registration.
	// Returns nil if no task is associated with this context.
	// Uses agent.CancellableTask to avoid circular imports.
	Task() agent.CancellableTask
}

Context provides the execution context for a tool.

type Definition

type Definition struct {
	Name        string
	Description string
	Parameters  map[string]any
}

Definition represents a tool definition for LLM function calling.

func ToDefinition

func ToDefinition(t Tool) Definition

ToDefinition converts a tool to a Definition.

type Predicate

type Predicate func(ctx agent.ReadonlyContext, tool Tool) bool

Predicate determines whether a tool should be available to the LLM. Used for filtering tools based on context, permissions, etc.

func AllowAll

func AllowAll() Predicate

AllowAll returns a Predicate that allows all tools.

func Combine

func Combine(predicates ...Predicate) Predicate

Combine combines multiple predicates with AND logic.

func DenyAll

func DenyAll() Predicate

DenyAll returns a Predicate that denies all tools.

func Not

func Not(p Predicate) Predicate

Not negates a predicate.

func Or

func Or(predicates ...Predicate) Predicate

Or combines multiple predicates with OR logic.

func StringPredicate

func StringPredicate(allowedTools []string) Predicate

StringPredicate creates a Predicate that allows only named tools.

type Request

type Request struct {
	// SystemInstruction can be appended to by tools
	SystemInstruction string

	// Messages is the conversation history (read-only recommended)
	Messages any

	// Config contains LLM configuration
	Config any

	// Metadata for tool-specific data
	Metadata map[string]any
}

Request is a simplified view of the LLM request for tool preprocessing. Tools can modify these fields to influence the LLM call.

type RequestProcessor

type RequestProcessor interface {
	// ProcessRequest modifies the LLM request before sending.
	// Called during the preprocessing phase of the reasoning loop.
	ProcessRequest(ctx Context, req *Request) error
}

RequestProcessor is an optional interface that tools can implement to modify the LLM request before it's sent.

This follows the adk-go pattern where tools can inject additional context, modify system instructions, or add tool-specific configuration.

Example use cases: - RAG tools adding retrieved context to the request - Authentication tools adding credentials - Context-aware tools modifying instructions based on state

type Result

type Result struct {
	// Content is the output content, typically a string or structured data.
	Content any

	// Streaming indicates this is an intermediate chunk, not the final result.
	// When true: UI should append to existing output
	// When false: This is the final result
	Streaming bool

	// Error is set if an error occurred during execution.
	// Can be set on intermediate chunks (partial failure) or final result.
	Error string

	// Metadata contains optional additional data about this result.
	Metadata map[string]any
}

Result represents the output of a tool execution. Used by both CallableTool (single result) and StreamingTool (multiple results).

type StreamingTool

type StreamingTool interface {
	Tool

	// CallStreaming executes the tool and yields incremental results.
	// Each yielded Result represents a chunk of output.
	//
	// The iterator pattern (iter.Seq2) aligns with Go 1.23+ and ADK-Go's
	// streaming patterns, providing consistent semantics across the codebase.
	//
	// Example implementation:
	//
	//	func (t *CommandTool) CallStreaming(ctx Context, args map[string]any) iter.Seq2[*Result, error] {
	//	    return func(yield func(*Result, error) bool) {
	//	        // Start command...
	//	        for line := range outputLines {
	//	            if !yield(&Result{Content: line, Streaming: true}, nil) {
	//	                return // Client disconnected
	//	            }
	//	        }
	//	        // Final result
	//	        yield(&Result{Content: finalOutput, Streaming: false}, nil)
	//	    }
	//	}
	CallStreaming(ctx Context, args map[string]any) iter.Seq2[*Result, error]

	// Schema returns the JSON schema for the tool's parameters.
	Schema() map[string]any
}

StreamingTool extends Tool with incremental output capability. This is a Hector extension for tools that produce real-time output.

Use StreamingTool for: - Command execution (docker pull, npm install, etc.) - Sub-agent calls that should stream responses - Any operation where incremental feedback improves UX

The streaming output maps to A2A `artifact-update` events with `append: true`, allowing the UI to display progress in real-time.

type Tool

type Tool interface {
	// Name returns the unique name of the tool.
	Name() string

	// Description returns a human-readable description of what the tool does.
	// Used by LLMs to decide when to use this tool.
	Description() string

	// IsLongRunning indicates whether this tool is a long-running async operation.
	// Long-running tools return a job ID and are polled for completion.
	// NOTE: For HITL (human approval), use RequiresApproval() instead.
	IsLongRunning() bool

	// RequiresApproval indicates whether this tool needs human approval before execution.
	// When true:
	// - Tool execution is paused before running
	// - Task transitions to `input_required` state
	// - Human must approve or deny the operation
	// - Tool executes only after approval
	//
	// This is semantically different from IsLongRunning():
	// - RequiresApproval: needs human decision, executes instantly once approved
	// - IsLongRunning: async operation, no human needed, polls for completion
	RequiresApproval() bool
}

Tool defines the base interface for a callable tool. This matches ADK-Go's tool.Tool interface for compatibility.

type ToolCall

type ToolCall struct {
	ID   string
	Name string
	Args map[string]any
}

ToolCall represents an LLM's request to invoke a tool.

type ToolResult

type ToolResult struct {
	ToolCallID string
	Content    string
	Error      string
	Metadata   map[string]any
}

ToolResult represents the result of a tool invocation. Used for building the conversation history.

type Toolset

type Toolset interface {
	// Name returns the name of this toolset.
	Name() string

	// Tools returns the available tools based on the current context.
	// This allows dynamic tool selection based on user, session, or other factors.
	Tools(ctx agent.ReadonlyContext) ([]Tool, error)
}

Toolset groups related tools and provides dynamic resolution. Toolsets enable lazy loading - tools are resolved only when needed.

Directories

Path Synopsis
Package agenttool provides a tool that allows an agent to call another agent.
Package agenttool provides a tool that allows an agent to call another agent.
Package approvaltool provides a Human-in-the-Loop (HITL) tool example.
Package approvaltool provides a Human-in-the-Loop (HITL) tool example.
Package commandtool provides a secure, streaming command execution tool.
Package commandtool provides a secure, streaming command execution tool.
Package controltool provides control flow tools for agent reasoning loops.
Package controltool provides control flow tools for agent reasoning loops.
Package functiontool provides a convenient way to create tools from typed Go functions.
Package functiontool provides a convenient way to create tools from typed Go functions.
Package mcptoolset provides a Toolset implementation for MCP servers.
Package mcptoolset provides a Toolset implementation for MCP servers.
Package searchtool provides a search tool for agents to query document stores.
Package searchtool provides a search tool for agents to query document stores.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL