使用说明
@yuuzu/sql-mcp
用于MSSQL、PostgreSQL和SQLite数据库操作的模型上下文协议(MCP)服务器。
安装
# Using bunx (recommended)
bunx @yuuzu/sql-mcp
# Using npx
npx @yuuzu/sql-mcp
特性
- 多数据库支持:MSSQL、PostgreSQL和SQLite(通过better-splite3)
- 8工具 用于数据库操作
- 三种查询模式:安全、可写、已满
- 高级身份验证:Windows身份验证(MSSQL)、SSL证书(PostgreSQL)
工具
| 工具 | 说明 |
|---|---|
connect-database |
连接到数据库服务器 |
disconnect |
断开当前连接 |
connection-status |
检查连接状态和查询模式 |
list-databases |
列出服务器上的所有数据库 |
switch-database |
切换到其他数据库 |
list-tables |
列出所有表和视图 |
describe-table |
获取表架构详细信息 |
execute-query |
执行SQL查询 |
查询模式
通过以下方式控制查询权限 SQL_MCP_MODE 环境变量:
| 模式 | 允许的操作 | 描述 |
|---|---|---|
safe (默认) |
SELECT、WITH、EXPLAIN | 只读,最安全 |
write |
+INSERT、UPDATE、DELETE | 允许修改数据 |
full |
+CREATE、DROP、ALTER、TRUNCATE | 完全访问,谨慎使用 |
注:
PRAGMA陈述(例如。PRAGMA table_info(users))在所有模式下都是允许的,包括safe,因为它们是SQLite中常用的只读元数据查询。
# Example: Enable write mode
SQL_MCP_MODE=write bunx @yuuzu/sql-mcp
使用示例
Claude桌面配置
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"sql-mcp": {
"command": "bunx",
"args": ["@yuuzu/sql-mcp"],
"env": {
"SQL_MCP_MODE": "safe"
}
}
}
}
连接到MSSQL
{
"tool": "connect-database",
"arguments": {
"engine": "mssql",
"server": "localhost",
"port": 1433,
"user": "sa",
"password": "your_password",
"database": "master"
}
}
连接到PostgreSQL
{
"tool": "connect-database",
"arguments": {
"engine": "postgres",
"server": "localhost",
"port": 5432,
"user": "postgres",
"password": "your_password",
"database": "postgres"
}
}
使用Windows身份验证(MSSQL)连接
{
"tool": "connect-database",
"arguments": {
"engine": "mssql",
"server": "localhost",
"windowsAuth": true
}
}
使用SSL连接(PostgreSQL)
{
"tool": "connect-database",
"arguments": {
"engine": "postgres",
"server": "your-server.com",
"user": "postgres",
"password": "your_password",
"ssl": {
"rejectUnauthorized": true,
"ca": "/path/to/ca-certificate.crt"
}
}
}
连接到SQLite(文件)
{
"tool": "connect-database",
"arguments": {
"engine": "sqlite",
"filename": "/absolute/path/to/database.db"
}
}
连接到SQLite(内存中)
适用于测试、演示或短暂的草稿工作区。
{
"tool": "connect-database",
"arguments": {
"engine": "sqlite",
"filename": ":memory:"
}
}
连接到SQLite(只读)
{
"tool": "connect-database",
"arguments": {
"engine": "sqlite",
"filename": "/absolute/path/to/database.db",
"readonly": true,
"fileMustExist": true
}
}
SQLite运行时注意事项: SQLite支持使用
better-sqlite3这需要Node.js。当通过时bunx,MSSQL和PostgreSQL正常工作,但SQLite连接将返回一个明确的错误,指示您使用npx或node相反。
发展
# Install dependencies
bun install
# Run in development mode
bun run dev
# Run tests
bun test
# Run tests with coverage
bun test --coverage
# Build
bun run build
# Type check
bun run typecheck
释放
发布是通过GitHub Actions自动完成的。要创建新版本,请执行以下操作:
# Create and push a version tag
git tag v1.0.0
git push origin v1.0.0
这将:
- 构建项目
- 发布到具有出处的npm
- 使用自动生成的发布说明创建GitHub版本
需求:设置 NPM_TOKEN GitHub存储库设置中的秘密。
许可证
麻省理工学院
