tool

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package tool is part of the GoFastr harness.

See docs/harness-architecture.md for the architecture this package implements.

Package tool defines the Tool abstraction, the tool registry, and supporting types. Built-in tools live in tool/builtins/; MCP tools are bridged through mcpclient/source.go; plugins register tools via the Plugin.Register hook.

The Tool signature is locked from v0.1:

Run(ctx, call, sink) (*ToolResult, error)

so streaming tools, cancellation, and middleware share one shape (see docs/harness-architecture.md § Extensibility → Tool middleware).

Index

Constants

View Source
const MaxSpawnDepth = 2

MaxSpawnDepth is the hard ceiling on sub-agent recursion. A depth of 2 means: top-level agent can spawn sub-agents (depth 1), and those sub-agents may NOT spawn further sub-agents (depth 2 would be blocked). Adjust if you intentionally need deeper trees.

Variables

View Source
var ErrToolUnknown = errors.New("tool: unknown")

ErrToolUnknown is returned by Lookup when the tool is not registered.

Functions

func SessionFromContext

func SessionFromContext(ctx context.Context) (ids.SessionID, bool)

SessionFromContext extracts the session ID set by WithSession. Returns (zero, false) when no session is attached — tools that REQUIRE the session should error in that case.

func SpawnDepthFromContext

func SpawnDepthFromContext(ctx context.Context) int

SpawnDepthFromContext returns the current sub-agent depth (0 = top level). Defaults to 0 when no depth has been set.

func WithRegistry

func WithRegistry(ctx context.Context, r *Registry) context.Context

WithRegistry attaches the active tool registry to the dispatch ctx.

func WithSession

func WithSession(ctx context.Context, s ids.SessionID) context.Context

WithSession returns ctx with the session ID attached. Engines call this before dispatching tools.

func WithSpawnDepth

func WithSpawnDepth(ctx context.Context, depth int) context.Context

WithSpawnDepth attaches a sub-agent depth counter to ctx.

func WithSpawner

func WithSpawner(ctx context.Context, s Spawner) context.Context

WithSpawner attaches a Spawner to the dispatch context. Engines call this before dispatching tools so the Agent tool can pick it up.

Types

type EventSink

type EventSink interface {
	// EmitProgress publishes a ToolCallProgress event for the
	// current ToolCall. The partial string is informational; the
	// model never sees these directly — surfaces render them.
	EmitProgress(partial string)

	// EmitEvent publishes an arbitrary engine event. Used sparingly
	// for tools that legitimately need to surface non-progress
	// information (e.g., a tool that detects a sub-agent spawn).
	EmitEvent(e control.Event)
}

EventSink lets middleware and tools emit ToolCallProgress events (and other side-channel signals) without holding a reference to the engine bus directly. The dispatcher provides the implementation.

type Handler

type Handler func(ctx context.Context, call ToolCall, sink EventSink) (*ToolResult, error)

Handler is the curried form a middleware receives. Calling Handler invokes the rest of the chain (or the tool itself if no middleware remains).

func Chain

func Chain(base Handler, ms ...Middleware) Handler

Chain composes a Handler from a base handler and a sequence of middleware. Middleware are wrapped in registration order — the first middleware listed sees the request first.

type Middleware

type Middleware func(ctx context.Context, call ToolCall, sink EventSink, next Handler) (*ToolResult, error)

Middleware wraps a Handler to add behavior (permission, sandbox, timeout, redaction, etc.). Composed in order at registration time.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry is the in-memory catalog of tools available to the engine. Multiple ToolSources can register; their tools are merged by name, with later registrations rejected on collision (to make shadowing explicit rather than silent).

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty Registry.

func RegistryFromContext

func RegistryFromContext(ctx context.Context) (*Registry, bool)

RegistryFromContext extracts the Registry attached by WithRegistry.

func (*Registry) List

func (r *Registry) List() []Tool

