SQL Server MCP🗄️
全面 模型上下文协议(MCP) 用于SQL server数据库操作的服务器。该服务器通过Claude Desktop或任何兼容MCP的客户端提供30多种数据库管理、查询、分析和管理工具。
](https://github.com/mohammadrehan1992/mssql-server-mcp/stargazers) ](https://github.com/mohammadrehan1992/mssql-server-mcp/network)  ](https://nodejs.org/)
🚀 特性
🔍 数据库发现和模式分析
- 列出数据库、表、视图、存储过程和函数
- 详细的表结构分析,包括列、数据类型、约束
- 索引和外键关系映射
- 完整的模式探索工具
📊 数据操作和查询
- 执行任何具有参数绑定的SQL查询
- 具有过滤、排序和分页功能的表数据检索
- 带条件WHERE子句的行计数
- 使用参数执行存储过程
⚡ 性能与监控
- 查询执行计划分析
- 数据库性能统计
- 表大小分析和存储度量
- 主动连接监控
- 查询性能见解
🔧 数据库管理
- 数据库备份创建(完整、差异、日志)
- 数据库完整性检查(DBCC CHECKDB)
- 数据库文件信息(.mdf、.ldf)
- 一般数据库信息和元数据
👥 安全和用户管理
- 列出数据库用户和角色
- 用户权限分析
- 安全主体信息
🔄 SQL Server代理和作业
- SQL Server代理作业列表
- 作业执行历史和状态
- 自动化任务监控
📦 安装
方法1:从GitHub安装(推荐)
# Install globally from GitHub
npm install -g https://github.com/mohammadrehan1992/mssql-server-mcp.git方法2:克隆并在本地安装
# Clone the repository
git clone https://github.com/mohammadrehan1992/mssql-server-mcp.git
# Navigate to the directory
cd mssql-server-mcp
# Install dependencies
npm install
# Install globally
npm install -g .方法3:下载并安装
- 从下载ZIP文件
- 提取文件
- 在提取的文件夹中打开终端
- 运行安装命令:
npm install
npm install -g .验证安装
安装后,验证其是否正常工作:
# Check if the command is available
sql-server-mcp --help
# Or check the installation path
which sql-server-mcp # On macOS/Linux
where sql-server-mcp # On Windows🔧 配置
Claude桌面设置
添加到您的Claude Desktop配置文件中:
窗户: %APPDATA%\Claude\claude_desktop_config.json\ macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"sqlserver": {
"command": "sql-server-mcp",
"env": {
"SQL_CONNECTION_STRING": "Server=localhost;Database=YourDatabase;Integrated Security=true;TrustServerCertificate=true;"
}
}
}
}注: 如果您是在本地而不是全局安装的,请使用服务器文件的完整路径: ``json { "mcpServers": { "sqlserver": { "command": "node", "args": ["/full/path/to/mssql-server-mcp/server.js"], "env": { "SQL_CONNECTION_STRING": "your-connection-string-here" } } } } ``连接字符串示例
Windows身份验证(推荐)
Server=localhost;Database=YourDatabase;Integrated Security=true;TrustServerCertificate=true;SQL Server Express
Server=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=true;TrustServerCertificate=true;本地数据库
Server=(localdb)\\MSSQLLocalDB;Database=YourDatabase;Integrated Security=true;TrustServerCertificate=true;使用用户名和密码
Server=localhost;Database=YourDatabase;User Id=username;Password=password;TrustServerCertificate=true;Azure SQL数据库
Server=your-server.database.windows.net;Database=YourDatabase;User Id=username;Password=password;Encrypt=true;带端口的命名实例
Server=localhost\\INSTANCE_NAME,1433;Database=YourDatabase;Integrated Security=true;TrustServerCertificate=true;🛠️ 可用工具
📋 Basic Operations (2 tools)
execute_query-使用可选参数执行任何SQL查询execute_stored_procedure-使用参数执行存储过程
🗂️ Schema Discovery (6 tools)
list_databases-列出SQL Server实例上的所有数据库list_tables-列出具有可选架构筛选的表list_views-列出具有可选架构筛选的数据库视图list_stored_procedures-列出具有可选架构筛选的存储过程list_functions-列出具有可选模式筛选的用户定义函数list_schemas-列出当前数据库中的所有架构
🔍 Table Structure Analysis (4 tools)
describe_table-获取详细的表结构(列、数据类型、约束)get_table_indexes-获取特定表的所有索引get_table_constraints-获取所有约束(PK、FK、CHECK等)get_foreign_keys-获取外键关系
📊 Data Operations (2 tools)
get_table_data-通过过滤、排序和分页检索表数据get_table_count-使用可选的WHERE条件获取行数
📈 Performance & Statistics (6 tools)
get_database_info-一般数据库信息和元数据get_table_sizes-表大小分析和存储度量get_active_connections-有关活动数据库连接的信息get_database_files-数据库文件信息(.mdf、.ldf)analyze_query_plan-获取查询的执行计划get_query_statistics-最近查询的性能统计
🔧 Backup & Maintenance (2 tools)
backup_database-创建数据库备份(完整、差异、日志)check_database_integrity-运行DBCC CHECKDB进行完整性验证
👥 Security & Users (3 tools)
list_users-列出所有数据库用户list_roles-列出所有数据库角色get_user_permissions-获取特定用户的权限
🔄 SQL Agent & Jobs (2 tools)
list_sql_agent_jobs-列出SQL Server代理作业get_job_history-获取作业执行历史记录
🧪 测试您的连接
创建一个测试脚本来验证您的连接是否正常:
// test-connection.js
const sql = require('mssql');
async function testConnection() {
const connectionString = 'Your-Connection-String-Here';
try {
const pool = await sql.connect(connectionString);
const result = await pool.request().query('SELECT @@VERSION as Version');
console.log('✅ Connection successful!');
console.log('SQL Server Version:', result.recordset[0].Version);
await pool.close();
} catch (error) {
console.error('❌ Connection failed:', error.message);
}
}
testConnection();运行方式:
node test-connection.js🚨 故障排除
常见连接问题
- “无法连接到本地主机:1433”
- SQL Server服务可能未运行 - TCP/IP协议可能已禁用 - 尝试 .\SQLEXPRESS 而不是 localhost
- “用户登录失败”
- 检查SQL身份验证的用户名/密码 - 验证是否启用了Windows身份验证 - 确保用户具有数据库访问权限
- “找不到服务器”
- 验证服务器名称和实例 - 检查SQL Server Browser服务是否正在运行 - 尝试使用显式端口: localhost,1433
启用TCP/IP协议
- 打开 配置管理器
- 首选 SQL Server网络配置 → MSSQLSERVER协议
- 启用 传输控制协议/网际协议
- 重新启动SQL Server服务
查找SQL Server实例
# List SQL Server services
sc query | findstr SQL
# List network instances
sqlcmd -L💡 使用示例
基本查询执行
Ask Claude: "Execute this query: SELECT TOP 10 * FROM Users WHERE Active = 1"模式探索
Ask Claude: "Show me all tables in the database and describe the Users table structure"性能分析
Ask Claude: "What are the largest tables in the database and show me recent query performance statistics"数据分析
Ask Claude: "Get the first 100 rows from the Orders table where OrderDate is from last month"数据库管理
Ask Claude: "Check database integrity and show me information about database files"🔒 安全考虑
- 使用 身份验证 尽可能提高安全性
- 将数据库用户权限限制为所需的最小访问权限
- 使用 信任服务器证书=true 仅用于当地发展
- 对于生产,配置正确的SSL证书
- 定期审查用户权限和访问日志
🤝 贡献
我们欢迎捐款!以下是您可以提供帮助的方式:
- 分叉存储库 在GitHub上
- 克隆你的叉子 本地:
git clone https://github.com/your-username/mssql-server-mcp.git- 创建要素分支:
git checkout -b feature/amazing-feature- 进行更改 并对其进行测试
- 提交您的更改:
git commit -m 'Add amazing feature'- 推你的叉子:
git push origin feature/amazing-feature- 打开拉取请求 在GitHub上
开发设置
# Clone the repo
git clone https://github.com/mohammadrehan1992/mssql-server-mcp.git
cd mssql-server-mcp
# Install dependencies
npm install
# Test your changes
node server.js报告问题
发现错误或有功能请求?请 打开一个问题 与:
- 问题/特征的清晰描述
- 复制步骤(针对bug)
- 您的环境详细信息(操作系统、Node.js版本、SQL Server版本)
- 预期行为与实际行为
📋 需求
- Node.js 16.0.0或更高版本
- SQL Server 2012年或更高版本(包括Express、LocalDB、Azure SQL)
- 视窗 (用于Windows身份验证)或SQL Server身份验证
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🙋♂️ 支持与社区
- 🐛 报告问题:
- 💬 讨论:
- 📚 文档:
- ⭐ 给我们一颗星: 如果这个项目对你有帮助,请考虑 主演它!
获取帮助
🌟 表示支持
如果这个项目对你有帮助,请考虑:
- ⭐ 对存储库进行标记
- 🍴 叉它 贡献
- 📢 分享它 与其他可能觉得有用的人一起
- 🐛 报告错误 以帮助改善它
📊 项目统计
______________________________________________________________________
由以下材料制成❤️ SQL Server社区
*由开发 穆罕默德·雷恩*
