createTool
Provide tools to an agent
Tools are defined using the createTool
function.
Options
The name of the tool. Used by the model to identify which tool to call.
A clear description of what the tool does. This helps the model understand when and how to use the tool.
A JSON Schema object or Zod type that defines the parameters the tool accepts. This is used to validate the model’s inputs and provide type safety.
The function that executes when the tool is called. It receives the validated parameters as its first argument and a context object as its second argument.
Lifecycle hooks that can intercept and modify inputs and outputs throughout the stages of tool execution.
Handler Function
The handler function receives two arguments:
input
: The validated parameters matching your schema definitioncontext
: An object containing:agent
: The Agent instance that called the toolnetwork
: The network instance, providing access to thenetwork.state
.
Example handler with full type annotations:
lifecycle
Called before the tool handler is executed. The onStart
hook can be used to:
- Modify input parameters before they are passed to the handler
- Prevent the tool from being called by throwing an error
Called after the tool handler has completed. The onFinish
hook can be used to:
- Modify the result before it is returned to the agent
- Perform cleanup operations