DEV Community

Cover image for MCP Postgres Guard: Preventing SQL Injections and Slow Queries in AI Agents
Tarek Mohamed
Tarek Mohamed

Posted on

MCP Postgres Guard: Preventing SQL Injections and Slow Queries in AI Agents

Giving an AI assistant access to a database through MCP can be transformative for data analytics, but executing raw LLM-generated SQL against production or staging databases carries severe risks:

  • Accidental DROP TABLE or DELETE WITHOUT WHERE clauses.
  • Expensive unindexed sequential scans locking the database.
  • SQL injection vulnerabilities.

mcp-postgres-guard is an audited, secure MCP server for PostgreSQL designed to act as a resilient safety firewall.

GitHub logo seotarek / mcp-postgres-guard

A secure Model Context Protocol (MCP) server for PostgreSQL: query auditing, slow-query analysis, and SQL injection prevention for AI agents.

MCP Postgres Guard πŸ˜πŸ›‘οΈ

License: MIT Python 3.8+ MCP Protocol

A security-first Model Context Protocol (MCP) server for PostgreSQL: query auditing, destructive command interception, and SQL injection prevention for autonomous AI agents.

Developed by Tarek Mohamed (@seotarek)


πŸ“Œ Why MCP Postgres Guard?

Allowing AI agents (Claude, Cursor, LangChain agents) direct SQL access creates severe operational risks: accidental table dropping, unbounded deletions, or SQL injection. MCP Postgres Guard intercepts and audits all agent-generated SQL queries before they reach your database:

[ AI Agent / Claude / Cursor ]
               β”‚
               β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚           MCP Postgres Guard Server          β”‚
 β”‚  β€’ Destructive Query Blocker (DROP/TRUNCATE) β”‚
 β”‚  β€’ Unbounded DELETE/UPDATE Prevention        β”‚
 β”‚  β€’ Stacked Query & RCE Probe Detection       β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                        β”‚ (Only If Verified Safe)
                        β–Ό
             [ Production Database ]

πŸ› οΈ MCP Tools

  • pg_validate_query: Validates whether a SQL statement is safe to execute for an autonomous agent.
  • pg_check_read_only: Enforces a strict read-only policy…

πŸ”’ Security Features

  1. AST Query Validation: Parses incoming SQL statements using pglast / sqlparse to enforce read-only semantics (SELECT only, no INSERT/UPDATE/DELETE/TRUNCATE/ALTER).
  2. Transaction Timeouts & Statement Limits: Caps execution time at configurable thresholds (e.g. 3000ms) to prevent Denial of Service (DoS).
  3. Query Plan Cost Analysis: Runs EXPLAIN before executing heavy queries; rejects plans with estimated costs exceeding safety budgets.
  4. Data Masking (PII): Automatically masks sensitive columns (emails, phone numbers, password hashes) in result sets.

Check out the full repository and setup guide on GitHub:
πŸ‘‰ https://github.com/seotarek/mcp-postgres-guard

Top comments (0)