MSSQL MCP Python服务器
](https://github.com/lorenzouriel/mssql-mcp-python/stargazers) ](https://github.com/lorenzouriel/mssql-mcp-python/network/members) ](https://github.com/lorenzouriel/mssql-mcp-python/issues) ](https://www.python.org/downloads/) ](https://github.com/lorenzouriel/mssql-mcp-python/releases) ](https://github.com/lorenzouriel/mssql-mcp-python/releases)
这是Python中的MCP(模型上下文协议)服务器实现,它将SQL server数据库功能安全地暴露给LLM客户端。
- 如果你想要一份完整的使用指南, 点击此处!
快速开始
1.安装依赖项
cd mssql-mcp-python
pip install -r requirements.txt
# or:
uv sync2.配置数据库
创建 .env 文件:
# For local SQL Server (Linux/Docker)
export MSSQL_CONNECTION_STRING="Driver={ODBC Driver 17 for SQL Server};Server=localhost,1433;Database=master;UID=sa;PWD=YourPassword123"
# Or for Windows Auth
export MSSQL_CONNECTION_STRING="Driver={ODBC Driver 17 for SQL Server};Server=localhost;Database=master;Trusted_Connection=yes"3.运行服务器
# With stdio transport (for MCP clients)
python -m mssql_mcp.cli
# With custom settings
MSSQL_QUERY_TIMEOUT=60 READ_ONLY=true python -m mssql_mcp.cli --log-level DEBUG
# Or with HTTP transport
python -m mssql_mcp.cli --transport http --bind 0.0.0.0:8080
# Build and run
docker build -t mssql-mcp:latest .
docker run -e MSSQL_CONNECTION_STRING="..." mssql-mcp:latest4.使用curl进行测试(HTTP模式)
# Health check
curl http://localhost:8080/health
# Readiness check
curl http://localhost:8080/ready
# Server info
curl http://localhost:8080/info
# Prometheus metrics
curl http://localhost:8080/metrics可用的MCP工具
服务器向MCP客户端公开这些工具:
1. execute_sql(sql, format="table")
执行SELECT查询(如果启用,则执行写入操作)
Input: "SELECT * FROM users LIMIT 10"
Output: ASCII table or JSON2. list_schemas()
列出所有数据库架构
Input: (none)
Output: Schema names list3. list_tables(schema, limit=200)
列出具有可选架构筛选器的表
Input: schema="dbo", limit=100
Output: Table list with metadata4. schema_discovery(schema)
获取完整的架构元数据(表、列、类型)
Input: schema="dbo"
Output: JSON with detailed column info5. get_database_info()
获取服务器/数据库元数据
Input: (none)
Output: Database name, version, machine name6. get_policy_info()
获取当前安全策略设置
Input: (none)
Output: Policy details (allowed operations, limits)7. check_db_connection()
数据库连接的健康检查
Input: (none)
Output: Connection status安全功能
✅ 默认情况下为只读
- 除非明确启用,否则只允许SELECT查询
- 写入要求
ENABLE_WRITES=true+ADMIN_CONFIRM令牌
✅ SQL注入防护
- 通过pyodbc进行参数化查询
- 多语句查询阻塞
- 禁止关键字检测(DROP、ALTER、EXEC等)
✅ 敏感数据保护
- 自动日志编辑(密码、连接字符串)
- 用于安全日志记录的查询哈希
- 响应体中没有凭据
✅ 资源限制
- 查询超时(默认30秒)
- 行限制(默认50000行)
- 查询长度限制(50KB)
- 连接池限制
✅ 审计跟踪
- 带有请求元数据的结构化日志记录
- 查询指标和统计数据
- 客户端ID跟踪(如果提供)
可观测性
普罗米修斯指标
可在 GET /metrics (HTTP模式):
mssql_queries_executed_total--按工具和状态列出的查询总数mssql_queries_blocked_total--按原因阻止查询mssql_query_duration_seconds--查询延迟直方图mssql_query_rows_returned--结果集大小直方图mssql_active_queries--当前正在执行查询mssql_server_ready--服务器就绪(0/1)
结构化日志
所有日志均为JSON格式(当 LOG_FORMAT=json):
{
"timestamp": "2024-01-15T10:30:00.123456",
"level": "INFO",
"logger": "mssql_mcp.tools",
"message": "Query allowed",
"module": "tools",
"function": "execute_sql",
"line": 42
}健康检查
GET /health--活体探针(始终为200)GET /ready--准备就绪探针(如果DB已连接,则为200)
常见任务
更改日志级别
LOG_LEVEL=DEBUG python -m mssql_mcp.cli启用写入操作
ENABLE_WRITES=true ADMIN_CONFIRM=secret python -m mssql_mcp.cli增加查询超时
MSSQL_QUERY_TIMEOUT=120 python -m mssql_mcp.cli运行多个实例
python -m mssql_mcp.cli --transport http --bind 127.0.0.1:8080
python -m mssql_mcp.cli --transport http --bind 127.0.0.1:8081 # Different port