NestJS MCP Module
@bamada
NestJS module for seamless Model Context Protocol (MCP) server integration using decorators.
Overview
What is NestJS MCP Module?
A NestJS module that simplifies building Model Context Protocol (MCP) servers. It exposes resources, tools, and prompts using familiar NestJS decorators and supports STDIO and HTTP/SSE transport.
How to use NestJS MCP Module?
Install the package and its peer dependencies, then import McpModule into your root module via forRoot or forRootAsync. Define MCP capabilities by decorating provider methods with @McpResource, @McpTool, or @McpPrompt. Configure the transport type (STDIO or SSE) and run your application. For STDIO, replace the NestJS logger with the included StderrLogger.
Key features of NestJS MCP Module
- Seamless MCP integration using NestJS method decorators.
- Automatic discovery of decorated providers.
- Supports STDIO and HTTP/SSE transport.
- Uses Zod schemas for tool and prompt argument validation.
- Configurable via
forRootandforRootAsyncpatterns. - Includes
StderrLoggerfor STDIO transport compatibility.
Use cases of NestJS MCP Module
- Building CLI-based MCP servers using STDIO transport.
- Exposing MCP endpoints over HTTP/SSE for web clients.
- Defining typed MCP tools with parameter validation.
- Providing static and template MCP resources.
- Creating MCP prompt templates for LLM interactions.
FAQ from NestJS MCP Module
What peer dependencies are required?
@modelcontextprotocol/sdk (^1.10.2), @nestjs/platform-express (^10 or ^11), and zod (^3.0.0) must be installed.
How do I configure the transport type?
Pass transport in McpModule.forRoot(options). Choose TransportType.STDIO, TransportType.SSE, or TransportType.NONE.
How do I handle logging for STDIO transport?
Replace the NestJS logger with the StderrLogger class provided by the module to prevent logs from interfering with STDIO MCP communication.
What endpoints are exposed for HTTP/SSE transport?
The McpHttpController exposes /api/mcp/sse for SSE connections and /api/mcp/messages for message exchange.
What decorators are available for defining MCP capabilities?
@McpResource, @McpTool, and @McpPrompt for resources, tools, and prompts respectively.