Give your AI tools custom UI components that show loading states and rich results.
Overview
Tools let the LLM perform actions (API calls, calculations, etc). This pattern connects a backend tool to a frontend component that renders its input/output with proper loading states. File convention:lib/ai/tools/{name}.ts → components/part/{name}.tsx
How it works
- Define tool with Vercel AI SDK’s
tool()- schema + execute function - Register in
lib/ai/tools/tools.ts - Route tool type to component in
components/message-parts.tsx - Render with loading/complete states based on
tool.state
| State | tool.state | Available |
|---|---|---|
| Loading | "input-available" | tool.input |
| Complete | "output-available" | tool.input + tool.output |
Code
1. Tool Definition
lib/ai/tools/get-weather.ts
2. Register Tool
lib/ai/tools/tools.ts
3. Route to Component
components/message-parts.tsx
4. UI Component
components/part/weather.tsx