mcpField
P
PostgreSQL MCP Server

PostgreSQL MCP Server

ローカル
github.com/vignesh-codesデータベース2779 週間前に更新

PostgreSQL MCP Server について

PostgreSQL MCP Server は、LLM が PostgreSQL のスキーマを確認し、クエリの実行や CRUD 操作を行うための MCP サーバーです。このフォークでは、読み取り専用のクエリツールに加えて、テーブルの作成、更新、削除、削除用のツールが追加されています。

主な機能

  1. 01読み取り専用トランザクションでの SQL クエリ実行
  2. 02名前と列定義によるテーブル作成
  3. 03テーブルエントリの挿入、更新、削除ツール
  4. 04テーブル削除ツール
  5. 05列名と型を含むテーブルごとの JSON スキーマリソース

活用シーン

  1. 01LLM によるローカル PostgreSQL へのサンプルテーブル作成とデータ投入
  2. 02自然言語による支払いステータスの照会と更新
  3. 03Claude Desktop を使ったデータベースの読み書きフローの検証

MCP 対応

このプロジェクトは MCP サーバーであり、query、テーブル作成、挿入、更新、削除、テーブル削除のツールと、テーブルのスキーマリソースを MCP クライアントに公開します。

設定

ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。

mcp.json · 共通設定
{
"mcpServers": {
"ai-agents-mcp-pg": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"mcp/postgres"
]
}
}
}
起動コマンドdocker run -i --rm mcp/postgres

ローカルサーバーにはマシン上の実行環境(Node.js 18 以上など)が必要です。設定ファイルの場所はクライアントごとに異なります。

パッケージ
ocimcp/postgres

ツール

ツール一覧はまだ取得されていません

ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。

詳細

PostgreSQL MCP サーバー

PostgreSQL データベースへのアクセスを提供する Model Context Protocol サーバーです。このサーバーにより、LLM がデータベースと対話してスキーマの検査、クエリの実行、データベースエントリに対する CRUD (Create、Read、Update、Delete) 操作を実行できるようになります。このリポジトリは PostgreSQL MCP Server の拡張版であり、テーブルの作成、エントリの挿入、エントリの更新、エントリの削除、テーブルの削除といった機能を提供します。

インストール

PostgreSQL MCP サーバーをインストールするには、次の手順に従ってください:

  1. Docker と Claude Desktop をインストールします
  2. リポジトリをクローンします: git clone https://github.com/vignesh-codes/ai-agents-mcp-pg.git
  3. PG Docker コンテナを実行します docker run --name postgres-container -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=admin_password -e POSTGRES_DB=mydatabase -p 5432:5432 -d postgres:latest
  4. MCP サーバーをビルドします: docker build -t mcp/postgres -f src/Dockerfile .
  5. Claude Desktop を開き、claude_desktop_config.json の mcpServers フィールドを更新して MCP サーバーに接続します:

Claude Desktop での使用

このサーバーを Claude Desktop アプリで使用するには、claude_desktop_config.json の "mcpServers" セクションに次の構成を追加します:

Docker
  • macOS で Docker を実行している場合、サーバーがホストネットワーク (例: localhost) で動作しているときは host.docker.internal を使用します。
  • ユーザー名/パスワードは postgresql://user:password@host:port/db-name のように PostgreSQL URL に追加できます。
{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "mcp/postgres",
        "postgresql://username:[email protected]:5432/mydatabase"
      ]
    }
  }
}

構成ファイルを更新した後は、必ず Claude Desktop アプリを再起動してください。

追加された機能

既存の機能
  • query
    • 接続されたデータベースに対して読み取り専用の SQL クエリを実行します。
    • 入力: sql (string): 実行する SQL クエリ。
    • すべてのクエリは READ-ONLY トランザクション内で実行されます。
