用 mcp-lite 搭建 MCP 服务器
模型上下文协议(MCP)让大型语言模型(LLM)能够与外部工具和数据源互动。通过 mcp-lite,你可以构建运行在 Supabase Edge Functions 上的轻量级 MCP 服务器,让你的 AI 助手能够在边缘执行自定义工具。
🌐 The Model Context Protocol (MCP) enables Large Language Models (LLMs) to interact with external tools and data sources. With mcp-lite, you can build lightweight MCP servers that run on Supabase Edge Functions, giving your AI assistants the ability to execute custom tools at the edge.
本指南向你展示如何使用 mcp-lite 在 Supabase Edge Functions 上搭建、开发和部署 MCP 服务器。
🌐 This guide shows you how to scaffold, develop, and deploy an MCP server using mcp-lite on Supabase Edge Functions.
mcp-lite 是什么? #
🌐 What is mcp-lite?
mcp-lite 是一个轻量、零依赖的 TypeScript 框架,用来搭建 MCP 服务器。它可以在任何支持 Fetch API 的环境中运行,包括 Node、Bun、Cloudflare Workers、Deno 和 Supabase Edge Functions。
为什么选择 Supabase Edge Functions + mcp-lite? #
🌐 Why Supabase Edge Functions + mcp-lite?
这个组合有几个好处:
🌐 This combination offers several advantages:
- 零冷启动:边缘函数保持热度,实现快速响应
- 全球分发:一次部署,随处运行
- 直接数据库访问:直接连接到你的 Supabase Postgres
- 最小占用:mcp-lite 没有任何运行时依赖
- 完全类型安全:Deno 中的 TypeScript 支持
- 基础部署:一条命令即可上线
先决条件 #
🌐 Prerequisites
你需要:
🌐 You need:
- Docker(在本地运行 Supabase)
- Deno(Supabase Edge Functions 运行时)
- Supabase CLI
创建一个新的MCP服务器 #
🌐 Create a new MCP server
从 create-mcp-lite@0.3.0 开始,你可以搭建一个完整的 MCP 服务器,它可以在 Supabase Edge Functions 上运行:
🌐 Starting with create-mcp-lite@0.3.0, you can scaffold a complete MCP server that runs on Supabase Edge Functions:
1npm create mcp-lite@latest当提示时,从模板选项中选择 Supabase Edge Functions(MCP 服务器)。
🌐 When prompted, select Supabase Edge Functions (MCP server) from the template options.
这个模板为 Edge Functions 开发创建了一个集中的结构:
🌐 The template creates a focused structure for Edge Functions development:
1my-mcp-server/2├── supabase/3│ ├── config.toml # Minimal Supabase config (Edge Functions only)4│ └── functions/5│ └── mcp-server/6│ ├── index.ts # MCP server implementation7│ └── deno.json # Deno imports and configuration8├── package.json9└── tsconfig.json了解项目结构 #
🌐 Understanding the project structure
最小化的 config.toml #
🌐 Minimal config.toml
这个模板包含一个最小的 config.toml,只运行 Edge Functions——没有数据库、存储或 Studio 界面。这样本地设置会很轻量:
🌐 The template includes a minimal config.toml that runs only Edge Functions - no database, storage, or Studio UI. This keeps your local setup lightweight:
1# Minimal config for running only Edge Functions (no DB, storage, or studio)2project_id = "starter-mcp-supabase"3[api]4enabled = true5port = 5432167[edge_runtime]8enabled = true9policy = "per_worker"10deno_version = 2你随时可以根据需要添加更多服务。
🌐 You can always add more services as needed.
两个 Hono 应用模式 #
🌐 Two Hono apps pattern
这个模板使用了 Supabase Edge Functions 所需的特定模式:
🌐 The template uses a specific pattern required by Supabase Edge Functions:
1// Root handler - matches the function name2const app = new Hono()34// MCP protocol handler5const mcpApp = new Hono()67mcpApp.get('/', (c) => {8 return c.json({9 message: 'MCP Server on Supabase Edge Functions',10 endpoints: {11 mcp: '/mcp',12 health: '/health',13 },14 })15})1617mcpApp.all('/mcp', async (c) => {18 const response = await httpHandler(c.req.raw)19 return response20})2122// Mount at /mcp-server (the function name)23app.route('/mcp-server', mcpApp)这是必须的,因为 Supabase 会将所有请求路由到 /<function-name>/*。外层的 app 处理函数级别的路由,而 mcpApp 处理你实际的 MCP 端点。
Deno 导入映射 #
🌐 Deno import maps
这个模板在 deno.json 中使用 Deno 的导入映射来管理依赖:
🌐 The template uses Deno's import maps in deno.json to manage dependencies:
1{2 "compilerOptions": {3 "lib": ["deno.window", "deno.ns"],4 "strict": true5 },6 "imports": {7 "hono": "npm:hono@^4.6.14",8 "mcp-lite": "npm:mcp-lite@0.8.2",9 "zod": "npm:zod@^4.1.12"10 }11}这让你在保持 Deno 生态系统的同时访问 npm 包。
🌐 This gives you npm package access while staying in the Deno ecosystem.
本地开发 #
🌐 Local development
启动 Supabase #
🌐 Start Supabase
进入你的项目目录并启动 Supabase 服务:
🌐 Navigate to your project directory and start Supabase services:
1supabase start履行你的职责 #
🌐 Serve your function
在另一个终端,本地运行你的 MCP 函数:
🌐 In a separate terminal, serve your MCP function locally:
1supabase functions serve --no-verify-jwt mcp-server或者用 npm 脚本(它运行相同的命令):
🌐 Or use the npm script (which runs the same command):
1npm run dev你的 MCP 服务器可用地址是:
🌐 Your MCP server is available at:
1http://localhost:54321/functions/v1/mcp-server/mcp测试你的服务器 #
🌐 Testing your server
通过将 MCP 服务器添加到你的 Claude Code、Claude Desktop、Cursor 或你喜欢的 MCP 客户端来测试它。
🌐 Test the MCP server by adding it to your Claude Code, Claude Desktop, Cursor, or your preferred MCP client.
使用 Claude 代码:
🌐 Using Claude Code:
1claude mcp add my-mcp-server -t http http://localhost:54321/functions/v1/mcp-server/mcp你也可以用 MCP 检查器来测试它:
🌐 You can also test it using the MCP inspector:
1npx @modelcontextprotocol/inspector然后在检查器界面里添加 MCP 端点 URL。
🌐 Then add the MCP endpoint URL in the inspector UI.
它是怎么运作的 #
🌐 How it works
MCP 服务器的设置很简单:
🌐 The MCP server setup is straightforward:
1import { McpServer, StreamableHttpTransport } from 'mcp-lite'2import { z } from 'zod'34// Create MCP server instance5const mcp = new McpServer({6 name: 'starter-mcp-supabase-server',7 version: '1.0.0',8 schemaAdapter: (schema) => z.toJSONSchema(schema as z.ZodType),9})1011// Define a tool12mcp.tool('sum', {13 description: 'Adds two numbers together',14 inputSchema: z.object({15 a: z.number(),16 b: z.number(),17 }),18 handler: (args: { a: number; b: number }) => ({19 content: [{ type: 'text', text: String(args.a + args.b) }],20 }),21})2223// Bind to HTTP transport24const transport = new StreamableHttpTransport()25const httpHandler = transport.bind(mcp)添加更多工具 #
🌐 Adding more tools
通过将工具直接添加到 mcp 实例来扩展你的 MCP 服务器。下面是添加数据库搜索工具的例子:
🌐 Extend your MCP server by adding tools directly to the mcp instance. Here's an example of adding a database search tool:
1mcp.tool('searchDatabase', {2 description: 'Search your Supabase database',3 inputSchema: z.object({4 table: z.string(),5 query: z.string(),6 }),7 handler: async (args) => {8 // Access Supabase client here9 // const { data } = await supabase.from(args.table).select('*')10 return {11 content: [{ type: 'text', text: `Searching ${args.table}...` }],12 }13 },14})你可以添加能够:
🌐 You can add tools that:
- 查询你的 Supabase 数据库
- 访问 Supabase 存储进行文件操作
- 调用外部接口
- 用自定义逻辑处理数据
- 与其他 Supabase 功能整合
部署到生产环境 #
🌐 Deploy to production
准备好后,将其部署到 Supabase 的全球边缘网络:
🌐 When ready, deploy to Supabase's global edge network:
1supabase functions deploy --no-verify-jwt mcp-server或者使用 npm 脚本:
🌐 Or use the npm script:
1npm run deploy你的 MCP 服务器将上线于:
🌐 Your MCP server will be live at:
1https://your-project-ref.supabase.co/functions/v1/mcp-server/mcp身份验证注意事项 #
🌐 Authentication considerations
这个模板使用 --no-verify-jwt 来快速开发。这意味着 Supabase 的 JWT 层不会强制进行身份验证。
🌐 The template uses --no-verify-jwt for quick development. This means authentication is not enforced by Supabase's JWT layer.
在生产环境中,你应该在 MCP 服务器层实现身份验证,遵循 MCP 授权规范。这样你就可以控制谁可以访问你的 MCP 工具。
🌐 For production, you should implement authentication at the MCP server level following the MCP Authorization specification. This gives you control over who can access your MCP tools.
安全最佳实践 #
🌐 Security best practices
在部署 MCP 服务器时:
🌐 When deploying MCP servers:
- 不要暴露敏感数据:在开发环境中使用服务器,并使用非生产数据
- 实现身份验证:为生产环境部署添加适当的身份验证
- 验证输入:始终验证并清理工具输入
- 限制工具范围:仅开放你用得上的工具
- 监控使用情况:跟踪工具调用并监测异常活动
如需更多安全指导,请参阅 MCP 安全指南。
🌐 For more security guidance, see the MCP security guide.
接下来做什么 #
🌐 What's next
当你的 MCP 服务器在 Supabase Edge Functions 上运行时,你可以:
🌐 With your MCP server running on Supabase Edge Functions, you can:
- 把它连接到你的 Supabase 数据库,用于数据驱动的工具
- 使用 Supabase Auth 来保护你的端点
- 访问 Supabase 存储进行文件操作
- 自动部署到多个地区
- 扩展以处理生产流量
- 与 Claude、Cursor 或自定义 MCP 客户端等 AI 助手集成
资源 #
🌐 Resources