Components as Tools

Let your agent render your React Native components in the chat by calling them as tools.

What is this?

Tool-based generative UI is the simplest form of generative UI. You register a React Native component with useComponent, and CopilotKit offers it to the agent as a tool. When the agent calls the tool, CopilotKit renders your component in the chat and passes the tool's arguments to it as props.

Unlike tool rendering, which draws UI for a real backend tool, here the tool is the component. There is no handler, no user interaction, and no server-side execution. The agent decides when to show it and fills in the data. CopilotKit renders it.

When should I use this?

Use useComponent when you want to:

  • Display rich UI (cards, charts, lists) in the chat
  • Show structured data the agent has derived from its reasoning
  • Render previews, status indicators, or visual summaries
  • Let the agent present information beyond plain text

For components that need user input, see Human-in-the-loop. For UI around a real backend tool, see Tool Call Rendering.

How it works in code

Nothing to wire on the agent

AG2's AG-UI stream surfaces frontend-registered tools to the model on every run, so the agent declares none of its own. A component registered with useComponent reaches the model through the AG-UI request payload, and the model calls it by name. Leave tools empty when the only tools in play are frontend ones.

src/agents/chart_agent.py
from ag2 import Agent
from ag2.ag_ui import AGUIStream
from ag2.config import OpenAIResponsesConfig

chart_agent = Agent(
    name="chart_agent",
    prompt=SYSTEM_PROMPT,
    config=OpenAIResponsesConfig(model="gpt-6-luna"),
    tools=[],
)

chart_stream = AGUIStream(chart_agent)

Tell the model when to call it

This is the part that is easy to miss. The tool arrives on every run, but a model with no instruction about it will answer in prose and never call it. Name the tool in prompt and say what it is for.

src/agents/chart_agent.py
SYSTEM_PROMPT = (
    "You are a data visualization assistant. "
    "When the user asks for a chart, call `render_bar_chart` with a "
    "concise title and a `data` array of `{label, value}` items. "
    "Keep chat responses brief and let the chart do the talking."
)

Define your component's props as a zod schema. The schema becomes the tool's parameters, which tell the model which arguments to send. Add zod to your app if it isn't there yet (npm install zod).

src/BarChart.tsx
import { Text, View } from "react-native";
import { z } from "zod";

export const barChartPropsSchema = z.object({
  title: z.string().describe("Chart title"),
  data: z.array(
    z.object({
      label: z.string(),
      value: z.number(),
    }),
  ),
});

type BarChartProps = z.infer<typeof barChartPropsSchema>;

export function BarChart({ title, data }: BarChartProps) {
  // While the agent streams the call, props can be missing or partial.
  const rows = data ?? [];
  const max = Math.max(1, ...rows.map((row) => row.value ?? 0));

  return (
    <View style={{ padding: 12, borderRadius: 12, backgroundColor: "#f4f4f8", gap: 6 }}>
      <Text style={{ fontWeight: "600" }}>{title ?? "Loading chart..."}</Text>
      {rows.map((row, i) => (
        <View key={i} style={{ flexDirection: "row", alignItems: "center", gap: 8 }}>
          <Text style={{ width: 64 }}>{row.label}</Text>
          <View
            style={{
              height: 12,
              borderRadius: 6,
              backgroundColor: "#5057cc",
              width: `${((row.value ?? 0) / max) * 70}%`,
            }}
          />
        </View>
      ))}
    </View>
  );
}

Register it with useComponent, in any component under CopilotKitProvider:

src/ChatScreen.tsx
import { useComponent } from "@copilotkit/react-native/headless";
import { CopilotChat } from "@copilotkit/react-native/components";
import { BarChart, barChartPropsSchema } from "./BarChart";

export function ChatScreen() {
  useComponent({
    name: "render_bar_chart",
    description: "Show numeric data as a bar chart.",
    parameters: barChartPropsSchema,
    render: BarChart,
  });

  return <CopilotChat />;
}

BarChart is your own component. It reads only its props and knows nothing about CopilotKit. Any React Native charting library works here too.

parameters is optional, but leaving it out advertises the tool with an empty parameter schema. The model then has nothing to fill in, so it calls the tool with no arguments and your component renders with no props. Pass a schema for any component that needs data.

The name you pass to useComponent is what the agent sees as the tool name. Make it a verb, like render_bar_chart or show_weather, so the model picks it when the user asks for that view. CopilotKit adds a line to the description that tells the model the tool displays a component.

To check it, ask the agent for a bar chart of quarterly sales. The chart renders in the chat with the data from the agent.

React Native rules for the component

  • Props stream in. The component renders while the agent is still writing the arguments, so a field can be undefined or a list can be half full. Guard every prop, as BarChart does.
  • Wrap text in Text. The chat draws your component inside a FlatList. A bare string, outside a Text, throws Text strings must be rendered within a <Text> component.
  • Bound long content. For content whose length you can't predict, use a ScrollView with a maxHeight, so a long answer scrolls instead of pushing the chat off screen.

Where the component renders

The rendered CopilotChat and CopilotModal from /components draw registered components for you. The root CopilotChat, CopilotPopup, and CopilotSidebar do not draw messages, so no tool call shows there unless you render it. The component is registered and the agent calls it, but nothing appears.

In your own chat, call useRenderToolCall() and render each tool call in your list:

import { useRenderToolCall } from "@copilotkit/react-native/headless";

const renderToolCall = useRenderToolCall();

// For each tool call on an assistant message:
renderToolCall({ toolCall, toolMessage });

toolMessage is the tool-role message whose toolCallId matches the call. It returns a React Native element, or null when nothing is registered for that tool. See Headless UI for a full list.

For every option, see the useComponent reference.

How is this guide?