关于 NestJS MCP Server Module
NestJS MCP Server Module 是用于通过 SSE 构建 MCP 服务的 NestJS 模块,支持自动注册工具和资源,并提供请求验证、进度通知及基于 NestJS Guard 的身份验证。
主要功能
- 01通过 SSE 传输并执行工具
- 02自动发现并注册工具和资源
- 03基于 Zod 的请求验证
- 04进度通知
- 05基于 NestJS Guard 的身份验证
适用场景
- 01在 NestJS 应用中构建 MCP 服务
- 02通过 SSE 暴露工具和资源
- 03使用 NestJS Guard 保护 MCP 端点
MCP 相关
它为客户端提供 MCP 服务,通过 `/sse` 建立 SSE 连接,并在 `/messages` 执行工具。模块会自动注册发现的工具和资源。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
暂未收录安装方式
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
还没有获取到工具列表
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
NestJS MCP 服务模块
一个用于创建基于服务器发送事件(SSE)传输的 MCP(Model Context Protocol)服务的 NestJS 模块。
功能
- 🚀 用于流式传输和工具执行的 SSE 传输
- 🔍 自动发现并注册
tool和resource - 💯 基于 Zod 的请求验证
- 📊 进度通知
- 🔒 基于 Guard 的身份验证
安装
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 连接端点(如果已配置,则受 Guard 保护)POST /messages:工具执行端点(如果已配置,则受 Guard 保护)
提示
可以在模块中使用全局前缀,但推荐通过以下方式排除这些端点:
app.setGlobalPrefix('/api', { exclude: ['sse', 'messages'] });
身份验证
你可以使用标准 NestJS Guard 保护 MCP 端点。
1. 创建 Guard
实现 CanActivate 接口。Guard 应处理请求验证(例如,检查 JWT、API 密钥),并可选择将用户信息附加到请求对象。
没有特别之处,更多详情请查看 NestJS 文档。
2. 应用 Guard
将 Guard 传递给 McpModule.forRoot 配置。Guard 将应用于 /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 Guard 相同。
评论
支持 Markdown
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。