这是一个用于PostgREST的MCP服务器。它允许LLMs通过REST API对您的应用程序执行CRUD操作。
此服务器适用于运行PostgREST的Supabase项目以及任何独立的PostgREST服务器。
以下工具可用:
postgrestRequest向一个已配置的PostgREST服务器发送HTTP请求。它接受以下参数:
method: 要使用的HTTP方法(例如:GET, POST, PATCH, DELETE)path: 要查询的路径(例如:/todos?id=eq.1)body: 请求体(对于POST和PATCH请求)它返回来自PostgREST服务器的JSON响应,包括GET请求的选择行和POST及PATCH请求的更新行。
sqlToRest将SQL查询转换为等效的PostgREST语法(作为方法和路径)。这对于复杂查询非常有用,这些查询通常难以由LLMs转换为有效的PostgREST语法。
请注意,PostgREST仅支持SQL的一个子集,因此并非所有查询都能转换。详情请参阅sql-to-rest。
它接受以下参数:
sql: 要转换的SQL查询。它返回一个对象,其中包含请求的method和path属性。然后,LLMs可以使用postgrestRequest工具来执行请求。
Claude Desktop是一款流行的LLM客户端,支持模型上下文协议。您可以将PostgREST服务器连接到Claude Desktop,以通过自然语言命令查询数据库。
您可以通过其配置文件添加MCP服务器到Claude Desktop:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %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端点的基本URLapiKey: 用于身份验证的API密钥(可选)schema: 从哪个Postgres模式提供API(例如:public)。请注意,任何非公共模式必须手动从PostgREST暴露。如果您正在构建自己的MCP客户端,可以使用您喜欢的传输方式编程连接到PostgREST服务器。MCP SDK提供了内置的标准输入输出(stdio)和服务端发送事件(SSE)传输。我们还提供了一个StreamTransport,如果您希望直接在内存中连接MCP服务器或将流式传输管道连接到自己的基于流的传输。
npm i @supabase/mcp-server-postgrest
yarn add @supabase/mcp-server-postgrest
pnpm 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';
// 为客户端和服务器创建流传输
const clientTransport = new StreamTransport();
const serverTransport = new StreamTransport();
// 将流连接在一起
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 对于本地
const apiKey = 'your-anon-key'; // 或服务角色,或用户JWT
const schema = 'public'; // 或任何其他公开的模式
const server = createPostgrestM