tool

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 20 Imported by: ? 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

Package tool defines a tool once and serves it to people and agents: as a command in a shell (CLI), as a rite command over rite.1, and as an MCP tool. A tool declares its input, its result, its progress events, and how they read as text; the host picks the format from the protocol it speaks.

Design: docs/specs/2026-11-28-ritesh-tools-design.md.

Index

Constants

View Source
const (
	ExitOK          = 0
	ExitFailed      = 1
	ExitUsage       = 2
	ExitInternal    = 70
	ExitTimeout     = 124
	ExitInterrupted = 130
)

Exit codes the CLI and rite adapters use.

Variables

This section is empty.

Functions

func AddRite

func AddRite(s *server.Server, r *Registry, o RiteOptions)

AddRite registers every tool of r as a command of s. A tool reads argv as in a shell and writes to the process's stdout and stderr, as the signed-in principal. Every client gets the usage and flags; the schemas go to clients that send the tool.schema capability.

func CLI

func CLI(ctx context.Context, r *Registry, args []string, o IO) int

CLI runs `args[0] args[1:]...` as a command in a shell, as Principal "local", and returns its exit code. With no args, or `help`, it lists the tools.

func Fail

func Fail(format string, args ...any) error

Fail returns an error a person or an agent can act on. Any other error a tool returns is unexpected and reported as an internal failure.

func IsFail

func IsFail(err error) bool

IsFail reports whether err is (or wraps) an error made by Fail.

func MCPServer

func MCPServer(r *Registry, impl *mcp.Implementation, o MCPOptions) *mcp.Server

MCPServer returns an MCP server serving every registered tool, through the SDK's typed AddTool with the registry's schemas. The host picks the transport: server.Run(ctx, &mcp.StdioTransport{}), or streamable HTTP.

func MustRegister

func MustRegister[In, Out, Event any](r *Registry, t Tool[In, Out, Event])

MustRegister is Register that panics, for registration at start.

func Register

func Register[In, Out, Event any](r *Registry, t Tool[In, Out, Event]) error

Register adds a tool. It fails when the tool's input has a field argv cannot take (anything but bool, string, integers, floats, and slices of those), when names, short flags, or JSON names collide, when positional fields are misordered, when a field is named help, json, or jsonl, and when a name is taken.

func Unsuccessful added in v0.3.1

func Unsuccessful(format string, args ...any) error

Unsuccessful is an error Run returns together with a result: the result is reported as usual and the run as failed, as ping does at 100% loss: exit 1 after the result in a shell and rite, isError over MCP with the result still in structuredContent. The message, when not empty, follows the result (on stderr in a shell and rite). The run is recorded as StatusFailed.

Types

type Annotations

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

Annotations are hints for agents, sent as MCP tool annotations.

type Call

type Call struct {
	// Principal is who asked: rite.1's Authenticate result, or "local" for
	// the CLI and stdio MCP.
	Principal string
	// Adapter is "cli", "rite", or "mcp".
	Adapter string
	Tool    string
}

Call describes one run to the tool and to the host's hooks.

type Flag

type Flag struct {
	Name     string // the JSON name; --name
	Short    string // one letter, or ""
	Arg      string // "1", "2", ... for a positional, "rest" for the remainder, or ""
	Type     string // boolean, string, integer, number
	Unsigned bool   // an unsigned integer
	Repeat   bool   // a slice
	Required bool
	Help     string
	Default  any
}

Flag is one field of a tool's input as argv takes it.

type Hooks

type Hooks struct {
	Admit  func(ctx context.Context, c Call) error
	Record func(ctx context.Context, c Call, o Outcome)
}

Hooks are the host's, both optional. Admit runs before a tool and may refuse the run (quotas, payment checks). Record runs after every run Admit saw. Input that fails parsing or the schema reaches neither.

type IO

type IO struct {
	Stdout io.Writer
	Stderr io.Writer
	Color  bool
	// Log, when set, receives the details of internal errors, and Stderr gets
	// only "internal error": the caller is not the operator. Nil: Stderr gets
	// the details, as in a shell.
	Log *slog.Logger
}

