NestJS MCP Server Module
About
A NestJS module for building MCP servers to expose tools and resources for AI, with support for multiple transport types.
Details
- Author
- kyee-rs
- Categories
- Developer Tools
Jump to
Setup
Install NestJS MCP Server Module in your MCP client (Claude Desktop, Cursor, Windsurf, and others).
Repository: https://github.com/kyee-rs/MCP-Nest
Follow the installation instructions in the repository README, then restart your MCP client.
A NestJS module to effortlessly expose tools, resources, and prompts for AI, from your NestJS applications using theModel Context Protocol (MCP).
With@rekog/mcp-nestyou define tools, resources, and prompts in a way that's familiar in NestJS and leverage the full power of dependency injection to utilize your existing codebase in building complex enterprise ready MCP servers.
- 🚀 Support for all Transport Types:
- Streamable HTTP
- HTTP+SSE
- STDIO
npm install @rekog/mcp-nest @modelcontextprotocol/sdk zod
// 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 {}
// greeting.tool.ts import type { Request } from 'express'; import { Injectable } from '@nestjs/common'; import { Tool, Resource, 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, request: Request) { const userAgent = request.get('user-agent') || 'Unknown'; const greeting = Hello, ${name}! Your user agent is: ${userAgent}; const totalSteps = 5; for (let i = 0; i < totalSteps; i++) { await new Promise((resolve) => setTimeout(resolve, 100)); // 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', }, ], }; } }
[!TIP] The above example shows how HTTPRequestheaders are accessed within MCP Tools. This is useful for identifying users, adding client-specific logic, and many other use cases. For more examples, see theAuthentication Tests.
The main difference is that you need to provide thetransportoption when importing the module.
McpModule.forRoot({ name: 'playground-stdio-server', version: '0.0.1', transport: McpTransportType.STDIO, });
The rest is the same, you can define tools, resources, and prompts as usual. An example of a standalone NestJS application using the STDIO transport is the following:
async function bootstrap() { const app = await NestFactory.createApplicationContext(AppModule, { logger: false, }); return app.close(); } void bootstrap();
Next, you can use the MCP server with an MCP Stdio Client (see example), or after building your project you can use it with the following MCP Client configuration:
{ "mcpServers": { "greeting": { "command": "node", "args": [ "<path to dist js file>", ] } } }
HTTP+SSE transport exposes two endpoints:
- GET /sse: SSE connection endpoint (Protected by guards if configured)
- POST /messages: Tool execution endpoint (Protected by guards if configured)
Streamable HTTP transport exposes the following endpoints:
- POST /mcp: Main endpoint for all MCP operations (tool execution, resource access, etc.). In stateful mode, this creates and maintains sessions.
- GET /mcp: Establishes Server-Sent Events (SSE) streams for real-time updates and progress notifications.Only available in stateful mode.
- DELETE /mcp: Terminates MCP sessions.Only available in stateful mode.
It's possible to use the module with global prefix, but the recommended way is to exclude those endpoints with:
app.setGlobalPrefix('/api', { exclude: ['sse', 'messages', 'mcp'] });
You can secure your MCP endpoints using standard NestJS Guards.
Implement theCanActivateinterface. The guard should handle request validation (e.g., checking JWTs, API keys) and optionally attach user information to the request object.
Nothing special, check the NestJS documentation for more details.
Pass your guard(s) to theMcpModule.forRootconfiguration. The guard(s) will be applied to both the/sseand/messagesendpoints.
// 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 {}
That's it! The rest is the same as NestJS Guards.
Theplaygrounddirectory contains examples to quickly test MCP and@rekog/mcp-nestfeatures. Refer to theplayground/README.mdfor more details.
TheMcpModule.forRoot()method accepts anMcpOptionsobject to configure the server. Here are the available options:
This is a web browser that enables your coding agent, such as Claude Code, to visit websites on your behalf and assist you in identifying bugs or creating UI test cases.
Create crafted UI components inspired by the best 21st.dev design engineers.
Bring agent evaluations, observability, and synthetic test set generation directly into your IDE for free with Galileo's new MCP server
An MCP server to help AI assistants to answer questions and generate AccelByte Extend SDK code more effectively .
MCP server for AI Diagram Maker — generate beautiful software engineering diagrams directly inside Cursor, Claude Desktop, Claude Code, or any MCP-compatible AI agent
ALAPI MCP Tools,Call hundreds of API interfaces via MCP
AI-powered SVG animation generator that transforms static files into animated SVG components using the Allyson platform
MCP server that gives AI assistants on-demand access to 1,500+ amCharts docs, ~300 code examples, and 1000+ class API references.
APIMatic MCP Server is used to validate OpenAPI specifications using APIMatic. The server processes OpenAPI files and returns validation summaries by leveraging APIMatic’s API.
One shared context layer for AI agents and humans — live API specs, DB schemas, and versioned contracts across repos so every agent and teammate works from the same source of truth.
Build and deploy full-stack Next.js apps with 98 tools for React, AWS, and MongoDB
Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.





