Skip to main content
Glama

schema-gate

CI PyPI License: MIT Python 3.10+ Zero Dependencies

Fail-Closed Schema-Boundary Contract Gate for AI Coding Agents
(Zero-dependency Python 3 library, CLI, Claude Code Hook, MCP stdio server, SARIF CI gate & Runtime DB-API SQL Proxy)


Why AGENTS.md and Import Linters Are Not Enough

Your CI blocks risk from importing auth. Your AGENTS.md politely tells AI coding agents not to couple domains. Then an agent gets a sprint ticket "freeze high-risk users" and writes:

UPDATE users SET status = 'risk_locked' WHERE id = :entity_id

Zero forbidden imports. Unit tests pass. Yet this single line breaks three architectural invariants at once:

  1. Concept Compression (G2) — stuffs fraud-risk state into auth's account-lifecycle column;

  2. Foreign-Writer Coupling (G3) — mutates a table column owned by another module behind its back;

  3. Domain Pollution (G5) — injects an undeclared magic string 'risk_locked' that silently breaks downstream billing / fulfillment switch statements.

Or an agent inside billing runs an in-place UPDATE payment_intents SET intent_state = 'reconciled' on an AppendOnlyFact audit table (G4), permanently erasing intent history so cross-system reconciliation becomes mathematically impossible.

In controlled live coding-agent trials (120 symmetric trials on the original 40-task suite), both AGENTS.md soft rules and executable import linters allowed 73.33% (22/30) of unsafe schema-boundary mutations. SchemaGate cut unsafe deliveries to 0.00% (0/30) ($p = 4.77 \times 10^{-7}$), drove 100% (20/20) single-turn agent self-repair via structured diagnostics, had 0/10 false blocks, and ran in ~1.08 ms at 4,096 contracted columns.

In a strictly disjoint cross-vendor, multi-language external holdout (5 unseen domains × Python & TypeScript/Prisma × Claude Sonnet 5.5, Kimi K3, DeepSeek Chat = 180 arm evaluations), AGENTS.md and import linters still let 62.22% (28/45) of unsafe patches slip through (Claude 86.7%, Kimi 80.0%, DeepSeek 20.0%; Python 58.3%, TypeScript/Prisma 66.7%). SchemaGate cut unsafe deliveries to 0.00% (0/45) ($p = 7.45 \times 10^{-9}$), achieved 100% (28/28) 1-turn auto-repair across all three vendors and both languages, with 0/15 false blocks.


Related MCP server: sqlproctor

Five Enforced Invariants (G1–G5)

Formally verified in Isabelle/HOL 2026 (session RC_SchemaBoundary, zero sorry):

Gate

Invariant

What It Blocks

G1

Syntactic Import Compliance

Forbidden cross-module import / require edges

G2

Concept Decompression

Overloading one column with multiple domain concepts

G3

Single-Writer Ownership

Foreign modules writing to a column owned by another module

G4

Append-Only History Integrity

Destructive UPDATE, DELETE, REPLACE, or DROP COLUMN on AppendOnlyFact ledgers

G5

Domain Closure

Writing undeclared sentinel/magic tags outside a column's allowed_tags

Supported Languages & Surfaces

  • Python (.py): AST imports, raw & parameterized SQL (:name, %(name)s, %s, ?, $1), f-strings / string concat, SQLAlchemy (update(Table).values(...), delete(Table), query(Model).update(...)), Django ORM (Model.objects...update(...), .delete()), attribute/subscript writes (user.status = ...).

  • TypeScript / JavaScript (.ts, .tsx, .js, .mjs, .cjs): ES/CJS imports, raw SQL strings & template literals, Prisma (prisma.<model>.update/updateMany/upsert/delete/deleteMany), Drizzle (db.update(tbl).set(...), db.delete(tbl)).

  • SQL Migrations (.sql): UPDATE, DELETE, REPLACE, INSERT, ALTER TABLE ... DROP COLUMN.

  • Runtime DB-API 2.0 SQL Proxy: wrap_dbapi_connection(conn, contract, module_name) intercepts dynamically constructed SQL at test/staging runtime using the exact same schema-contract.json.


Installation

Zero third-party dependencies (Python 3.10+ stdlib only):

pip install schema-boundary-gate

Or from source:

pip install -e .

Quickstart

1. CLI & CI Gate (Text / JSON / SARIF)

# Generate starter schema-contract.json
schema-gate init

# Check repository (exit 0 on PASS, exit 2 on BLOCK with repair hints)
schema-gate check --contract schema-contract.json src/

# Check only staged git files or PR diff against main
schema-gate check --contract schema-contract.json --staged
schema-gate check --contract schema-contract.json --diff origin/main --format sarif

Run the interactive multi-language + runtime-proxy demo:

bash examples/demo.sh

2. Claude Code PreToolUse Hook (1-Turn Auto-Repair)

Add to .claude/settings.json (see examples/claude-code-settings.json):

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Write|Edit|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "schema-gate hook claude-code --contract schema-contract.json"
          }
        ]
      }
    ]
  }
}

When Claude Code attempts a shortcut mutation, schema-gate exits 2 before disk write with exact rule codes and repair hints so the agent automatically decompresses the write into an owned column on the next turn.

3. MCP Stdio Server (Claude Code / Cursor / Any MCP Agent)

{
  "mcpServers": {
    "schema-gate": {
      "command": "schema-gate",
      "args": ["mcp", "--contract", "schema-contract.json"]
    }
  }
}

Exposed MCP tools:

  • schema_gate_inspect_contract — inspect table owners, concepts, append-only tables, and allowed enum tags before coding

  • schema_gate_check_patch — dry-run check a Python / TypeScript / SQL patch against G1–G5

  • schema_gate_check_files — scan files or directories

  • schema_gate_validate_sql — check a raw SQL statement + parameters

4. Runtime SQL Proxy (Dynamic SQL Protection in Tests)

import sqlite3
from schema_gate import SchemaContract, wrap_dbapi_connection

contract = SchemaContract.from_json_file("schema-contract.json")
conn = wrap_dbapi_connection(sqlite3.connect(":memory:"), contract, module_name="risk")
# Any dynamic SQL violating G2-G5 raises SchemaBoundaryViolationError before execution!

5. Pre-commit Hook

repos:
  - repo: https://github.com/iannil/schema-gate
    rev: v0.1.0
    hooks:
      - id: schema-gate

Running Tests

python3 -m unittest discover -s tests -v

Paper & Formal Proofs

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to query architecture context, data contracts, and blast radius to prevent cross-repo architectural breakage before merging.
      22
      Apache 2.0
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables deterministic semantic verification of agent-generated SQL queries against a git-versioned contract, blocking incorrect queries and returning structured feedback for self-correction.
      MIT
    • A
      license
      D
      quality
      B
      maintenance
      Enables AI agents to inspect, draft, validate, and locally verify contract changes against frozen policies, with separate input and output roots and no approval or verified-signing capability.
      10
      MIT