ITool: a name, a description, an optional input schema, and a handler. The agent loop exposes the tool to the model, validates the arguments the model supplies against your schema, runs your handler, and feeds the result back into the conversation.
Defining a tool
ImplementITool<P, R>, where P is the validated input and R is the handler’s return type. Register the class with decorator.tool():
getTools, or pass it per request:
The ITool contract
| Member | Required | Purpose |
|---|---|---|
getName | ✅ | Unique tool name the model uses to call it. |
getDescription | ✅ | Plain-language description of when and how to use it — the model reads this. |
handler | ✅ | Runs the tool. Receives the validated input, returns the result. |
getInputSchema | — | ArkType assertion validating the model’s arguments before handler runs. |
onBeforeCall | — | Hook that runs before the tool is invoked; can short-circuit the call. |
onAfterCall | — | Hook that runs after the tool returns. |
A clear
getDescription and a precise getInputSchema are what make a tool reliable — the model decides whether and how to call the tool entirely from those two. Describe the input fields in the schema and the tool’s purpose in the description.Validated input
WhengetInputSchema is present, the agent loop validates the model’s arguments against it before your handler runs. Inside handler, the input already matches the schema — no manual checks needed. The schema is an ArkType assertion built with Assert from @ooneex/validation:
getInputSchema for a tool that takes no arguments.
Call hooks
A tool can observe or gate its own invocation with two optional hooks. When any tool on a run declares either hook, the agent loop bridges them onto the run automatically.onBeforeCall
Runs before the handler. Return a decision to allow, modify, or block the call — useful for permission checks or argument rewriting:
onAfterCall
Runs after the handler returns — for logging, metrics, or auditing:
ChatMiddlewareContext as their first argument, so they can read ctx.context — the runtime context passed to the call.