Overview
The CopilotKit endpoint is a Next.js App Router API route that bridges the frontend chat interface with the MABQ BigQuery Agent backend using the HttpAgent.Endpoint Details
CopilotKit runtime endpoint for agent communication
Implementation
The endpoint is implemented inapp/api/copilotkit/route.ts:
CopilotRuntime Configuration
The CopilotRuntime manages the connection between the UI and the agent backend.Runtime Setup
Dictionary of available agents keyed by agent name
The primary agent instance connected to the MABQ backend
HttpAgent Configuration
The HttpAgent proxies requests to the FastAPI backend:Backend URL from
NEXT_PUBLIC_API_URL environment variableHTTP headers forwarded to the backend on every request
Azure AD Bearer token from the frontend request
Request Flow
Required Headers
Clients must include an Authorization header when calling this endpoint:Azure AD access token in Bearer format
Request content type
Example Request
Request Body
The request body follows the CopilotKit protocol:User’s natural language query for the agent
Conversation thread identifier for multi-turn conversations
Unique run identifier for this request
Response Format
The endpoint returns a CopilotKit-formatted response:Agent’s response message
Structured data from the agent (SQL query, results, etc.)
Conversation thread identifier
Example Response
Service Adapter
The endpoint uses an empty adapter as a placeholder:The EmptyAdapter is used because agent logic is handled entirely by the backend. Future implementations may add frontend-side processing.
Environment Variables
Backend API URL (e.g.,
https://mabq-backend-1093163678323.us-east4.run.app)Example Configuration
.env.local
Error Handling
The endpoint will return errors from the backend or CopilotKit runtime:403 Forbidden (Authentication Failed)
500 Internal Server Error (Backend Failure)
422 Unprocessable Entity (Invalid Request)
Authorization Header Forwarding
The key security feature is forwarding the Authorization header:- Each backend request is authenticated with the user’s token
- The backend can validate user permissions
- User identity is preserved throughout the request chain
Integration with CopilotKit UI
The frontend uses this endpoint via the CopilotKit provider:Related Documentation
- HTTP Agent - HttpAgent configuration details
- Authentication - Backend token validation
- FastAPI Endpoints - Backend API reference
- Agent Interface - BigQuery agent details