docs/content/docs/integrations/microsoft-agent-framework/generative-ui/tool-rendering.mdx
import { Accordions, Accordion } from "fumadocs-ui/components/accordion"; import { IframeSwitcher } from "@/components/content" import DefaultToolRendering from "@/snippets/shared/guides/default-tool-rendering.mdx" <IframeSwitcher id="backend-tools-example" exampleUrl="https://feature-viewer.copilotkit.ai/microsoft-agent-framework-dotnet/feature/backend_tool_rendering?sidebar=false&chatDefaultOpen=false" codeUrl="https://feature-viewer.copilotkit.ai/microsoft-agent-framework-dotnet/feature/backend_tool_rendering?view=code&sidebar=false&codeLayout=tabs" exampleLabel="Demo" codeLabel="Code" height="700px" />
<Callout> This example demonstrates the [implementation](#implementation) section applied in the <a href="https://feature-viewer.copilotkit.ai/microsoft-agent-framework-dotnet/feature/agentic_chat" target="_blank">CopilotKit feature viewer</a>. </Callout>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.
Define a tool function that your agent can call. Microsoft Agent Framework will automatically convert this into a tool the LLM can call.
<Tabs groupId="language_microsoft-agent-framework_agent" items={['.NET', 'Python']} persist> <Tab value=".NET"> ```csharp title="Program.cs" 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"]!;
// [!code highlight:4]
// 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();
```
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)
```
import { useRenderTool } from "@copilotkit/react-core/v2"; // [!code highlight]
// ...
const YourMainContent = () => {
// ...
// [!code highlight:11]
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>
);
},
});
// ...
}
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>