Skip to main content

Overview

The MABQ BigQuery Agent backend is built with FastAPI and integrates the Google ADK (Agent Development Kit) to provide a conversational interface for BigQuery data analysis.

Application Configuration

The FastAPI app is configured with strict CORS policies and security middleware.
app = FastAPI(title="Transelec MABQ Agent")

CORS Middleware

The application enforces strict CORS to allow only authorized frontend origins:
FRONTEND_URL = os.environ.get(
    "FRONTEND_URL", 
    "https://mabq-frontend-1093163678323.us-east4.run.app"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=[FRONTEND_URL], 
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
allow_origins
list[str]
Single frontend URL from environment variable FRONTEND_URL
allow_credentials
bool
default:"true"
Enables cookie and authorization header support
allow_methods
list[str]
default:"['*']"
All HTTP methods allowed
allow_headers
list[str]
default:"['*']"
All headers allowed for authenticated requests

Endpoints

ADK Agent Endpoint

The main conversational agent endpoint is registered at the root path by the ADK framework.
from ag_ui_adk import ADKAgent, add_adk_fastapi_endpoint

adk_wrapper = ADKAgent(
    adk_agent=agente_backend, 
    app_name="transelec-mabq", 
    user_id="verified-user",
    use_in_memory_services=True
)

add_adk_fastapi_endpoint(app, adk_wrapper, path="/")
path
string
default:"/"
Root path where the ADK agent is mounted
use_in_memory_services
bool
default:"true"
Uses in-memory session storage for conversation state

Response Format

The ADK endpoint returns agent responses in a streaming or JSON format depending on the client configuration.
message
string
Agent’s response to the user query
sql
string
Generated SQL query (when applicable)
results
array
Query results from BigQuery (when executed)

Documentation Endpoints

FastAPI automatically generates interactive API documentation:
  • Swagger UI: /docs
  • ReDoc: /redoc
  • OpenAPI Schema: /openapi.json
These endpoints are exempt from authentication for development purposes.

Security Middleware

All endpoints (except /docs, /openapi.json, and GET requests to /) are protected by the authentication middleware. See Authentication for details.
The middleware exempts OPTIONS requests and specific paths for development and root access (see main.py:34-37).

Running the Server

The application runs with Uvicorn on port 8000:
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)
host
string
default:"0.0.0.0"
Binds to all network interfaces for container deployment
port
int
default:"8000"
HTTP port for the FastAPI application

Environment Variables

FRONTEND_URL
string
required
Authorized frontend origin for CORS
AZURE_TENANT_ID
string
required
Azure AD tenant ID for JWT validation
AZURE_CLIENT_ID
string
required
Azure AD application client ID (audience)

Example Deployment

apiVersion: v1
kind: Service
metadata:
  name: mabq-backend
spec:
  ports:
  - port: 8000
    targetPort: 8000
  selector:
    app: mabq-backend

Build docs developers (and LLMs) love