SQL Server MCP Server
# SQL Server MCP Server
一个基于模型上下文协议(MCP)的 SQL Server 数据库操作服务器,让 AI 助手能够通过标准化接口与 SQL Server 数据库交互。
## 功能特性
- **查询执行**:执行带参数化的 SELECT 查询
- **数据修改**:执行 INSERT、UPDATE、DELETE 操作
- **结构查询**:列出数据库表、查看表结构
- **性能分析**:使用执行计划分析查询性能
- **连接管理**:支持环境变量自动连接,安全可靠
## 安装
```bash
npm install sqlserver-mcp-ddz
```
或直接使用 npx:
```bash
npx sqlserver-mcp-ddz
```
## 配置
### 方式一:环境变量自动连接(推荐)
在 MCP 客户端配置中添加环境变量,服务器启动时会自动连接数据库:
```json
{
"mcpServers": {
"sqlserver-mcp-ddz": {
"command": "npx",
"args": ["-y", "sqlserver-mcp-ddz"],
"env": {
"SQLSERVER_HOST": "127.0.0.1",
"SQLSERVER_PORT": "1433",
"SQLSERVER_USER": "sa",
"SQLSERVER_PASSWORD": "your_password",
"SQLSERVER_DATABASE": "your_database",
"SQLSERVER_ENCRYPT": "true",
"SQLSERVER_TRUST_CERTIFICATE": "false"
}
}
}
}
```
**支持的环境变量:**
| 变量名 | 说明 | 必填 | 默认值 |
|--------|------|------|--------|
| `SQLSERVER_HOST` | 数据库主机地址 | 是 | - |
| `SQLSERVER_PORT` | 数据库端口 | 否 | `1433` |
| `SQLSERVER_USER` | 数据库用户名 | 是 | - |
| `SQLSERVER_PASSWORD` | 数据库密码 | 是 | - |
| `SQLSERVER_DATABASE` | 数据库名称 | 是 | - |
| `SQLSERVER_ENCRYPT` | 是否加密连接 | 否 | `true` |
| `SQLSERVER_TRUST_CERTIFICATE` | 是否信任服务器证书 | 否 | `false` |
### 方式二:手动连接
不在配置中添加环境变量时,需要通过 `connect_db` 工具手动连接:
```json
{
"mcpServers": {
"sqlserver": {
"command": "npx",
"args": ["-y", "sqlserver-mcp-ddz"]
}
}
}
```
## 可用工具
### `connect_db`
连接到 SQL Server 数据库(环境变量配置后可省略此步骤)。
**参数:**
- `host`(字符串,必填):数据库主机地址
- `port`(数字,可选):数据库端口,默认 1433
- `user`(字符串,必填):数据库用户名
- `password`(字符串,必填):数据库密码
- `database`(字符串,必填):数据库名称
- `encrypt`(布尔,可选):是否加密连接,默认 true
- `trustServerCertificate`(布尔,可选):是否信任服务器证书,默认 false
**示例:**
```json
{
"host": "localhost",
"port": 1433,
"user": "sa",
"password": "your_password",
"database": "mydb",
"encrypt": true,
"trustServerCertificate": false
}
```
### `query`
执行 SELECT 查询并返回结果。
**参数:**
- `sql`(字符串,必填):SQL SELECT 查询语句
- `params`(数组,可选):查询参数
**示例:**
```json
{
"sql": "SELECT * FROM users WHERE age > @param0",
"params": [18]
}
```
### `execute`
执行 INSERT、UPDATE 或 DELETE 查询。
**参数:**
- `sql`(字符串,必填):SQL 语句
- `params`(数组,可选):查询参数
**示例:**
```json
{
"sql": "INSERT INTO users (name, email) VALUES (@param0, @param1)",
"params": ["张三", "zhangsan@example.com"]
}
```
### `list_tables`
列出当前数据库中的所有表。
### `describe_table`
获取指定表的结构信息,包括列、主键和外键。
**参数:**
- `table`(字符串,必填):表名
**示例:**
```json
{
"table": "users"
}
```
### `explain`
使用执行计划分析 SQL 查询性能。
**参数:**
- `sql`(字符串,必填):要分析的 SQL 查询
**示例:**
```json
{
"sql": "SELECT * FROM users WHERE email = 'test@example.com'"
}
```
### `show_statement`
执行系统存储过程或 SHOW 类语句。
**参数:**
- `sql`(字符串,必填):SQL 语句
**示例:**
```json
{
"sql": "EXEC sp_who"
}
```
## 安全说明
- `query` 工具仅允许 SELECT、SHOW、WITH 和 EXEC 查询
- 数据修改操作请使用 `execute` 工具
- 所有查询支持参数化输入,防止 SQL 注入
- 数据库凭据仅在运行时传递,不会被持久化存储
TDQS
Scored across 7 tools
The core split between read (query), write (execute), and metadata (list_tables, describe_table) is clear, and explain is distinctly performance-focused. The only fuzzy boundary is execute vs. show_statement, since both run arbitrary SQL against the server and only the description distinguishes DML from stored procedures/SHOW statements.
Four tools follow a verb_noun pattern (describe_table, show_statement, connect_db, list_tables) while three are bare verbs (execute, explain, query), producing a mixed but still readable convention. 'query' is especially weak as a name since it is both a verb and a noun and doesn't pair with the others.
Seven tools is well-scoped for a database access server: one connection tool, two metadata tools, three execution tools, and one diagnostics tool. Each tool covers a distinct slice of the workflow and none feels redundant or padded.
The read/execute/metadata/explain loop is covered, but there is no DDL support (CREATE/ALTER/DROP TABLE) since execute is limited to INSERT/UPDATE/DELETE, no transaction control, no disconnect or list_databases to pair with connect_db. Agents can handle typical data workflows but will hit dead ends on schema changes and connection management.