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 TABLEorDELETE WITHOUT WHEREclauses. - 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.
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 ππ‘οΈ
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
-
AST Query Validation: Parses incoming SQL statements using
pglast/sqlparseto enforce read-only semantics (SELECTonly, noINSERT/UPDATE/DELETE/TRUNCATE/ALTER). - Transaction Timeouts & Statement Limits: Caps execution time at configurable thresholds (e.g. 3000ms) to prevent Denial of Service (DoS).
-
Query Plan Cost Analysis: Runs
EXPLAINbefore executing heavy queries; rejects plans with estimated costs exceeding safety budgets. - 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)