MCP Microsoft SQL Server
可配置 模型上下文协议(MCP)服务器 用于Microsoft SQL Server与Claude Code和其他MCP客户端的集成。使AI助手能够通过具有完整读/写功能的基于项目的配置安全地与SQL Server数据库交互。
🌟 特性
🔧 基于项目的配置
- 多个数据库连接 -在不同的项目和数据库之间切换
- 特定于架构的访问 -限制对每个项目特定架构的访问
- 可配置权限 -对读/写/删除操作进行细粒度控制
- 基于环境的设置 -开发、暂存和生产的不同配置
🛡️ 安全与安保
- 交易管理 -写入操作出错时自动回滚
- 查询验证 -防止SQL注入并验证所有操作
- WHERE条款执行 -UPDATE/DELETE操作的强制WHERE子句
- 行限制 -可配置的限制,以防止意外的大规模操作
- 审核日志记录 -跟踪所有数据库操作以实现问责制
🔍 数据库操作
- 读取操作:带分页和筛选的SELECT查询
- 写入操作:具有事务安全性的INSERT、UPDATE、DELETE
- 模式探索:浏览表、列、关系和索引
- 表格管理:获取元数据、统计数据和示例数据
- 配置管理:在项目配置之间动态切换
🚀 人工智能集成
- Claude桌面集成 -与Claude Desktop应用程序无缝设置
- MCP协议合规性 -适用于任何MCP兼容客户端
- 自然语言接口 -使用简明英语与数据库交互
- 错误处理 -为人工智能和人类提供清晰、可操作的错误信息
📋 先决条件
- .NET 9.0或更高版本
- Microsoft SQL Server (任何支持的版本)
- 克劳德桌面 (用于克劳德整合)
- 适当的数据库权限 对于您要执行的操作
🚀 快速开始
1.安装
# Clone the repository
git clone https://github.com/yourusername/mcp-ms-sql-server.git
cd mcp-ms-sql-server
# Build the project
dotnet build -c Release2.创建项目配置
在中为您的项目创建配置文件 Configurations/ 目录:
// Configurations/my-project.json
{
"name": "My E-Commerce Project",
"connectionString": "Server=localhost;Database=ECommerceDB;Integrated Security=true;",
"allowedSchema": "dbo",
"permissions": {
"allowRead": true,
"allowWrite": true,
"allowDelete": false
},
"security": {
"requireWhereClause": true,
"maxRowsPerQuery": 1000,
"auditOperations": true
}
}3.配置克劳德桌面
添加到您的Claude Desktop配置中:
视窗: %APPDATA%\Claude\claude_desktop_config.json macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
{
"mcpServers": {
"sql-server": {
"type": "stdio",
"command": "C:\\Git\\mcp-ms-sql-server\\McpMsSqlServer\\bin\\Release\\net9.0\\McpMsSqlServer.exe",
"env": {
"MCP_CONFIG_NAME": "my-project"
}
}
}
}备注:首先构建项目 dotnet build -c Release 创建可执行文件。
4.开始使用
在Claude Desktop中,您现在可以问:
- “显示数据库中的所有表”
- “插入一个名为'John Doe'并发送电子邮件的新客户”john@example.com'"
- “销售额排名前十的产品是什么?”
- “将ID为123的产品价格更新为29.99美元”
📖 配置指南
项目配置结构
{
"name": "Project Display Name",
"connectionString": "Your SQL Server connection string",
"allowedSchema": "schema_name",
"permissions": {
"allowRead": true,
"allowWrite": true,
"allowDelete": false,
"allowSchemaChanges": false
},
"security": {
"requireWhereClause": true,
"maxRowsPerQuery": 1000,
"maxRowsPerUpdate": 100,
"maxRowsPerDelete": 10,
"auditOperations": true
},
"querySettings": {
"timeoutSeconds": 30,
"enableQueryPlan": false,
"allowJoins": true,
"allowSubqueries": true
},
"restrictedTables": ["sensitive_table", "audit_log"],
"allowedOperations": ["SELECT", "INSERT", "UPDATE", "DELETE"]
}示例配置
Development Environment
{
"name": "Development Database",
"connectionString": "Server=dev-server;Database=DevDB;Integrated Security=true;",
"allowedSchema": "dbo",
"permissions": {
"allowRead": true,
"allowWrite": true,
"allowDelete": true,
"allowSchemaChanges": true
},
"security": {
"requireWhereClause": false,
"maxRowsPerQuery": 5000,
"auditOperations": false
}
}Production Environment
{
"name": "Production Database",
"connectionString": "Server=prod-server;Database=ProdDB;User Id=app_user;Password=secure_password;",
"allowedSchema": "app",
"permissions": {
"allowRead": true,
"allowWrite": true,
"allowDelete": false,
"allowSchemaChanges": false
},
"security": {
"requireWhereClause": true,
"maxRowsPerQuery": 100,
"maxRowsPerUpdate": 10,
"auditOperations": true
},
"restrictedTables": ["user_passwords", "payment_info"]
}🛠️ 可用工具
配置管理(4个工具)
- 列表配置 -显示所有可用的项目配置
- 交换机配置 -切换到其他项目配置
- GetCurrentConfiguration -查看当前配置详细信息
- 测试连接 -测试数据库连接
核心数据库操作(6个工具)
- 执行查询 -在允许的架构内运行SELECT查询
- GetChemainfo -探索数据库结构和对象
- GetTableInfo -获取表元数据和示例数据
- 插入记录 -插入具有交易支持的新记录
- 更新记录 -更新现有记录(需要WHERE子句)
- 删除记录 -删除记录(需要WHERE子句)
高级功能(6个工具)
- BuildQuery -从自然语言生成SQL查询
- 分析查询性能 -分析查询执行计划
- GetDatabase性能统计信息 -数据库性能指标
- 发现数据 -按模式搜索表/列
- 分析表关系 -查找表关系
- 简介数据质量 -分析数据质量和统计数据
🔒 安全考虑
最佳实践
- 使用专用数据库用户 具有最低限度的所需权限
- 启用审核日志记录 适用于生产环境
- 设置适当的行限制 防止意外的大规模行动
- 限制敏感表 使用
restrictedTables配置 - 使用WHERE子句要求 用于更新/删除操作
- 定期安全审查 配置和权限
连接字符串安全
# Use environment variables for sensitive data
export DB_PASSWORD="your_secure_password"{
"connectionString": "Server=myserver;Database=mydb;User Id=myuser;Password=${DB_PASSWORD};"
}🤝 贡献
我们欢迎捐款!请查看我们的 贡献指南 了解详情。
开发设置
# Clone the repo
git clone https://github.com/yourusername/mcp-ms-sql-server.git
cd mcp-ms-sql-server
# Install dependencies
dotnet restore
# Run tests
dotnet test
# Build and test
dotnet build -c Release📚 文档
🐛 故障排除
常见问题
连接失败
# Test your connection string
dotnet run -- --test-connection --config your-project权限不足
- 检查您的数据库用户权限
- 验证
allowedSchema配置 - 确保用户有权访问指定的架构
未找到配置
- 验证配置文件是否存在于
Configurations/ - 检查
MCP_CONFIG_NAME环境变量 - 确保JSON语法有效
📄 许可证
此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。
🌟 致谢
📞 支持
- 问题:
- 讨论:
- 文档: 维基
______________________________________________________________________
由以下材料制成❤️ 面向MCP和AI开发社区
