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 test and b4 build. Run b4 typegen yourself to refresh editor types. Use b4 check to validate the app without writing files.
  • Two discovery locations. B4.run finds tools in <route>/tools/ (only that route can use them) and in src/tools/ (every route can). Put a tool in src/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 agent routes, the model calls tools. It picks them by name once b4 build binds them. The example uses a workflow route to show the typed ctx.tools call.