tool() helper from the Vercel AI SDK and registered in the chat route.
Tool structure
Every tool has three fields:| Field | Purpose |
|---|---|
description | Natural language description the model uses to decide when to call the tool |
parameters | A Zod schema that defines and validates the tool’s input arguments |
execute | Async function that receives the validated parameters and returns a result |
Simple tool example: getWeather
lib/ai/tools/get-weather.ts is the simplest tool in the codebase. It fetches weather data from a public API and returns the JSON result directly to the model.
lib/ai/tools/get-weather.ts
execute is serialized and sent back to the model as the tool result. The model can then use that data to compose its response.
Adding a custom tool
Create the tool file
Create a new file under
lib/ai/tools/. Use get-weather.ts as a starting point.lib/ai/tools/get-stock-price.ts
Import the tool in the chat route
Open
app/(chat)/api/chat/route.ts and import your tool at the top of the file alongside the existing tool imports:app/(chat)/api/chat/route.ts
Register the tool in the tools object
Add your tool to the
tools object passed to streamText:app/(chat)/api/chat/route.ts
Add the tool to experimental_activeTools
The
experimental_activeTools array controls which tools are offered to the model on each request. Add your tool’s key to the array:app/(chat)/api/chat/route.ts
Tools are disabled entirely for the reasoning model (
chat-model-reasoning) because it does not support tool calling.Streaming tools vs. simple tools
Not all tools work the same way. The codebase has two distinct patterns.- Simple tools
- Streaming tools
Simple tools like
getWeather call tool() directly and return a plain value. The execute function has no side effects beyond the API call.Data stream event types
When a tool writes todataStream, the client-side artifact components listen for specific event type values in their onStreamPart handler. The built-in event types are:
| Type | Emitted by | Description |
|---|---|---|
kind | createDocument | Artifact type being created |
id | createDocument | New document ID |
title | createDocument | Document title |
clear | createDocument, updateDocument | Signals the client to clear the artifact panel |
text-delta | textDocumentHandler | Incremental text content chunk |
code-delta | codeDocumentHandler | Incremental code content chunk |
suggestion | requestSuggestions | A single inline suggestion object |
finish | createDocument, updateDocument | Signals streaming is complete |
type string and handle it in the artifact client’s onStreamPart callback.