# Microsoft SQL Server 桥接服务
## 基本信息
- Slug: `dperussina-mssql-mcp-server`
- Source: modelscope
- Publisher: @dperussina/mssql-mcp-server
- Categories: databases / developer-tools
- Hosted: No
- License: GNU General Public License v3.0
- Source URL: https://www.modelscope.cn/mcp/servers/@dperussina/mssql-mcp-server
## 简介
一个易于使用的桥梁，让像 Claude 和 Cursor IDE 这样的 AI 助手能够直接查询和探索 Microsoft SQL Server 数据库。无需编码经验！
## 安装提示

```bash
# Clone this repository git clone https://github.com/dperussina/mssql-mcp-server.git # Navigate to the project directory cd mssql-mcp-server # Install dependencies npm install # Copy the example environment file cp .env.example .env
```

## MCP Server 详情

# MS SQL MCP Server 1.1

一个易于使用的桥梁，让像 Claude 这样的 AI 助手可以直接查询和探索 Microsoft SQL Server 数据库。无需编码经验！

## 这个工具能做什么？

这个工具允许 AI 助手：
1. **发现**您 SQL Server 数据库中的表
2. **查看**表结构（列、数据类型等）
3. **执行**安全的只读 SQL 查询
4. **生成**从自然语言请求转换而来的 SQL 查询

## 🌟 为什么你需要这个工具

### 桥接数据与 AI 之间的鸿沟
- **无需编码**：无需编写复杂的集成代码，直接让 Claude 和其他 AI 助手访问您的 SQL Server 数据库
- **保持控制**：所有查询默认为只读，确保您的数据安全
- **私有且安全**：数据库凭据保持本地存储，永远不会发送到外部服务

### 实用优势
- **节省数小时的手动工作**：不再需要复制粘贴数据或查询结果以与 AI 共享
- **深入分析**：AI 可以浏览您的整个数据库模式，并提供跨多个表的见解
- **自然语言界面**：用简单的英语询问关于数据的问题
- **解决上下文限制问题**：访问超过常规 AI 上下文窗口的大数据集

### 适合人群
- **数据分析师**：希望在不共享凭证的情况下获得 AI 帮助解释 SQL 数据
- **开发者**：寻找通过自然对话快速探索数据库结构的方法
- **业务分析师**：不需要 SQL 专业知识即可获得洞察
- **数据库管理员**：希望向 AI 工具提供受控访问

## 🚀 快速入门指南

