Tools are functions that can be called by the AI model during conversations. They allow your agent to perform actions, retrieve information, or interact with external systems based on user requests.
Create a tool in src/tools/:
import { Autonomous } from "@botpress/runtime" ;
import { z } from "@botpress/sdk" ;
export default new Autonomous . Tool ({
name: "myTool" ,
description: "A tool that does something useful" ,
input: z . object ({}), // Input schema
output: z . object ({}), // Output schema
handler : async ({}) => {
// Tool logic
return {};
},
});
Define input and output schemas using Zod schemas. The AI model uses these schemas to understand when and how to call your tool:
import { Autonomous } from "@botpress/runtime" ;
import { z } from "@botpress/sdk" ;
export default new Autonomous . Tool ({
name: "getWeather" ,
description: "Get the current weather for a location" ,
input: z . object ({
location: z . string (). describe ( "The city or location name" ),
unit: z . enum ([ "celsius" , "fahrenheit" ]). optional (). describe ( "Temperature unit" ),
}),
output: z . object ({
temperature: z . number (),
condition: z . string (),
}),
handler : async ({ location , unit }) => {
// Fetch weather data
const weather = await fetchWeatherData ( location , unit );
return {
temperature: weather . temp ,
condition: weather . condition ,
};
},
});
Provide an array of tools to your agent in a conversation handler:
import { Conversation } from "@botpress/runtime" ;
import getWeather from "../tools/weather" ;
export default new Conversation ({
channel: "*" ,
handler : async ({ execute }) => {
await execute ({
instructions: "You are a helpful weather assistant." ,
tools: [ getWeather ],
});
},
});
You can convert actions to tools using the asTool() method:
import { Conversation , actions } from "@botpress/runtime" ;
export default new Conversation ({
channel: "*" ,
handler : async ({ execute }) => {
await execute ({
instructions: "You are a helpful assistant." ,
tools: [ actions . calculateTotal . asTool ()],
});
},
});
The handler function receives the input parameters and executes the tool’s logic. It can also call custom actions.
Basic handler
Calling actions
import { Autonomous } from "@botpress/runtime" ;
import { z } from "@botpress/sdk" ;
export default new Autonomous . Tool ({
name: "searchDatabase" ,
description: "Search the database" ,
input: z . object ({
query: z . string (),
}),
handler : async ({ query }) => {
// Perform search
return { results: [] };
},
});
You can provide multiple tools to your agent:
import { Conversation } from "@botpress/runtime" ;
import { getWeather , searchDatabase , processOrder } from "../tools/index" ;
export default new Conversation ({
channel: "*" ,
handler : async ({ execute }) => {
await execute ({
instructions: "You are a helpful assistant with access to various tools." ,
tools: [
getWeather ,
searchDatabase ,
processOrder ,
],
});
},
});
The description field is crucial—it helps the AI model understand when to use your tool. Write clear, concise descriptions:
export default new Autonomous . Tool ({
name: "calculateTotal" ,
description: "Calculate the total price of items including tax and shipping. Use this when the user asks about prices, costs, or totals." ,
input: z . object ({
items: z . array ( z . object ({
price: z . number (),
quantity: z . number (),
})),
}),
// ...
});
Best practices
Write clear, descriptive tool names and descriptions
Use Zod schemas with .describe() to provide context for each input parameter
Keep tool handlers focused on a single responsibility
Use tools for operations that require external data or actions
Test tools independently before using them in conversations
Tools are ideal for operations that the AI model needs to perform dynamically based on user requests. They enable your agent to go beyond just generating text and actually interact with systems, retrieve data, and perform actions.
Reference
Unique tool name. Must be a valid TypeScript identifier.
Human-readable description for the LLM. Helps the AI understand when and how to use the tool.
Optional Zui/Zod schema for input validation. Defines the parameters the tool accepts.
Optional Zui/Zod schema for output validation. Defines the return type of the tool.
handler
(args: any, ctx: object) => Promise<any>
required
Async function that implements the tool logic. See handler parameters below for details.
Optional array of alternative names for the tool.
Optional metadata object for storing additional tool information.
Optional partial input values that will be automatically applied to all tool calls, removing them from LLM control.
retry
(args: { input: any; attempt: number; error?: unknown }) => boolean | Promise<boolean>
Optional custom retry logic function. Receives an object with input, attempt number, and optional error, returns Boolean indicating whether to retry.
Handler parameters
The handler function receives validated input and execution context:
The validated input arguments matching the tool’s input schema. All properties are typed based on the schema definition.
Tool call context containing metadata about the tool call. Unique identifier for this specific tool call.