nest-mcp-sse

nest-mcp-sse

好的,这句话翻译成中文是: **基于 Nest 快速开发 SSE MCP 服务器** 更详细的解释: * **基于 (jī yú):** Based on / Using * **Nest:** Nest (一个 Node.js 框架) * **快速开发 (kuài sù kāi fā):** Rapidly develop / Quickly develop * **SSE:** Server-Sent Events (服务器发送事件) * **MCP:** Mission Control Panel (任务控制面板) * **服务器 (fú wù qì):** Server 所以,这句话的意思是:使用 Nest 框架快速开发一个基于服务器发送事件的任务控制面板服务器。

HxinY499

开发者工具
访问服务器

README

nest-mcp-sse

这是一个用于在 NestJS 工程下快速开发 SSE 传输类型 McpServer 的动态模块,使用此模块,你将无需理会连接 Transport、生命周期管理、McpServer 实例化等杂事,聚焦于 MCP tool 的开发。

此模块没有过度封装🥳

只做了 MCP Server 实例管理和 SSE Transport 管理,没有对官方 sdk 的原生 MCP Server 的方法做任何封装,尽可能不增加学习成本。

功能特点

  • 快速在 NestJS 应用中创建 MCP 服务器
  • 支持多个 MCP Server 实例管理
  • 自动处理 SSE 连接生命周期

安装

# npm
npm install nest-mcp-sse @modelcontextprotocol/sdk zod

# yarn
yarn add nest-mcp-sse @modelcontextprotocol/sdk zod

# pnpm
pnpm add nest-mcp-sse @modelcontextprotocol/sdk zod

用法

1. 导入模块

在你的模块中导入 McpModule

import { Module } from "@nestjs/common";
import { McpModule } from "nest-mcp-sse";