### 第一步：安装先决条件
- 安装 [Node.js](https://nodejs.org/)（版本 14 或更高）
- 访问 Microsoft SQL Server 数据库（本地或 Azure）

### 第二步：克隆并设置
```bash
# Clone this repository
git clone https://github.com/dperussina/mssql-mcp-server.git

# Navigate to the project directory
cd mssql-mcp-server

# Install dependencies
npm install

# Copy the example environment file
cp .env.example .env
```


### 第三步：配置数据库连接
编辑 `.env` 文件，填写您的数据库凭据：
```
DB_USER=your_username
DB_PASSWORD=your_password
DB_SERVER=your_server_name_or_ip
DB_DATABASE=your_database_name
PORT=3333
TRANSPORT=stdio
SERVER_URL=http://localhost:3333
DEBUG=false                     # Set to 'true' for detailed logging (helpful for troubleshooting)
QUERY_RESULTS_PATH=/path/to/query_results  # Directory where query results will be saved as JSON files
```


### 第四步：启动服务器
```bash
# Start with default stdio transport
npm start

# OR start with HTTP/SSE transport for network access
npm run start:sse
```


### 第五步：试用！
```bash
# Run the interactive client
npm run client
```


## 📊 示例用例

请根据实际需求替换 `#0`, `#1`, `#2`, `#3` 中的内容。

1. **无需编写SQL即可探索数据库结构**
   ```javascript
   mcp_SQL_mcp_discover_database()
   ```

2. **获取特定表的详细信息**
   ```javascript
   mcp_SQL_mcp_table_details({ tableName: "Customers" })
   ```

3. **运行安全查询**
   ```javascript
   mcp_SQL_mcp_execute_query({ sql: "SELECT TOP 10 * FROM Customers", returnResults: true })
   ```

4. **按名称模式查找表**
   ```javascript
   mcp_SQL_mcp_discover_tables({ namePattern: "%user%" })
   ```

5. **使用分页导航大型结果集**
   ```javascript
   // 第一页
   mcp_SQL_mcp_execute_query({ 
     sql: "SELECT * FROM Users ORDER BY Username OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY", 
     returnResults: true 
   })
   
   // 下一页
   mcp_SQL_mcp_execute_query({ 
     sql: "SELECT * FROM Users ORDER BY Username OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY", 
     returnResults: true 
   })
   ```

6. **基于游标的分页以获得最佳性能**
   ```javascript
   // 第一页
   mcp_SQL_mcp_execute_query({ 
     sql: "SELECT TOP 10 * FROM Users ORDER BY Username", 
     returnResults: true 
   })
   
   // 使用最后一个值作为游标获取下一页
   mcp_SQL_mcp_execute_query({ 
     sql: "SELECT TOP 10 * FROM Users WHERE Username > 'last_username' ORDER BY Username", 
     returnResults: true 
   })
   ```

7. **提出自然语言问题**
   ```
   "显示上个月订单最多的前5位客户"
   ```

## 💡 实际应用

### 商业智能
- **销售业绩分析**："显示过去一年的月度销售趋势，并按地区识别表现最好的产品。"
- **客户细分**："根据购买频率、平均订单价值和地理位置分析我们的客户基础。"
- **财务报告**："创建季度损益报告，比较今年与去年的数据。"

### 数据库管理
- **架构优化**："通过检查查询性能数据帮助我识别缺少索引的表。"
- **数据质量审核**："查找所有信息不完整或值无效的客户记录。"
- **使用情况分析**："显示哪些表被最频繁访问以及哪些查询资源消耗最大。"

### 开发
- **API探索**："我正在构建一个API - 帮助我分析数据库架构以设计合适的端点。"
- **查询优化**："审查这个复杂的查询并建议性能改进。"
- **数据库文档**："创建我们数据库结构的全面文档，解释关系。"

## 🖥️ 交互式客户端功能

捆绑的客户端提供了一个易于使用的菜单驱动界面：

1. **列出可用资源** - 查看可获取的信息
2. **列出可用工具** - 查看可以执行的操作
3. **执行 SQL 查询** - 运行只读的 SQL 查询
4. **获取表详情** - 查看任意表的结构
5. **读取数据库模式** - 查看所有表及其关系
6. **生成 SQL 查询** - 将自然语言转换为 SQL

## 🧠 有效的提示与工具使用指南

在通过此 MCP 服务器与 Claude 或其他 AI 助手合作时，您请求的方式对结果有很大影响。以下是如何帮助 AI 有效使用数据库工具的方法：

### 基本工具调用格式

当提示 AI 使用此工具时，请遵循以下结构：

```
Can you use the SQL MCP tools to [your goal]?

For example:
- Check what tables exist in my database
- Query the Customers table and show me the first 10 records
- Find all orders from the past month
```


### 必要的命令和语法

以下是主要工具及其正确的语法：

```javascript
// Discover the database structure
mcp_SQL_mcp_discover_database()

// Get detailed information about a specific table
mcp_SQL_mcp_table_details({ tableName: "YourTableName" })

// Execute a query and return results
mcp_SQL_mcp_execute_query({ 
  sql: "SELECT * FROM YourTable WHERE Condition", 
  returnResults: true 
})

// Find tables by name pattern
mcp_SQL_mcp_discover_tables({ namePattern: "%pattern%" })

// Access saved query results (for large result sets)
mcp_SQL_mcp_get_query_results({ uuid: "provided-uuid-here" })
```


**何时使用每个工具：**
- **数据库发现**：当 AI 不熟悉您的数据库结构时，首先使用此工具。
- **表详情**：在编写查询之前，专注于特定表时使用。
- **查询执行**：当需要检索或分析实际数据时使用。
- **按模式发现表**：当查找与特定领域相关的表时使用。

### 有效的提示模式

#### 分步工作流程
对于复杂的任务，指导 AI 按照一系列步骤进行：

```
I'd like to analyze our sales data. Please:
1. First use mcp_SQL_mcp_discover_tables to find tables related to sales
2. Use mcp_SQL_mcp_table_details to examine the structure of relevant tables
3. Create a query with mcp_SQL_mcp_execute_query that shows monthly sales by product category
```


#### 先结构后查询
```
First, discover what tables exist in my database. Then, look at the structure
of the Customers table. Finally, show me the top 10 customers by total purchase amount.
```


#### 请求解释
```
Query the top 5 underperforming products based on sales vs. forecasts,
and explain your approach to writing this query.
```


### SQL Server 方言注意事项

提醒 AI 关于 SQL Server 的特定语法：

```
Please use SQL Server syntax for pagination:
- For offset/fetch: "OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY"
- For cursor-based: "WHERE ID > last_id ORDER BY ID"
```


### 纠正工具使用

如果 AI 使用了错误的语法，您可以这样帮助它：

```
That's not quite right. Please use this format for the tool call:
mcp_SQL_mcp_execute_query({ 
  sql: "SELECT * FROM Customers WHERE Region = 'West'",
  returnResults: true
})
```


### 通过提示排除故障

如果 AI 在处理数据库任务时遇到困难，可以尝试以下方法：

1. **更具体地说明表信息**：“在编写该查询之前，请检查 CustomerOrders 表是否存在以及它有哪些列。”

2. **将复杂任务分解为步骤**：“让我们一步一步来。首先，查看 Products 表的结构。然后，检查 Orders 表……”

3. **请求中间结果**：“先在这个表上运行一个简单的查询，以便我们在尝试更复杂的分析之前验证数据格式。”

4. **请求查询解释**：…

