Chatbot Tool Usage
WithuseChat and streamText, you can use tools in your chatbot application.
The AI SDK supports three types of tools in this context:
- Automatically executed server-side tools
- Automatically executed client-side tools
- Tools that require user interaction, such as confirmation dialogs
- The user enters a message in the chat UI.
- The message is sent to the API route.
- In your server side route, the language model generates tool calls during the
streamTextcall. - All tool calls are forwarded to the client.
- Server-side tools are executed using their
executemethod and their results are forwarded to the client. - Client-side tools that should be automatically executed are handled with the
onToolCallcallback. You must calladdToolOutputto provide the tool result. - Client-side tool that require user interactions can be displayed in the UI.
The tool calls and results are available as tool invocation parts in the
partsproperty of the last assistant message. - When the user interaction is done,
addToolOutputcan be used to add the tool result to the chat. - The chat can be configured to automatically submit when all tool results are available using
sendAutomaticallyWhen. This triggers another iteration of this flow.
Tool result submission can be configured using the
sendAutomaticallyWhen
option. You can use the lastAssistantMessageIsCompleteWithToolCalls helper
to automatically submit when all tool results are available. This simplifies
the client-side code while still allowing full control when needed.Example
In this example, we’ll use three tools:getWeatherInformation: An automatically executed server-side tool that returns the weather in a given city.askForConfirmation: A user-interaction client-side tool that asks the user for confirmation.getLocation: An automatically executed client-side tool that returns a random city.
API route
Client-side page
The client-side page uses theuseChat hook to create a chatbot application with real-time message streaming.
Tool calls are displayed in the chat UI as typed tool parts.
Please make sure to render the messages using the parts property of the message.
There are three things worth mentioning:
-
The
onToolCallcallback is used to handle client-side tools that should be automatically executed. In this example, thegetLocationtool is a client-side tool that returns a random city. You calladdToolOutputto provide the result (withoutawaitto avoid potential deadlocks).Always checkif (toolCall.dynamic)first in youronToolCallhandler. Without this check, TypeScript will throw an error like:Type 'string' is not assignable to type '"toolName1" | "toolName2"'when you try to usetoolCall.toolNameinaddToolOutput. -
The
sendAutomaticallyWhenoption withlastAssistantMessageIsCompleteWithToolCallshelper automatically submits when all tool results are available. -
The
partsarray of assistant messages contains tool parts with typed names liketool-askForConfirmation. The client-side toolaskForConfirmationis displayed in the UI. It asks the user for confirmation and displays the result once the user confirms or denies the execution. The result is added to the chat usingaddToolOutputwith thetoolparameter for type safety.
Error handling
Sometimes an error may occur during client-side tool execution. Use theaddToolOutput method with a state of output-error and errorText value instead of output record the error.
Tool Execution Approval
Tool execution approval lets you require user confirmation before a server-side tool runs. Unlike client-side tools that execute in the browser, tools with approval still execute on the server—but only after the user approves. Use tool execution approval when you want to:- Confirm sensitive operations (payments, deletions, external API calls)
- Let users review tool inputs before execution
- Add human oversight to automated workflows
Server Setup
Enable approval by settingneedsApproval on your tool. See Tool Execution Approval for configuration options including dynamic approval based on input.
Client-Side Approval UI
When a tool requires approval, the tool part state isapproval-requested. Use addToolApprovalResponse to approve or deny:
Auto-Submit After Approval
If nothing happens after you approve a tool execution, make sure you either
call
sendMessage manually or configure sendAutomaticallyWhen on the
useChat hook.lastAssistantMessageIsCompleteWithApprovalResponses to automatically continue the conversation after approvals:
Dynamic Tools
When using dynamic tools (tools with unknown types at compile time), the UI parts use a genericdynamic-tool type instead of specific tool types:
- MCP (Model Context Protocol) tools without schemas
- User-defined functions loaded at runtime
- External tool providers
Tool call streaming
Tool call streaming is enabled by default in AI SDK 5.0, allowing you to stream tool calls while they are being generated. This provides a better user experience by showing tool inputs as they are generated in real-time.useChat hook.
The typed tool parts of assistant messages will also contain partial tool calls.
You can use the state property of the tool part to render the correct UI.
Server-side Multi-Step Calls
You can also use multi-step calls on the server-side withstreamText.
This works when all invoked tools have an execute function on the server side.