新しい機能
  1. テーブルの作成

    • テーブル名と列定義を指定して、新しいテーブルを動的に作成する機能。
    • Claude Desktop からの入力:
      {
        "tableName": "example_table",
        "columns": [
          { "name": "id", "type": "SERIAL PRIMARY KEY" },
          { "name": "name", "type": "VARCHAR(255)" },
          { "name": "age", "type": "INTEGER" }
        ]
      }
      
  2. エントリの挿入

    • 指定したテーブルに新しいエントリを挿入します。
    • Claude Desktop からの入力:
      {
        "tableName": "example_table",
        "values": {
          "name": "John Doe",
          "age": 30
        }
      }
      
  3. エントリの更新

    • 条件に基づいてテーブル内の既存のエントリを更新します。
    • Claude Desktop からの入力:
      {
        "tableName": "example_table",
        "values": {
          "age": 35
        },
        "conditions": "name = 'John Doe'"
      }
      
  4. エントリの削除

    • 条件に基づいてテーブル内の特定のエントリを削除します。
    • Claude Desktop からの入力:
      {
        "tableName": "example_table",
        "conditions": "name = 'John Doe'"
      }
      
  5. テーブルの削除

    • データベースから既存のテーブルを削除します。
    • Claude Desktop からの入力:
      {
        "tableName": "example_table"
      }
      
リソース

