MCP Database Server
MCP データベースサーバー
柱: プロトコル、相互運用性 & API
ステータス: 月1 - 必須
LLMクライアントにスキーマ、安全なクエリ、ストアドプロシージャを公開するためのPython製MCPサーバー。
問題
LLMは、広範な認証情報を付与したり破壊的なSQLを実行する自由を与えずに、企業データベースを照会する必要がある。
Related MCP server: DB Insights MCP Server
MVP
FastMCPを使用したMCPサーバー
許可されたスキーマの一覧表示
読み取り専用クエリ
ストアドプロシージャの許可リスト
呼び出しの監査
アーキテクチャ
MCPサーバー(
mcp.server.MCPServer、@mcp.tool()で登録されたツール)スキーマレジストリ(テーブル/カラムの許可リスト、
db.py)読み取り専用オーソライザー(
sqlite3.Connection.set_authorizer、security.py)監査ロガー(各ツール呼び出しのインメモリトレイル、
audit.py)
図
flowchart LR
A0[MCP client] --> A1[MCP server: list_tables / describe_table / run_readonly_query]
A1 --> A2[SQLite authorizer: allowlist + read-only]
A2 --> A3[SQLite: invoices]
A1 --> A4[Audit log]スタック
Python
mcp(Model Context Protocol公式SDK)SQLite(stdlibの
sqlite3、アクセス制御にset_authorizerを使用)unittest
Docker Compose
実行方法
MCP公式SDKによる実際の実装。アクセス制御はSQLに対する正規表現(回避が容易)を使用せず、SQLite自体のset_authorizerを使用する。これは、行が実行される前に、読み取られる各テーブル/カラムと各ステートメントタイプを承認または拒否する。MCPサーバーを実行するには:
python3.12 -m venv .venv && source .venv/bin/activate
pip install -e .
python -m mcp_database_server # sobe o servidor MCP via stdio
python -m unittest discover -s testsテスト
test_security.py: オーソライザーがINSERT/UPDATE/DELETE/DROP/ALTER、ATTACH、許可リスト外のテーブル(例:sqlite_master)、およびスタックされたステートメント(SELECT 1; DROP TABLE ...)をブロックする。test_server.py: 各ツールのロジックを直接呼び出し(プロトコルなし)、すべての呼び出しが監査イベントを生成することを含む。test_integration.py:python -m mcp_database_serverを実際のサブプロセスとして起動し、mcp.client.stdio+ClientSession経由で通信する - サーバーがMCPを話すことを証明する。Python関数が動作するだけではない。
リスクと決定事項
SQLインジェクションと過剰な権限: 正規表現ではなく、ドライバーレベルで
set_authorizerを使用して解決。単一のSQLite接続はスレッドセーフではない。MCPサーバーは各ツール呼び出しをワーカースレッドにディスパッチするため、
ServerState.lockがアクセスを直列化する(実装中に発見・修正された実際のバグで、test_integration.pyでカバー)。MCPツールは、スタックトレースを漏らす代わりに、LLMクライアントに明確なメッセージ付きの
ValueErrorを返す。
次のステップ
実際のデータがある場合、SQLAlchemy経由でSQLiteをPostgreSQLに置き換える。
クライアントごとの認証/許可リストを追加する(現在は、接続されたMCPクライアントはすべてツールにアクセスできる)。
監査ログをプロセス外に永続化する(現在はインメモリのみ)。
This server cannot be deployed
Maintenance
Related MCP Connectors
Safe, read-only Postgres and MySQL access for AI agents. Audit log + column-level controls.
Query 40 databases from Claude, ChatGPT, or Cursor — on any device. Read-only, encrypted, audited.
Query your org's data in natural language — read-only MCP access to SQL, NoSQL, files & warehouses.
Read-only finance and operations controls for AI agents with evidence and safe next actions.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceProvides secure, read-only access to Microsoft SQL Server with multi-layer protection, enabling safe query execution, schema discovery, and SQL script analysis through natural language.2-
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants to query SQL databases safely with read-only access, allowing schema discovery and SELECT queries while blocking writes and DDL operations.-
- AlicenseNot gradedqualityDmaintenanceEnables LLM clients to query SQL databases via natural language with read-only, AST-validated, and capped queries, ensuring safety guarantees.2MIT
- AlicenseNot gradedqualityBmaintenanceProvides read-only access to databases for MCP-compatible AI tools, allowing schema exploration and SELECT queries without exposing credentials or risking data changes.60 npm3MIT