@Module({
  imports: [
    McpModule.register({
      controllerBaseUrl: "api/mcp",
      mcpServerConfigs: [
        {
          serverId: "my-mcp-server",
          serverInfo: {
            name: "my-mcp-server",
            version: "1.0.0",
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

另外,你也可以通过forRootforFeature导入模块,区别如下:

  • forRoot:通常在根模块注册
  • forFeature:和 forRoot 注册的模块共享 Service 实例
  • register:用一次注册一次,不和任何模块共享

2. 操作 McpServer

创建 MCP Server 之后,你可以通过getServer方法获取到官方 sdk 的原生 MCP Server 实例,这个实例没有任何封装,完全是原生的。

比如,你可以注册工具

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    this.mcpServerService.getServer("server-id")?.tool(
      "get-current-time",
      {
        format: z
          .enum(["iso", "locale", "unix"])
          .optional()
          .default("iso")
          .describe("时间格式: iso, locale, 或 unix"),
      },
      async ({ format }: { format: "iso" | "locale" | "unix" }) => {
        const now = new Date();
        let timeValue: string | number;

        switch (format) {
          case "iso":
            timeValue = now.toISOString();
            break;
          case "locale":
            timeValue = now.toLocaleString();
            break;
          case "unix":
            timeValue = now.getTime();
            break;
          default:
            timeValue = now.toISOString();
        }

        return {
          content: [{ type: "text", text: String(timeValue) }],
        };
      }
    );
  }
}

3. 手动注册服务器实例

上面的例子直接在McpModule.register中传入了mcpServerConfigs,这会在模块初始化时就创建 MCP Server。

当然这不是必需的,你可以不传入mcpServerConfigs,在 Controller 或 Service 中手动注册 MCP Server:

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    // 手动注册一个MCP Server实例
    this.mcpServerService.registerServer({
      serverId: "another-server",
      serverInfo: {
        name: "another-mcp-server",
        version: "1.0.0",
      },
    });
  }
}

服务器访问端点

注册模块后,将自动创建以下端点:

  • SSE 连接端点: /{controllerBaseUrl}/{serverId}/sse
  • 消息处理端点: /{controllerBaseUrl}/{serverId}/messages

例如,如果你使用 controllerBaseUrl: 'api/mcp'serverId: 'my-mcp-server',则访问 URL 为:

  • http://localhost:3000/api/mcp/my-mcp-server/sse - 用于 SSE 连接
  • http://localhost:3000/api/mcp/my-mcp-server/messages - 用于消息处理

API

McpModule.register

  • controllerBaseUrl:MCP Controller 的基本 URL 路径
  • sseEndpoint:SSE 连接端点名称,默认为 sse
  • messagesEndpoint:消息处理端点名称,默认为 messages
  • mcpServerConfigs

forRoot 同 register,forFeature 只接收 mcpServerConfigs

McpServerService

McpModule 导出的 Service,用于管理多个 McpServer 实例。

你可以注入到你的 Controller 或 Service 中,使用以下方法:

registerServer

  • 作用:注册一个 McpServer 实例
  • 参数:同McpModule.forRootmcpServerConfigs
  • 返回值:注册的 McpServer 实例

getServer

  • 作用:获取指定 McpServer 实例
  • 参数:serverId
  • 返回值:McpServer 实例

getServerIds

  • 作用:获取所有已注册的 McpServer 实例 id
  • 返回值:string[] 所有已注册的 McpServer 实例 id 数组

hasServer

  • 作用:判断是否已注册指定 McpServer 实例
  • 参数:serverId
  • 返回值:boolean

connect

  • 作用:连接 McpServer 到传输层,一般不需要使用,除非你希望手动管理连接。
  • 参数:
    • serverId
    • transport,Transport 实例
  • 返回值:void

McpTransportService

McpModule 导出的 Service,用于管理多个 SSEServerTransport 实例。

通常不需要使用,除非你希望手动管理连接。

以下仅列出可能使用到的方法,更多方法请参阅源码

getTransport

  • 作用:获取特定传输连接
  • 参数:serverId,sessionId
  • 返回值:SSEServerTransport 实例

getActiveSessionIds

  • 作用:获取 McpServer 的所有活跃会话 ID
  • 参数:serverId
  • 返回值:string[] 所有活跃会话 ID 数组

高级配置

自定义端点名称

你可以在模块注册时自定义 SSE 和消息端点名称:

McpModule.register({
  controllerBaseUrl: "api/mcp",
  sseEndpoint: "connect", // 默认为 'sse'
  messagesEndpoint: "rpc", // 默认为 'messages'
  mcpServerConfigs: [
    /* ... */
  ],
});

示例代码

你可以 clone 该仓库,按以下步骤运行示例代码

pnpm install

cd example

pnpm install

pnpm start
# nest-mcp-sse

这是一个用于在 NestJS 工程下快速开发 SSE 传输类型 McpServer 的动态模块,使用此模块,你将无需理会连接 Transport、生命周期管理、McpServer 实例化等杂事,聚焦于 MCP tool 的开发。

**此模块没有过度封装**🥳

只做了 MCP Server 实例管理和 SSE Transport 管理,没有对官方 sdk 的原生 MCP Server 的方法做任何封装,尽可能不增加学习成本。

## 功能特点

- 快速在 NestJS 应用中创建 MCP 服务器
- 支持多个 MCP Server 实例管理
- 自动处理 SSE 连接生命周期

## 安装

```bash
# npm
npm install nest-mcp-sse @modelcontextprotocol/sdk zod

# yarn
yarn add nest-mcp-sse @modelcontextprotocol/sdk zod

# pnpm
pnpm add nest-mcp-sse @modelcontextprotocol/sdk zod

用法

1. 导入模块

在你的模块中导入 McpModule

import { Module } from "@nestjs/common";
import { McpModule } from "nest-mcp-sse";

@Module({
  imports: [
    McpModule.register({
      controllerBaseUrl: "api/mcp",
      mcpServerConfigs: [
        {
          serverId: "my-mcp-server",
          serverInfo: {
            name: "my-mcp-server",
            version: "1.0.0",
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

另外,你也可以通过forRootforFeature导入模块,区别如下:

  • forRoot:通常在根模块注册
  • forFeature:和 forRoot 注册的模块共享 Service 实例
  • register:用一次注册一次,不和任何模块共享

2. 操作 McpServer

创建 MCP Server 之后,你可以通过getServer方法获取到官方 sdk 的原生 MCP Server 实例,这个实例没有任何封装,完全是原生的。

比如,你可以注册工具

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    this.mcpServerService.getServer("server-id")?.tool(
      "get-current-time",
      {
        format: z
          .enum(["iso", "locale", "unix"])
          .optional()
          .default("iso")
          .describe("时间格式: iso, locale, 或 unix"),
      },
      async ({ format }: { format: "iso" | "locale" | "unix" }) => {
        const now = new Date();
        let timeValue: string | number;

        switch (format) {
          case "iso":
            timeValue = now.toISOString();
            break;
          case "locale":
            timeValue = now.toLocaleString();
            break;
          case "unix":
            timeValue = now.getTime();
            break;
          default:
            timeValue = now.toISOString();
        }

        return {
          content: [{ type: "text", text: String(timeValue) }],
        };
      }
    );
  }
}

3. 手动注册服务器实例

上面的例子直接在McpModule.register中传入了mcpServerConfigs,这会在模块初始化时就创建 MCP Server。

当然这不是必需的,你可以不传入mcpServerConfigs,在 Controller 或 Service 中手动注册 MCP Server:

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    // 手动注册一个MCP Server实例
    this.mcpServerService.registerServer({
      serverId: "another-server",
      serverInfo: {
        name: "another-mcp-server",
        version: "1.0.0",
      },
    });
  }
}

服务器访问端点

注册模块后,将自动创建以下端点:

  • SSE 连接端点: /{controllerBaseUrl}/{serverId}/sse
  • 消息处理端点: /{controllerBaseUrl}/{serverId}/messages

例如,如果你使用 controllerBaseUrl: 'api/mcp'serverId: 'my-mcp-server',则访问 URL 为:

  • http://localhost:3000/api/mcp/my-mcp-server/sse - 用于 SSE 连接
  • http://localhost:3000/api/mcp/my-mcp-server/messages - 用于消息处理

API

McpModule.register

  • controllerBaseUrl:MCP Controller 的基本 URL 路径
  • sseEndpoint:SSE 连接端点名称,默认为 sse
  • messagesEndpoint:消息处理端点名称,默认为 messages
  • mcpServerConfigs

forRoot 同 register,forFeature 只接收 mcpServerConfigs

McpServerService

McpModule 导出的 Service,用于管理多个 McpServer 实例。

你可以注入到你的 Controller 或 Service 中,使用以下方法:

registerServer

  • 作用:注册一个 McpServer 实例
  • 参数:同McpModule.forRootmcpServerConfigs
  • 返回值:注册的 McpServer 实例

getServer

  • 作用:获取指定 McpServer 实例
  • 参数:serverId
  • 返回值:McpServer 实例

getServerIds

  • 作用:获取所有已注册的 McpServer 实例 id
  • 参数:无
  • 返回值:string[] 所有已注册的 McpServer 实例 id 数组

hasServer

  • 作用:判断是否已注册指定 McpServer 实例
  • 参数:serverId
  • 返回值:boolean

connect

  • 作用:连接 McpServer 到传输层,一般不需要使用,除非你希望手动管理连接。
  • 参数:
    • serverId
    • transport,Transport 实例
  • 返回值:void

McpTransportService

McpModule 导出的 Service,用于管理多个 SSEServerTransport 实例。

通常不需要使用,除非你希望手动管理连接。

以下仅列出可能使用到的方法,更多方法请参阅源码

getTransport

  • 作用:获取特定传输连接
  • 参数:serverId,sessionId
  • 返回值:SSEServerTransport 实例

getActiveSessionIds

  • 作用:获取 McpServer 的所有活跃会话 ID
  • 参数:serverId
  • 返回值:string[] 所有活跃会话 ID 数组

高级配置

自定义端点名称

你可以在模块注册时自定义 SSE 和消息端点名称:

McpModule.register({
  controllerBaseUrl: "api/mcp",
  sseEndpoint: "connect", // 默认为 'sse'
  messagesEndpoint: "rpc", // 默认为 'messages'
  mcpServerConfigs: [
    /* ... */
  ],
});

示例代码

你可以 clone 该仓库,按以下步骤运行示例代码

pnpm install

cd example

pnpm install

pnpm start
# nest-mcp-sse

这是一个用于在 NestJS 工程下快速开发 SSE 传输类型 McpServer 的动态模块,使用此模块,你将无需理会连接 Transport、生命周期管理、McpServer 实例化等杂事,聚焦于 MCP tool 的开发。

**此模块没有过度封装**🥳

只做了 MCP Server 实例管理和 SSE Transport 管理,没有对官方 sdk 的原生 MCP Server 的方法做任何封装,尽可能不增加学习成本。

## 功能特点

- 快速在 NestJS 应用中创建 MCP 服务器
- 支持多个 MCP Server 实例管理
- 自动处理 SSE 连接生命周期

## 安装

```bash
# npm
npm install nest-mcp-sse @modelcontextprotocol/sdk zod

# yarn
yarn add nest-mcp-sse @modelcontextprotocol/sdk zod

# pnpm
pnpm add nest-mcp-sse @modelcontextprotocol/sdk zod

用法

1. 导入模块

在你的模块中导入 McpModule

import { Module } from "@nestjs/common";
import { McpModule } from "nest-mcp-sse";

@Module({
  imports: [
    McpModule.register({
      controllerBaseUrl: "api/mcp",
      mcpServerConfigs: [
        {
          serverId: "my-mcp-server",
          serverInfo: {
            name: "my-mcp-server",
            version: "1.0.0",
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

另外,你也可以通过forRootforFeature导入模块,区别如下:

  • forRoot:通常在根模块注册
  • forFeature:和 forRoot 注册的模块共享 Service 实例
  • register:用一次注册一次,不和任何模块共享

2. 操作 McpServer

创建 MCP Server 之后,你可以通过getServer方法获取到官方 sdk 的原生 MCP Server 实例,这个实例没有任何封装,完全是原生的。

比如,你可以注册工具

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    this.mcpServerService.getServer("server-id")?.tool(
      "get-current-time",
      {
        format: z
          .enum(["iso", "locale", "unix"])
          .optional()
          .default("iso")
          .describe("时间格式: iso, locale, 或 unix"),
      },
      async ({ format }: { format: "iso" | "locale" | "unix" }) => {
        const now = new Date();
        let timeValue: string | number;

        switch (format) {
          case "iso":
            timeValue = now.toISOString();
            break;
          case "locale":
            timeValue = now.toLocaleString();
            break;
          case "unix":
            timeValue = now.getTime();
            break;
          default:
            timeValue = now.toISOString();
        }

        return {
          content: [{ type: "text", text: String(timeValue) }],
        };
      }
    );
  }
}

3. 手动注册服务器实例

上面的例子直接在McpModule.register中传入了mcpServerConfigs,这会在模块初始化时就创建 MCP Server。

当然这不是必需的,你可以不传入mcpServerConfigs,在 Controller 或 Service 中手动注册 MCP Server:

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    // 手动注册一个MCP Server实例
    this.mcpServerService.registerServer({
      serverId: "another-server",
      serverInfo: {
        name: "another-mcp-server",
        version: "1.0.0",
      },
    });
  }
}

服务器访问端点

注册模块后,将自动创建以下端点:

  • SSE 连接端点: /{controllerBaseUrl}/{serverId}/sse
  • 消息处理端点: /{controllerBaseUrl}/{serverId}/messages

例如,如果你使用 controllerBaseUrl: 'api/mcp'serverId: 'my-mcp-server',则访问 URL 为:

  • http://localhost:3000/api/mcp/my-mcp-server/sse - 用于 SSE 连接
  • http://localhost:3000/api/mcp/my-mcp-server/messages - 用于消息处理

API

McpModule.register

  • controllerBaseUrl:MCP Controller 的基本 URL 路径
  • sseEndpoint:SSE 连接端点名称,默认为 sse
  • messagesEndpoint:消息处理端点名称,默认为 messages
  • mcpServerConfigs

forRoot 同 register,forFeature 只接收 mcpServerConfigs

McpServerService

McpModule 导出的 Service,用于管理多个 McpServer 实例。

你可以注入到你的 Controller 或 Service 中,使用以下方法:

registerServer

  • 作用:注册一个 McpServer 实例
  • 参数:同McpModule.forRootmcpServerConfigs
  • 返回值:注册的 McpServer 实例

getServer

  • 作用:获取指定 McpServer 实例
  • 参数:serverId
  • 返回值:McpServer 实例

getServerIds

  • 作用:获取所有已注册的 McpServer 实例 id
  • 参数:serverId
  • 返回值:string[] 所有已注册的 McpServer 实例 id 数组

hasServer

  • 作用:判断是否已注册指定 McpServer 实例
  • 参数:serverId
  • 返回值:boolean

connect

  • 作用:连接 McpServer 到传输层,一般不需要使用,除非你希望手动管理连接。
  • 参数:
    • serverId
    • transport,Transport 实例
  • 返回值:void

McpTransportService

McpModule 导出的 Service,用于管理多个 SSEServerTransport 实例。

通常不需要使用,除非你希望手动管理连接。

以下仅列出可能使用到的方法,更多方法请参阅源码

getTransport

  • 作用:获取特定传输连接
  • 参数:serverId,sessionId
  • 返回值:SSEServerTransport 实例

getActiveSessionIds

  • 作用:获取 McpServer 的所有活跃会话 ID
  • 参数:serverId
  • 返回值:string[] 所有活跃会话 ID 数组

高级配置

自定义端点名称

你可以在模块注册时自定义 SSE 和消息端点名称:

McpModule.register({
  controllerBaseUrl: "api/mcp",
  sseEndpoint: "connect", // 默认为 'sse'
  messagesEndpoint: "rpc", // 默认为 'messages'
  mcpServerConfigs: [
    /* ... */
  ],
});

示例代码

你可以 clone 该仓库,按以下步骤运行示例代码

pnpm install

cd example

pnpm install

pnpm start
# nest-mcp-sse

这是一个用于在 NestJS 工程下快速开发 SSE 传输类型 McpServer 的动态模块,使用此模块,你将无需理会连接 Transport、生命周期管理、McpServer 实例化等杂事,聚焦于 MCP tool 的开发。

**此模块没有过度封装**🥳

只做了 MCP Server 实例管理和 SSE Transport 管理,没有对官方 sdk 的原生 MCP Server 的方法做任何封装,尽可能不增加学习成本。

## 功能特点

- 快速在 NestJS 应用中创建 MCP 服务器
- 支持多个 MCP Server 实例管理
- 自动处理 SSE 连接生命周期

## 安装

```bash
# npm
npm install nest-mcp-sse @modelcontextprotocol/sdk zod

# yarn
yarn add nest-mcp-sse @modelcontextprotocol/sdk zod

# pnpm
pnpm add nest-mcp-sse @modelcontextprotocol/sdk zod

用法

1. 导入模块

在你的模块中导入 McpModule

import { Module } from "@nestjs/common";
import { McpModule } from "nest-mcp-sse";

@Module({
  imports: [
    McpModule.register({
      controllerBaseUrl: "api/mcp",
      mcpServerConfigs: [
        {
          serverId: "my-mcp-server",
          serverInfo: {
            name: "my-mcp-server",
            version: "1.0.0",
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

另外,你也可以通过forRootforFeature导入模块,区别如下:

  • forRoot:通常在根模块注册
  • forFeature:和 forRoot 注册的模块共享 Service 实例
  • register:用一次注册一次,不和任何模块共享

2. 操作 McpServer

创建 MCP Server 之后,你可以通过getServer方法获取到官方 sdk 的原生 MCP Server 实例,这个实例没有任何封装,完全是原生的。

比如,你可以注册工具

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    this.mcpServerService.getServer("server-id")?.tool(
      "get-current-time",
      {
        format: z
          .enum(["iso", "locale", "unix"])
          .optional()
          .default("iso")
          .describe("时间格式: iso, locale, 或 unix"),
      },
      async ({ format }: { format: "iso" | "locale" | "unix" }) => {
        const now = new Date();
        let timeValue: string | number;

        switch (format) {
          case "iso":
            timeValue = now.toISOString();
            break;
          case "locale":
            timeValue = now.toLocaleString();
            break;
          case "unix":
            timeValue = now.getTime();
            break;
          default:
            timeValue = now.toISOString();
        }

        return {
          content: [{ type: "text", text: String(timeValue) }],
        };
      }
    );
  }
}

3. 手动注册服务器实例

上面的例子直接在McpModule.register中传入了mcpServerConfigs,这会在模块初始化时就创建 MCP Server。

当然这不是必需的,你可以不传入mcpServerConfigs,在 Controller 或 Service 中手动注册 MCP Server:

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    // 手动注册一个MCP Server实例
    this.mcpServerService.registerServer({
      serverId: "another-server",
      serverInfo: {
        name: "another-mcp-server",
        version: "1.0.0",
      },
    });
  }
}

服务器访问端点

注册模块后,将自动创建以下端点:

  • SSE 连接端点: /{controllerBaseUrl}/{serverId}/sse
  • 消息处理端点: /{controllerBaseUrl}/{serverId}/messages

例如,如果你使用 controllerBaseUrl: 'api/mcp'serverId: 'my-mcp-server',则访问 URL 为:

  • http://localhost:3000/api/mcp/my-mcp-server/sse - 用于 SSE 连接
  • http://localhost:3000/api/mcp/my-mcp-server/messages - 用于消息处理

API

McpModule.register

  • controllerBaseUrl:MCP Controller 的基本 URL 路径
  • sseEndpoint:SSE 连接端点名称,默认为 sse
  • messagesEndpoint:消息处理端点名称,默认为 messages
  • mcpServerConfigs

forRoot 同 register,forFeature 只接收 mcpServerConfigs

McpServerService

McpModule 导出的 Service,用于管理多个 McpServer 实例。

你可以注入到你的 Controller 或 Service 中,使用以下方法:

registerServer

  • 作用:注册一个 McpServer 实例
  • 参数:同McpModule.forRootmcpServerConfigs
  • 返回值:注册的 McpServer 实例

getServer

  • 作用:获取指定 McpServer 实例
  • 参数:serverId
  • 返回值:McpServer 实例

getServerIds

  • 作用:获取所有已注册的 McpServer 实例 id
  • 参数:serverId
  • 返回值:string[] 所有已注册的 McpServer 实例 id 数组

hasServer

  • 作用:判断是否已注册指定 McpServer 实例
  • 参数:serverId
  • 返回值:boolean

connect

  • 作用:连接 McpServer 到传输层,一般不需要使用,除非你希望手动管理连接。
  • 参数:
    • serverId
    • transport,Transport 实例
  • 返回值:void

McpTransportService

McpModule 导出的 Service,用于管理多个 SSEServerTransport 实例。

通常不需要使用,除非你希望手动管理连接。

以下仅列出可能使用到的方法,更多方法请参阅源码

getTransport

  • 作用:获取特定传输连接
  • 参数:serverId,sessionId
  • 返回值:SSEServerTransport 实例

getActiveSessionIds

  • 作用:获取 McpServer 的所有活跃会话 ID
  • 参数:serverId
  • 返回值:string[] 所有活跃会话 ID 数组

高级配置

自定义端点名称

你可以在模块注册时自定义 SSE 和消息端点名称:

McpModule.register({
  controllerBaseUrl: "api/mcp",
  sseEndpoint: "connect", // 默认为 'sse'
  messagesEndpoint: "rpc", // 默认为 'messages'
  mcpServerConfigs: [
    /* ... */
  ],
});

示例代码

你可以 clone 该仓库,按以下步骤运行示例代码

pnpm install

cd example

pnpm install

pnpm start
# nest-mcp-sse

这是一个用于在 NestJS 工程下快速开发 SSE 传输类型 McpServer 的动态模块,使用此模块,你将无需理会连接 Transport、生命周期管理、McpServer 实例化等杂事,聚焦于 MCP tool 的开发。

**此模块没有过度封装**🥳

只做了 MCP Server 实例管理和 SSE Transport 管理,没有对官方 sdk 的原生 MCP Server 的方法做任何封装,尽可能不增加学习成本。

## 功能特点

- 快速在 NestJS 应用中创建 MCP 服务器
- 支持多个 MCP Server 实例管理
- 自动处理 SSE 连接生命周期

## 安装

```bash
# npm
npm install nest-mcp-sse @modelcontextprotocol/sdk zod

# yarn
yarn add nest-mcp-sse @modelcontextprotocol/sdk zod

# pnpm
pnpm add nest-mcp-sse @modelcontextprotocol/sdk zod

用法

1. 导入模块

在你的模块中导入 McpModule

import { Module } from "@nestjs/common";
import { McpModule } from "nest-mcp-sse";

@Module({
  imports: [
    McpModule.register({
      controllerBaseUrl: "api/mcp",
      mcpServerConfigs: [
        {
          serverId: "my-mcp-server",
          serverInfo: {
            name: "my-mcp-server",
            version: "1.0.0",
          },
        },
      ],
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

另外,你也可以通过forRootforFeature导入模块,区别如下:

  • forRoot:通常在根模块注册
  • forFeature:和 forRoot 注册的模块共享 Service 实例
  • register:用一次注册一次,不和任何模块共享

2. 操作 McpServer

创建 MCP Server 之后,你可以通过getServer方法获取到官方 sdk 的原生 MCP Server 实例,这个实例没有任何封装,完全是原生的。

比如,你可以注册工具

import { Controller } from "@nestjs/common";
import { McpServerService } from "nest-mcp-sse";
import { z } from "zod";

@Controller()
export class AppController {
  constructor(private readonly mcpServerService: McpServerService) {
    this.mcpServerService.getServer("server-id")?.tool(
      "get-current-time",
      {
        format: z
          .enum(["iso", "locale", "unix"])
          .optional()
          .default("iso")
          .describe("时间格式: iso, locale, 或 unix"),
      },
      async ({ format }: { format: "iso" | "locale" | "unix" }) => {
        const now = new Date();
        let timeValue: string | number;

        switch (format) {
          case "iso":
            timeValue = now.toISOString();
            break;
          case "locale":
            timeValue = now.toLocaleString();
            break;
          case "unix":
            timeValue = now.getTime();
            break;
          default:
            timeValue = now.toISOString();
        }

        return {
          content: [{ type: "text", text: String(timeValue) }],
        };
      }
    );
  }
}

3. 手动注册服务器实例

上面的例子直接在McpModule.register中传入了mcpServerConfigs,这会在模块初始化时就创建 MCP Server。

当然这不是必需的,你可以不传入mcpServerConfigs,在 Controller 或 Service 中手动注册 MCP Server:

import { Controller } from "@nestjs/common";
import { McpServerService }

推荐服务器

Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
MCP Package Docs Server

MCP Package Docs Server

促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。

精选
本地
TypeScript
Claude Code MCP

Claude Code MCP

一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。

精选
本地
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。

精选
本地
JavaScript
mermaid-mcp-server

mermaid-mcp-server

一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。

精选
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

精选
TypeScript
Linear MCP Server

Linear MCP Server

一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

精选
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Curri MCP Server

Curri MCP Server

通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。

官方
本地
JavaScript