MCP PostgreSQL Server

by Maxim2324
Verified

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

Integrations

  • Manages configuration through environment variables using .env files for storing database credentials, server settings, and security parameters.

  • Supports repository cloning for installation and source code management during development.

  • Provides example code for API interaction, allowing clients to execute queries and interact with analysis templates.

MCP PostgreSQLサーバー

次の機能を備えたモデル コントローラー プロバイダー (MCP) サーバー:

  • PostgreSQLデータベースに接続する
  • テーブルスキーマをリソースとして公開する
  • 読み取り専用SQLクエリを実行するためのツールを提供します
  • 一般的なデータ分析タスクのプロンプトが含まれています

特徴

  • スキーマ探索: データベースのスキーマ、テーブル、列を参照します
  • 読み取り専用クエリ実行: データベースに対してSQLクエリを安全に実行します
  • データ分析プロンプト: 一般的な分析タスク用の事前構築された SQL テンプレート
  • データの可視化: 可視化のためのデータを生成する
  • 関係探索: テーブルの関係と外部キーを視覚化する
  • APIドキュメント: 自動生成されたOpenAPI仕様

建築

このアプリケーションは、モデル - コントローラー - プロバイダー (MCP) パターンに従います。

  • モデル層: データベースとの直接的なやり取り
  • プロバイダー層: ビジネスロジックとデータ処理
  • コントローラー層: APIエンドポイントとリクエスト処理

セキュリティ機能

  • 読み取り専用クエリ検証
  • SQLインジェクション保護
  • レート制限
  • パラメータ化されたクエリ
  • 認証サポート
  • CORS設定

インストール

  1. リポジトリをクローンします。
    git clone <repository-url> cd mcp-postgres-server
  2. 依存関係をインストールします:
    npm install
  3. .env.templateに基づいて.envファイルを作成します。
    cp .env.template .env
  4. PostgreSQL データベースの資格情報を使用して.envファイルを更新します。
  5. サーバーを起動します。
    npm start

構成

すべての構成は環境変数を通じて管理されます。

  • サーバー: ポート、環境、CORS設定
  • データベース: 接続の詳細、プールの設定
  • セキュリティ: JWT設定、レート制限
  • クエリ: 実行制限、結果サイズ制限

APIエンドポイント

スキーマエンドポイント

  • GET /api/schemas - すべてのスキーマを一覧表示する
  • GET /api/schemas/:schema/tables - スキーマ内のテーブルを一覧表示する
  • GET /api/schemas/:schema/tables/:table - テーブルスキーマの詳細を取得する
  • GET /api/schemas/:schema/relationships - テーブルの関係を取得する
  • GET /api/structure - 完全なデータベース構造を取得する
  • GET /api/search?q=term - テーブルと列を検索

クエリエンドポイント

  • POST /api/query - SQLクエリを実行する
  • POST /api/query/explain - クエリ実行プランを取得する
  • GET /api/schemas/:schema/tables/:table/sample - サンプルデータを取得する
  • GET /api/schemas/:schema/tables/:table/stats - テーブル統計を取得する

分析プロンプトエンドポイント

  • GET /api/prompts - 分析プロンプトテンプレートの一覧を取得する
  • GET /api/prompts/:templateId - プロンプトテンプレートの詳細を取得する
  • POST /api/prompts/:templateId/generate - テンプレートからSQLを生成する
  • GET /api/schemas/:schema/tables/:table/analysis/suggest - 分析の提案を取得する

クエリの例

基本的なテーブルクエリ

// API request fetch('/api/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ sql: 'SELECT * FROM users LIMIT 10' }) }) .then(response => response.json()) .then(data => console.log(data));

分析プロンプトの使用

// Get suggested analysis for a table fetch('/api/schemas/public/tables/orders/analysis/suggest') .then(response => response.json()) .then(suggestions => { // Use a suggestion const suggestionId = suggestions.data[0].templateId; const params = suggestions.data[0].params; // Generate SQL from the template return fetch(`/api/prompts/${suggestionId}/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ params }) }); }) .then(response => response.json()) .then(data => { // Execute the generated SQL return fetch('/api/query', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ sql: data.data.sql }) }); }) .then(response => response.json()) .then(results => console.log(results));

発達

  • 開発モードで実行: npm run dev
  • テストを実行: npm test
  • リントコード: npm run lint

ライセンス

マサチューセッツ工科大学

You must be authenticated.

A
security – no known vulnerabilities
A
license - permissive license
A
quality - confirmed to work

PostgreSQL データベースに接続し、スキーマを安全に探索し、読み取り専用の SQL クエリを実行し、事前に構築されたテンプレートを使用してデータ分析を実行するためのツールを提供するサーバー。

  1. Current Implementations
    1. 1. MCP PostgreSQL Server (mcp-psql/)
    2. 2. MCP Figma Server (mcp-figma/)
  2. Purpose
    1. Getting Started
      1. Contributing
        1. License
          ID: vaztx3bu73