showcase/shell-docs/src/content/ag-ui/concepts/tools.mdx
Tools are a fundamental concept in the AG-UI protocol that enable AI agents to interact with external systems and incorporate human judgment into their workflows. By defining tools in the frontend and passing them to agents, developers can create sophisticated human-in-the-loop experiences that combine AI capabilities with human expertise.
In AG-UI, tools are functions that agents can call to:
Tools bridge the gap between AI reasoning and real-world actions, allowing agents to accomplish tasks that would be impossible through conversation alone.
Tools follow a consistent structure that defines their name, purpose, and expected parameters:
interface Tool {
name: string; // Unique identifier for the tool
description: string; // Human-readable explanation of what the tool does
parameters: {
// JSON Schema defining the tool's parameters
type: "object";
properties: {
// Tool-specific parameters
};
required: string[]; // Array of required parameter names
};
}
The parameters field uses JSON Schema to define
the structure of arguments that the tool accepts. This schema is used by both
the agent (to generate valid tool calls) and the frontend (to validate and parse
tool arguments).
A key aspect of AG-UI's tool system is that tools are defined in the frontend and passed to the agent during execution:
// Define tools in the frontend
const userConfirmationTool = {
name: "confirmAction",
description: "Ask the user to confirm a specific action before proceeding",
parameters: {
type: "object",
properties: {
action: {
type: "string",
description: "The action that needs user confirmation",
},
importance: {
type: "string",
enum: ["low", "medium", "high", "critical"],
description: "The importance level of the action",
},
},
required: ["action"],
},
};
// Pass tools to the agent during execution
agent.runAgent({
tools: [userConfirmationTool],
// Other parameters...
});
This approach has several advantages:
When an agent needs to use a tool, it follows a standardized sequence of events:
ToolCallStart: Indicates the beginning of a tool call with a unique ID and tool name
{
type: EventType.TOOL_CALL_START,
toolCallId: "tool-123",
toolCallName: "confirmAction",
parentMessageId: "msg-456" // Optional reference to a message
}
ToolCallArgs: Streams the tool arguments as they're generated
{
type: EventType.TOOL_CALL_ARGS,
toolCallId: "tool-123",
delta: '{"act' // Partial JSON being streamed
}
{
type: EventType.TOOL_CALL_ARGS,
toolCallId: "tool-123",
delta: 'ion":"Depl' // More JSON being streamed
}
{
type: EventType.TOOL_CALL_ARGS,
toolCallId: "tool-123",
delta: 'oy the application to production"}' // Final JSON fragment
}
ToolCallEnd: Marks the completion of the tool call
{
type: EventType.TOOL_CALL_END,
toolCallId: "tool-123"
}
The frontend accumulates these deltas to construct the complete tool call arguments. Once the tool call is complete, the frontend can execute the tool and provide results back to the agent.
After a tool has been executed, the result is sent back to the agent as a "tool message":
{
id: "result-789",
role: "tool",
content: "true", // Tool result as a string
toolCallId: "tool-123" // References the original tool call
}
This message becomes part of the conversation history, allowing the agent to reference and incorporate the tool's result in subsequent responses.
The AG-UI tool system is especially powerful for implementing human-in-the-loop workflows. By defining tools that request human input or confirmation, developers can create AI experiences that seamlessly blend autonomous operation with human judgment.
For example:
confirmAction tool with details about the decisionThis pattern enables use cases like:
CopilotKit provides a simplified way to work with
AG-UI tools in React applications through its
useCopilotAction hook:
// Define a tool for user confirmation
useCopilotAction({
name: "confirmAction",
description: "Ask the user to confirm an action",
parameters: {
type: "object",
properties: {
action: {
type: "string",
description: "The action to confirm",
},
},
required: ["action"],
},
handler: async ({ action }) => {
// Show a confirmation dialog
const confirmed = await showConfirmDialog(action);
return confirmed ? "approved" : "rejected";
},
});
This approach makes it easy to define tools that integrate with your React components and handle the tool execution logic in a clean, declarative way.
Here are some common types of tools used in AG-UI applications:
{
name: "confirmAction",
description: "Ask the user to confirm an action",
parameters: {
type: "object",
properties: {
action: {
type: "string",
description: "The action to confirm"
},
importance: {
type: "string",
enum: ["low", "medium", "high", "critical"],
description: "The importance level"
}
},
required: ["action"]
}
}
{
name: "fetchUserData",
description: "Retrieve data about a specific user",
parameters: {
type: "object",
properties: {
userId: {
type: "string",
description: "ID of the user"
},
fields: {
type: "array",
items: {
type: "string"
},
description: "Fields to retrieve"
}
},
required: ["userId"]
}
}
{
name: "navigateTo",
description: "Navigate to a different page or view",
parameters: {
type: "object",
properties: {
destination: {
type: "string",
description: "Destination page or view"
},
params: {
type: "object",
description: "Optional parameters for the navigation"
}
},
required: ["destination"]
}
}
{
name: "generateImage",
description: "Generate an image based on a description",
parameters: {
type: "object",
properties: {
prompt: {
type: "string",
description: "Description of the image to generate"
},
style: {
type: "string",
description: "Visual style for the image"
},
dimensions: {
type: "object",
properties: {
width: { type: "number" },
height: { type: "number" }
},
description: "Dimensions of the image"
}
},
required: ["prompt"]
}
}
When designing tools for AG-UI:
Tools in AG-UI bridge the gap between AI reasoning and real-world actions, enabling sophisticated workflows that combine the strengths of AI and human intelligence. By defining tools in the frontend and passing them to agents, developers can create interactive experiences where AI and humans collaborate efficiently.
The tool system is particularly powerful for implementing human-in-the-loop workflows, where AI can suggest actions but defer critical decisions to humans. This balances automation with human judgment, creating AI experiences that are both powerful and trustworthy.