Tools whose executor closes over React state. Declare the contract with stubTool() in a "use generative" file and supply the executor with useAuiToolOverrides.
Most tools are static: their executor is fixed at build time. Some tools need to read or write component state, for example to add to a list the user can also edit, mutate a canvas, or pre-fill a form. The executor for those has to close over a React setter, which can't live in a build-split "use generative" file.
The pattern: declare the model-facing contract in the toolkit with execute: stubTool(), and supply the real executor at runtime in the component that owns the state with useAuiToolOverrides. The tool kinds table lists stubTool() next to the other kinds; this page is the walkthrough.
Warning
useAuiToolOverrides is experimental and its API may change.
How the pieces fit
- The toolkit entry keeps the description, parameters, and renderer. The compiler marks it
type: "frontend", sends its schema to your server build, and strips the stub from the client build. useAuiToolOverridesregisters a partial tool at priority 1000. Registrations are merged per tool name with the higher priority winning field by field, so the override'sexecutereplaces the missing one and everything else comes from the toolkit.- The runtime runs that executor in the browser when the model calls the tool, like any other frontend
execute.
1. Declare the contract with stubTool()
Give the tool its description, parameters, and renderer, and mark the executor as a stub. The model can call the tool because your server knows the schema; nothing executes until a component supplies the implementation. Keep the schema in a separate non-directive module so the component can import the argument type too:
import { z } from "zod";
export type Task = { id: string; title: string; done: boolean };
export const manageTasksParameters = z.object({
action: z.enum(["add", "toggle", "remove", "clear"]),
id: z.string().optional(),
title: z.string().optional(),
});
export type ManageTasksArgs = z.infer<typeof manageTasksParameters>;"use generative";
import { defineToolkit, stubTool } from "@assistant-ui/react";
import { manageTasksParameters } from "./state";
export default defineToolkit({
manage_tasks: {
description:
'Manage tasks on the board. Actions: "add" (requires title), ' +
'"toggle" (requires id), "remove" (requires id), "clear".',
parameters: manageTasksParameters,
execute: stubTool(),
renderText: {
running: ({ args }) => `Updating tasks: ${args.action}`,
complete: "Tasks updated",
},
},
});A stub tool is a frontend tool, so the compiler requires a render or renderText. Your route has to import this toolkit through AISDKToolkit; see Backend tools.
2. Supply the executor with useAuiToolOverrides
The component that owns the state registers the toolkit, then renders a small child that returns null and provides the executor closing over setTasks:
import {
AuiConfig,
AuiProvider,
Tools,
useAui,
useAuiToolOverrides,
} from "@assistant-ui/react";
import { useState, type Dispatch, type SetStateAction } from "react";
import type { ManageTasksArgs, Task } from "./state";
import toolkit from "./task-board-toolkit";
function TaskBoard() {
const [tasks, setTasks] = useState<Task[]>([]);
const aui = useAui();
const config = AuiConfig({ tools: Tools({ toolkit }) });
return (
<AuiProvider extends={aui} config={config}>
<TaskBoardToolOverrides setTasks={setTasks} />
<TaskList tasks={tasks} />
</AuiProvider>
);
}
function TaskBoardToolOverrides({
setTasks,
}: {
setTasks: Dispatch<SetStateAction<Task[]>>;
}) {
useAuiToolOverrides({
manage_tasks: {
execute: async ({ action, id, title }: ManageTasksArgs) => {
switch (action) {
case "add": {
const task = {
id: crypto.randomUUID(),
title: title ?? "Untitled",
done: false,
};
setTasks((prev) => [...prev, task]);
return { success: true, id: task.id };
}
case "toggle":
setTasks((prev) =>
prev.map((t) => (t.id === id ? { ...t, done: !t.done } : t)),
);
return { success: true };
case "remove":
setTasks((prev) => prev.filter((t) => t.id !== id));
return { success: true };
case "clear":
setTasks([]);
return { success: true };
}
},
},
});
return null;
}The override supplies only the execute; the description, parameters, and renderText stay in the toolkit file. Return a useful payload, such as a new item's id: it becomes the call's result, the renderer receives it, and the model reads it on the next turn. To send images or files back to the model, see multi-modal tool results.
The executor receives the same context as any frontend execute, including an abortSignal that fires when the user stops the run; see Cancellation.
Registering the toolkit inside the component that owns the state, as above, keeps its renderer scoped to that component. It does not hide the tool from the model: your server knows the schema either way, so the model can call the tool while no override is mounted, and then nothing runs.
Rules for overrides
- Keep the keys stable after mount. Adding or removing a key while mounted is not observed.
- Define a name in one mounted provider at a time. Two overrides for the same tool at the same priority throw
You tried to define a tool with the name manage_tasks, but it already exists. - Closures stay fresh. The hook registers once on mount and reads the latest
executeon every call, so you do not need to memoize the overrides object. - Unmounting removes the override. The toolkit entry becomes a stub with no executor again.
- An override can carry any partial tool field. To hide a tool at runtime instead, override
disabled: true. That hides only tools the client uploads; a compiled tool stays visible to the model through your server build and a call to it gets an error result. See Disabling a tool.
Without the build plugin
A plain toolkit entry can omit execute and let an override supply it. Declare type: "frontend" yourself and keep parameters and a render or renderText:
"use client";
import { defineToolkit } from "@assistant-ui/react";
import { manageTasksParameters } from "./state";
export default defineToolkit({
manage_tasks: {
type: "frontend",
description: "Manage tasks on the board.",
parameters: manageTasksParameters,
renderText: { running: "Updating tasks…", complete: "Tasks updated" },
},
});Mount the same useAuiToolOverrides component. The client uploads a frontend tool only once it has an execute, so the model sees this tool only while the override is mounted, and your route reads it from tools with frontendTools; see Backend tools.
Troubleshooting
| Symptom | Cause and fix |
|---|---|
[assistant-ui] stubTool() has no runtime implementation at import | The file was not compiled. Add "use generative" as the first line and the withAui or aui() plugin |
| The model calls the tool and nothing happens | No override is mounted for that name. Mount useAuiToolOverrides where the tool must work |
| The model never calls the tool | Your server does not know the schema. Import the toolkit through AISDKToolkit in the route |
You tried to define a tool with the name …, but it already exists. | Two mounted overrides define the same name. Keep one |
| A tool you added after mount never runs | Keys are read once. Declare every key on first render |
When to use this vs. Interactables
If you mainly want the model to update a piece of component state with a partial-update tool generated for you, Interactables does that without a stubTool. Use dynamic tools when you want full control over the tool's name, schema, executor logic, and return value. The two compose: the companion tools section shows an Interactable with a stubTool for a side effect that state cannot express.