Skip to content
Home

部署 MCP 服务器

在 Supabase 上使用 Edge Functions 构建和部署 Model Context Protocol (MCP) 服务器。

🌐 Build and deploy Model Context Protocol (MCP) servers on Supabase using Edge Functions.

先决条件 #

🌐 Prerequisites

在开始之前,确保你有:

🌐 Before you begin, make sure you have:

部署你的 MCP 服务器 #

🌐 Deploy your MCP server

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

🌐 Step 1: Create a new project

先创建一个新的 Supabase 项目:

🌐 Start by creating a new Supabase project:

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

第二步:创建 MCP 服务器功能 #

🌐 Step 2: Create the MCP server function

为你的 MCP 服务器创建一个新的 Edge 功能:

🌐 Create a new Edge Function for your MCP server:

1
supabase functions new mcp

supabase/functions/mcp/index.ts 的内容替换为:

🌐 Replace the contents of supabase/functions/mcp/index.ts with:

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 an 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:本地测试 #

🌐 Step 3: Test locally

启动 Supabase 本地开发环境:

🌐 Start the Supabase local development stack:

1
supabase start

在另一个终端中,运行你的函数:

🌐 In a separate terminal, serve your function:

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

你的 MCP 服务器现在正在运行于:

🌐 Your MCP server is now running at:

1
http://localhost:54321/functions/v1/mcp

用 curl 测试 #

🌐 Test with curl

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

🌐 You can also test your MCP server directly with curl. Call the add tool:

1
curl -X POST 'http://localhost: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)格式:

🌐 The response uses Server-Sent Events (SSE) format:

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

用 MCP 检查器测试 #

🌐 Test with MCP Inspector

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

🌐 Test your server with the official MCP Inspector:

1
npx -y @modelcontextprotocol/inspector

在检查器界面中使用本地端点 http://localhost:54321/functions/v1/mcp 来浏览可用工具并进行交互式测试。

🌐 Use the local endpoint http://localhost:54321/functions/v1/mcp in the inspector UI to explore available tools and test them interactively.

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

🌐 Step 4: Deploy to production

当你准备好部署时,连接你的项目并部署函数:

🌐 When you're ready to deploy, link your project and deploy the function:

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

你的 MCP 服务器将在以下地址可用:

🌐 Your MCP server will be available at:

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

更新你的 MCP 客户端配置,使用生产环境的 URL。

🌐 Update your MCP client configuration to use the production URL.

示例 #

🌐 Examples

你可以在这里找到现成可用的MCP服务器实现:

🌐 You can find ready-to-use MCP server implementations here:

资源 #

🌐 Resources