Skip to main content
Glama
MCP Database Guardrail

MCP Database Guardrail

by ernestoh0183

πŸ›‘οΈ MCP Database Guardrail

License: MIT Python: 3.10+ Protocol: MCP Zero-Trust: Read--Only

Enterprise-grade Model Context Protocol (MCP) servers for secure, read-only database exploration (SQLite, PostgreSQL, MySQL) with AST guardrails and zero-trust controls.

Official companion open-source repository for the 4Ruy.com engineering series on AI Skills and Model Context Protocol:


πŸ’‘ The Problem: The "House Key" vs. The "Hotel Keycard"

Giving Large Language Models (LLMs) direct, unconstrained database credentials is like handing a stranger the master key to your entire house: they can open personal drawers, read sensitive records, or demolish a wall with a sledgehammer.

MCP Database Guardrail implements the Hotel Keycard Architecture:

  1. Scoped Room Access: The AI agent is only granted access to whitelisted tables and schemas, isolating internal administration and security metadata.

  2. Read-Only Observation: The agent can inspect the table schema and execute analytical read operations (SELECT, WITH), but is structurally blocked from destructive actions (INSERT, UPDATE, DELETE, DROP, ALTER, TRUNCATE).

  3. Resource Safeguards: Enforces hard limits (LIMIT 50) and strict execution timeouts (10s) to prevent runaway Cartesian joins or memory exhaustion.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     AI Host / Agent Client      β”‚
β”‚ (Claude Desktop, Cursor, etc.)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚ JSON-RPC 2.0 (MCP Protocol)
                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           MCP Database Guardrail Server                β”‚
β”‚                                                        β”‚
β”‚  β”œβ”€β”€ Resource: Database Schema Inspection (Read-Only)  β”‚
β”‚  β”œβ”€β”€ Tool: Safe SQL Execution (AST Guardrail)          β”‚
β”‚  └── Prompt: Business KPI Analysis Template            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                 β”‚ Strict SELECT / Read-Only Transaction
                 β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Target Database Engine      β”‚
β”‚  (SQLite / PostgreSQL / MySQL)  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Related MCP server: sqlgate-mcp

πŸš€ Key Features

  • Zero-Dependency Quickstart: Run locally in 10 seconds with server_sqlite.py (automatically seeds an empresa_demo.db dataset).

  • Multi-Engine Support: Ready-to-use production servers for SQLite, PostgreSQL, and MySQL.

  • AST & Token Guardrails: Programmatic rejection of dangerous keywords, multi-statement injection attempts, and DDL mutations.

  • Automatic LIMIT Enforcement: Appends LIMIT 50 if omitted by the model, preventing buffer overflows.

  • Native MCP Components:

    • Resources (schema): Allows models to understand relational schemas without consuming execution tokens blindly.

    • Tools (ejecutar_consulta_segura): Controlled query execution returning structured JSON with latency metrics.

    • Prompts (asistente_kpis_negocio): Standard operating guidelines for autonomous data analysis.


πŸ“¦ Installation & Setup

1. Clone the repository

git clone https://github.com/ernestoh0183/mcp-database-guardrail.git
cd mcp-database-guardrail

2. Create virtual environment & install dependencies

python -m venv .venv
# On Windows:
.venv\Scripts\activate
# On Linux/macOS:
source .venv/bin/activate

pip install -e .

πŸ› οΈ Configuration for AI Clients

Add the server to your favorite MCP-compatible client configuration file:

Claude Desktop (claude_desktop_config.json)

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json

  • Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "sqlite-guardrail": {
      "command": "python",
      "args": [
        "/absolute/path/to/mcp-database-guardrail/server_sqlite.py"
      ],
      "env": {
        "SQLITE_DB_PATH": "empresa_demo.db"
      }
    }
  }
}

Cursor (.cursor/mcp.json or Settings -> Features -> MCP)

{
  "mcpServers": {
    "postgres-guardrail": {
      "command": "python",
      "args": [
        "/absolute/path/to/mcp-database-guardrail/server_postgres.py"
      ],
      "env": {
        "PG_HOST": "localhost",
        "PG_PORT": "5432",
        "PG_DATABASE": "empresa_prod",
        "PG_USER": "readonly_ai_user",
        "PG_PASSWORD": "your_secure_password_here",
        "PG_SCHEMA": "public"
      }
    }
  }
}

πŸ§ͺ Quick Test in Terminal

You can test the server directly with the official MCP Inspector:

npx @modelcontextprotocol/inspector python server_sqlite.py

Then ask your AI agent:

"Show me the top 3 best-selling products this month and their total revenue."

The model will:

  1. Inspect sqlite://schema to locate the pedidos and productos tables.

  2. Draft a SELECT query with JOIN and GROUP BY.

  3. Invoke ejecutar_consulta_segura through the MCP protocol.

  4. Return an executive summary with verified, grounded figures.


πŸ“„ License & Attribution

This project is licensed under the MIT License - see the LICENSE file for details.

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 safely explore and query databases across SQLite, PostgreSQL, MySQL, and MSSQL, with schema introspection, natural language queries, and data profiling.
      MIT
    • A
      license
      Not graded
      quality
      C
      maintenance
      Enables AI agents to query Postgres, BigQuery and DuckDB read-only through policy-checked SQL, with AST-based guards that refuse writes and dangerous functions, column masking on the way out, and an audit log of every query. Also exposes schema browsing and query-planning tools so agents can see allowed tables, columns and costs before running anything.
      MIT
    • A
      license
      A
      quality
      B
      maintenance
      Enables AI agents to inspect database schemas and run strictly read-only SQL queries with guardrails against destructive statements, multi-statement chaining, and unbounded scans. Supports SQLite natively and PostgreSQL optionally.
      4
      84 PyPI
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables AI assistants to explore and query PostgreSQL or SQLite databases read-only, with SQL allow-listing, automatic row limits, statement timeouts, column masking, per-tool rate limits and JSONL audit logging.
      MIT