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
- func AddRite(s *server.Server, r *Registry, o RiteOptions)
- func CLI(ctx context.Context, r *Registry, args []string, o IO) int
- func Fail(format string, args ...any) error
- func IsFail(err error) bool
- func MCPServer(r *Registry, impl *mcp.Implementation, o MCPOptions) *mcp.Server
- func MustRegister[In, Out, Event any](r *Registry, t Tool[In, Out, Event])
- func Register[In, Out, Event any](r *Registry, t Tool[In, Out, Event]) error
- type Annotations
- type Call
- type Flag
- type Hooks
- type IO
- type Info
- type MCPOptions
- type None
- type Outcome
- type Registry
- type Render
- type RiteOptions
- type Schema
- type Status
- type Text
- type Tool
- type UsageError
Constants ¶
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 ¶
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 ¶
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 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 ¶
MustRegister is Register that panics, for registration at start.
func Register ¶
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.
Types ¶
type Annotations ¶
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 Registry ¶
type Registry struct {
Hooks Hooks
// contains filtered or unexported fields
}
Registry holds tools. Register adds them; the adapters serve them.
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