使用说明
SQLServer MCP服务器
特性
- 获取表架构
- 验证性能
- 运行安全诊断(带回滚功能)
先决条件
环境变量
您可以设置:
- 运输方式。
- 端点
- 此服务的自定义主机名、端口号。
- SQLServer用户/密码、数据库名称、启用/禁用信任证书和加密。
- 允许PoC模式(无LLM的呼叫功能)检查此服务的机制,只需设置
IS_ENV向 概念验证。否则,通过正常的传输方式进行通信(stdio,sse,streamable-http).
请参考 .env.example.
您的目标SQLServer
别忘了启动它。
用法
启动服务器
局部的,局部的
uv venv
source .venv/bin/activate
uv sync
然后运行
uv run main.py
容器组成
假设你的根仓库是 mcp-server-sqlserver:
services:
mcp-server-sqlserver:
container_name: mcp-server-sqlserver
build:
context: ./mcp-server-sqlserver
dockerfile: Dockerfile
ports:
- "4200:4200"
env_file:
- ./mcp-server-sqlserver/.env
volumes:
- ./mcp-server-sqlserver:/app
networks:
- same_network_as_sqlserver_if_exists
tty: true
restart: always
将MCP服务器添加到您的代理
示例
例如,通过以下方式使用MCP服务器 LangChain 目标表上没有任何描述(需要在提问前描述一些内容):
================================ Human Message =================================
From dbo.NOTIFICATION in master database, which customer id who did not read notification yet?
================================== Ai Message ==================================
[{'type': 'text', 'text': 'To get the list of customer IDs that have unread notifications in the dbo.NOTIFICATION table, we can run the following SQL query using the run_safe_diagnostic tool:'}, {'type': 'tool_use', 'name': 'run_safe_diagnostic', 'input': {'db': 'master', 'sql': 'SELECT DISTINCT CUSTOMER_ID \nFROM dbo.NOTIFICATION\nWHERE IS_READ = 0;'}, 'id': 'tooluse_l_vou1pPTHWRresWHCqnvg'}]
Tool Calls:
run_safe_diagnostic (tooluse_l_vou1pPTHWRresWHCqnvg)
Call ID: tooluse_l_vou1pPTHWRresWHCqnvg
Args:
db: master
sql: SELECT DISTINCT CUSTOMER_ID
FROM dbo.NOTIFICATION
WHERE IS_READ = 0;
================================= Tool Message =================================
Name: run_safe_diagnostic
Error: ToolException('Error executing tool run_safe_diagnostic: (pyodbc.ProgrammingError) (\'42S22\', "[42S22] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid column name \'CUSTOMER_ID\'. (207) (SQLExecDirectW)")\n[SQL: SELECT DISTINCT CUSTOMER_ID \nFROM dbo.NOTIFICATION\nWHERE IS_READ = 0;]\n(Background on this error at: https://sqlalche.me/e/20/f405)')
Please fix your mistakes.
================================== Ai Message ==================================
[{'type': 'text', 'text': 'Oops, it seems the dbo.NOTIFICATION table does not have a CUSTOMER_ID column. Let me check the table schema first:'}, {'type': 'tool_use', 'name': 'get_table_schema', 'input': {'db': 'master', 'table': 'NOTIFICATION'}, 'id': 'tooluse_Ik7C2avnR4uDyb1ZpDGxCw'}]
Tool Calls:
get_table_schema (tooluse_Ik7C2avnR4uDyb1ZpDGxCw)
Call ID: tooluse_Ik7C2avnR4uDyb1ZpDGxCw
Args:
db: master
table: NOTIFICATION
================================= Tool Message =================================
Name: get_table_schema
[{"COLUMN_NAME": "NOTI_ID", "DATA_TYPE": "bigint", "IS_NULLABLE": "NO"}, {"COLUMN_NAME": "CUST_ID", "DATA_TYPE": "varchar", "IS_NULLABLE": "YES"}, {"COLUMN_NAME": "SUBJECT_TH", "DATA_TYPE": "varchar", "IS_NULLABLE": "YES"}, {"COLUMN_NAME": "SUBJECT_EN", "DATA_TYPE": "varchar", "IS_NULLABLE": "YES"}, ..., {"COLUMN_NAME": "IMAGE_ACTION_2", "DATA_TYPE": "varchar", "IS_NULLABLE": "YES"}]
================================== Ai Message ==================================
[{'type': 'text', 'text': "Ah I see, the customer ID column is named CUST_ID. Let's try the query again with the correct column name:"}, {'type': 'tool_use', 'name': 'run_safe_diagnostic', 'input': {'db': 'master', 'sql': 'SELECT DISTINCT CUST_ID\nFROM dbo.NOTIFICATION \nWHERE IS_READ = 0;'}, 'id': 'tooluse_625QEvaeSjeynA9raV4K6g'}]
Tool Calls:
run_safe_diagnostic (tooluse_625QEvaeSjeynA9raV4K6g)
Call ID: tooluse_625QEvaeSjeynA9raV4K6g
Args:
db: master
sql: SELECT DISTINCT CUST_ID
FROM dbo.NOTIFICATION
WHERE IS_READ = 0;
================================= Tool Message =================================
Name: run_safe_diagnostic
[
{
"cpu_time": 2,
"logical_reads": 651,
"rows": [
{
"CUST_ID": "lj2koe562"
}
]
}
]
================================== Ai Message ==================================
The query returns a list of distinct CUST_ID values from the NOTIFICATION table where the IS_READ column is 0, indicating unread notifications.
Based on the result, the customer ID "lj2koe562" has unread notifications in this table.
发展
- ✅ 运输选择
- ✅ 本地通信
- ✅ 远程通信
- ⬜ 消息处理
- ⬜ 请求处理
- ⬜ 进度报告
- ⬜ 错误管理
- ⬜ 安全注意事项
- ✅ 运输安全
- ⬜ 消息验证
- ⬜ 资源保护
- ⬜ 错误处理
- ⬜ 调试和监控
- ⬜ 日志记录
- ⬜ 诊断
- ⬜ 测试