Tool Rendering
Render your agent's tool calls with custom UI components.
What is this?#
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.
When should I use this?#
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.
Implementation#
Give your agent a tool to call#
Define a tool function that your agent can call. Microsoft Agent Framework will automatically convert this into a tool the LLM can call.
using System.ComponentModel;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Agents.AI.Hosting.AGUI.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAGUI();
var app = builder.Build();
string endpoint = builder.Configuration["AZURE_OPENAI_ENDPOINT"]!;
string deployment = builder.Configuration["AZURE_OPENAI_DEPLOYMENT_NAME"]!;
// Define the weather tool function
[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get weather for")] string location)
=> $"The weather for {location} is 70 degrees.";
// Create the agent with tools
var agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.GetChatClient(deployment)
.CreateAIAgent(
name: "AGUIAssistant",
tools: [AIFunctionFactory.Create(GetWeather)]);
// Map the AG-UI endpoint
app.MapAGUI("/", agent);
await app.RunAsync();from __future__ import annotations
import os
import uvicorn
from agent_framework import Agent, tool
from agent_framework.openai import OpenAIChatClient
from agent_framework.ag_ui import add_agent_framework_fastapi_endpoint
from dotenv import load_dotenv
from fastapi import FastAPI
from typing import Annotated
from pydantic import Field
load_dotenv()
@tool
def get_weather(
location: Annotated[str, Field(description="The location to get weather for")],
) -> str:
normalized = location.strip() or "the requested location"
return f"The weather for {normalized} is 70 degrees."
def _build_chat_client():
if os.getenv("AZURE_OPENAI_ENDPOINT"):
return OpenAIChatClient(
model=os.getenv("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", "gpt-4o-mini"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
)
if os.getenv("OPENAI_API_KEY"):
return OpenAIChatClient(
model=os.getenv("OPENAI_CHAT_MODEL_ID", "gpt-4o-mini"),
api_key=os.getenv("OPENAI_API_KEY"),
)
raise RuntimeError(
"Set either AZURE_OPENAI_ENDPOINT + AZURE_OPENAI_API_KEY, or OPENAI_API_KEY."
)
chat_client = _build_chat_client()
agent = Agent(
name="MyAgent",
instructions="You are a helpful assistant.",
client=chat_client,
tools=[get_weather]
)
app = FastAPI(title="Microsoft Agent Framework - Quickstart")
add_agent_framework_fastapi_endpoint(app=app, agent=agent, path="/")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)Render the tool call in your frontend#
At this point, your agent will be able to call the get_weather tool. Now
we just need to add a useRenderTool hook to render the tool call in
the UI.
Important
In order to render a tool call in the UI, the name of the action must match the name of the tool.
// ...
const YourMainContent = () => {
// ...
useRenderTool({
name: "get_weather",
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>
);
},
});
// ...
};Give it a try!#
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.
Default Tool Rendering#
useDefaultRenderTool provides a catch-all renderer for any tool that doesn't have a specific useRenderToolCall defined. This is useful for:
- Displaying all tool calls during development
- Rendering MCP (Model Context Protocol) tools
- Providing a generic fallback UI for unexpected tools
import { useDefaultRenderTool } from "@copilotkit/react-core/v2";
// ...
const YourMainContent = () => {
// ...
useDefaultRenderTool({
render: ({ name, args, status, result }) => {
return (
<div style={{ color: "black" }}>
<span>
{status === "complete" ? "✓" : "⏳"}
{name}
</span>
{status === "complete" && result && (
<pre>{JSON.stringify(result, null, 2)}</pre>
)}
</div>
);
},
});
// ...
};Unlike useRenderToolCall, which targets a specific tool by name,
useDefaultRenderTool catches all tools that don't have a dedicated
renderer.
In v2, use useDefaultRenderTool
for wildcard fallback rendering, and
useRenderTool for named or wildcard
renderer registration.