List returns all tools, sorted by name. Callers should treat the result as a snapshot; the registry may change after this returns.

func (*Registry) Lookup

func (r *Registry) Lookup(name string) (Tool, error)

Lookup returns the tool with the given name, or an error if absent.

func (*Registry) Register

func (r *Registry) Register(ctx context.Context, src ToolSource) error

Register adds a ToolSource and ingests its current tool set. Returns an error on duplicate source name or duplicate tool name across sources.

func (*Registry) Replace

func (r *Registry) Replace(ctx context.Context, sourceName string, tools []Tool) error

Replace updates the tool set for a single source (e.g., MCP server reconnect with a different tool list). Tools no longer exposed by the source are removed; new ones are added; collisions with other sources return an error and leave the registry unchanged.

func (*Registry) SourceOf

func (r *Registry) SourceOf(toolName string) string

SourceOf returns the name of the source that registered the given tool, or "" if the tool is not registered.

type Spawner

type Spawner interface {
	Spawn(ctx context.Context, systemHint string, userPrompt string) (string, error)
}

Spawner lets a tool launch a fresh sub-agent inside the current session. The engine implements this — sub-agents share the parent's Provider, Model, Tools, and middleware, but get a fresh History. The Spawn call blocks until the sub-agent finishes and returns the final assistant text.

func SpawnerFromContext

func SpawnerFromContext(ctx context.Context) (Spawner, bool)

SpawnerFromContext returns the Spawner previously attached with WithSpawner, or (nil, false) if none.

type Tool

type Tool interface {
	// Name returns the canonical tool name (e.g., "Read", "Bash").
	Name() string

	// Description is the natural-language description surfaced to
	// the model in tool_use catalogs.
	Description() string

	// InputSchema returns the JSON Schema bytes describing the
	// tool's argument shape.
	InputSchema() []byte

	// Mutating declares whether this tool's invocation has
	// observable side effects. The persistence layer uses this for
	// the intent/outcome ledger: mutating tools fsync their intent
	// before spawning so crash-mid-call can be detected.
	//
	// Read-only tools (Read, Glob, Ls, Grep, WebFetch): false.
	// Anything that touches the filesystem, runs commands, or
	// performs an HTTP write: true.
	Mutating() bool

	// Run executes the tool. The sink lets the tool emit streaming
	// progress (ToolCallProgress events) before the final result.
	// Middleware (redaction, timeout, sandbox) wraps Run and
	// receives the same sink for transparent observation.
	Run(ctx context.Context, call ToolCall, sink EventSink) (*ToolResult, error)
}

type ToolCall

type ToolCall struct {
	ID    ids.CallID      // unique per turn; matches ToolUse.ID
	Name  string          // tool name to invoke
	Input json.RawMessage // argument JSON, validated against Tool.InputSchema()
}

ToolCall is an invocation of a tool by the model or by a plugin.

type ToolResult

type ToolResult struct {
	Content []control.ContentBlock // typically a single text block
	IsError bool
}

ToolResult is the outcome of a tool call, fed back to the model.

type ToolSource

type ToolSource interface {
	// Name identifies the source for logging + the catalog ("builtin",
	// "mcp:kiln", "plugin:foo").
	Name() string

	// Tools returns the current snapshot of tools this source
	// exposes. Sources that hot-reload (MCP server reconnects, skill
	// changes) should re-call Registry.Replace on change.
	Tools(ctx context.Context) ([]Tool, error)
}

ToolSource produces tools to register with the engine. Built-ins, MCP-bridged tools, and plugin-contributed tools all implement this.

Directories

Path Synopsis
Package builtins is part of the GoFastr harness.
Package builtins is part of the GoFastr harness.
Package pack is part of the GoFastr harness.
Package pack is part of the GoFastr harness.
Package permission is part of the GoFastr harness.
Package permission is part of the GoFastr harness.

Jump to

Keyboard shortcuts

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