Tool

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.

Tool Components Example

Installation

pnpm dlx shadcn-svelte@latest add https://svelte-ai-elements.vercel.app/r/tool.json

If you prefer using jsrepo, please install via the command below:

pnpm dlx jsrepo add @ai/elements/ai-elements/tool

Usage

Usage with AI SDK

Build a simple stateful weather app that renders the last message in a tool using useChat.

Add the following component to your frontend:

+page.svelte

Add the following route to your backend:

api/chat/+server.ts

Features

  • Collapsible interface for showing/hiding tool details
  • Visual status indicators with icons and badges
  • Support for multiple tool execution states (pending, running, completed, error)
  • Formatted parameter display with JSON syntax highlighting
  • Result and error handling with appropriate styling
  • Composable structure for flexible layouts
  • Accessible keyboard navigation and screen reader support
  • Consistent styling that matches your design system
  • Auto-opens completed tools by default for better UX

Props

Tool

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the tool container
childrenSnippet-Child components (typically ToolHeader, ToolInput, ToolOutput, ToolContent)
...restPropsCollapsibleProps-All other Collapsible component props are supported

ToolHeader

PropTypeDefaultDescription
typestring-The name/type of the tool being executed
state'input-streaming' | 'input-available' | 'output-available' | 'output-error'-Current state of the tool execution
classstring-Additional CSS classes to apply to the header
...restPropsCollapsibleTriggerProps-All other CollapsibleTrigger component props are supported

ToolInput

PropTypeDefaultDescription
inputany-The input parameters passed to the tool (will be JSON stringified)
classstring-Additional CSS classes to apply to the input container
...restPropsHTMLAttributes<HTMLDivElement>-All other div props are supported

ToolOutput

PropTypeDefaultDescription
outputany-The output result from the tool execution
errorTextstring-Error message to display if the tool execution failed
classstring-Additional CSS classes to apply to the output container
childrenSnippet-Custom content to render instead of the default output display
...restPropsHTMLAttributes<HTMLDivElement>-All other div props are supported

ToolContent

PropTypeDefaultDescription
classstring-Additional CSS classes to apply to the content container
childrenSnippet-Custom content to display in the collapsible area
...restPropsCollapsibleContentProps-All other CollapsibleContent component props are supported