部署 MCP 服务器
在 Supabase 上使用 Edge Functions 构建和部署 Model Context Protocol (MCP) 服务器。
🌐 Build and deploy Model Context Protocol (MCP) servers on Supabase using Edge Functions.
本指南涵盖不需要认证的 MCP 服务器。Edge Functions 上的 MCP 认证支持即将推出。
🌐 This guide covers MCP servers that do not require authentication. Auth support for MCP on Edge Functions is coming soon.
先决条件 #
🌐 Prerequisites
在开始之前,确保你有:
🌐 Before you begin, make sure you have:
- 安装并运行了 Docker 或兼容的运行时(本地开发必需)
- Deno 已安装(Supabase Edge Functions 运行时)
- Supabase CLI 已安装并已认证
- Node.js 20 或更高版本(Supabase CLI 所需)
部署你的 MCP 服务器 #
🌐 Deploy your MCP server
第1步:创建一个新项目 #
🌐 Step 1: Create a new project
先创建一个新的 Supabase 项目:
🌐 Start by creating a new Supabase project:
1mkdir my-mcp-server2cd my-mcp-server3supabase init完成这一步后,你应该有一个项目目录,其中有一个包含 config.toml 的 supabase 文件夹,以及一个空的 functions 目录。
🌐 After this step, you should have a project directory with a supabase folder containing config.toml and an empty functions directory.
第二步:创建 MCP 服务器功能 #
🌐 Step 2: Create the MCP server function
为你的 MCP 服务器创建一个新的 Edge 功能:
🌐 Create a new Edge Function for your MCP server:
1supabase functions new mcp本教程使用官方的 MCP TypeScript SDK(链接)和 WebStandardStreamableHTTPServerTransport,但你也可以使用任何兼容 Edge Runtime 的 MCP 框架,比如 mcp-lite 或 mcp-handler。
🌐 This tutorial uses the official MCP TypeScript SDK with the WebStandardStreamableHTTPServerTransport, but you can use any MCP framework that's compatible with the Edge Runtime, such as mcp-lite or mcp-handler.
把 supabase/functions/mcp/index.ts 的内容替换为:
🌐 Replace the contents of supabase/functions/mcp/index.ts with:
1// Setup type definitions for built-in Supabase Runtime APIs2import 'jsr:@supabase/functions-js/edge-runtime.d.ts'34import { McpServer } from 'npm:@modelcontextprotocol/sdk@1.25.3/server/mcp.js'5import { WebStandardStreamableHTTPServerTransport } from 'npm:@modelcontextprotocol/sdk@1.25.3/server/webStandardStreamableHttp.js'6import { Hono } from 'npm:hono@^4.9.7'7import { z } from 'npm:zod@^4.1.13'89// Create Hono app10const app = new Hono()1112// Create your MCP server13const server = new McpServer({14 name: 'mcp',15 version: '0.1.0',16})1718// Register an addition tool19server.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)3031// Handle MCP requests32app.all('*', async (c) => {33 const transport = new WebStandardStreamableHTTPServerTransport()34 await server.connect(transport)35 return transport.handleRequest(c.req.raw)36})3738Deno.serve(app.fetch)完成这一步后,你应该会在 supabase/functions/mcp/index.ts 有一个新文件。
🌐 After this step, you should have a new file at supabase/functions/mcp/index.ts.
在 Edge Functions 中,路径会以函数名为前缀。如果你的函数名字不是 mcp,请使用基础路径来配置 Hono:new Hono().basePath('/your-function-name')。
🌐 Within Edge Functions, paths are prefixed with the function name. If your function is named something other than mcp, configure Hono with a base path: new Hono().basePath('/your-function-name').
步骤3:本地测试 #
🌐 Step 3: Test locally
启动 Supabase 本地开发环境:
🌐 Start the Supabase local development stack:
1supabase start在另一个终端中,运行你的函数:
🌐 In a separate terminal, serve your function:
1supabase functions serve --no-verify-jwt mcp你的 MCP 服务器现在正在运行于:
🌐 Your MCP server is now running at:
1http://localhost:54321/functions/v1/mcp--no-verify-jwt 标志会在 Edge Function 层禁用 JWT 验证,这样你的 MCP 服务器就可以接受未认证的请求。受认证的 MCP 支持即将推出。
🌐 The --no-verify-jwt flag disables JWT verification at the Edge Function layer so your MCP server can accept unauthenticated requests. Authenticated MCP support is coming soon.
用 curl 测试 #
🌐 Test with curl
你也可以直接用 curl 测试你的 MCP 服务器。调用 add 工具:
🌐 You can also test your MCP server directly with curl. Call the add tool:
1curl -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": 313 }14 }15 }'MCP 可流式 HTTP 传输需要 Accept: application/json, text/event-stream 头部来表明客户端支持 JSON 和服务器发送事件的响应。
🌐 The MCP Streamable HTTP transport requires the Accept: application/json, text/event-stream header to indicate the client supports both JSON and Server-Sent Events responses.
预期回应:
这个响应使用了服务器推送事件(SSE)格式:
🌐 The response uses Server-Sent Events (SSE) format:
1event: message2data: {"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:
1npx -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.
完成这一步后,你的 MCP 服务器应该已经在本地运行,并且可以在 MCP 检查器中测试 add 工具了。
🌐 After this step, you should have your MCP server running locally and be able to test the add tool in the MCP Inspector.
步骤4:部署到生产环境 #
🌐 Step 4: Deploy to production
当你准备好部署时,连接你的项目并部署函数:
🌐 When you're ready to deploy, link your project and deploy the function:
1supabase link --project-ref <your-project-ref>2supabase functions deploy --no-verify-jwt mcp你的 MCP 服务器将在以下地址可用:
🌐 Your MCP server will be available at:
1https://<your-project-ref>.supabase.co/functions/v1/mcp更新你的 MCP 客户端配置,使用生产环境的 URL。
🌐 Update your MCP client configuration to use the production URL.
完成这一步之后,你就拥有了一个可以从任何地方访问的完整部署的MCP服务器。你可以使用MCP Inspector和你的生产URL来测试它。
🌐 After this step, you have a fully deployed MCP server accessible from anywhere. You can test it using the MCP Inspector with your production URL.
示例 #
🌐 Examples
你可以在这里找到现成可用的MCP服务器实现:
🌐 You can find ready-to-use MCP server implementations here:
- 简单 MCP 服务器 - 无需认证示例
资源 #
🌐 Resources