IO is where a run writes. Color is set only when Stdout is a terminal. Each event and the result reach Stdout in one Write, up to 32 KiB.

type Info

type Info struct {
	Name        string
	Summary     string
	Description string
	Annotations Annotations
	Input       *Schema
	Output      *Schema
	// Event is nil for a tool without events.
	Event *Schema
	// Usage is one line, generated: `name [flags] <arg> [rest...]`.
	Usage string
	// Flags are the input's fields as argv sees them, in field order.
	Flags []Flag
}

Info is what adapters publish about a tool.

type MCPOptions

type MCPOptions struct {
	// Principal names the caller of a request. Nil: "local", as for stdio.
	Principal func(req *mcp.CallToolRequest) string
	// Log receives internal errors, whose details never reach the client.
	// Nil: slog.Default().
	Log *slog.Logger
	// Server options for mcp.NewServer.
	Server *mcp.ServerOptions
}

MCPOptions configure the MCP adapter.

type None

type None struct{}

None is the Event type of a tool that reports no progress.

type Outcome

type Outcome struct {
	Status   Status
	Duration time.Duration
	Err      error
}

Outcome is what Record is told after a run.

type Registry

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

Registry holds tools. Register adds them; the adapters serve them.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) Run

func (r *Registry) Run(ctx context.Context, c Call, words []string, o IO) int

Run runs one tool from argv: the CLI's and the rite adapter's path.

func (*Registry) Tools

func (r *Registry) Tools() []*Info

Tools lists the registered tools in the order they were registered.

type Render

type Render struct{ Color bool }

Render says how text output may look: Color is set for a terminal only.

type RiteOptions

type RiteOptions struct {
	// Log receives internal errors, whose details never reach the client.
	// Nil: slog.Default().
	Log *slog.Logger
}

RiteOptions configure the rite adapter.

type Schema

type Schema = jsonschema.Schema

Schema is a JSON Schema (draft 2020-12), as the MCP Go SDK uses.

type Status

type Status string

Status is how a run ended.

const (
	StatusOK          Status = "ok"
	StatusRefused     Status = "refused"     // Admit refused it
	StatusFailed      Status = "failed"      // the tool returned Fail
	StatusTimeout     Status = "timeout"     // Tool.Timeout passed
	StatusInterrupted Status = "interrupted" // the caller stopped it
	StatusInternal    Status = "internal"    // an unexpected error or panic
)

The ways a run ends.

type Text

type Text[Out, Event any] struct {
	// Event writes one event as one line. Nil: events are not shown as text.
	Event func(w io.Writer, ev Event, o Render) error
	// Result writes the result. Nil: the result is shown as indented JSON.
	Result func(w io.Writer, out Out, o Render) error
}

Text renders a tool's events and result for a person.

type Tool

type Tool[In, Out, Event any] struct {
	Name string
	// Summary is one line: rite's help, MCP's title.
	Summary string
	// Description is for agents and `man`: what the tool does, what it
	// contacts, and its limits.
	Description string
	Annotations Annotations
	// Timeout ends a run (exit 124). Zero: no limit of the tool's own.
	Timeout time.Duration
	// Schema adjusts the generated input schema: defaults, enums, ranges.
	// A jsonschema tag carries a description only.
	Schema func(in *Schema)

	// Run does the work. emit reports progress; the returned Out is the
	// whole answer, and nothing exists only in events.
	Run  func(ctx context.Context, c Call, in In, emit func(Event)) (Out, error)
	Text Text[Out, Event]
}

Tool is one tool. In, Out, and Event are flat structs: see Register for the fields an In may have.

type UsageError

type UsageError struct{ Msg string }

UsageError is argv the tool cannot take: exit 2, with the usage.

func (*UsageError) Error

func (e *UsageError) Error() string

Jump to

Keyboard shortcuts

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