SQL Server数据库交互协议

MCP SQL Server是一个模型上下文协议服务器,使AI助手如Claude能够通过本地连接安全地与SQL Server数据库交互,提供数据库探索、查询执行和数据分析工具。

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

使用说明

MCP SQL Server

一个模型上下文协议(MCP)服务器,使像Claude这样的人工智能助手能够通过本地连接安全地与SQL server数据库交互。此服务器通过受控界面提供数据库探索、查询执行和数据分析工具。

特性

  • 🔒 安全本地数据库访问:使用SQL身份验证连接到SQL Server
  • 🔍 模式探索:发现表、列和关系
  • 📊 查询执行:运行具有适当错误处理的SQL查询
  • 🛡️ 安全控制:内置防止有害操作的保护措施

先决条件

  • Node.js 18.0或更高版本
  • npm(节点包管理器)
  • SQL Server
  • Claude桌面应用程序

安装

1.克隆存储库

git clone https://github.com/MKhan997733/mcp-server-sql.git
cd mcp-server-sql

2.安装依赖项

npm install

3.配置环境变量

创建一个 .env 项目根目录中的文件:

DB_SERVER=
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_PORT=

Claude桌面配置

1.找到Claude桌面配置文件

配置文件位于:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 视窗: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

2.添加MCP服务器配置

编辑 claude_desktop_config.json 文件并添加您的MCP服务器:

对于SQL身份验证:

{
  "mcpServers": {
    "sql-server": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-server-sql/index.js"
      ],
      "env": {
        "DB_SERVER": "localhost",
        "DB_NAME": "your_database",
        "DB_USER": "your_username",
        "DB_PASSWORD": "your_password",
        "DB_PORT": "port"
      }
    }
  }
}

重要:替换 /absolute/path/to/mcp-server-sql/ 使用克隆存储库的实际路径。

3.重新启动克劳德桌面

保存配置文件后,重新启动Claude Desktop以使更改生效。

用法

配置后,您可以通过Claude与SQL Server数据库交互,询问以下问题:

  • “我的数据库中有哪些表?”
  • “显示Users表的架构”
  • “查询订单表并显示上周的订单”
  • “按总购买金额查找前10名客户”
  • “显示数据库中的所有存储过程”

交互示例

查找表:

You: Can you show me what tables exist in my database?
Claude: I'll help you discover the tables in your SQL Server database...

查询数据:

You: Show me all customers who made purchases over $1000 in the last month
Claude: I'll query your database to find customers with purchases over $1000...

模式探索:

You: What columns does the Products table have?
Claude: Let me examine the structure of the Products table...

可用工具

MCP服务器为Claude提供以下工具:

  1. discover_tables:列出SQL Server数据库中的所有表
  2. table_details:获取特定表的详细架构信息
  3. execute_query:运行SELECT查询(默认情况下为只读)
  4. get_stored_procedures:列出所有存储过程
  5. get_table_indexes:查看表上的索引
  6. get_table_conconstraints:视图约束(PK、FK等)

安全最佳实践

  1. 使用专用SQL Server登录:为MCP服务器创建具有最低所需权限的特定登录名
  2. 只读访问:将数据库用户配置为只读访问
  3. 仅限本地连接:服务器只接受到localhost的连接
  4. 审计日志:启用SQL Server审核日志记录以跟踪所有查询

运行服务器

# Run directly

npm build

node <absolute-path>/build/index.js

(人工智能生成,由MKHAN997733审核)