サーバーはデータベース内の各テーブルのスキーマ情報を提供します:

  • テーブルスキーマ (postgres://<host>/<table>/schema)
    • 各テーブルの JSON スキーマ情報。
    • 列名とデータ型を含みます。
    • データベースのメタデータから自動的に検出されます。

サンプル

Claude Desktop への入力
lets create three tables - users, payments and orders tables. Each table must have a unique id and other required columns. Payments table must have user id as foreign key. Orders table must have payment id as foreign key. Add some sample data to these tables
Claude Desktop からの出力
I'll help you create these three interconnected tables and populate them with sample data. Let's create them one by one.



1. First, let's create the users table:
{
  `columns`: [
    {
      `name`: `id`,
      `type`: `INTEGER PRIMARY KEY`
    },
    {
      `name`: `name`,
      `type`: `TEXT`
    },
    {
      `name`: `email`,
      `type`: `TEXT`
    },
    {
      `name`: `created_at`,
      `type`: `TEXT`
    }
  ],
  `tableName`: `users`
}

2. Now, let's create the payments table with a foreign key reference to users:
{
  `columns`: [
    {
      `name`: `id`,
      `type`: `INTEGER PRIMARY KEY`
    },
    {
      `name`: `user_id`,
      `type`: `INTEGER`
    },
    {
      `name`: `amount`,
      `type`: `REAL`
    },
    {
      `name`: `status`,
      `type`: `TEXT`
    },
    {
      `name`: `payment_date`,
      `type`: `TEXT`
    }
  ],
  `tableName`: `payments`
}

3. Finally, let's create the orders table with a foreign key reference to payments:
{
  `columns`: [
    {
      `name`: `id`,
      `type`: `INTEGER PRIMARY KEY`
    },
    {
      `name`: `payment_id`,
      `type`: `INTEGER`
    },
    {
      `name`: `product_name`,
      `type`: `TEXT`
    },
    {
      `name`: `quantity`,
      `type`: `INTEGER`
    },
    {
      `name`: `order_date`,
      `type`: `TEXT`
    }
  ],
  `tableName`: `orders`
}

Now, let's add some sample data to these tables.

First, let's add some users:
{
  `values`: {
    `id`: `1`,
    `name`: `John Doe`,
    `email`: `[email protected]`,
    `created_at`: `2024-01-01`
  },
  `tableName`: `users`
}

{
  `values`: {
    `id`: `2`,
    `name`: `Jane Smith`,
    `email`: `[email protected]`,
    `created_at`: `2024-01-05`
  },
  `tableName`: `users`
}

Now, let's add some payments for these users:
{
  `values`: {
    `id`: `1`,
    `amount`: `99.99`,
    `status`: `completed`,
    `user_id`: `1`,
    `payment_date`: `2024-01-10`
  },
  `tableName`: `payments`
}

{
  `values`: {
    `id`: `2`,
    `amount`: `149.99`,
    `status`: `completed`,
    `user_id`: `2`,
    `payment_date`: `2024-01-12`
  },
  `tableName`: `payments`
}


Finally, let's add some orders linked to these payments:
{
  `values`: {
    `id`: `1`,
    `quantity`: `1`,
    `order_date`: `2024-01-10`,
    `payment_id`: `1`,
    `product_name`: `Laptop`
  },
  `tableName`: `orders`
}

{
  `values`: {
    `id`: `2`,
    `quantity`: `2`,
    `order_date`: `2024-01-12`,
    `payment_id`: `2`,
    `product_name`: `Smartphone`
  },
  `tableName`: `orders`
}

Let's verify the data by querying each table:

{
  `sql`: `SELECT * FROM users;`
}

{
  `sql`: `SELECT * FROM payments;`
}

{
  `sql`: `SELECT * FROM orders;`
}

Great! We have now created three interconnected tables with sample data. Here's a summary of what we've set up:

1. Users table with basic user information
2. Payments table linked to users through user_id
3. Orders table linked to payments through payment_id

The data shows two users who have each made one payment and placed one order. We can now perform various queries to analyze the relationships between these tables. Would you like to see any specific queries or add more data?

Claude Desktop への入力
add few more users with IN_PROGRESS and UNSUCCESSFUL payment status
Claude Desktop からの出力
I'll add a few more users with different payment statuses.

First, let's add new users:

{
  `values`: {
    `id`: `3`,
    `name`: `Mike Johnson`,
    `email`: `[email protected]`,
    `created_at`: `2024-01-08`
  },
  `tableName`: `users`
}

{
  `values`: {
    `id`: `4`,
    `name`: `Sarah Wilson`,
    `email`: `[email protected]`,
    `created_at`: `2024-01-09`
  },
  `tableName`: `users`
}

{
  `values`: {
    `id`: `5`,
    `name`: `Tom Brown`,
    `email`: `[email protected]`,
    `created_at`: `2024-01-11`
  },
  `tableName`: `users`
}

Now, let's add payments with IN_PROGRESS and UNSUCCESSFUL statuses for these users:
{
  `values`: {
    `id`: `3`,
    `amount`: `299.99`,
    `status`: `IN_PROGRESS`,
    `user_id`: `3`,
    `payment_date`: `2024-01-13`
  },
  `tableName`: `payments`
}

{
  `values`: {
    `id`: `4`,
    `amount`: `199.99`,
    `status`: `UNSUCCESSFUL`,
    `user_id`: `4`,
    `payment_date`: `2024-01-13`
  },
  `tableName`: `payments`
}

{
  `values`: {
    `id`: `4`,
    `amount`: `199.99`,
    `status`: `UNSUCCESSFUL`,
    `user_id`: `4`,
    `payment_date`: `2024-01-13`
  },
  `tableName`: `payments`
}


{
  `values`: {
    `id`: `5`,
    `amount`: `399.99`,
    `status`: `IN_PROGRESS`,
    `user_id`: `5`,
    `payment_date`: `2024-01-13`
  },
  `tableName`: `payments`
}

Let's add orders for the IN_PROGRESS payments (note that we won't add orders for the UNSUCCESSFUL payment):
{
  `values`: {
    `id`: `3`,
    `quantity`: `1`,
    `order_date`: `2024-01-13`,
    `payment_id`: `3`,
    `product_name`: `Gaming Console`
  },
  `tableName`: `orders`
}

{
  `values`: {
    `id`: `4`,
    `quantity`: `1`,
    `order_date`: `2024-01-13`,
    `payment_id`: `5`,
    `product_name`: `4K TV`
  },
  `tableName`: `orders`
}

