Skip to main content
Glama
RathodDarshil

PostgreSQL Query MCP Server

MCP Postgres クエリサーバー

Claude Desktop およびその他の MCP クライアントで動作するように設計された、読み取り専用モードで PostgreSQL データベースをクエリするためのモデル コンテキスト プロトコル (MCP) サーバー実装。

概要

このプロジェクトは、以下を提供するモデル コンテキスト プロトコル (MCP) サーバーを実装します。

  1. PostgreSQL データベースへの安全な読み取り専用インターフェース

  2. MCPプロトコルによるClaude Desktopとの統合

  3. SELECTクエリのみが実行されるようにするSQLクエリ検証

  4. クエリタイムアウト保護(10秒)

Related MCP server: PostgreSQL MCP Server

前提条件

  • Node.js (v14以降)

  • npm (Node.js に付属)

  • PostgreSQL データベース (接続詳細はコマンドラインから提供)

インストール

# Clone the repository
git clone https://github.com/RathodDarshil/mcp-postgres-query-server.git
cd mcp-postgres-query-server

# Install dependencies
npm install

# Build the project
npm run build

Claudeデスクトップに接続しています

Claude Desktop を自動的に起動して MCP サーバーに接続するように設定できます。

  1. Claude Desktop 構成ファイルにアクセスします。

    • クロードデスクトップを開く

    • 設定 > 開発者 > 設定の編集に移動します

    • これにより、デフォルトのテキストエディタで設定ファイルが開きます。

  2. claude_desktop_config.jsonのmcpServersセクションに postgres-query-server を追加します。

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/path/to/your/mcp-postgres-query-server/dist/index.js",
                "postgresql://username:password@hostname:port/database"
            ]
        }
    }
}
  1. /path/to/your/プロジェクト ディレクトリへの実際のパスに置き換えます。

  2. PostgreSQL 接続文字列を実際のデータベース資格情報に置き換えます。

  3. ファイルを保存し、Claude Desktopを再起動します。設定のMCPサーバー選択ドロップダウンにMCPサーバーが表示されるはずです。

構成例

postgres-query を使用した構成ファイルの完全な例を次に示します。

{
    "mcpServers": {
        "postgres-query": {
            "command": "node",
            "args": [
                "/Users/darshilrathod/mcp-servers/mcp-postgres-query-server/dist/index.js",
                "postgresql://user:password@localhost:5432/mydatabase"
            ]
        }
    }
}

構成の更新

Claude Desktop の構成を更新するには:

  1. クロードデスクトップを開く

  2. 設定 > 開発者 > 設定の編集に移動します

  3. 設定ファイルに変更を加える

  4. ファイルを保存する

  5. 変更を有効にするには、Claude Desktopを再起動してください。

  6. MCPサーバーのコードを更新した場合は、再起動する前に必ずnpm run buildで再構築してください。

特徴

  • 読み取り専用データベースアクセス: セキュリティのため、SELECTクエリのみが許可されます

  • クエリ検証: 潜在的に有害なSQL操作を防止します

  • タイムアウト保護: 10秒以上実行されたクエリは自動的に終了します

  • MCPプロトコルサポート: モデルコンテキストプロトコルの完全な実装

  • JSONレスポンスフォーマット: クエリ結果は構造化されたJSON形式で返されます

API

ツール

クエリ-Postgres

構成された PostgreSQL データベースに対して読み取り専用の SQL クエリを実行します。

パラメータ:

  • query (文字列): 実行するSQL SELECTクエリ

応答:

  • 以下を含む JSON オブジェクト:

    • rows : 結果セットの行

    • rowCount : 返される行数

    • fields : 列のメタデータ

例:

query-postgres: SELECT * FROM users LIMIT 5

発達

メインサーバーの実装はsrc/index.tsにあります。主なコンポーネントは次のとおりです。

  • PostgreSQL接続プールの設定

  • クエリ検証ロジック

  • MCP サーバーの構成

  • ツールとリソースの定義

サーバーの動作を変更するには、次の操作を行います。

  • isReadOnlyQuery()のクエリ検証ロジックを編集します。

  • MCP サーバーに追加のツールやリソースを追加する

  • クエリのタイムアウト期間を変更します(現在は10秒)

セキュリティに関する考慮事項

  • サーバーはすべてのクエリを検証し、読み取り専用であることを確認します。

  • データベースへの接続にはSSLを使用します

  • クエリタイムアウトによりリソース枯渇を防止

  • 書き込み操作は許可されていません

  • データベースの資格情報はファイルに保存されず、コマンドライン引数を介して直接渡されます。

ライセンス

ISC

貢献

貢献を歓迎します!お気軽にプルリクエストを送信してください。

Available Tools

1 tool
query-postgresD
ParametersJSON Schema
NameRequiredDescriptionDefault
queryYes

TDQS

D1/5.0
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Tool has no description.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness1/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Tool has no description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Tool has no description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Tool has no description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose1/5

Does the description clearly state what the tool does and how it differs from similar tools?

Tool has no description.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Tool has no description.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

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

  1. 1 tool update
    • First observedquery-postgres

TDQS

D1.7/5.0

Scored across 1 tool

Disambiguation5/5

With only one tool, there is no possibility of ambiguity or overlap between tools. The tool's purpose is singular and clear, eliminating any risk of misselection.

Naming Consistency5/5

A single tool inherently has perfect naming consistency, as there are no other tools to compare it against. The name 'query-postgres' follows a clear verb-noun pattern without any conflicting conventions.

Tool Count2/5

A single tool is too few for a PostgreSQL server's apparent scope, which typically involves multiple operations like querying, inserting, updating, and deleting data. This minimal set severely limits functionality and agent workflows.

Completeness1/5

The tool surface is severely incomplete for a PostgreSQL domain, covering only querying. Obvious gaps include create, update, delete, schema management, and transaction handling, which will cause frequent agent failures in database interactions.

Maintenance

ActivityInactive
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers

  • -
    license
    Not graded
    quality
    A
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases. This server enables LLMs to inspect database schemas and execute read-only queries.
    122,323 npm
    91,120
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.
    122,323 npm
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server that provides read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only queries.
    122,323 npm
    MIT
  • A
    license
    Not graded
    quality
    D
    maintenance
    A Model Context Protocol server providing read-only access to PostgreSQL databases, enabling LLMs to inspect database schemas and execute read-only SQL queries.
    122,323 npm
    MIT