Add a Tool
A tool is a TypeScript file in a route's tools/ directory. Let's add a lookup tool to a /research workflow and call it with full types:
import { z } from "zod"
export default z.object({
query: z.string(),
count: z.number().default(0),
})NotesCopy link to section: Notes
- Types regenerate for you. The dev server runs typegen on reload, and so do
b4 run,b4 testandb4 build. Runb4 typegenyourself to refresh editor types. Useb4 checkto validate the app without writing files. - Two discovery locations. B4.run finds tools in
<route>/tools/(only that route can use them) and insrc/tools/(every route can). Put a tool insrc/tools/when several routes need it. - Declare types anywhere. B4.run reads input and output types written inline, declared as local aliases or interfaces, or imported from another module. Keep the root input an object so B4.run can emit the model-facing tool schema.
- Plain JSON in, plain JSON out. Tool inputs and outputs are serialized between the model and your code, so skip
Date,Map, and class instances. - In
agentroutes, the model calls tools. It picks them by name onceb4 buildbinds them. The example uses aworkflowroute to show the typedctx.toolscall.