copilotkit-develop
Use when building AI-powered features with CopilotKit v2 -- adding chat interfaces, registering frontend tools, sharing application context with agents, handling agent interrupts, and working with the CopilotKit runtime.
What this skill does
# CopilotKit v2 Development Skill
## Live Documentation (MCP)
This plugin includes an MCP server (`copilotkit-docs`) that provides `search-docs` and `search-code` tools for querying live CopilotKit documentation and source code.
- **Claude Code:** Auto-configured by the plugin's `.mcp.json` -- no setup needed.
- **Codex:** Requires manual configuration. See the [copilotkit-debug skill](../copilotkit-debug/SKILL.md#mcp-setup) for setup instructions.
## Architecture Overview
CopilotKit v2 is built on the AG-UI protocol (`@ag-ui/client` / `@ag-ui/core`). The stack has three layers:
1. **Runtime** (`@copilotkit/runtime`) -- Server-side. Hosts agents, handles SSE/Intelligence transport, middleware, transcription.
2. **Core** (`@copilotkit/core`) -- Shared state management, tool registry, suggestion engine. Not imported directly by apps.
3. **React** (`@copilotkit/react`) -- Provider, chat components, hooks. Re-exports everything from `@ag-ui/client` so apps need only one import.
## Workflow
### 1. Set Up the Runtime (Server)
Create a `CopilotRuntime` (or the explicit `CopilotSseRuntime` / `CopilotIntelligenceRuntime`) and expose it via `createCopilotEndpoint` (Hono) or `createCopilotEndpointExpress` (Express).
```ts
import { CopilotRuntime, createCopilotEndpoint } from "@copilotkit/runtime";
import { LangGraphAgent } from "@ag-ui/langgraph";
const runtime = new CopilotRuntime({
agents: {
myAgent: new LangGraphAgent({ /* ... */ }),
},
});
const app = createCopilotEndpoint({
runtime,
basePath: "/api/copilotkit",
});
```
### 2. Wrap Your App with the Provider (Client)
```tsx
import { CopilotKitProvider } from "@copilotkit/react";
function App() {
return (
<CopilotKitProvider runtimeUrl="/api/copilotkit">
<YourApp />
</CopilotKitProvider>
);
}
```
### 3. Add a Chat UI
Use `<CopilotChat>`, `<CopilotPopup>`, or `<CopilotSidebar>`:
```tsx
import { CopilotChat } from "@copilotkit/react";
function ChatPage() {
return <CopilotChat agentId="myAgent" />;
}
```
### 4. Register Frontend Tools
Let the agent call functions in the browser:
```tsx
import { useFrontendTool } from "@copilotkit/react";
import { z } from "zod";
useFrontendTool({
name: "highlightCell",
description: "Highlight a spreadsheet cell",
parameters: z.object({ row: z.number(), col: z.number() }),
handler: async ({ row, col }) => {
highlightCell(row, col);
return "done";
},
});
```
### 5. Share Application Context
Provide runtime data to the agent:
```tsx
import { useAgentContext } from "@copilotkit/react";
useAgentContext({
description: "The user's current shopping cart",
value: cart, // any JSON-serializable value
});
```
### 6. Handle Agent Interrupts
When an agent pauses for human input:
```tsx
import { useInterrupt } from "@copilotkit/react";
useInterrupt({
render: ({ event, resolve }) => (
<div>
<p>{event.value.question}</p>
<button onClick={() => resolve({ approved: true })}>Approve</button>
</div>
),
});
```
### 7. Render Tool Calls in Chat
Show custom UI when tools execute:
```tsx
import { useRenderTool } from "@copilotkit/react";
import { z } from "zod";
useRenderTool({
name: "searchDocs",
parameters: z.object({ query: z.string() }),
render: ({ status, parameters, result }) => {
if (status === "executing") return <Spinner>Searching {parameters.query}...</Spinner>;
if (status === "complete") return <Results data={result} />;
return <div>Preparing...</div>;
},
}, []);
```
## Quick Reference: Hooks
| Hook | Purpose |
|------|---------|
| `useFrontendTool` | Register a tool the agent can call in the browser |
| `useComponent` | Register a React component as a chat-rendered tool (convenience wrapper around `useFrontendTool`) |
| `useAgentContext` | Share JSON-serializable application state with the agent |
| `useAgent` | Get the `AbstractAgent` instance for an agent ID; subscribe to message/state/run-status changes |
| `useInterrupt` | Handle `on_interrupt` events from agents with render + optional handler/filter |
| `useHumanInTheLoop` | Register a tool that pauses execution until the user responds via a rendered UI |
| `useRenderTool` | Register a renderer for tool calls (by name or wildcard `"*"`) |
| `useDefaultRenderTool` | Register a wildcard `"*"` renderer using the built-in expandable card UI |
| `useRenderToolCall` | Internal hook returning a function to resolve the correct renderer for a given tool call |
| `useRenderActivityMessage` | Internal hook for rendering activity messages by type |
| `useRenderCustomMessages` | Internal hook for rendering custom message decorators |
| `useSuggestions` | Read the current suggestion list and control reload/clear |
| `useConfigureSuggestions` | Register static or dynamic (LLM-generated) suggestion configs |
| `useThreads` | List, rename, archive, and delete Intelligence platform threads |
## Quick Reference: Components
| Component | Purpose |
|-----------|---------|
| `CopilotKitProvider` | Root provider -- configures runtime URL, headers, agents, error handler |
| `CopilotChat` | Full chat interface connected to an agent (inline layout) |
| `CopilotPopup` | Chat in a floating popup with toggle button |
| `CopilotSidebar` | Chat in a collapsible sidebar with toggle button |
| `CopilotChatView` | Headless chat view with slots for message view, input, scroll, suggestions |
| `CopilotChatInput` | Chat input textarea with send/stop/transcribe controls |
| `CopilotChatMessageView` | Renders the message list |
| `CopilotChatSuggestionView` | Renders suggestion pills |
## Quick Reference: Runtime
| Export | Purpose |
|--------|---------|
| `CopilotRuntime` | Auto-detecting runtime (delegates to SSE or Intelligence) |
| `CopilotSseRuntime` | Explicit SSE-mode runtime |
| `CopilotIntelligenceRuntime` | Intelligence-mode runtime with durable threads |
| `createCopilotEndpoint` | Create a Hono app with all CopilotKit routes |
| `createCopilotEndpointExpress` | Create an Express router with all CopilotKit routes |
| `CopilotKitIntelligence` | Intelligence platform client configuration |
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.