Custom tools
A tool is a Go function the model can call. tool.Define takes its name,
a description for the model and a typed handler; the input schema comes
from the input type:
weather, err := tool.Define("weather", "Look up the current weather.", func(_ context.Context, in weatherInput) (tool.Result, error) { return tool.Text("Sunny in " + in.City), nil }, tool.WithReadOnly(), tool.WithIdempotent())if err != nil { panic(err)}fmt.Println(string(weather.InputSchema()))
res, _ := weather.Run(context.Background(), nil, json.RawMessage(`{"city":"Lisbon"}`))fmt.Println(res.Content[0].Text)
_, err = weather.Run(context.Background(), nil, json.RawMessage(`{"units":"kelvin"}`))fmt.Println(errors.Is(err, tool.ErrInvalidInput), tool.Classify(err))// Output:// {"type":"object","properties":{"city":{"type":"string","description":"City name."},"units":{"type":"string","enum":["metric","imperial"]}},"required":["city"],"additionalProperties":false}// Sunny in Lisbon// true failedStruct tags shape the schema: json names a field, desc describes it,
enum lists allowed values, and omitempty makes a field optional. zelk
validates the model’s input against the schema before your handler runs,
so a handler sees only valid input.
Give tools to an agent
Section titled “Give tools to an agent”WithTools adds them after the built-in ones:
type orderInput struct { ID string `json:"id" desc:"The order's ID."`}lookup, err := tool.Define("order_status", "Look up an order's status.", func(_ context.Context, in orderInput) (tool.Result, error) { return tool.Text("Order " + in.ID + " shipped on Monday."), nil }, tool.WithReadOnly())if err != nil { panic(err)}p, err := replay.New(provider.Caps{}, replay.ToolCalls(history.ToolCall{ID: "c1", Name: "order_status", Input: json.RawMessage(`{"id":"A-17"}`)}), replay.Reported("met", "It shipped on Monday."))if err != nil { panic(err)}ctx := context.Background()a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeDefault), zelk.WithTools(lookup))if err != nil { panic(err)}defer a.Close(ctx)s, err := a.Session(ctx, "support-1")if err != nil { panic(err)}x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Where is order A-17?")))if err != nil { panic(err)}res, err := x.Wait(ctx)if err != nil { panic(err)}// A read-only tool runs in default mode without asking anyone.fmt.Println(res.Outcome, res.Text)// Output: success It shipped on Monday.Properties that decide permission
Section titled “Properties that decide permission”A tool’s options tell the permission engine what it does:
| Option | Means |
|---|---|
tool.WithReadOnly() |
it only reads, so it runs in every mode without asking |
tool.WithIdempotent() |
running it again with the same input is safe; with WithReadOnly, recovery after a crash runs it again |
tool.WithEnforcedIdempotency() |
it deduplicates by the call’s idempotency key, so recovery runs it again with the same key |
tool.WithWritesPath(field) |
it writes the one file the field names, so checkpoints save it first |
tool.WithURL(field) |
it acts on the URL in the field, so domain: rules apply to it |
tool.WithReadsPath(field) |
it reads the file the field names and sends it on, so protected paths and Read rules apply |
A tool that changes something and must not do it twice declares
tool.WithEnforcedIdempotency and then has to keep its promise: zelk gives
every attempt of a call the same key, tool.Call.IdempotencyKey() in a
DefineCall handler, and the tool records the key together with the change,
in the same transaction or write, and does nothing for a key it has seen.
Declare it only once the tool does. Without it, and unless the tool is read-only and idempotent, a call that
was running when the process died isn’t run again: the model is told its
outcome is unknown.
A tool that isn’t read-only asks in default mode and is refused in
plan. A tool from an MCP server is never trusted with these claims. Rules name it like any tool,
such as order_status or Deploy(*"service":"web"*).
The call’s context
Section titled “The call’s context”tool.DefineCall gives the handler the call itself, with the session’s
working directory and recorded facts:
defined, err := tool.DefineCall("Where", "Show the session working directory.", func(_ context.Context, c tool.Call, _ struct{}) (tool.Result, error) { return tool.Text(c.Cwd()), nil }, tool.WithReadOnly())if err != nil { panic(err)}result, err := defined.Run(context.Background(), nil, json.RawMessage(`{}`))if err != nil { panic(err)}fmt.Println(result.Content[0].Text)// Output: .Return tool.Text for a result, tool.Failuref for a failure the model
should see, and an error only for what isn’t the model’s to handle.
MCP and hooks from Go connects servers other people wrote and runs your code at fixed points of the loop.