AI agents
An agent steered by prompt injection or a poisoned tool result can write SQL that deletes, exfiltrates or opens up data. Lexega checks each query the agent writes against your policy before it executes, and returns a decision the agent cannot change.
A list of dangerous statements always has gaps, so start from what the agent may do. The first rule makes anything that is not a query critical. The second catches a query that writes or exports data anyway, such as a PostgreSQL WITH … DELETE … RETURNING. The policy blocks critical and high findings.
So copying data out to a stage, swapping or cloning tables, calling procedures, changing grants or network policies, and a delete stacked after a select are all blocked. Plain queries pass, and so do UNION, INTERSECT and EXCEPT (set_select in the rule), unless one raises a high finding.
A query can still call functions, so the database role decides which functions the agent can reach, and rules can name the ones to block. Widen the list with more statement kinds, such as insert into a scratch schema, as your agent needs.
rules:
- id: AGENT-READ-ONLY
risk_level: critical
message: "Only queries are allowed from the agent."
triggers:
kind:
not_in: [select, set_select]
- id: AGENT-QUERY-WRITES
risk_level: critical
message: "This query writes or exports data."
triggers:
all_of:
- kind: { in: [select, set_select] }
- any_of:
- query.writes_table: { count: { gt: 0 } }
- query.file_exports: { count: { gt: 0 } }
schema_version: 1
policy_id: agent
policy_version: "1"
policies: []
severity_actions:
- critical: block
high: block
default_action: allow
Agents that speak the Model Context Protocol, such as Claude Code and Cursor, get one tool, check_sql. The server loads the policy once and stays warm, so each check skips process start-up.
{
"mcpServers": {
"lexega": {
"command": "lexega-sql",
"args": [
"mcp",
"--policy", "agent-policy.yaml",
"--custom-rules", "agent-rules.yaml",
"--env", "prod",
"--strict"
]
}
}
}Pipe the SQL to the program and read a JSON decision from standard output. Exit code 2 means blocked, including a statement --strict refuses. Exit code 1 means the check did not run, for example because the policy file is missing; treat that as blocked too.
echo "$SQL" | lexega-sql analyze --stdin -q \ --policy agent-policy.yaml --custom-rules agent-rules.yaml \ --env prod --mode runtime --strict --decision-out -
Policy, environment and exceptions are fixed when the server starts. The tool accepts only the SQL and its dialect.
The same policy file gates pull requests in CI and queries at runtime, so a rule written once applies to both.
Each call writes a decision record hashed over the SQL, the policy and the result, the same evidence CI produces.
With --strict, a statement the parser cannot fully analyze is refused instead of allowed. Under the rules and policy above it comes back blocked: a blocked decision from the MCP server, exit code 2 from the command line.
Lexega decides whether a query is acceptable under your policy: an unbounded write, a dropped masking policy, a grant to everyone, a credential in a statement. The database role decides what the agent can reach at all. Give the agent the narrowest role that does the job, and put the policy check in front of it.
Runtime mode and the MCP server need no key; a trial or Team key adds analysis across statements and procedures. The full reference, with a read-only agent example, is in Runtime / Agent Integration.
Install free and run the MCP server and runtime checks with no key. A 30-day trial adds analysis across statements and procedures.