System Overview
MABQ is a multi-tier application that combines Microsoft Teams authentication, Next.js frontend, FastAPI backend, and Google’s ADK framework to provide secure, AI-powered SQL generation for BigQuery.Frontend Layer
Next.js 16 with Microsoft Teams integration
Backend Layer
FastAPI with Azure AD authentication
Agent Layer
Google ADK with Gemini 2.5 Pro
Data Layer
BigQuery with read-only access
Component Architecture
Frontend: Next.js with Teams Integration
The frontend is built with Next.js 16 and runs entirely within Microsoft Teams as a tab application. Key files:frontend-agente/app/page.tsx- Main chat interfacefrontend-agente/app/api/copilotkit/route.ts- Backend proxy
- Only runs within Microsoft Teams environment
- Uses Teams SSO for authentication
- Passes JWT token to backend in Authorization header
- No credential storage in browser
Backend: FastAPI with Azure AD Security
The backend provides a secure FastAPI server that validates Azure AD tokens and initializes the ADK agent. Key file:POC_ADK/main.py
Security Middleware
Every request is validated using Azure AD JWT tokens with RSA signature verification:- Extracts JWT token from Authorization header
- Fetches public keys from Microsoft’s JWKS endpoint
- Validates token signature using RSA cryptography
- Verifies audience, issuer, and expiration
- Checks email domain allowlist (
@transelec.cl) - Attaches user profile to request state
- Blocks unauthorized requests with 403
CORS Configuration
ADK Agent Integration
Agent Layer: Google ADK with Gemini
The agent is built using Google’s Agent Development Kit (ADK) and powered by Gemini 2.5 Pro. Key file:POC_ADK/data_agente/agent.py
BigQuery Toolset Configuration
write_mode=WriteMode.BLOCKED ensures the agent can only read data, never modify it.
Agent Instructions
The agent is instructed with strict guidelines for SQL generation:- Read-only operations only
- Must test queries before returning them
- Returns only SQL code, no explanations
- Rejects write operations with helpful message
Agent Definition
Request Flow
Here’s what happens when a user asks a question:Frontend Processing
Frontend captures the message and sends it via CopilotKit:
- CopilotKit sends POST to
/api/copilotkit - Next.js API route forwards to backend with Authorization header
- Request includes JWT token from Teams SSO
Authentication
Backend security middleware validates the request:
- Extracts Bearer token from header
- Validates JWT signature with Azure AD public keys
- Checks token expiration and audience
- Verifies email domain
- Attaches user profile to request
Agent Processing
ADK agent receives the query:
- Gemini 2.5 Pro analyzes the natural language
- Determines it needs BigQuery data
- Uses
bigquery_toolsetto explore schema - Generates SQL query
- Tests query execution
- Validates results
BigQuery Execution
The toolset executes the query:
- Authenticates with service account credentials
- Runs read-only query against specified dataset
- Returns results to agent
- Write operations are blocked by
WriteMode.BLOCKED
Response Generation
Agent formats the response:
- Confirms query works correctly
- Returns ONLY the SQL code block
- No explanations or summaries (per instructions)
Authentication Flow
MABQ uses a multi-layer authentication approach combining Microsoft Teams SSO with Azure AD token validation.
Security Architecture
Multi-Layer Defense
MABQ implements security at every layer:Frontend
- Teams-only access
- No credential storage
- SSO token handling
Backend
- JWT signature validation
- Email domain allowlist
- CORS restrictions
Agent
- Read-only mode enforced
- Write commands blocked
- Instruction-level guardrails
BigQuery
- Service account permissions
- Dataset-level access control
- Audit logging
Write Protection
Three layers prevent data modification:- Toolset Level -
WriteMode.BLOCKEDin BigQuery configuration - Instruction Level - Agent instructed to reject write operations
- Permission Level - Service account has read-only IAM role
Audit Trail
All requests are logged with user context:Data Flow Diagram
Deployment Architecture
MABQ is designed for deployment on Google Cloud Run:Backend Service
- Python 3.11 container
- Uvicorn ASGI server
- Port 8080
- Service account identity
Frontend Service
- Node 20 Alpine container
- Next.js standalone build
- Port 3000
- Static asset serving
Backend Dockerfile
Frontend Dockerfile
Configuration Management
Environment Variables
Configuration is managed through environment variables for security and flexibility: Backend (POC_ADK/):
frontend-agente/):
Vertex AI Initialization
Key Design Decisions
Why Teams-only access?
Why Teams-only access?
Microsoft Teams provides:
- Built-in SSO with Azure AD
- Corporate authentication and authorization
- User context and identity
- Secure token management
- Integration with existing enterprise tools
Why separate frontend and backend?
Why separate frontend and backend?
Separation provides:
- Independent scaling of UI and API
- Security layer between user and AI agent
- Flexibility to support multiple clients
- Simplified CORS and authentication flow
- Better error handling and logging
Why Google ADK instead of LangChain?
Why Google ADK instead of LangChain?
Google ADK offers:
- Native integration with Vertex AI and BigQuery
- Optimized for Google Cloud services
- Built-in toolsets for BigQuery operations
- Better performance with Gemini models
- Official support from Google
Why read-only mode?
Why read-only mode?
Read-only enforcement:
- Prevents accidental data modification
- Eliminates data corruption risk
- Simplifies security audit
- Meets compliance requirements
- Builds user trust
Why in-memory sessions?
Why in-memory sessions?
In-memory session service:
- Simplifies deployment (no external state store)
- Reduces latency
- Sufficient for stateless SQL generation
- Cloud Run instances handle session affinity
- Can be upgraded to persistent storage later
Performance Considerations
Cold Start Optimization
Cloud Run containers include:- Minimal base images (Python slim, Node Alpine)
- Pre-installed dependencies
- Optimized layer caching
Query Caching
The agent validates queries before returning them, which:- Ensures SQL correctness
- Reduces user errors
- May increase latency on first run
- Benefits from BigQuery’s built-in caching
Scaling Strategy
Cloud Run automatically scales based on:- Request concurrency
- CPU and memory usage
- Configured min/max instances
Next Steps
Quickstart
Get MABQ running locally
Development
Extend and customize the agent
Configuration
Tune agent behavior and security
API Reference
Explore FastAPI endpoints