Supabase MCP工具
一个提供与 Supabase 数据库、存储和边缘函数交互工具的 MCP 服务器。
服务介绍
Supabase MCP 服务器
这是一个 Model Context Protocol (MCP) 服务器,提供了与 Supabase 数据库、存储和边缘函数交互的全面工具。该服务器实现了 Supabase 服务与 MCP 兼容应用程序之间的无缝集成。
概览
Supabase MCP 服务器充当 MCP 客户端与 Supabase 服务套件之间的桥梁,提供以下功能:
- 带有丰富查询能力的数据库操作
- 文件和资产的存储管理
- 边缘函数调用
- 项目和组织管理
- 用户认证和管理
- 基于角色的访问控制
架构
该服务器使用 TypeScript 构建,并遵循模块化架构:
supabase-server/
├── src/
│ ├── index.ts # Main server implementation
│ └── types/
│ └── supabase.d.ts # Type definitions
├── package.json
├── tsconfig.json
├── config.json.example # Example configuration file
└── .env.example # Environment variables template
关键组件
- Server 类:实现 MCP 服务器接口并处理所有客户端请求
- 类型定义:为所有操作提供全面的 TypeScript 定义
- 环境配置:通过环境变量进行安全的配置管理
- 错误处理:具有详细错误信息的强大错误处理
前提条件
- Node.js 16.x 或更高版本
- 一个包含以下内容的 Supabase 项目:
- 项目 URL
- 服务角色密钥(用于管理员操作)
- 访问令牌(用于管理操作)
- MCP 兼容的客户端
安装
通过 Smithery 安装
要通过 Smithery 自动安装适用于 Claude Desktop 的 Supabase 服务器:
npx -y @smithery/cli install supabase-server --client claude
- 克隆仓库:
git clone https://github.com/DynamicEndpoints/supabase-mcp.git
cd supabase-mcp
- 安装依赖项:
npm install
- 创建环境配置:
cp .env.example .env
- 配置环境变量:
SUPABASE_URL=your_project_url_here
SUPABASE_KEY=your_service_role_key_here
SUPABASE_ACCESS_TOKEN=your_access_token_here # Required for management operations
- 创建服务器配置:
cp config.json.example config.json
- 构建服务器:
npm run build
配置
服务器通过环境变量和 config.json 文件支持广泛的配置选项。以下是配置选项的详细说明:
服务器配置
{
"server": {
"name": "supabase-server", // Server name
"version": "0.1.0", // Server version
"port": 3000, // Port number (if running standalone)
"host": "localhost" // Host address (if running standalone)
}
}
Supabase 配置
{
"supabase": {
"project": {
"url": "your_project_url",
"key": "your_service_role_key",
"accessToken": "your_access_token"
},
"storage": {
"defaultBucket": "public", // Default storage bucket
"maxFileSize": 52428800, // Max file size in bytes (50MB)
"allowedMimeTypes": [ // Allowed file types
"image/*",
"application/pdf",
"text/*"
]
},
"database": {
"maxConnections": 10, // Max DB connections
"timeout": 30000, // Query timeout in ms
"ssl": true // SSL connection
},
"auth": {
"autoConfirmUsers": false, // Auto-confirm new users
"disableSignup": false, // Disable public signups
"jwt": {
"expiresIn": "1h", // Token expiration
"algorithm": "HS256" // JWT algorithm
}
}
}
}
日志配置
{
"logging": {
"level": "info", // Log level
"format": "json", // Log format
"outputs": ["console", "file"], // Output destinations
"file": {
"path": "logs/server.log", // Log file path
"maxSize": "10m", // Max file size
"maxFiles": 5 // Max number of files
}
}
}
安全配置
{
"security": {
"cors": {
"enabled": true,
"origins": ["*"],
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
"allowedHeaders": ["Content-Type", "Authorization"]
},
"rateLimit": {
"enabled": true,
"windowMs": 900000, // 15 minutes
"max": 100 // Max requests per window
}
}
}
监控配置
{
"monitoring": {
"enabled": true,
"metrics": {
"collect": true,
"interval": 60000 // Collection interval in ms
},
"health": {
"enabled": true,
"path": "/health" // Health check endpoint
}
}
}
请参阅 config.json.example 以获取完整的示例配置文件。
MCP 集成
将服务器添加到您的 MCP 设置中(cline_mcp_settings.json):
{
"mcpServers": {
"supabase": {
"command": "node",
"args": ["path/to/supabase-server/build/index.js"],
"env": {
"SUPABASE_URL": "your_project_url",
"SUPABASE_KEY": "your_service_role_key",
"SUPABASE_ACCESS_TOKEN": "your_access_token"
},
"config": "path/to/config.json" // Optional: path to configuration file
}
}
}
可用工具
数据库操作
create_record
在表中创建新记录,并支持返回特定字段。
{
table: string;
data: Record<string, any>;
returning?: string[];
}
示例:
{
table: "users",
data: {
name: "John Doe",
email: "john@example.com"
},
returning: ["id", "created_at"]
}
read_records
读取带有高级过滤、连接和字段选择的记录。
{
table: string;
select?: string[];
filter?: Record<string, any>;
joins?: Array<{
type?: 'inner' | 'left' | 'right' | 'full';
table: string;
on: string;
}>;
}
示例:
{
table: "posts",
select: ["id", "title", "user.name"],
filter: { published: true },
joins: [{
type: "left",
table: "users",
on: "posts.user_id=users.id"
}]
}
update_record
更新带有过滤和返回功能的记录。
{
table: string;
data: Record<string, any>;
filter?: Record<string, any>;
returning?: string[];
}
示例:
{
table: "users",
data: { status: "active" },
filter: { email: "john@example.com" },
returning: ["id", "status", "updated_at"]
}
delete_record
删除带有过滤和返回功能的记录。
{
table: string;
filter?: Record<string, any>;
returning?: string[];
}
示例:
{
table: "posts",
filter: { status: "draft" },
returning: ["id", "title"]
}
存储操作
upload_file
将文件上传到 Supabase 存储,并提供可配置的选项。
{
bucket: string;
path: string;
file: File | Blob;
options?: {
cacheControl?: string;
contentType?: string;
upsert?: boolean;
};
}
示例:
{
bucket: "avatars",
path: "users/123/profile.jpg",
file: imageBlob,
options: {
contentType: "image/jpeg",
upsert: true
}
}
download_file
从 Supabase 存储下载文件。
{
bucket: string;
path: string;
}
示例:
{
bucket: "documents",
path: "reports/annual-2023.pdf"
}
边缘函数
invoke_function
使用参数和自定义选项调用 Supabase 边缘函数。
{
function: string;
params?: Record<string, any>;
options?: {
headers?: Record<string, string>;
responseType?: 'json' | 'text' | 'arraybuffer';
};
}
示例:
{
function: "process-image",
params: {
url: "https://example.com/image.jpg",
width: 800
},
options: {
responseType: "json"
}
}
用户管理
list_users
支持分页列出用户。
{
page?: number;
per_page?: number;
}
create_user
创建带有元数据的新用户。
{
email: string;
password: string;
data?: Record<string, any>;
}
update_user
更新用户信息。
{
user_id: string;
email?: string;
password?: string;
data?: Record<string, any>;
}
delete_user
删除用户。
{
user_id: string;
}
assign_user_role
为用户分配角色。
{
user_id: string;
role: string;
}
remove_user_role
移除用户的角色。
{
user_id: string;
role: string;
}
错误处理
服务器为常见场景提供了详细的错误消息:
- 无效参数
- 身份验证失败
- 权限问题
- 速率限制
- 网络错误
- 数据库约束
错误以标准化格式返回:
{
code: ErrorCode;
message: string;
details?: any;
}
开发
运行测试
npm test
构建
npm run build
代码检查
npm run lint
贡献
- 叉取仓库
- 创建功能分支
- 提交您的更改
- 推送到该分支
- 创建一个拉取请求
许可证
MIT 许可证 - 详情请参阅 LICENSE 文件
支持
如需支持,请:
- 检查 issues 中已有的问题/解决方案
- 创建一个新的 issue,包含详细的复现步骤
- 包含相关的错误消息和环境详情