Help us shape the future of Go! Take the 2026 Go Developer Survey

tools

package
v0.21.168 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package tools defines the contract every tool implements so an LLM agent can call it: a name, a description injected into the prompt, a JSON Schema for the arguments, and a string-to-string call.

Implementing a tool

The recommended shape keeps the real logic in a typed Run and makes Call a thin JSON adapter, which also satisfies Tool:

type Request struct {
    City string `json:"city" jsonschema:"required,title=City,description=City name."`
}

func New() (*Tool, error) {
    sc, err := schema.New(reflect.TypeOf(Request{}))
    if err != nil {
        return nil, errors.Wrap(err, "failed to create schema")
    }
    return &Tool{funcParams: sc.Parameters}, nil
}

func (t *Tool) Name() string                   { return "get_weather" }
func (t *Tool) Description() string            { return "Current temperature for a city." }
func (t *Tool) Parameters() *jsonschema.Schema { return t.funcParams }

func (t *Tool) Run(ctx context.Context, req *Request) (*Result, error) { ... }

func (t *Tool) Call(ctx context.Context, input string) (string, error) {
    var req Request
    if err := json.Unmarshal(llmutils.CleanJSON([]byte(input)), &req); err != nil {
        return "", errors.WithStack(chatmodel.ErrFailedUnmarshalInput)
    }
    out, err := t.Run(ctx, &req)
    if err != nil {
        return "", err
    }
    return out.GetContent(), nil
}

Always run raw arguments through llmutils.CleanJSON: models wrap JSON in prose or code fences.

Error contract

Returning chatmodel.ErrFailedUnmarshalInput tells the assistant to ask the model to fix its arguments against the schema; it is not counted as a failure. Any other error is serialized into the tool response as {"error": "..."} so the model can recover, and the run continues. A failing tool therefore never aborts a run by itself.

Concurrency

All tools requested in one LLM turn are executed in parallel goroutines, so an implementation must be safe for concurrent use.

Naming

Tools are registered under their lower-cased name, so names that differ only in case collide. The name "web_search" is reserved: registering a tool with that name attaches the provider-native web search tool instead of the function. Keep names stable, because they appear in stored message history.

MCP

Implement IMCPTool (and usually MCPTool) to publish the same tool over the Model Context Protocol; *mcp.Server satisfies McpServerRegistrator.

See tools/tavily for a complete reference implementation, and Documentation/tools.md for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback interface {
	OnToolStart(ctx context.Context, tool ITool, assistantName, input string)
	OnToolEnd(ctx context.Context, tool ITool, assistantName, input string, output string)
	OnToolError(ctx context.Context, tool ITool, assistantName, input string, err error)
}

Callback receives tool execution events. Tool calls run in parallel goroutines, so implementations must be safe for concurrent use.

type Description added in v0.17.134

type Description struct {
	Name        string `json:"Name" yaml:"Name"`
	Description string `json:"Description" yaml:"Description"`
}

Description is a compact, prompt-friendly summary of a tool.

type Descriptions added in v0.17.134

type Descriptions []Description

Descriptions is a renderable collection of tool descriptions.

func GetDescriptions

func GetDescriptions(list ...ITool) Descriptions

GetDescriptions returns the name and description of each tool, for embedding a tool catalog in a prompt.

func (Descriptions) Render added in v0.17.134

func (d Descriptions) Render(format llmutils.RenderFormat) string

Render renders the descriptions in the requested format; markdown uses ToMarkdown, other formats delegate to llmutils.RenderToString.

func (Descriptions) ToMarkdown added in v0.17.134

func (d Descriptions) ToMarkdown() string

ToMarkdown renders the descriptions as a markdown list, with each description collapsed to a single line.

type IMCPTool added in v0.1.9

type IMCPTool interface {
	ITool
	RegisterMCP(registrator McpServerRegistrator) error
}

IMCPTool is an interface that extends ITool to include functionality for registering the tool with an MCP server. The RegisterMCP method allows the tool to be registered with a given MCP Server.

type ITool

type ITool interface {
	// Name returns the name of the Tool.
	Name() string
	// Description returns the description of the tool, to be used in the prompt.
	// Should not exceed LLM model limit.
	Description() string
	// Parameters returns the parameters definition of the function, to be used in the prompt.
	Parameters() *jsonschema.Schema

	// Call executes the tool with the given input and returns the result.
	// If the tool fails to parse the input, it should return ErrFailedUnmarshalInput error.
	Call(context.Context, string) (string, error)
}

ITool is a tool for the llm agent to interact with different applications.

type MCPTool added in v0.1.9

type MCPTool[I any] interface {
	IMCPTool
	RunMCP(context.Context, *I) (*mcp.ToolResponse, error)
}

MCPTool is an IMCPTool whose MCP handler returns rich content such as images or embedded resources.

type McpServerRegistrator added in v0.1.9

type McpServerRegistrator interface {
	RegisterTool(name string, description string, handler any) error
}

type Tool

type Tool[I any, O any] interface {
	ITool
	Run(context.Context, *I) (*O, error)
}

Tool is an ITool with a typed Run, so the same implementation is usable from Go code and from an LLM.

Directories

Path Synopsis
Package tavily implements a web search tool backed by the Tavily API (https://docs.tavily.com), and serves as the reference implementation of the tools.ITool contract.
Package tavily implements a web search tool backed by the Tavily API (https://docs.tavily.com), and serves as the reference implementation of the tools.ITool contract.

Jump to

Keyboard shortcuts

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