Skip to main content
Glama
pg-guard-mcp

pg-guard-mcp

pg-guard-mcp

PyPI

PostgreSQL 用 MCP サーバーです。読み取り専用アクセスをプロトコルと権限のレベルで強制します。クエリ文字列を解析して祈るだけの方式ではありません。

インストール

pip install pg-guard-mcp
# or, without installing anything permanently:
uvx pg-guard-mcp

Related MCP server: PostgreSQL MCP Server

これが存在する理由

公式の @modelcontextprotocol/server-postgres が実装していた読み取り専用モードは、COMMIT; の 1 文で回避できました。その実装は、エージェントのクエリを BEGIN TRANSACTION READ ONLY でラップし、全体を 1 つの文字列として送信していました。Postgres はこのモードでセミコロン区切りの複数文を受け付けるため、SELECT 1; COMMIT; DROP SCHEMA public CASCADE; は読み取り専用トランザクションを早期に閉じ、その DROP を通常の書き込みとして実行できてしまいました。この問題により、このパッケージは非推奨になりました。(Datadog Security Labs のレポート)

pg-guard が存在するのは、この「文字列検査だけで『読み取り専用』を強制する」というバグの分類が、今も MCP エコシステムで広く見られるからです。これは 3 つの独立した層で防御するため、どれか 1 つのミスが致命的になることはありません。

  1. プロトコル層(実際の境界). すべてのクエリは、Postgres の拡張クエリプロトコル(Parse / Bind / Execute)でのみ実行され、単純クエリプロトコルは使われません。拡張プロトコルは構造上、1 つの Parse メッセージに複数の文を拒否します。Postgres 自身が、私たちのコードが実行される前にそのクエリを否定するのです。どんな文字列を送っても、Datadog のエクスプロイトがここで機能しないのはこのためです。

  2. セッション層. すべての接続は、セッションレベルで default_transaction_read_only = on を設定します。万一書き込みとしてデータベースに到達したクエリがあっても、Postgres が拒否します。

  3. 送信前チェック層. クエリを送信する前に、複数文とトランザクション制御キーワード(COMMIT、ROLLBACK、BEGIN、SAVEPOINT、...)をチェックし、明確なエラーで拒否します。これは主防御ではなく、迅速かつ明確に失敗させるためのものです。

さらに、書き込み権限が REVOKE されているデータベースロールで接続することが推奨されます(起動時にも確認されます)。権限層でも二重の安全策を取っていることになります。

ツール

Tool

機能

pg_run_query(sql)

読み取り専用の文を 1 つだけ実行し、結果行を返します

pg_explain_query(sql)

クエリを実行せずに実行プランを返します

pg_list_tables(schema="public")

スキーマ内のテーブル/ビューを一覧表示します

pg_describe_table(table_name, schema="public")

テーブルの列を一覧表示します

pg_check_privileges()

接続中のロールが現在保持している書き込み権限を報告します。通常は空になるはずです

セットアップ

pip install pg-guard-mcp
export PG_GUARD_DSN="host=127.0.0.1 dbname=mydb user=myapp_readonly password=..."
pg-guard-mcp

MCP クライアントが、PG_GUARD_DSN を環境設定に設定した上で pg-guard-mcp コマンド(恒久的なインストールを避けるなら uvx pg-guard-mcp)を指すようにしてください。

サポートされている環境変数は .env.example に、適切に制限された読み取り専用ロールのセットアップ例(このプロジェクト自身のテストが前提にしている設定)は scripts/setup_dev_db.sh にあります。

テスト

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

tests/test_safety.py は純 Python で、データベースは不要です。tests/test_db.py と tests/test_server.py は実際のローカル PostgreSQL インスタンスに対して実行されます。これには、公式の Postgres MCP サーバーが非推奨になった原因となったものと同一のエクスプロイトペイロードも含まれており、pgguard_test に接続できない場合は自動的にスキップされます。一度 scripts/setup_dev_db.sh を実行すれば、そのデータベースが作成されます。

ステータス

v0.1.0、PyPI で公開中。58 件のテストが成功しています(単体テスト+実 PostgreSQL を使った統合テスト。公式の server-postgres を非推奨にした完全なエクスプロイトも、公開版パッケージを pip installat 설치한 fresh 环境で実行)。

ライセンス

MIT

Available Tools

5 tools
pg_check_privilegesA

Report any write privilege (INSERT/UPDATE/DELETE/TRUNCATE) the connected role actually holds on any table. An empty list is the expected, safe result — anything else means the privilege layer of defense is missing and the role should be locked down (see README.md), even though the protocol and session layers still hold on their own.

Inputs: no named parameters.

pg_describe_tableA

List the columns of a table: name, data type, nullability, and default, in column order.

Inputs: table_name (required), schema.

pg_explain_queryA

Return the PostgreSQL query plan for a read-only SELECT/WITH statement, without running it. Useful for checking whether a query will be slow before running it for real.

Inputs: sql (required).

pg_list_tablesA

List the tables and views visible to the connected role in the given schema (default: public).

Inputs: schema.

pg_run_queryA

Run a single read-only SQL statement (SELECT / WITH / EXPLAIN / SHOW) against the configured PostgreSQL database and return the rows.

Rejects anything that isn't exactly one plain read-only statement — multiple statements, transaction-control keywords (COMMIT, ROLLBACK, BEGIN, ...), and any write/DDL/admin keyword anywhere in the query are all refused before the query is sent to the database. Results are capped at PG_GUARD_ROW_LIMIT rows (default 1000).

Inputs: sql (required).

Tool Schema Changelog

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

Loading tool schema changelog…

    TDQS

    A4.3/5.0

    Scored across 5 tools

    Disambiguation5/5

    Each tool has a clearly distinct role: executing read-only queries, explaining query plans, listing tables, describing table schemas, and checking write privileges. There is no functional overlap or ambiguity between them.

    Naming Consistency5/5

    All tool names follow the same pg_ prefix with a consistent verb_noun pattern: run_query, explain_query, list_tables, check_privileges, describe_table. The naming is uniform and predictable.

    Tool Count5/5

    Five tools is well-scoped for a PostgreSQL guard/observer server. Each tool serves a clear, non-redundant purpose and the set feels complete without being bloated.

    Completeness5/5

    The tool surface covers the core read-only workflow: exploring available tables, inspecting schemas, validating query plans, running queries, and verifying the security posture. No obvious dead ends or missing security-relevant operations are apparent.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      C
      maintenance
      Enables secure read-only access to PostgreSQL databases through SELECT queries only, with tools for exploring schemas, listing tables, and executing common queries while preventing any data modification operations.
      4
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      Provides secure, read-only access to PostgreSQL databases for schema inspection and data querying. It enables users to list tables, describe structures, and execute SELECT statements while strictly blocking destructive operations.
      1
      MIT