showcase/shell-docs/src/content/docs/integrations/mastra/generative-ui/tool-rendering.mdx
<IframeSwitcher id="tool-based-generative-ui-example" exampleUrl="https://feature-viewer.copilotkit.ai/mastra/feature/backend_tool_rendering?sidebar=false&chatDefaultOpen=false" codeUrl="https://feature-viewer.copilotkit.ai/mastra/feature/backend_tool_rendering?view=code&sidebar=false&codeLayout=tabs" exampleLabel="Demo" codeLabel="Code" height="700px" />
Tools are a way for the LLM to call predefined, typically, deterministic functions. CopilotKit allows you to render these tools in the UI as a custom component, which we call Generative UI.
Rendering tools in the UI is useful when you want to provide the user with feedback about what your agent is doing, specifically when your agent is calling tools. CopilotKit allows you to fully customize how these tools are rendered in the chat.
Add a new tool definition:
export const weatherInfo = createTool({
id: "weatherInfo",
inputSchema: z.object({
location: z.string(),
}),
description: `Fetches the current weather information for a given location`,
execute: async ({ location }) => {
// Tool logic here (e.g., API call)
console.log("Using tool to fetch weather information for", location);
return { temperature: 20, conditions: "Sunny" }; // Example return
},
});
Then, pass the tool to the agent:
export const weatherAgent = new Agent({
name: "Weather Agent",
instructions:
"You are a helpful assistant that provides current weather information. When asked about the weather, use the weather information tool to fetch the data.",
model: openai("gpt-5.4-mini"),
tools: {
weatherInfo,
},
});
// ...
const YourMainContent = () => {
// ...
// [!code highlight:12]
useRenderTool({
name: "weatherInfo",
render: ({ status, args }) => {
return (
<p className="text-gray-500 mt-2">
{status !== "complete" && "Calling weather API..."}
{status === "complete" &&
`Called the weather API for ${args.location}.`}
</p>
);
},
});
// ...
};
Try asking the agent to get the weather for a location. You should see the custom UI component that we added render the tool call and display the arguments that were passed to the tool.
</Step> </Steps>