Effortlessly Build Model Context Protocol (MCP) Servers with NestJS
Are you looking for a streamlined way to create MCP servers using NestJS? This guide introduces the @rekog/mcp-nest
module, a powerful tool that simplifies the process with Server-Sent Events (SSE), automatic discovery, and more. Learn how to quickly integrate this module into your NestJS application and build robust, scalable MCP servers.
What is @rekog/mcp-nest
and Why Should You Use It?
The @rekog/mcp-nest
module is a NestJS package that simplifies the creation of Model Context Protocol (MCP) servers, leveraging Server-Sent Events (SSE) for real-time communication. It offers a range of features designed to streamline development. If you need to integrate with model context protocol then this NestJS library will save you time and effort.
Here's why @rekog/mcp-nest
stands out:
- SSE Transport: Enables real-time streaming and tool execution.
- Automatic Discovery: Automatically detects and registers tools and resources.
- Zod Validation: Ensures robust request validation using Zod schemas.
- Progress Notifications: Provides real-time updates on long-running processes.
- Guard-Based Authentication: Secure your endpoints with NestJS guards.
Getting Started: Installation and Basic Setup of NestJS MCP server
Ready to dive in? Here's how to install and set up the @rekog/mcp-nest
module in your NestJS project.
-
Install the Package:
-
Import the Module: Import the
McpModule
into yourapp.module.ts
file as shown below. Configure it with theforRoot
method, providing a name and version for your MCP server.
Defining Tools and Resources for your MCP Server
Tools and resources are the heart of your MCP server. Here's how to define them using the @rekog/mcp-nest
module.
-
Create a Tool: Use the
@Tool
decorator to define a tool. The example below simulates a long operation with progress updates usingcontext.reportProgress
.
Securing Your MCP Endpoints with NestJS Guards
Security is paramount. Protect your MCP endpoints using NestJS Guards by following these steps.
-
Create a Guard: Implement the
CanActivate
interface. Handle request validation (e.g., checking JWTs, API keys) and optionally attach user information to the request object. Leverage existing NestJS authentication strategies for enhanced security -
Apply the Guard: Pass your guard(s) to the
McpModule.forRoot
configuration. Specify the guard you want to apply to the MCP module. Remember to provide the guard to the application.
Understanding API Endpoints
The @rekog/mcp-nest
module exposes two primary API endpoints:
GET /sse
: Establishes an SSE connection for real time updates. Protected by guards if configured.POST /messages
: Used for tool execution. Also protected by guards, if configured.
Tips for Optimal Integration
Here's a tip to ensure smooth integration:
-
When using a global prefix, exclude the MCP endpoints to avoid conflicts:
Build Scalable MCP Servers with NestJS
The @rekog/mcp-nest
module empowers you to build robust and scalable MCP servers with ease. Its features, including SSE transport, automatic discovery, and guard-based authentication, make it a valuable asset for any NestJS developer working with the Model Context Protocol. Start building your MCP server today and experience the benefits of streamlined development.