Skip to main content

pg-guard-mcp

A PostgreSQL MCP server that enforces read-only access at the protocol and privilege level — not by parsing the query string and hoping.

Why this exists

The official @modelcontextprotocol/server-postgres shipped a read-only mode that a single COMMIT; could bypass: it wrapped the agent's query in BEGIN TRANSACTION READ ONLY and sent the whole thing as one string. Postgres accepts semicolon-separated multiple statements in that mode, so SELECT 1; COMMIT; DROP SCHEMA public CASCADE; closed the read-only transaction early and ran the drop as an ordinary write. The package was deprecated over it. (Datadog Security Labs writeup)

pg-guard-mcp exists because that bug class — "read-only" enforced only by string inspection — is still common across the MCP ecosystem. It defends in three independent layers, so no single mistake is fatal:

  1. Protocol layer (the real boundary). Every query runs through Postgres's extended query protocol (Parse/Bind/Execute), never the simple query protocol. The extended protocol structurally rejects more than one statement per Parse message — Postgres itself refuses it, before any of our code runs. This is why the Datadog exploit cannot work here regardless of what string is submitted.
  2. Session layer. Every connection sets default_transaction_read_only = on at the session level, so even a query that somehow reached the database as a write is rejected by Postgres.
  3. Pre-flight layer. Before a query is even sent, it's checked for multiple statements and transaction-control keywords (COMMIT, ROLLBACK, BEGIN, SAVEPOINT, ...) and rejected with a clear error. This exists to fail fast and loud, not as the primary defense.

On top of that, connecting with a database role that has had write privileges REVOKEd is the recommended (and startup-checked) setup — belt and suspenders at the privilege layer too.

Tools

Tool Does
pg_run_query(sql) Run one read-only statement, return rows
pg_explain_query(sql) Return the query plan without running it
pg_list_tables(schema="public") List tables/views in a schema
pg_describe_table(table_name, schema="public") List a table's columns
pg_check_privileges() Report any write grant the connected role actually holds — should always come back empty

Setup

pip install -e ".[dev]"
export PG_GUARD_DSN="host=127.0.0.1 dbname=mydb user=myapp_readonly password=..."
python -m pg_guard_mcp.server

See .env.example for all supported environment variables, and scripts/setup_dev_db.sh for a working example of setting up a properly-restricted read-only role (the setup this project's own tests run against).

Testing

pip install -e ".[dev]"
pytest tests/ -v

tests/test_safety.py is pure-Python and needs no database. tests/test_db.py and tests/test_server.py run against a real local PostgreSQL instance — including the exact exploit payload that deprecated the official Postgres MCP server — and skip automatically if pgguard_test isn't reachable. Run scripts/setup_dev_db.sh once to create it.

Status

Early build, 58 passing tests (unit + live-Postgres integration). Not yet published to PyPI.

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_guard_mcp-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_guard_mcp-0.1.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file pg_guard_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: pg_guard_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.5

File hashes

Hashes for pg_guard_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a38b6813c3fb1e0ccb320f59a44aea7d648ce1f9d9e61185547c94ecbf1c0568
MD5 aa4979e21f35183771d8674d63d664b5
BLAKE2b-256 ff5ced52ba9d2eb0d63b48b63c21e75725f3d5a81502461a8862afb7a500000f

See more details on using hashes here.

File details

Details for the file pg_guard_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pg_guard_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.5

File hashes

Hashes for pg_guard_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eae446b1a856c0f8f14b86056a15833531f13fecdd88ade28b89801d9988d407
MD5 3a31000d3be4a00e56fa8602b39f752c
BLAKE2b-256 7273fa2ccf678c368301b45d2fd6c662fa725d388aac19e411d2d9352e565a0f

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page