An agentic AI system that reviews BigQuery SQL queries before you run them — catching performance issues, estimating cost, and suggesting rewrites.
Paste a BigQuery SQL query and the agent:
- Static analysis — instantly flags
SELECT *, missing partition filters, cartesian joins - Schema fetch — reads partition keys, clustering fields, and row counts from BigQuery
- Cost estimate — dry-runs the query to get bytes scanned without executing it
- Rewrite — returns improved SQL with a plain-English explanation
- Severity rating —
none/low/medium/high/critical
The agent is modelled as a LangGraph state machine:
- call_llm — sends messages + tools to Claude/Gemini
- run_tools — dispatches
get_table_schema,dry_run_sql,write_report - route — conditional edge: loop back if tool calls remain, stop when
write_reportis called
| Mode | How |
|---|---|
| CLI | python agent.py --sql "SELECT * FROM ..." |
| Web UI | python server.py → open localhost:5001 |
| MCP | Any MCP-compatible client (Claude Code, Cursor, Zed, OpenClaw) |
git clone https://github.com/ARAVINDHRAJA123/sql-review-agent.git
cd sql-review-agent
python3 -m venv venv && source venv/bin/activate
pip install -r requirements.txt
export GCP_PROJECT=your-project
export BQ_LOCATION=asia-south1
export GEMINI_API_KEY=your-key # free tier: aistudio.google.com
# or: export ANTHROPIC_API_KEY=your-key
gcloud auth application-default loginclaude mcp add -s user sql-review -- \
/path/to/venv/bin/python /path/to/mcp_server.pyTools available in any MCP client:
review_sql— full agentic review (LLM + BQ)quick_check— instant static analysis, no LLM needed
python agent.py --sql "SELECT * FROM \`project.dataset.table\`"
python agent.py --file query.sql --verbosepython server.py
# open http://localhost:5001pytest
pytest tests/test_tools.py -vsql-review-agent/
├── agent.py ← raw tool-use loop (Claude + Gemini)
├── graph_agent.py ← LangGraph state machine (drop-in replacement)
├── mcp_server.py ← FastMCP server (review_sql + quick_check tools)
├── server.py ← Flask web UI + JSON API
├── tools/
│ ├── bq_tools.py ← dry_run, schema, metadata, read-only guard
│ └── sql_tools.py ← static analysis, table extraction
├── tests/
│ ├── test_tools.py ← 24 unit tests
│ └── test_agent.py ← 7 unit tests
└── docs/
├── architecture_graph.svg ← state machine diagram
└── web_ui.png ← Flask UI screenshot
Scan .sql files or directories in GitHub Actions without BigQuery credentials or LLM API keys:
python ci_review.py queries/ --fail-on high --read-only --format markdown
python ci_review.py queries/ --fail-on critical --format json
# exits 2 if a path is missing or no .sql files are found (use --allow-empty to accept that)- Universal instructions & skill:
AGENTS.md+.github/skills/sql-review/SKILL.md(agentskills.io-compliant; mirrored to.claude/skills/and.agents/skills/for Claude Code and Codex). - Deterministic
PreToolUseSQL Guard Hook (hooks/sql_guard_hook.py): Auto-detects VS Code Copilot (.github/hooks/sql-guard.json), Copilot CLI, Claude Code, and Cursor payloads. Blocks non-read-only BigQuery DDL/DML, mutatingbqsubcommands, and critical anti-patterns (CROSS JOIN, implicit cartesian joins) beforebqruns, including insidebash -c,eval,$(…)and backticks, while allowing--dry_runchecks. - Wiring the SQL guard hook. The hook uses only the Python standard library, so any
python3works. It blocks by exiting2; for GitHub Copilot CLI it exits0with a top-levelpermissionDecision: "deny"instead, because Copilot CLI ignores a hook's output on exit2and would not show the reason.- VS Code Copilot and Copilot CLI:
.github/hooks/sql-guard.jsonis picked up automatically. Copilot CLI only runs repository hooks in a trusted folder: approve the folder when it asks, or add it totrustedFoldersin~/.copilot/config.json. - Claude Code (
.claude/settings.json):{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "python3 \"$CLAUDE_PROJECT_DIR/hooks/sql_guard_hook.py\"", "timeout": 10 } ] } ] } } - Cursor (
.cursor/hooks.json, not yet tested in Cursor itself):{ "version": 1, "hooks": { "beforeShellExecution": [ { "command": "python3 hooks/sql_guard_hook.py", "timeout": 10 } ] } }
- VS Code Copilot and Copilot CLI:
- MCP config (VS Code
.vscode/mcp.json/ Claude.mcp.json/ Cursor.cursor/mcp.json):{ "mcpServers": { "sql-review": { "command": "/path/to/venv/bin/python", "args": ["/path/to/sql-review-agent/mcp_server.py"] } } }
