Setup
1. Start the Agent Control Server (for remote execution)
IMPORTANT: You must start/restart the server to load the SQL evaluator!2. Set OpenAI API Key
3. Install pre-reqs
4. Setup SQL Controls (One-Time)
- SQL safety control (blocks DROP, DELETE, TRUNCATE, ALTER, GRANT)
- Policy with the control
- Assigns policy to the SQL agent
For local execution, create the control withexecution: "sdk"insetup_sql_controls.py(seesql_control_data_sdk) and enableAGENT_CONTROL_LOCAL_EVAL=truewhen running the agent.
Running the Example
Local vs Remote Control Execution
Remote (server-side) controls:- Default mode
- Requires running the Agent Control server
- Uses
@control()to call/api/v1/evaluationon the server
- Set
AGENT_CONTROL_LOCAL_EVAL=true - Controls must be configured with
execution: "sdk" - Uses
agent_control.check_evaluation_with_local(...)before executing the tool
Expected Behavior
Safe Query (SELECT with LIMIT):How It Works
1. The @control() Decorator
2. Automatic Tool Call Detection
The@control() decorator:
- Detects this is a tool (via
nameattribute) - Creates a
Steppayload withtype="tool",name, andinput - Sends to server for evaluation before execution
- Blocks execution if control triggers deny action
3. SQL Control Execution
The SQL is evaluated using thesql evaluator:
- Parses the query
- Checks for blocked operations (DROP, DELETE, etc.)
- Validates LIMIT clauses
- Returns deny/allow decision
4. Fail-Safe Error Handling
If the control check fails with an error:- ✅ Execution is BLOCKED (fail-safe)
- ✅ RuntimeError is raised
- ❌ Query never executes
Troubleshooting
”Evaluator ‘sql’ not found”
Cause: Server was started before evaluators were installed, or using old code. Fix:“Policy ‘sql-protection-policy’ already exists”
Cause: Setup script was run multiple times. Fix: Either delete the policy via the API or use a different name insetup_sql_controls.py.
DROP TABLE still executes
Causes:- Server not running or evaluators not loaded (remote mode)
- Control not assigned to agent’s policy
- Control data missing/invalid (control not returned to agent)
- Local mode enabled but control is still
execution: "server"
- Restart server with
make run - Re-run
setup_sql_controls.py - Verify decorator code is up-to-date
Architecture
Files
sql_agent_protection.py- Main SQL agent with@control()decoratorsetup_sql_controls.py- One-time setup script for controls/policypyproject.toml- Dependencies and configurationREADME.md- This file
Key Security Features
- Fail-Safe by Default: Errors block execution
- Server-Side Validation: Remote controls enforced centrally
- SDK-Side Validation: Local controls run before tool execution
- Automatic Detection: Decorator auto-detects tool vs LLM calls
Source Code
View the complete example with all scripts and setup instructions:LangChain SQL Agent Example