tool

package
v1.0.12 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Callable tools the model may invoke, with a registry that runs them safely

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallFrom

func CallFrom(ctx context.Context) (protocol.ToolCall, bool)

Call the context was opened for, if any

func Report

func Report(ctx context.Context, p Progress)

Delivers progress to the reporter in the context, stamping the call identity

func ValidName

func ValidName(name string) bool

Reports whether name is acceptable to every supported protocol, letters, digits, underscores and dashes up to 64 bytes

func ValidSchema

func ValidSchema(raw json.RawMessage) error

Checks that a parameter schema is a JSON object describing an object

func WithCall

func WithCall(ctx context.Context, call protocol.ToolCall) context.Context

Carries the call a tool is serving so progress reports name it

func WithReporter

func WithReporter(ctx context.Context, r Reporter) context.Context

Carries a reporter so tools can stream output and progress to whoever runs them

Types

type Annotated

type Annotated interface {
	Annotations() Annotations
}

Tool that declares its annotations

type Annotations

type Annotations struct {
	ReadOnly    bool
	Destructive bool
	Idempotent  bool
	OpenWorld   bool
}

Behavior hints in the shape MCP defines, the scheduler reading ReadOnly and Idempotent and policies the rest

func Describe

func Describe(t Tool) Annotations

Reports the annotations of t, with every hint off for tools that declare none

type Outcome

type Outcome struct {
	protocol.ToolResult
	Data     any
	Started  time.Time
	Duration time.Duration
}

Completed run of one call with its structured data and timing

type Progress

type Progress struct {
	CallID string
	Tool   string
	// Name of the output stream the text belongs to, such as stdout or stderr
	Stream string
	Text   string
	Done   int
	Total  int
}

Partial output or a progress mark a running tool reports while it works

type Registry

type Registry struct {
	// Budget for one run, unlimited when zero
	Timeout time.Duration
	// contains filtered or unexported fields
}

Ordered set of tools keyed by name

func (*Registry) Add

func (r *Registry) Add(tools ...Tool) error

Adds tools in order, rejecting bad names, missing schemas and duplicates without registering any

func (*Registry) Definitions

func (r *Registry) Definitions() []protocol.Tool

Lists definitions of enabled tools in registration order for a protocol request

func (*Registry) Disabled

func (r *Registry) Disabled() []string

Names turned off, sorted, registered or not

func (*Registry) Enabled

func (r *Registry) Enabled(name string) bool

Reports whether the tool is on, true for names never turned off

func (*Registry) Len

func (r *Registry) Len() int

func (*Registry) Lookup

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

func (*Registry) Names

func (r *Registry) Names() []string

Lists registered names in registration order

func (*Registry) OnChange

func (r *Registry) OnChange(fn func())

Registers a callback run after every change to the set of tools

func (*Registry) ReadOnly

func (r *Registry) ReadOnly() []string

Lists the names of registered tools annotated read only, sorted

func (*Registry) Remove

func (r *Registry) Remove(name string) bool

Drops the named tool and reports whether it was registered

func (*Registry) Run

func (r *Registry) Run(ctx context.Context, call protocol.ToolCall) (out Outcome)

Runs one call, turning unknown tools, errors, panics and cancellation into error results

func (*Registry) RunEach

func (r *Registry) RunEach(ctx context.Context, calls []protocol.ToolCall, done func(i int, out Outcome)) []Outcome

Runs read only calls concurrently and every other call alone in order, reporting each outcome through done as soon as it is known

func (*Registry) SetEnabled

func (r *Registry) SetEnabled(name string, on bool)

Turns a tool on or off by name, an off tool withheld from requests and refused when called

type Reporter

type Reporter func(Progress)

Receives progress from running tools

type Result

type Result struct {
	Content []protocol.Part
	IsError bool
	// Structured form of the outcome for callers that render rather than read, never sent to the model
	Data any
}

Outcome of one run, before the registry attaches the call identity

func Error

func Error(s string) Result

Builds a failed text result the model is expected to recover from

func Errorf

func Errorf(format string, args ...any) Result

Builds a failed formatted text result

func Image

func Image(mediaType string, data []byte) Result

Builds a successful result carrying one image

func Text

func Text(s string) Result

Builds a successful text result

func Textf

func Textf(format string, args ...any) Result

Builds a successful formatted text result

func (Result) With

func (r Result) With(data any) Result

Copy of the result carrying structured data

type Tool

type Tool interface {
	Definition() protocol.Tool
	Run(ctx context.Context, args json.RawMessage) (Result, error)
}

Something the model can call by name with JSON arguments

Directories

Path Synopsis
File tools that read, write, edit, glob and grep inside an optional root
File tools that read, write, edit, glob and grep inside an optional root
Model Context Protocol client that exposes the tools of a server as local tools
Model Context Protocol client that exposes the tools of a server as local tools
Web tool that fetches a URL and returns its content as readable text
Web tool that fetches a URL and returns its content as readable text

Jump to

Keyboard shortcuts

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