AI Safety & Guardrails

Preventing Destructive AI SQL Queries in Claude Code, Cursor & Autonomous Agents

When developers hand terminal control or database tooling to autonomous coding agents like Claude Code, Cursor, or Windsurf, the agent frequently attempts to "test" its code by drafting and running SQL queries. Without explicit guardrails, agents regularly hallucinate destructive queries, bypass soft-delete rules, or perform cascading deletes across production tables.

The 3 Major Failure Modes of AI Database Interactions

1. Unconstrained Destructive DML

Agents executing DELETE FROM users without a WHERE clause during migration generation or debugging steps, or executing DROP TABLE when attempting to recreate temporary tables.

2. Hallucinated Multi-Hop JOINs

In schemas with 15+ tables, LLMs invent imaginary bridge tables (e.g. user_orders instead of orders.user_id) or produce cartesian products that spike database CPU to 100%.

3. Bypassing Soft-Deletes & Policies

Autonomous agents don't inherently know your organization uses deleted_at IS NULL or multi-tenant isolation, leading to silent data leaks in query generation.

Why Traditional SQL MCP Servers Aren't Enough

Many teams connect raw database MCP servers directly to their agent. However, raw database connectors have no semantic awareness of business rules:

The Solution: Compile-Time Safety Guardrails

Instead of giving the LLM raw credentials or pasting massive database dumps into system prompts, Schemap compiles a token-compressed topological map with deterministic guardrails.

When Schemap compiles your database into your repository's AGENTS.md or .cursor/rules/database.mdc, it injects explicit anti-hallucination guardrails:

<!-- schemap:start -->
# AGENTS.md - Database Context & Guardrails

## AI Safety & Anti-Hallucination Guardrails
- [SAFETY] Immutability Guardrail: Do not generate DELETE or UPDATE queries for audit/financial records: `payments`, `audit_logs`
- [SAFETY] Soft-Delete Guardrail: Always include `deleted_at IS NULL` when querying `users`, `teams`, `workspaces`
- [SAFETY] Multi-Tenant Filter: Always filter queries by `organization_id = CURRENT_ORG_ID`

## Table Map & Verified JOIN Topologies
### Table: `orders`
Columns: id, user_id, order_number, status, total_cents, created_at
Foreign Keys: user_id -> users.id
<!-- schemap:end -->

Step-by-Step Implementation

Step 1: Install Schemap

Run Schemap locally without installing global bloat using pip or uvx:

pip install schemap-tool

Step 2: Define Your Guardrails in schemap.yaml

Configure immutability, exclusions, and business definitions in your local config:

database:
  connection_url: "postgresql://postgres:password@localhost:5432/production_replica"
  exclude_tables:
    - "spatial_ref_sys"
    - "secret_tokens"

output:
  file_path: "./AGENTS.md"
  format: "markdown"

guardrails:
  immutable_tables:
    - "payments"
    - "audit_logs"
  soft_delete_column: "deleted_at"

Step 3: Compile Database Context

schemap sync

Both Claude Code and Cursor automatically ingest AGENTS.md on every prompt. Your agent now understands your exact database relationships, respects immutability constraints, and writes correct SQL queries without needing a live connection to execute blind destructive mutations.

Using the Dynamic Model Context Protocol (MCP) Mode

If you prefer dynamic querying over static context, run Schemap's built-in MCP server:

schemap mcp --snippet cursor

This enables tool calls like schemap_validate_query and schemap_get_join_path, ensuring your agent verifies SQL before touching any database.

Stop Database Hallucinations Today

Compile token-compressed database context and protect your schema from autonomous AI errors.

Star Schemap on GitHub