The application uses ADK, Gemini, LangChain tools to power an SQL Agent with built-in multi-layer authorization and safety controls.
Frontend: Streamlit app (streamlit_ui.py)
Backend: FastAPI service (main.py)
Security-First Pipeline:
- Safety Check Agent - Determines if request is secure/not secure
- Security Router Agent - Routes: exits immediately if not secure, continues if secure
- Refinement Loop - Processes query (only if secure)
Agents:
- Coordinator: sql_agent (sql_agent.py)
- Security Agents:
- Safety Check agent (agent.py) - Binary security decision
- Security Router agent (agent.py) - Routes based on security
- Processing Subagents (in Refinement Loop):
- Rephraser agent (rewrite_prompt.py) - Includes security passthrough
- Generator agent (generator.py)
- Analyzer agent (analyzer.py)
- Reflexion agent (reflexion.py)
- Routing agent (routing.py)
Function Tools:
- get_schema tool (db_tools.py)
- run_sql_query tool (db_tools.py) - Includes SQL-level validation
Models:
- Gemini 2.5 pro
The system uses a clean three-step security pipeline:
- Analyzes user request intent
- Binary decision: SECURE or NOT SECURE
- Fast, focused security validation
- If NOT SECURE: Outputs rejection message and exits immediately
- If SECURE: Outputs "proceeding" and continues to processing
- Rewrites query
- Generates SQL
- Analyzes results
- Iterative refinement
Multi-Layer Defense:
- Layer 1: LLM-based intent analysis (Safety Check)
- Layer 2: Routing decision (Security Router)
- Layer 3: SQL-level validation (db_tools)
Only read-only SELECT queries are permitted. Dangerous operations (DROP, DELETE, TRUNCATE, etc.) are blocked immediately.
See ARCHITECTURE_CLEAN.md for detailed documentation.
git clone https://github.com/gabrielpreda/adk-sql-agent.git
cd adk-sql-agentThe file should contain the following:
GOOGLE_GENAI_USE_VERTEXAI=TRUE
GOOGLE_CLOUD_PROJECT=YOUR_PROJECT
GOOGLE_CLOUD_LOCATION=YOUR_REGION
Run:
pip install -r requirements.txtRun:
uvicorn main:app --reloadRun:
streamlit run streamlit_ui.pyFrom the root folder run:
adk webThen select the sql_agent folder. ADK web will discover in agent.py the root_agent and you will be able to test, monitor, debug the application.
The Agentic workflow is shown in the following figure (current step: Generator agent receives the result from run_sql_query tool).
The next figure shows the result of Generator Agent.
The next figure shows:
- Result of Analyzer Agent.
- Resolution of the Reflection Agent, based on previous agent analysis.
- The rationale for Routing Agent to route to Rewrite Query Agent.
- The reasoning of Rewrite Query Agent and the new query generated.
Agent Engine is Google Cloud's managed service for deploying ADK agents.
- Google Cloud Project with billing enabled
- gcloud CLI installed and configured
- ADK CLI version 1.3.0+
- Set your project ID and region:
export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_CLOUD_LOCATION=us-central1- Run the deployment script:
./deploy.sh- Test your deployed agent:
adk run --project=$GOOGLE_CLOUD_PROJECT --region=$GOOGLE_CLOUD_LOCATION --agent=adk-sql-agentAlternative deployment using Google Cloud Run for broader region availability.
- Set your project ID and region:
export GOOGLE_CLOUD_PROJECT=your-project-id
export GOOGLE_CLOUD_LOCATION=us-central1- Run the Cloud Run deployment script:
./deploy_cloudrun.sh- The script will output your service URL. Test it:
# Check status
curl https://your-service-url/status
# Send a query
curl -X POST https://your-service-url/query \
-H 'Content-Type: application/json' \
-d '{"query": "Show me the top 10 customers"}'If you prefer manual deployment:
# Build container
gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/adk-sql-agent
# Deploy to Cloud Run
gcloud run deploy adk-sql-agent \
--image gcr.io/$GOOGLE_CLOUD_PROJECT/adk-sql-agent \
--platform managed \
--region $GOOGLE_CLOUD_LOCATION \
--allow-unauthenticated \
--set-env-vars GOOGLE_GENAI_USE_VERTEXAI=TRUE \
--set-env-vars GOOGLE_CLOUD_PROJECT=$GOOGLE_CLOUD_PROJECT \
--set-env-vars GOOGLE_CLOUD_LOCATION=$GOOGLE_CLOUD_LOCATION \
--memory 4Gi \
--cpu 2We show here the sequence of operations for one query.







