Overview
TheHttpAgent from @ag-ui/client is used to connect the CopilotKit frontend to the MABQ BigQuery Agent FastAPI backend. It handles HTTP communication and authorization header forwarding.
Purpose
The HttpAgent serves as a bridge between:- Frontend: CopilotKit chat interface in Next.js
- Backend: FastAPI application with the ADK agent
Installation
Basic Configuration
The HttpAgent is initialized with a backend URL and authentication headers:The backend API endpoint URL
HTTP headers to include in every request to the backend
Implementation in MABQ
In the MABQ project, the HttpAgent is configured inapp/api/copilotkit/route.ts:
Configuration Parameters
URL Configuration
The backend URL is read from an environment variable with a fallback:Environment variable containing the backend API URL
Headers Configuration
The most critical configuration is the Authorization header forwarding:Azure AD Bearer token from the incoming request
Authorization Header Forwarding
The HttpAgent forwards the user’s authentication token from the frontend request to the backend:Header Extraction
The authorization header is extracted from the incoming Next.js request:Header Forwarding
The header is then passed to the HttpAgent configuration:Request Flow
When a user sends a message through CopilotKit:- Frontend: User types message in chat UI
- CopilotKit: Sends request to
/api/copilotkitwith Authorization header - Next.js Route: Extracts Authorization header from request
- HttpAgent: Initialized with backend URL and Authorization header
- Backend Request: HttpAgent forwards message to FastAPI with Authorization header
- Authentication: FastAPI validates JWT token
- Agent Processing: BigQuery agent processes the query
- Response: Results flow back through the chain
Environment Variables
Configure the HttpAgent with these environment variables:Frontend (.env.local)
The URL of your deployed FastAPI backend
Error Handling
The HttpAgent will propagate errors from the backend:Backend Unreachable
Authentication Failure (403)
Backend Error (500)
Integration with CopilotRuntime
The HttpAgent is registered with CopilotRuntime as a named agent:The primary agent instance used by CopilotKit
The key
default_agent can be any string. CopilotKit will use this agent for all requests unless otherwise specified.Multiple Agents
You can configure multiple HttpAgents for different backends:Security Considerations
Example Security Setup
Testing the HttpAgent
Local Development
.env.local
Production
.env.production
Verifying Connection
Check that the HttpAgent can reach the backend:Related Documentation
- CopilotKit Endpoint - API route implementation
- Authentication - Backend JWT validation
- FastAPI Endpoints - Backend API reference
- Agent Interface - BigQuery agent configuration