入门

部署 MCP 服务器


使用 模型上下文协议 (MCP) 服务器在 Supabase 上构建和部署,使用 边缘函数

先决条件#

在开始之前,请确保您已安装

部署您的 MCP 服务器#

步骤 1:创建一个新项目#

首先创建一个新的 Supabase 项目

1
mkdir my-mcp-server
2
cd my-mcp-server
3
supabase init

步骤 2:创建 MCP 服务器函数#

为您的 MCP 服务器创建一个新的边缘函数

1
supabase functions new mcp

用以下内容替换 supabase/functions/mcp/index.ts 的内容

supabase/functions/mcp/index.ts
1
// Setup type definitions for built-in Supabase Runtime APIs
2
import 'jsr:@supabase/functions-js/edge-runtime.d.ts'
3
4
import { McpServer } from 'npm:@modelcontextprotocol/sdk@1.25.3/server/mcp.js'
5
import { WebStandardStreamableHTTPServerTransport } from 'npm:@modelcontextprotocol/sdk@1.25.3/server/webStandardStreamableHttp.js'
6
import { Hono } from 'npm:hono@^4.9.7'
7
import { z } from 'npm:zod@^4.1.13'
8
9
// Create Hono app
10
const app = new Hono()
11
12
// Create your MCP server
13
const server = new McpServer({
14
name: 'mcp',
15
version: '0.1.0',
16
})
17
18
// Register a simple addition tool
19
server.registerTool(
20
'add',
21
{
22
title: 'Addition Tool',
23
description: 'Add two numbers together',
24
inputSchema: { a: z.number(), b: z.number() },
25
},
26
({ a, b }) => ({
27
content: [{ type: 'text', text: String(a + b) }],
28
})
29
)
30
31
// Handle MCP requests
32
app.all('*', async (c) => {
33
const transport = new WebStandardStreamableHTTPServerTransport()
34
await server.connect(transport)
35
return transport.handleRequest(c.req.raw)
36
})
37
38
Deno.serve(app.fetch)

步骤 3:本地测试#

启动 Supabase 本地开发堆栈

1
supabase start

在另一个终端中,提供您的函数

1
supabase functions serve --no-verify-jwt mcp

您的 MCP 服务器现在正在运行于

1
https://:54321/functions/v1/mcp

使用 curl 测试#

您也可以使用 curl 直接测试您的 MCP 服务器。调用 add 工具

1
curl -X POST 'https://:54321/functions/v1/mcp' \
2
-H 'Content-Type: application/json' \
3
-H 'Accept: application/json, text/event-stream' \
4
-d '{
5
"jsonrpc": "2.0",
6
"id": 1,
7
"method": "tools/call",
8
"params": {
9
"name": "add",
10
"arguments": {
11
"a": 5,
12
"b": 3
13
}
14
}
15
}'

预期响应

响应使用服务器发送事件 (SSE) 格式

1
event: message
2
data: {"result":{"content":[{"type":"text","text":"8"}]},"jsonrpc":"2.0","id":1}

使用 MCP Inspector 测试#

使用官方 MCP Inspector 测试您的服务器

1
npx -y @modelcontextprotocol/inspector

在 inspector UI 中使用本地端点 https://:54321/functions/v1/mcp 来探索可用的工具并交互式地测试它们。

步骤 4:部署到生产环境#

准备好部署时,链接您的项目并部署该函数

1
supabase link --project-ref <your-project-ref>
2
supabase functions deploy --no-verify-jwt mcp

您的 MCP 服务器将在

1
https://<your-project-ref>.supabase.co/functions/v1/mcp

更新您的 MCP 客户端配置以使用生产 URL。

示例#

您可以在此处找到现成的 MCP 服务器实现

资源#