MCP巢模块

@rekog-labs/MCP-Nest
0 Stars 355 次浏览 rekog-labs 更新于 2026-08-23

一个 NestJS 模块,允许将服务暴露为带有 Server-Sent Events 传输的 MCP 服务器,从而便于客户端发现和服务执行。

该服务暂未提供标准配置,请参考 README 手动接入

服务介绍

NestJS MCP 服务器模块

一个用于创建带有 Server-Sent Events (SSE) 传输的 MCP (Model Context Protocol) 服务器的 NestJS 模块。

特性

  • 🚀 用于流式传输和工具执行的 SSE 传输
  • 🔍 自动发现和注册 toolresource
  • 💯 基于 Zod 的请求验证
  • 📊 进度通知
  • 🔒 基于守卫的身份验证

安装

npm install @rekog/mcp-nest @modelcontextprotocol/sdk zod

快速开始

1. 导入模块

// app.module.ts
import { Module } from '@nestjs/common';
import { McpModule } from '@rekog/mcp-nest';
import { GreetingTool } from './greeting.tool';

@Module({
  imports: [
    McpModule.forRoot({
      name: 'my-mcp-server',
      version: '1.0.0',
    }),
  ],
  providers: [GreetingTool],
})
export class AppModule {}

2. 定义工具和资源

// greeting.tool.ts
import { Injectable } from '@nestjs/common';
import { Tool, Context } from '@rekog/mcp-nest';
import { z } from 'zod';
import { Progress } from '@modelcontextprotocol/sdk/types';

@Injectable()
export class GreetingTool {
  constructor() {}

  @Tool({
    name: 'hello-world',
    description:
      'Returns a greeting and simulates a long operation with progress updates',
    parameters: z.object({
      name: z.string().default('World'),
    }),
  })
  async sayHello({ name }, context: Context) {
    const greeting = `Hello, ${name}!`;

    const totalSteps = 5;
    for (let i = 0; i < totalSteps; i++) {
      await new Promise((resolve) => setTimeout(resolve, 500));

      // Send a progress update.
      await context.reportProgress({
        progress: (i + 1) * 20,
        total: 100,
      } as Progress);
    }

    return {
      content: [{ type: 'text', text: greeting }],
    };
  }

  @Resource({
    uri: 'mcp://hello-world/{userName}',
    name: 'Hello World',
    description: 'A simple greeting resource',
    mimeType: 'text/plain',
  })
  // Different from the SDK, we put the parameters and URI in the same object.
  async getCurrentSchema({ uri, userName }) {
    return {
      content: [
        {
          uri,
          text: `User is ${userName}`,
          mimeType: 'text/plain',
        },
      ],
    };
  }
}

完成!

API 端点

  • GET /sse: SSE 连接端点(如果配置了守卫则受保护)
  • POST /messages: 工具执行端点(如果配置了守卫则受保护)

提示

可以使用全局前缀来使用该模块,但推荐的方式是排除这些端点:

app.setGlobalPrefix('/api', { exclude: ['sse', 'messages'] });

身份验证

您可以使用标准的 NestJS 守卫来保护您的 MCP 端点。

1. 创建一个守卫

实现 CanActivate 接口。守卫应该处理请求验证(例如检查 JWT、API 密钥),并可选地将用户信息附加到请求对象上。

没有特别之处,请参阅 NestJS 文档以获取更多细节。

2. 应用守卫

将您的守卫传递给 McpModule.forRoot 配置。守卫将应用于 /sse/messages 两个端点。

// app.module.ts
import { Module } from '@nestjs/common';
import { McpModule } from '@rekog/mcp-nest';
import { GreetingTool } from './greeting.tool';
import { AuthGuard } from './auth.guard';

@Module({
  imports: [
    McpModule.forRoot({
      name: 'my-mcp-server',
      version: '1.0.0',
      guards: [AuthGuard], // Apply the guard here
    }),
  ],
  providers: [GreetingTool, AuthGuard], // Ensure the Guard is also provided
})
export class AppModule {}

就这样!其余部分与 NestJS 守卫相同。

相关 MCP 服务