SQL MCP服务器
一种模型上下文协议(MCP)服务器,提供对SQL server数据库的安全只读访问。此服务器允许AI助手和其他MCP客户端查询数据库模式,并执行具有内置安全限制的SELECT查询。
特性
- 只读访问:只允许SELECT查询-不允许INSERT、UPDATE、DELETE或DDL操作
- 模式探索:获取表架构、列信息和可用表
- 样品数据:从表中检索示例数据(限制为10行)
- 参数化查询:支持参数化SQL查询以防止SQL注入
- 安全控制:内置查询验证和表/模式过滤
- MCP集成:完全符合AI助手集成的模型上下文协议
先决条件
- .NET 8.0或更高版本
- SQL Server(支持Microsoft.Data.SQL客户端的任何版本)
- Visual Studio代码(推荐)或任何。NET IDE
安装
- 克隆存储库:
git clone https://github.com/SyedShahriyarAli/SqlMcpServer.git
cd SqlMcpServer- 还原NuGet包:
dotnet restore- 更新中的连接字符串
SqlMcpServer/Services/SqlService.cs:
private readonly string _connectionString = "your-connection-string-here";- 构建项目:
dotnet build配置
数据库连接
更新中的连接字符串 SqlService.cs:
private readonly string _connectionString = "Server=your-server;Database=your-database;Integrated Security=true;";安全设置
服务器包括几个可以在中配置的安全控件 SqlService.cs:
- 允许的桌子:限制对特定表的访问
- 允许的模式:限制对特定架构的访问(默认值:
dbo) - 禁止使用的关键字:防止执行危险的SQL操作
private readonly HashSet _allowedTables = []; // Empty = all tables allowed
private readonly HashSet _allowedSchemas = ["dbo"]; // Only dbo schema by defaultMCP客户端配置
对于支持MCP的Visual Studio代码,请将此配置添加到您的 .vscode/mcp.json:
{
"servers": {
"sql-mcp-server": {
"type": "stdio",
"command": "dotnet",
"args": [
"run",
"--project",
"path/to/SqlMcpServer/SqlMcpServer.csproj"
]
}
}
}