SQL Server数据库集成工具

MCP SQL Server是一个用于SQL Server数据库集成与Claude Code交互的工具,提供查询执行、模式探索、数据库监控和性能分析等功能。

  • by fabriciofs
  • TypeScript
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 fabriciofs/mcp-sql-server

使用说明

MCP SQL Server

用于SQL server与Claude Code集成的模型上下文协议(MCP)服务器。直接从Claude查询、监视和分析您的SQL Server数据库。

特性

  • 查询执行 -使用参数化输入执行SELECT查询
  • 模式探索 -浏览表、列、过程和索引
  • 数据库监控 -跟踪活动查询、阻塞会话、等待统计数据和连接
  • 性能分析 -识别缺失的索引、未使用的索引和碎片
  • 写入操作 -启用时插入、更新、删除(只读=假)

安装

选项1:来自npm(推荐)

npx @fabriciofs/mcp-sql-server

选项2:全局安装

npm install -g @fabriciofs/mcp-sql-server

选项3:在本地克隆和构建

git clone https://github.com/fabriciofs/mcp-sql-server.git
cd mcp-sql-server
npm install
npm run build

配置

Claude代码集成

添加到您的Claude Code MCP设置中(~/.claude/settings.json 或项目 .claude/settings.json):

{
  "mcpServers": {
    "sqlserver": {
      "command": "npx",
      "args": ["-y", "@fabriciofs/mcp-sql-server"],
      "env": {
        "SQL_CONNECTION_URL": "sqlserver://user:password@localhost:1433/database",
        "READONLY": "true"
      }
    }
  }
}

或者使用单独的连接参数:

{
  "mcpServers": {
    "sqlserver": {
      "command": "npx",
      "args": ["-y", "@fabriciofs/mcp-sql-server"],
      "env": {
        "SQL_SERVER": "localhost",
        "SQL_DATABASE": "mydb",
        "SQL_USER": "sa",
        "SQL_PASSWORD": "yourpassword",
        "SQL_PORT": "1433",
        "SQL_TRUST_CERT": "true",
        "READONLY": "true"
      }
    }
  }
}

环境变量

连接(选择一种方法)

方法1:连接URL

SQL_CONNECTION_URL=sqlserver://user:password@host:port/database?TrustServerCertificate=true

方法2:单个参数

SQL_SERVER=localhost
SQL_DATABASE=mydb
SQL_USER=sa
SQL_PASSWORD=yourpassword
SQL_PORT=1433              # Optional, default: 1433
SQL_ENCRYPT=true           # Optional, default: true
SQL_TRUST_CERT=false       # Optional, default: false

必需设置

变量 描述
READONLY 必修的。 吃起来 true 用于只读模式或 false 启用写入操作

可选设置

变量 默认值 描述
QUERY_TIMEOUT 30000 查询超时(毫秒)(最大值:120000)
MAX_ROWS 1000 要返回的最大行数(最大值:5000)
POOL_MIN 2 最小连接池大小
POOL_MAX 10 最大连接池大小
LOG_LEVEL info 日志级别:调试、信息、警告、错误

可用工具

查询工具

工具 说明
sql_execute 使用参数化输入执行SELECT查询

架构工具

工具 说明
schema_list_tables 列出数据库中的所有表和视图
schema_describe_table 获取详细的表信息(列、索引、外键)
schema_list_columns 在所有表中搜索列
schema_list_procedures 列出存储过程
schema_list_indexes 列出具有使用统计信息的索引

监控工具

工具 说明
monitor_active_queries 监视当前正在运行的查询
monitor_blocking 监控阻塞会话和锁链
monitor_wait_stats 监控性能瓶颈的等待统计数据
monitor_database_size 监控数据库大小和文件使用情况
monitor_connections 监控活动连接
monitor_performance_counters 监视SQL Server性能计数器

分析工具

工具 说明
analyze_query 分析查询执行计划和统计
analyze_suggest_indexes 根据查询模式建议缺少的索引
analyze_unused_indexes 查找未使用的索引
analyze_duplicate_indexes 查找重复或重叠的索引
analyze_fragmentation 分析索引碎片级别
analyze_statistics 分析表统计信息以查找过时数据

写入工具(只读=仅为false)

工具 说明
sql_insert 在表格中插入一行
sql_update 更新表中的行
sql_delete 从表中删除行

用法示例

配置后,您可以要求Claude与您的数据库进行交互:

"List all tables in the database"
"Describe the Users table"
"Show me active queries running for more than 5 seconds"
"Find unused indexes in the Orders table"
"Analyze the fragmentation of all indexes"
"What are the top wait statistics?"

安全考虑

  • 在生产中始终使用READONLY=true 除非明确要求写访问权限
  • 使用环境变量安全地存储凭据
  • 使用具有最低所需权限的SQL Server帐户
  • 考虑远程连接的网络安全(VPN、防火墙规则)

需求

  • Node.js>=20.0.0
  • SQL Server 2016或更高版本
  • 您要执行的操作的适当SQL Server权限

发展

# Install dependencies
npm install

# Build
npm run build

# Development mode (watch)
npm run dev

# Type check
npm run typecheck

# Run MCP Inspector
npm run inspector

许可证

MIT许可证-请参阅 许可证 文件以获取详细信息。