使用说明
MCP SQL Server
提供对SQL的全面访问的模型上下文协议服务器 数据库,从MSSQL支持开始。该服务器使AI助手能够 检查数据库模式、执行查询和执行数据库操作 企业级安全和性能监控。
当前状态:MSSQL完全支持。 MySQL和PostgreSQL支持即将到来 在未来的版本中。
✨ 主要特点
- 数据库支持:完全支持MSSQL(MySQL和PostgreSQL即将推出)
- 架构检查:全面的数据库结构分析和元数据 提取
- 查询执行:使用参数化查询和结果安全执行SQL 格式化
- 性能监控:实时指标、查询统计和 绩效报告
- 安全第一:SQL注入防御、连接加密和 访问控制
- 批量操作:高效的批量查询执行和事务处理 管理
- 连接池:通过可配置池优化连接管理 设置
- MCP集成:原生支持Claude Desktop、Cursor和其他 MCP兼容工具
📦 安装和设置
先决条件
- Node.js 18.0.0+ - 点击此处下载
- 数据库服务器:Microsoft SQL Server(MSSQL)
- AI助手:Claude Desktop、Cursor或任何与MCP兼容的工具
备注:目前只支持MSSQL。MySQL和PostgreSQL支持 在未来的版本中添加。
快速安装
# Install globally via NPM
npm install -g @donggyunryu/mcp-sql
基本配置
参见
env.example
创建一个 .env 使用数据库设置的文件:
# Create .env file with your database settings
# DB_TYPE=mssql # Current only MSSQL supported
DB_HOST=localhost
DB_PORT=1433
DB_DATABASE=your_database
DB_USER=your_username
DB_PASSWORD=your_password
DB_ENCRYPT=false
DB_TRUST_SERVER_CERTIFICATE=true
测试连接
# Test your database connection
npm run test:db # in local environment
#or
npx @donggyunryu/mcp-sql test:db
🚀 快速开始
当地环境
-
克隆存储库:
git clone https://github.com/ryudg/mcp-sql.git cd mcp-sql -
安装依赖项
npm install -
构建项目
npm run build -
添加到您的
mcp.json或setting.json,claude_desktop_config.json{ "mcpServers": { "mcp-sql": { "command": "node", "args": ["/full/path/to/mcp-mysql/build/index.js"], "env": { // "DB_TYPE": "mssql", # Current only MSSQL supported "DB_SERVER": "localhost", "DB_PORT": "1433", "DB_DATABASE": "your_database", "DB_USER": "your_username", "DB_PASSWORD": "your_password", "DB_ENCRYPT": "false", "DB_TRUST_SERVER_CERTIFICATE": "true", "NODE_ENV": "development", "LOG_LEVEL": "info" } } } }
配置AI助手
对于Claude Desktop:
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"mcp-sql": {
"command": "npx",
"args": ["-y", "@donggyunryu/mcp-sql"],
"env": {
// "DB_TYPE": "mssql", # Current only MSSQL supported
"DB_HOST": "localhost",
"DB_PORT": "1433",
"DB_DATABASE": "your_database",
"DB_USER": "your_username",
"DB_PASSWORD": "your_password",
"DB_ENCRYPT": "false",
"DB_TRUST_SERVER_CERTIFICATE": "true"
}
}
}
}
对于光标(VSCode、Windsurf等IDE):
添加到MCP设置中:
{
"mcpServers": {
"mcp-sql": {
"command": "npx",
"args": ["-y", "@donggyunryu/mcp-sql"],
"env": {
// "DB_TYPE": "mssql", # Current only MSSQL supported
"DB_HOST": "localhost",
"DB_PORT": "1433",
"DB_DATABASE": "your_database",
"DB_USER": "your_username",
"DB_PASSWORD": "your_password",
"DB_ENCRYPT": "false",
"DB_TRUST_SERVER_CERTIFICATE": "true"
}
}
}
}
开始使用
重新启动AI助手并尝试:
"Show me all tables in my database"
"What's the structure of the users table?"
"Find all active users from the last 30 days"
💬 使用示例
模式探索
"Show me all tables in the database"
"What's the structure of the users table?"
"List all columns in the orders table with their data types"
数据查询
"Find all active users registered in the last 30 days"
"Show me the top 10 customers by total order value"
"Get all pending orders from this month"
性能监控
"Show me current connection pool status"
"What are the slowest queries in the last hour?"
"Generate a performance report for today"
🛠️ 可用工具
MCP SQL server为数据库交互提供了以下工具:
架构和发现工具
| 工具 | 说明 | 参数 |
|---|---|---|
list_tables |
列出数据库中的所有表 | pattern (可选):过滤模式 |
describe_table |
获取详细的表格结构 | table_name:要描述的表的名称 |
get_schema |
获取完整的数据库架构 | include_system_tables:包括系统表 |
get_schema_statistics |
获取架构统计信息 | 无 |
查询执行工具
| 工具 | 说明 | 参数 |
|---|---|---|
execute_query |
安全执行SQL查询 | query:SQL语句parameters:绑定参数 |
start_batch_processing |
批量处理多个查询 | queries:SQL语句数组 |
性能和监控工具
| 工具 | 说明 | 参数 |
|---|---|---|
get_connection_pool_status |
获取数据库连接池状态 | 无 |
get_query_stats |
获取查询执行统计信息 | period:时间段(可选) |
start_performance_monitoring |
开始性能监控 | interval:监测间隔(ms) |
generate_performance_report |
生成性能分析报告 | timeRange:报告期 |
clear_caches |
初始化缓存数据 | 无 |
🛡️ 安全
- 连接加密:TLS/SSL连接支持
- 参数绑定:SQL注入预防
- 访问控制:基于数据库用户权限的控制
- 日志记录:完成查询执行日志记录
- 输入验证:全面的输入净化
安全最佳实践
- 使用具有最低所需权限的专用数据库用户
- 尽可能为数据库连接启用SSL/TLS加密
- 使用具有适当限制的连接池
- 从不将凭据提交到版本控制
- 对所有敏感配置使用环境变量
🗺️ Roadmap
即将推出的功能
-
🔮 多数据库支持
- MySQL适配器实现
- PostgreSQL适配器实现
- 统一数据库接口
-
⚡ 性能和缓存
- 查询结果缓存系统
- 高级性能优化
- 连接池增强
-
🧪 测试与质量
- 全面测试覆盖率(100%)
- 自动化CI/CD流水线
- 性能基准测试
-
🚀 高级功能
- GraphQL集成支持
- 增强的监控仪表板
- 可扩展性插件架构
-
📊 分析与洞察
- 高级查询分析
- 数据库性能洞察
- 使用统计和报告
支持与社区
- 📋 问题: 报告错误或请求功能
- 💬 讨论: 社区讨论和问答
- 📖 文档: 综合指南和API文档
📝 许可证
该项目根据MIT许可证获得许可。请参阅 许可证 文件 了解详情。
为AI驱动的开发社区打造
MCP SQL Server v1.0.0 -连接人工智能和数据库