Token导航 LogoToken导航TokenDH.com
Mssql MCP Python logo
AI代理stdio官方级别未说明来源级核验

Mssql MCP Python

MCP Server

一个Python实现的MCP服务器,安全地将SQL Server数据库功能暴露给LLM客户端。

工具数

0

提示词数

0

GitHub Stars

27

资源数

0
数据库连接数据安全Python模型集成

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

lorenzouriel

提供方

lorenzouriel

最后核验

2026/5/17 20:20

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

pip install -r requirements.txt

详细介绍

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 sync

2.配置数据库

创建 .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:latest

4.使用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 JSON

2. list_schemas()

列出所有数据库架构

Input: (none)
Output: Schema names list

3. list_tables(schema, limit=200)

列出具有可选架构筛选器的表

Input: schema="dbo", limit=100
Output: Table list with metadata

4. schema_discovery(schema)

获取完整的架构元数据(表、列、类型)

Input: schema="dbo"
Output: JSON with detailed column info

5. get_database_info()

获取服务器/数据库元数据

Input: (none)
Output: Database name, version, machine name

6. 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

目录标签

目录标签

数据库连接数据安全Python模型集成本地部署SQLServerMCP协议LLM集成

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP