MCP Lambda Server

MCP Lambda Server

允许Lambda作为MCP服务器运行的Lambda层。

简介

@markvp/mcp-lambda-layer 是一个 Node.js 包,使您能够在 AWS Lambda 上运行支持 SSE 的 MCP(模型上下文协议)服务器。

安装

npm install @markvp/mcp-lambda-layer

基本使用

  1. 创建 Lambda 函数并导入包:
import { MCPHandlerFactory } from '@markvp/mcp-lambda-layer';
import { z } from 'zod';

// 配置 MCP 处理器
const factory = new MCPHandlerFactory({
  tools: {
    summarize: {
      params: {
        text: z.string(),
      },
      handler: async ({ text }) => {
        // 实现您的摘要逻辑
        const summary = await yourSummarizeImplementation(text);
        return {
          content: [{ type: 'text', text: summary }],
        };
      },
    },
  },
  prompts: {
    generate: {
      description: '基于提示生成内容',
      handler: async extra => {
        // 实现您的生成逻辑
        const result = await yourGenerateImplementation(extra.prompt);
        return {
          content: [{ type: 'text', text: result }],
        };
      },
    },
  },
});

// 导出处理器
export const handler = factory.getHandler();

Lambda 配置要求

  • 运行时: Node.js 18.x+
  • 处理程序: index.handler
  • 函数 URL: 必须启用,且需开启响应流
  • 内存: 至少 128MB
  • 超时: 建议 120秒

⚠️ 重要注意事项

  • 响应流仅适用于函数 URL,不支持 API Gateway 或 ALB
  • 官方仅支持 Node.js 运行时用于响应流
  • 该包自动处理 CORS 和 HTTP 方法验证

功能概述

  • 使 MCP TypeScript SDK 与 AWS Lambda 兼容
  • 通过 Lambda 响应流支持 SSE(服务器发送事件)
  • 内置 CORS 和 HTTP 方法处理
  • 完整 TypeScript 支持

许可证

MIT