Database MCP Server

データベースMCPサーバー

さまざまなデータベース システムに接続して対話するためのツールを提供するモデル コンテキスト プロトコル (MCP) サーバー。

特徴

  • マルチデータベースのサポート: SQLite、PostgreSQL、MySQL/MariaDB、SQL Server データベースに接続します
  • 統合インターフェース: サポートされているすべてのデータベース タイプでデータベース操作を行うための共通ツール
  • データベース固有の拡張機能: 必要に応じて、データベース固有の機能のための特定のツール
  • スキーマ管理: テーブルとインデックスの作成、変更、削除
  • クエリ実行: 生のSQLクエリを実行するか、構造化クエリツールを使用する
  • トランザクションサポート: トランザクションの開始、コミット、ロールバック

インストール

前提条件

  • Python 3.8以上
  • 必要な Python パッケージ (pip で自動的にインストールされます):
    • SQLアルケミー
    • 使用するデータベースに応じて、さまざまなデータベース ドライバーがあります。
      • SQLite (Python に含まれています)
      • PostgreSQL: psycopg2-binary
      • MySQL/MariaDB: mysql-connector-python
      • SQL サーバー: pyodbc

ソースからのインストール

# Clone the repository git clone <repository-url> # Install the package pip install -e .

構成

サーバーは、環境変数、構成ファイル、または実行時に接続の詳細を提供することで構成できます。

環境変数

  • DB_CONFIG_PATH : JSON設定ファイルへのパス
  • DB_CONNECTIONS : 接続IDのカンマ区切りリスト、または接続の詳細を含むJSON文字列

設定ファイルの形式

{ "connections": { "sqlite_conn": { "type": "sqlite", "db_path": "/path/to/database.db" }, "postgres_conn": { "type": "postgres", "host": "localhost", "port": 5432, "database": "mydatabase", "user": "myuser", "password": "mypassword" } } }

使用法

サーバーの実行

クロードのMCPサーバーとして

# Run with default settings python -m db_mcp_server # Specify a configuration file python -m db_mcp_server --config /path/to/config.json # Set logging level python -m db_mcp_server --log-level DEBUG

スタンドアロン Web サーバーとして (任意の LLM 用)

# Run as a web server python -m db_mcp_server.web_server # Specify host and port python -m db_mcp_server.web_server --host 0.0.0.0 --port 8000 # Specify configuration file and logging level python -m db_mcp_server.web_server --config /path/to/config.json --log-level DEBUG

利用可能なMCPツール

接続管理

  • add_connection : 新しいデータベース接続を追加する
  • test_connection : データベース接続をテストする
  • list_connections : すべてのデータベース接続を一覧表示する
  • remove_connection : データベース接続を削除する

クエリ実行

  • execute_query : SQLクエリを実行する
  • get_records : テーブルからレコードを取得する
  • insert_record : テーブルにレコードを挿入する
  • update_record : テーブル内のレコードを更新する
  • delete_record : テーブルからレコードを削除する

スキーマ管理

  • list_tables : データベース内のすべてのテーブルを一覧表示する
  • get_table_schema : テーブルのスキーマを取得する
  • create_table : 新しいテーブルを作成する
  • drop_table : テーブルを削除する
  • create_index : テーブルにインデックスを作成する
  • drop_index : インデックスを削除する
  • alter_table : テーブル構造を変更する

トランザクション管理

  • begin_transaction : トランザクションを開始する
  • commit_transaction : トランザクションをコミットする
  • rollback_transaction : トランザクションをロールバックする

接続を追加する

{ "connection_id": "my_sqlite_db", "type": "sqlite", "db_path": "/path/to/database.db" }

クエリを実行する

{ "connection_id": "my_sqlite_db", "query": "SELECT * FROM users WHERE age > ?", "params": [21] }

テーブルを作成する

{ "connection_id": "my_sqlite_db", "table": "users", "columns": [ { "name": "id", "type": "INTEGER", "primary_key": true, "nullable": false }, { "name": "name", "type": "TEXT", "nullable": false }, { "name": "email", "type": "TEXT", "nullable": true } ] }

レコードを挿入

{ "connection_id": "my_sqlite_db", "table": "users", "data": { "name": "John Doe", "email": "john@example.com" } }

発達

テストの実行

# Run all tests python -m unittest discover # Run specific test file python -m unittest tests.test_sqlite

他のLLMからの接続

スタンドアロンのウェブサーバーとして実行する場合、他のLLM(Llama 3など)はHTTP経由でデータベースMCPサーバーに接続できます。サーバーは以下のエンドポイントを公開します。

エンドポイント

  • /list_tools - GET または POST: 利用可能なすべてのツールとその説明および入力スキーマのリストを返します。
  • /call_tool - POST: 特定のデータベースツールを実行する

例: 別の LLM からの呼び出し

このサーバーを他のLLMと併用するには、LLMにサーバーへのHTTPリクエストを生成させます。Llama 3のようなLLMのプロンプトの構造例を以下に示します。

You can interact with a database by making HTTP requests to a database service at http://localhost:8000. The service provides the following endpoints: 1. To get a list of available tools: Make a POST request to: http://localhost:8000/list_tools 2. To execute a database tool: Make a POST request to: http://localhost:8000/call_tool with a JSON body like: { "name": "tool_name", "arguments": { "param1": "value1", "param2": "value2" } } For example, to execute a SQL query, you would make a request like: POST http://localhost:8000/call_tool Content-Type: application/json { "name": "execute_query", "arguments": { "connection_id": "my_db", "query": "SELECT * FROM users" } }

クライアント統合用のサンプル Python コード

import requests import json # Base URL of the database MCP server BASE_URL = "http://localhost:8000" # List available tools def list_tools(): response = requests.post(f"{BASE_URL}/list_tools") return response.json() # Execute a database tool def call_tool(tool_name, arguments): payload = { "name": tool_name, "arguments": arguments } response = requests.post(f"{BASE_URL}/call_tool", json=payload) return response.json() # Example: List tables in a database def list_tables(connection_id): return call_tool("list_tables", {"connection_id": connection_id}) # Example: Execute a SQL query def execute_query(connection_id, query, params=None): return call_tool("execute_query", { "connection_id": connection_id, "query": query, "params": params }) # Example: Add a new connection def add_connection(connection_id, db_type, **kwargs): args = {"connection_id": connection_id, "type": db_type} args.update(kwargs) return call_tool("add_connection", args)

ライセンス

MITライセンス

-
security - not tested
-
license - not tested
-
quality - not tested

統一されたインターフェイスを通じてさまざまなデータベース システム (SQLite、PostgreSQL、MySQL/MariaDB、SQL Server) に接続し、対話するためのツールを提供するモデル コンテキスト プロトコル サーバー。

  1. Features
    1. Installation
      1. Prerequisites
      2. Installing from Source
    2. Configuration
      1. Environment Variables
      2. Configuration File Format
    3. Usage
      1. Running the Server
      2. Available MCP Tools
    4. Examples
      1. Add a Connection
      2. Execute a Query
      3. Create a Table
      4. Insert Records
    5. Development
      1. Running Tests
    6. Connecting from Other LLMs
      1. Endpoints
      2. Example: Calling from Another LLM
      3. Sample Python Code for Client Integration
    7. License
      ID: 6602yuum3i