What is CopilotKit?
CopilotKit is a framework for building AI copilots in React applications. It provides:- Chat UI Components: Pre-built, customizable chat interface
- Runtime Management: Handles agent communication and streaming
- State Synchronization: Manages conversation state between frontend and backend
- Custom Headers: Supports authentication and context passing
Architecture Overview
CopilotKit forwards to runtime
CopilotKit sends the message to
/api/copilotkit with authorization headersCopilotKit Provider Setup
The main page wraps the chat interface with theCopilotKit provider:
Provider Configuration
| Property | Value | Description |
|---|---|---|
runtimeUrl | /api/copilotkit | Next.js API route that handles agent communication |
agent | default_agent | Agent name registered in the CopilotRuntime |
headers | Authorization + X-Visual-Name | Custom headers passed to the backend |
The
Authorization header contains the Microsoft Teams SSO token, which the backend validates. The X-Visual-Name header passes the user’s display name for personalization.CopilotChat Component
TheCopilotChat component renders the chat interface:
Configuration Options
instructions: System prompt sent to the agent for contextlabels.title: Chat window title displayed to userslabels.initial: Welcome message shown on first loadclassName: Tailwind classes for full-screen layout
Backend Runtime Configuration
The/api/copilotkit route configures the CopilotRuntime:
HttpAgent Configuration
TheHttpAgent from @ag-ui/client acts as a proxy to the backend:
Runtime Flow
Request Headers
When a user sends a message, the following headers are passed:| Header | Source | Destination | Purpose |
|---|---|---|---|
Authorization | Teams SDK → Frontend | Frontend → Backend | SSO token for authentication |
X-Visual-Name | Teams SDK → Frontend | Frontend → Backend | User display name for context |
Message Processing
CopilotKit Sends Request
CopilotKit sends a POST request to
/api/copilotkit with:- Message content
- Conversation history
- Authorization header
- X-Visual-Name header
HttpAgent Forwards to Backend
The HttpAgent proxies the request to the LangGraph backend at
BACKEND_URLBackend Processes Query
The backend:
- Validates the Teams token
- Extracts user intent
- Generates and executes SQL
- Returns streaming response
Empty Service Adapter
TheEmptyAdapter is a placeholder required by the CopilotKit runtime:
This adapter doesn’t perform any processing since all agent logic is handled by the LangGraph backend. It’s required by the
copilotRuntimeNextJSAppRouterEndpoint API.Environment Variables
| Variable | Default | Description |
|---|---|---|
NEXT_PUBLIC_API_URL | https://mabq-backend-1093163678323.us-east4.run.app | Backend LangGraph agent URL |
Next Steps
Teams Integration
Learn about Microsoft Teams authentication flow
Backend Architecture
Understand the LangGraph agent backend