@suabase/mcp服务器postgrest
这是一个MCP服务器 PostgREST.它允许LLM通过REST API在您的应用程序上执行CRUD操作。
此服务器可与Supabase项目(运行PostgREST)和任何独立的PostgREST服务器配合使用。
工具
以下工具可用:
postgrestRequest
向执行HTTP请求 配置 PostgREST服务器。它接受以下论点:
method:要使用的HTTP方法(例如。GET,POST,PATCH,DELETE)path:查询路径(例如。/todos?id=eq.1)body:请求体(forPOST和PATCH请求:
它从PostgREST服务器返回JSON响应,包括以下内容的选定行 GET 请求和更新的行 POST 和 PATCH 请求:
sqlToRest
将SQL查询转换为等效的PostgREST语法(作为方法和路径)。对于LLM难以转换为有效PostgREST语法的复杂查询非常有用。
请注意,PostgREST只支持SQL的一个子集,因此并非所有查询都会转换。看 sql-to-rest 了解更多详情。
它接受以下论点:
sql:要转换的SQL查询。
它返回一个包含以下内容的对象 method 和 path 请求的属性。LLM然后可以使用 postgrestRequest 执行请求的工具。
用法
使用克劳德桌面
克劳德桌面版 是支持模型上下文协议的流行LLM客户端。您可以将PostgREST服务器连接到Claude Desktop,通过自然语言命令查询数据库。
您可以通过Claude Desktop的配置文件将MCP服务器添加到Claude Desktop:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json
- 窗户:
%APPDATA%\Claude\claude_desktop_config.json
添加您的Supabase项目 _(或任何PostgREST服务器)_ 在Claude Desktop中,将以下配置添加到 mcpServers 配置文件中的对象:
{
"mcpServers": {
"todos": {
"command": "npx",
"args": [
"-y",
"@supabase/mcp-server-postgrest@latest",
"--apiUrl",
"https://your-project-ref.supabase.co/rest/v1",
"--apiKey",
"your-anon-key",
"--schema",
"public"
]
}
}
}配置
apiUrl:PostgREST端点的基本URL
apiKey:用于身份验证的API密钥 _(可选)_
schema:为API提供服务的Postgres模式(例如。public).请注意,任何非公共模式都必须从PostgREST手动公开。
程序化(自定义MCP客户端)
安装
npm i @supabase/mcp-server-postgrestyarn add @supabase/mcp-server-postgrestpnpm add @supabase/mcp-server-postgrest示例
以下示例使用 StreamTransport 以在MCP客户端和服务器之间直接连接。
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamTransport } from '@supabase/mcp-utils';
import { createPostgrestMcpServer } from '@supabase/mcp-server-postgrest';
// Create a stream transport for both client and server
const clientTransport = new StreamTransport();
const serverTransport = new StreamTransport();
// Connect the streams together
clientTransport.readable.pipeTo(serverTransport.writable);
serverTransport.readable.pipeTo(clientTransport.writable);
const client = new Client(
{
name: 'MyClient',
version: '0.1.0',
},
{
capabilities: {},
}
);
const supabaseUrl = 'https://your-project-ref.supabase.co'; // http://127.0.0.1:54321 for local
const apiKey = 'your-anon-key'; // or service role, or user JWT
const schema = 'public'; // or any other exposed schema
const server = createPostgrestMcpServer({
apiUrl: `${supabaseUrl}/rest/v1`,
apiKey,
schema,
});
// Connect the client and server to their respective transports
await server.connect(serverTransport);
await client.connect(clientTransport);
// Call tools, etc
const output = await client.callTool({
name: 'postgrestRequest',
arguments: {
method: 'GET',
path: '/todos',
},
});