mcp-sqlserver
mcp-sqlserver
一个功能强大的 Model Context Protocol (MCP) 服务器,专为 Microsoft SQL Server 设计。可将 AI 助手(Claude、Gemini、Kiro、OpenAI、Copilot、Cursor)直接连接到你的 SQL Server 数据库,并具备企业级安全控制。
39 个工具,涵盖 7 大类别:架构发现、查询执行、DDL、存储过程、性能/DBA 诊断、开发人员实用工具和服务器管理。
变更日志:版本历史请查看 CHANGELOG.md,详细发布说明请查看 GitHub Releases。
v1.3 的新增内容
多服务器支持 — 在一个配置中定义 dev/staging/prod 服务器,使用
server参数切换list_servers工具 — 一眼查看所有已配置的连接每服务器安全 — 每个服务器拥有独立的安全模式、行数上限和阻止的数据库
向后兼容 — 已有的单服务器配置无需任何更改即可继续使用
Related MCP server: SQL Server MCP
v1.2 的新增内容
16 个新工具 — DBA 诊断、代码生成、ER 图、架构差异、数据采样等
SQL 注入防护 — 所有查询现在使用参数化输入和转义标识符
ISO 日期格式 — 日期显示为
2025-01-27,而不是原始 JavaScript Date 字符串可流式 HTTP 传输 — 使用
--http <port>远程托管 MCP 服务器健康检查 — 验证连接状态和服务器响应
功能
服务器管理(1 个工具)
工具 | 描述 |
| 列出所有已配置的服务器连接,包括主机、数据库、认证方式和安全模式 |
多服务器: 每个工具都接受可选的
server参数,用于指定目标命名服务器。省略时使用默认服务器。
架构发现(9 个工具)
工具 | 描述 |
| 列出实例上所有可访问的数据库 |
| 列出数据库中的 schema |
| 列出表,并显示行数和大小 |
| 列出数据库中的视图 |
| 详细列信息:类型、默认值、可空性、标识、计算列 |
| 表的外键关系 |
| 索引信息,包括包含列 |
| PK、UNIQUE、CHECK 和 DEFAULT 约束 |
| 表上的触发器定义 |
查询执行(3 个工具)
工具 | 描述 |
| 运行 SELECT 查询,自动限制行数 |
| 运行 INSERT/UPDATE/DELETE/MERGE(需要 |
| 将查询结果导出为 CSV 或 JSON 格式 |
DDL 操作(1 个工具)
工具 | 描述 |
| 运行 CREATE/ALTER/DROP 语句(需要 |
存储过程(3 个工具)
工具 | 描述 |
| 列出数据库中的存储过程 |
| 查看存储过程的参数和源代码 |
| 使用命名参数执行存储过程(需要 |
性能与 DBA(16 个工具)
工具 | 描述 |
| 查询的预估执行计划 |
| 从 |
| 行数、总/已用/未用大小以及碎片百分比 |
| 索引扫描、查找、查找和其他更新统计信息 |
| 缺失索引建议,附带可直接使用的 CREATE INDEX DDL |
| 服务器版本、语言类型、CPU 数量、内存、运行时间 |
| 数据库大小、文件布局、状态、恢复模式、对象计数 |
| 服务器等待统计信息 — 识别 CPU、I/O、锁瓶颈 |
| 从 |
| 当前阻塞链 — 哪些会话正在阻塞其他会话 |
| 长驻未提交事务(可能持有锁处) |
| 按表(数据、索引、未用)详细磁盘空间使用情况 |
| 最近备份历史:类型、大小、持续时间、备份设备路径 |
| 查询存储(SQL Server 2016+)中资源消耗最高的查询 — 可按 CPU、执行的、读取、写入或执行次数排序 |
| 重建或重组碎片索引(需要 |
| 连接健康检查,包含延迟、版本和活动会话 |
开发人员实用工具(6 个工具)
compare_schemas — Schema 对比
并排比较两个数据库。显示表、列和类型差异 — 非常适合联调开发库与生产库。
compare_schemas(source_database: "DevDB", target_database: "ProdDB")输出内容:仅存在于源/目标中的表、仅存在于源/目标中的列,以及列的类型/可空性差异。
generate_code — 代码生成
根据任意表的 schema 生成带类型的代码:
TypeScript — 使用正确类型的接口(
number、string、Date、Buffer | null)C# — 使用可空值类型的类(
int?、DateTime?、decimal?)SQL — 带完整列定义的
CREATE TABLE脚本
generate_code(table: "Products", language: "typescript")
→ export interface Products {
productId: number;
productName: string;
unitPrice: number | null;
...
}generate_insert_scripts — 将数据导出为 INSERT
根据已有表数据生成 INSERT 语句 — 非常适合迁移脚本、种子数据或备份小型参考表。
generate_insert_scripts(table: "Categories", top: 10)
→ INSERT INTO [dbo].[Categories] ([CategoryName], [Description]) VALUES (N'Beverages', N'Soft drinks...');generate_er_diagram — ER 图
根据外键关联生成 Mermaid ER 图。将输出粘贴到任何兼容 Mermaid 的渲染器中(如 GitHub、Notion、VS Code 等)即可。
generate_er_diagram(database: "Northwind")
→ erDiagram
Products }o--|| Categories : "CategoryID"
Products }o--|| Suppliers : "SupplierID"
Orders }o--|| Customers : "CustomerID"
...generate_test_data — 测试数据生成
根据列名和类型生成带模拟数据的真实 INSERT 语句。对常见模式(邮箱、电话、姓名、城市、价格等)提供智能启发逻辑。
generate_test_data(table: "Customers", count: 5)
→ INSERT INTO [dbo].[Customers] (...) VALUES (N'Alice', N'user1@example.com', N'New York', ...);sample_table — 随机采样
使用 NEWID() 从任意表中随机抽取行 — 有助于 AI 助手了解数据分布,而无需扫描整个表。
sample_table(table: "Orders", count: 5)安全
三种安全模式
模式 | SELECT | INSERT/UPDATE/DELETE | DDL | 存储过程 |
| 是 | 否 | 否 | 只读(列出/描述) |
| 是 | 是 | 否 | 完整(执行) |
| 是 | 是 | 是 | 完整(执行) |
SQL 注入防护
所有用户提供的值均作为参数化查询输入(@param)传入。对象标识符(数据库、schema、表名)使用 SQL Server 方括号语法([name],其中 ] 转义为 ]])进行转义。
其他安全功能
数据库和 schema 的允许/禁止列表
自动行数限制(可通过
maxRowCount配置)阻止的关键字检测(如 xp_cmdshell、SHUTDOWN、PRINT DATABASE 等)
列级数据掩码,用于保护 PII(个人身份信息)
根据安全模式进行查询类型验证
数据掩码
对查询结果中的敏感列进行掩码:
security:
maskColumns:
- pattern: "*.password"
mask: "***"
- pattern: "*.ssn"
mask: "XXX-XX-XXXX"
- pattern: "dbo.users.email"
mask: "***@***.***"掩码格式:[schema.]table.column(使用 * 作为通配符)
身份验证
认证方式 | Config | 要求 |
SQL Server |
|
|
Windows (NTLM) |
|
|
Windows (SSPI) |
| 无需凭据;需要 |
Azure AD |
|
|
Windows 身份验证
NTLM — 开箱即用,无需额外包:
connection:
host: YOUR_SERVER\SQLEXPRESS
authentication:
type: windows
user: YourUsername
password: YourPassword
domain: YOUR_DOMAIN
trustServerCertificate: trueSSPI / 集成安全性 — 使用当前 Windows 登录会话:
npm install msnodesqlv8connection:
host: YOUR_SERVER\SQLEXPRESS
authentication:
type: windows
trustServerCertificate: true注意: 通过
npx使用msnodesqlv8等可选依赖时,它们可能不会被自动安装。对于 SSPI,请考虑全局安装(npm install -g @tugberkgunver/mcp-sqlserver msnodesqlv8),或改用 NTLM 模式。
传输
stdio(默认)
标准输入/输出传输 — 供 Claude Desktop、VS Code、Cursor 等 MCP 客户端使用。
可流式 HTTP
用于远程托管或 Web 集成:
mcp-sqlserver --config mssql-mcp.yaml --http 3000启动后会提供:
MCP 端点:
http://localhost:3000/mcp健康检查:
http://localhost:3000/health→{"status":"ok","mode":"readonly"}
包含 CORS 支持,适用于浏览器客户端。
快速开始
安装
npm install -g @tugberkgunver/mcp-sqlserver配置
在工作目录中创建 mssql-mcp.yaml:
connection:
host: localhost
port: 1433
database: MyDatabase
authentication:
type: sql
user: sa
password: YourPassword123
trustServerCertificate: true
security:
mode: readonly
maxRowCount: 1000
blockedDatabases:
- master
- msdb
- tempdb
- model查看 config.example.yaml 获取所有选项。
多服务器配置
定义多个命名服务器,以便从单个配置中管理开发/暂存/生产环境:
defaultServer: dev
connections:
dev:
host: dev-server.example.com
database: MyDatabase
authentication:
type: sql
user: sa
password: DevPass123
trustServerCertificate: true
security:
mode: admin
maxRowCount: 5000
prod:
host: prod-server.example.com
database: MyDatabase
authentication:
type: sql
user: readonly_user
password: ProdReadOnly
security:
mode: readonly
blockedDatabases: [master, msdb, tempdb, model]
# Global security defaults (applied to all servers unless overridden)
security:
maxRowCount: 1000
blockedKeywords: [xp_cmdshell, SHUTDOWN, DROP DATABASE]然后在任意工具调用中使用 server 参数:
list_tables(server: "prod", database: "MyDatabase")
health_check(server: "dev")
compare_schemas(source_database: "DevDB", target_database: "StagingDB", server: "dev")MCP 客户端配置
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}使用配置文件:
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver", "--config", "/path/to/mssql-mcp.yaml"]
}
}
}添加到 .vscode/mcp.json:
{
"servers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}添加到 ~/.cursor/mcp.json:
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}添加到 .kiro/settings/mcp.json:
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}添加到 ~/.gemini/settings.json:
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}添加到 ~/.windsurf/mcp.json:
{
"mcpServers": {
"mssql": {
"command": "npx",
"args": ["-y", "@tugberkgunver/mcp-sqlserver"],
"env": {
"MSSQL_HOST": "localhost",
"MSSQL_DATABASE": "MyDatabase",
"MSSQL_USER": "sa",
"MSSQL_PASSWORD": "YourPassword123"
}
}
}
}在 Windows 上,使用 cmd 作为命令包装器:
{
"mcpServers": {
"mssql": {
"command": "cmd",
"args": ["/c", "npx", "-y", "@tugberkgunver/mcp-sqlserver", "--config", "path/to/config.yaml"]
}
}
}环境变量
变量 | 描述 |
| SQL Server 主机名 |
| SQL Server 端口(默认:1433) |
| 默认数据库 |
| SQL 身份验证用户名 |
| SQL 身份验证密码 |
| YAML 配置文件路径 |
环境变量会覆盖配置文件中的值。
开发
git clone https://github.com/gunvertugberk/mcp-sqlserver.git
cd mcp-sqlserver
npm install
npm run build
npm start -- --config ./mssql-mcp.yaml许可证
MIT
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseAqualityDmaintenanceEnables AI agents to securely connect to and query Microsoft SQL Server databases with read-only access, schema discovery, and relationship mapping. Features advanced security protections, health monitoring, and bulk operations for production environments.975MIT
- AlicenseAqualityCmaintenanceEnables AI assistants to interact with Microsoft SQL Server databases through query execution, schema discovery, CRUD operations, stored procedures, and data export with built-in safety controls.18Apache 2.0
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to securely interact with Microsoft SQL Server databases to query data, inspect schemas, and retrieve metadata with read-only operations by default and optional write capabilities.1MIT
- AlicenseNot gradedqualityDmaintenanceEnables AI assistants to interact with Microsoft SQL Server databases through a standardized interface. Supports executing SQL queries, browsing database schemas, and viewing table data with flexible authentication options for both local and Azure SQL databases.5MIT
Related MCP Connectors
Explore, query, and inspect SQLite databases with ease. List tables, preview results, and view det…
Connect your AI assistants to Keboola and expose your data, transformations, SQL queries, ...
Connect AI assistants to GitHub - manage repos, issues, PRs, and workflows through natural language.
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/Debanjan29/readonly-mssql-mcp-db'
If you have feedback or need assistance with the MCP directory API, please join our Discord server