MCP Database Server
MCP Database Server
분야: 프로토콜, 상호운용성 및 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 (표준 라이브러리의
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