Let's verify the data by checking all payments with their status:

{
  `sql`: `SELECT u.name, p.amount, p.status, p.payment_date 
FROM users u 
JOIN payments p ON u.id = p.user_id 
ORDER BY p.payment_date;`
}

Now we have a good mix of payment statuses in our database:
- 2 completed payments (John and Jane)
- 2 in-progress payments (Mike and Tom)
- 1 unsuccessful payment (Sarah)

The orders table only contains entries for completed and in-progress payments, as it makes sense not to create orders for unsuccessful payments. Would you like to see any specific queries or add more data?
Claude Desktop への入力
Fetch the users info who have IN_PROGRESS payment status
Claude Desktop からの出力
I'll write a query to fetch user information for those who have payments with IN_PROGRESS status.
{
  `sql`: `
SELECT u.*, p.amount, p.payment_date
FROM users u
JOIN payments p ON u.id = p.user_id
WHERE p.status = 'IN_PROGRESS'
ORDER BY u.id;
`
}

We can see that two users have payments in progress:
1. Mike Johnson (attempting to pay $299.99)
2. Tom Brown (attempting to pay $399.99)

Both payments were initiated on January 13, 2024. Would you like to see any additional information about these users or their orders?

Claude Desktop への入力
Lets update the payment status to completed for Mike Johnson
Claude Desktop からの出力

I'll update Mike Johnson's payment status from IN_PROGRESS to completed.
{
  `values`: {
    `status`: `completed`
  },
  `tableName`: `payments`,
  `conditions`: {
    `user_id`: `3`
  }
}


Let's verify the update by checking all payments again:

{
  `sql`: `
SELECT u.name, p.amount, p.status, p.payment_date 
FROM users u 
JOIN payments p ON u.id = p.user_id 
ORDER BY p.status, p.payment_date;
`
}

The update was successful. Now we have:
- 3 completed payments (John, Jane, and Mike)
- 1 in-progress payment (Tom)
- 1 unsuccessful payment (Sarah)

Would you like to make any other updates to the data?

DB エントリ

mydatabase=# SELECT * FROM users;
 id |     name     |       email       | created_at 
----+--------------+-------------------+------------
  1 | John Doe     | [email protected]  | 2024-01-01
  2 | Jane Smith   | [email protected]  | 2024-01-05
  3 | Mike Johnson | [email protected]  | 2024-01-08
  4 | Sarah Wilson | [email protected] | 2024-01-09
  5 | Tom Brown    | [email protected]   | 2024-01-11
(5 rows)

mydatabase=# SELECT * FROM payments;
 id | user_id | amount |    status    | payment_date 
----+---------+--------+--------------+--------------
  1 |       1 |  99.99 | completed    | 2024-01-10
  2 |       2 | 149.99 | completed    | 2024-01-12
  4 |       4 | 199.99 | UNSUCCESSFUL | 2024-01-13
  5 |       5 | 399.99 | IN_PROGRESS  | 2024-01-13
  3 |       3 | 299.99 | completed    | 2024-01-13
(5 rows)

mydatabase=# SELECT * FROM orders;
 id | payment_id |  product_name  | quantity | order_date 
----+------------+----------------+----------+------------
  1 |          1 | Laptop         |        1 | 2024-01-10
  2 |          2 | Smartphone     |        2 | 2024-01-12
  3 |          3 | Gaming Console |        1 | 2024-01-13
  4 |          5 | 4K TV          |        1 | 2024-01-13
(4 rows)

ライセンス

この MCP サーバーは MIT ライセンスの下でライセンスされています。これは、MIT ライセンスの諸条件に従うことを条件として、ソフトウェアを自由に使用、変更、配布できることを意味します。詳細については、プロジェクトリポジトリ内の LICENSE ファイルを参照してください。

コメント

MCP は初めてですか?

プロトコルの概要から最初のサーバーの導入まで、よくある疑問にまとめて答えます。

AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。