File: tool.md | Updated: 11/15/2025
Slash Forward
Docs Cookbook Providers Playground AI ElementsAI ElementsLeft sparkleRight sparkle AI GatewayGateway
Search...⌘KFeedback GitHub Vercel LogoSign in with Vercel
Chatbot
A collapsible component for displaying tool invocation details in AI chatbot interfaces.
The Tool component displays a collapsible interface for showing/hiding tool details. It is designed to take the ToolUIPart type from the AI SDK and display it in a collapsible interface.
Preview
Code
database_queryPending
database_queryAwaiting Approval
database_queryResponded
database_queryRunning
database_queryCompleted
database_queryError
database_queryDenied
AI Elements
shadcn CLI
Manual
npx ai-elements@latest add tool
Build a simple stateful weather app that renders the last message in a tool using useChat
.
Add the following component to your frontend:
app/page.tsx
'use client';
import { useChat } from '@ai-sdk/react';
import { DefaultChatTransport, type ToolUIPart } from 'ai';
import { Button } from '@/components/ui/button';
import { MessageResponse } from '@/components/ai-elements/message';
import {
Tool,
ToolContent,
ToolHeader,
ToolInput,
ToolOutput,
} from '@/components/ai-elements/tool';
type WeatherToolInput = {
location: string;
units: 'celsius' | 'fahrenheit';
};
type WeatherToolOutput = {
location: string;
temperature: string;
conditions: string;
humidity: string;
windSpeed: string;
lastUpdated: string;
};
type WeatherToolUIPart = ToolUIPart<{
fetch_weather_data: {
input: WeatherToolInput;
output: WeatherToolOutput;
};
}>;
const Example = () => {
const { messages, sendMessage, status } = useChat({
transport: new DefaultChatTransport({
api: '/api/weather',
}),
});
const handleWeatherClick = () => {
sendMessage({ text: 'Get weather data for San Francisco in fahrenheit' });
};
const latestMessage = messages[messages.length - 1];
const weatherTool = latestMessage?.parts?.find(
(part) => part.type === 'tool-fetch_weather_data',
) as WeatherToolUIPart | undefined;
return (
<div className="max-w-4xl mx-auto p-6 relative size-full rounded-lg border h-[600px]">
<div className="flex flex-col h-full">
<div className="space-y-4">
<Button onClick={handleWeatherClick} disabled={status !== 'ready'}>
Get Weather for San Francisco
</Button>
{weatherTool && (
<Tool defaultOpen={true}>
<ToolHeader type="tool-fetch_weather_data" state={weatherTool.state} />
<ToolContent>
<ToolInput input={weatherTool.input} />
<ToolOutput
output={
<MessageResponse>
{formatWeatherResult(weatherTool.output)}
</MessageResponse>
}
errorText={weatherTool.errorText}
/>
</ToolContent>
</Tool>
)}
</div>
</div>
</div>
);
};
function formatWeatherResult(result: WeatherToolOutput): string {
return `**Weather for ${result.location}**
**Temperature:** ${result.temperature}
**Conditions:** ${result.conditions}
**Humidity:** ${result.humidity}
**Wind Speed:** ${result.windSpeed}
*Last updated: ${result.lastUpdated}*`;
}
export default Example;
Add the following route to your backend:
app/api/weather/route.tsx
import { streamText, UIMessage, convertToModelMessages } from 'ai';
import { z } from 'zod';
// Allow streaming responses up to 30 seconds
export const maxDuration = 30;
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages),
tools: {
fetch_weather_data: {
description: 'Fetch weather information for a specific location',
parameters: z.object({
location: z
.string()
.describe('The city or location to get weather for'),
units: z
.enum(['celsius', 'fahrenheit'])
.default('celsius')
.describe('Temperature units'),
}),
inputSchema: z.object({
location: z.string(),
units: z.enum(['celsius', 'fahrenheit']).default('celsius'),
}),
execute: async ({ location, units }) => {
await new Promise((resolve) => setTimeout(resolve, 1500));
const temp =
units === 'celsius'
? Math.floor(Math.random() * 35) + 5
: Math.floor(Math.random() * 63) + 41;
return {
location,
temperature: `${temp}°${units === 'celsius' ? 'C' : 'F'}`,
conditions: 'Sunny',
humidity: `12%`,
windSpeed: `35 ${units === 'celsius' ? 'km/h' : 'mph'}`,
lastUpdated: new Date().toLocaleString(),
};
},
},
},
});
return result.toUIMessageStreamResponse();
}
Shows a tool in its initial state while parameters are being processed.
Preview
Code
web_searchPending
Shows a tool that's actively executing with its parameters.
Preview
Code
image_generationRunning
Shows a completed tool with successful results. Opens by default to show the results. In this instance, the output is a JSON object, so we can use the CodeBlock component to display it.
Preview
Code
database_queryCompleted
Shows a tool that encountered an error during execution. Opens by default to display the error.
Preview
Code
api_requestError
<Tool />Prop
Type
...props?React.ComponentProps<typeof Collapsible>
<ToolHeader />Prop
Type
type?ToolUIPart["type"]
state?ToolUIPart["state"]
className?string
...props?React.ComponentProps<typeof CollapsibleTrigger>
<ToolContent />Prop
Type
...props?React.ComponentProps<typeof CollapsibleContent>
<ToolInput />Prop
Type
input?ToolUIPart["input"]
...props?React.ComponentProps<"div">
<ToolOutput />Prop
Type
output?React.ReactNode
errorText?ToolUIPart["errorText"]
...props?React.ComponentProps<"div">
Task
A collapsible task list component for displaying AI workflow progress, with status indicators and optional descriptions.
Canvas
A React Flow-based canvas component for building interactive node-based interfaces.
On this page
Installation
Usage in AI SDK
Features
Examples
Input Streaming (Pending)
Input Available (Running)
Output Available (Completed)
Output Error
Props
<Tool />
<ToolHeader />
<ToolContent />
<ToolInput />
<ToolOutput />
GitHubEdit this page on GitHub Scroll to topCopy pageOpen in chat