NestJS MCP Server Module

by rekog-labs

670 stars
566 downloads
Not rated
GitHub

About

A NestJS module to effortlessly create Model Context Protocol (MCP) servers for exposing AI tools, resources, and prompts.

Details

Author
rekog-labs
GitHub stars
670
Downloads
566
Categories
Other, AI

- Expose NestJS methods as MCP tools with automatic discovery and Zod validation
- Multi-transport support: HTTP+SSE, Streamable HTTP, and STDIO
- Serve static and dynamic content through MCP resource and resource template systems
- Define reusable prompt templates for AI interactions
- Guard-based authentication with OAuth and built-in or external authorization servers
- Interactive tool calls with user input elicitation and full HTTP request access

Install the package and its peer dependencies via npm, import McpModule.forRoot() in your NestJS module, then define tools using the @Tool decorator with Zod-based parameter validation and automatic discovery.

NestJS MCP Server Module

<div align="center">

[![CI][ci-image]][ci-url]
[![Code Coverage][code-coverage-image]][code-coverage-url]
[![NPM Version][npm-version-image]][npm-url]
[![NPM Downloads][npm-downloads-image]][npm-url]
[![NPM License][npm-license-image]][npm-url]

</div>

A NestJS module to effortlessly expose tools, resources, and prompts for AI, from your NestJS applications using the Model Context Protocol (MCP).

With @rekog/mcp-nest you 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.

Features

- πŸš€ Multi-Transport Support: HTTP+SSE, Streamable HTTP, and STDIO
- πŸ”§ Tools: Expose NestJS methods as MCP tools with automatic discovery and Zod validation
- πŸ› οΈ Elicitation: Interactive tool calls with user input elicitation
- 🌐 HTTP Request Access: Full access to request context within MCP handlers
- πŸ” Per-Tool Authorization: Implement fine-grained authorization for tools
- πŸ“ Resources: Serve content and data through MCP resource system
- πŸ“š Resource Templates: Dynamic resources with parameterized URIs
- πŸ’¬ Prompts: Define reusable prompt templates for AI interactions
- πŸ” Guard-based Authentication: Guard-based security with OAuth support
- 🏠 Built-in Authorization Server β€” Using the built-in Authorization Server for easy setups. (Beta)
- 🌐 External Authorization Server β€” Securing your MCP server with an external authorization server (Keycloak, Auth0, etc).
- πŸ’‰ Dependency Injection: Leverage NestJS DI system throughout MCP components
- πŸ” Server mutation and instrumentation β€” Mutate the underlying mcp server for custom logic or instrumentation purposes.

Are you interested to build ChatGPT widgets (with the OpenAI SDK) or MCP apps?
Find out how to do that with @rekog/MCP-Nest in this repository MCP-Nest-Samples

> [!TIP]
> You can easily learn about this package using the chat tab in Context7. Better yet, connect the Context7 MCP server to allow your AI agents to access the documentation and implement MCP-Nest for you.

Installation

npm install @rekog/mcp-nest @modelcontextprotocol/sdk zod@^4

Optional dependencies

If you use the built-in authorization server with the TypeORM store, install the following optional peer dependencies:

npm install @nestjs/typeorm typeorm

Quick Start

// 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 { Injectable } from '@nestjs/common';
import { Tool, Context } from '@rekog/mcp-nest';
import { z } from 'zod';

@Injectable()
export class GreetingTool {
@Tool({
name: 'greeting-tool',
description: 'Returns a greeting with progress updates',
parameters: z.object({
name: z.string().default('World'),
}),
})
async sayHello({ name }, context: Context) {
await context.reportProgress({ progress: 50, total: 100 });
return Hello, ${name}!;
}
}

Documentation

- Tools Guide - Define and expose NestJS methods as MCP tools
- Discovery and Registration of Tools - Automatic discovery and manual registration of tools
- Dynamic Capabilities Guide - Register tools, resources, and prompts programmatically at runtime
- Resources Guide - Serve static and dynamic content
- Resource Templates Guide - Create parameterized resources
- Prompts Guide - Build reusable prompt templates
- Built-in Authorization Server - Secure your MCP server with built-in OAuth
- External Authorization Server - Securing your MCP server with an external authorization server (Keycloak, Auth0, etc)
- Server examples - MCP servers examples (Streamable HTTP, HTTP, and STDIO) and with Fastify support

Playground

The playground directory contains working examples for all features.
Refer to playground/README.md for details.

<!-- Badges -->
[ci-url]: https://github.com/rekog-labs/MCP-Nest/actions/workflows/pipeline.yml
[ci-image]: https://github.com/rekog-labs/MCP-Nest/actions/workflows/pipeline.yml/badge.svg
[npm-url]: https://www.npmjs.com/package/@rekog/mcp-nest
[npm-version-image]: https://img.shields.io/npm/v/@rekog/mcp-nest
[npm-downloads-image]: https://img.shields.io/npm/d18m/@rekog/mcp-nest
[npm-license-image]: https://img.shields.io/npm/l/@rekog/mcp-nest
[code-coverage-url]: https://codecov.io/gh/rekog-labs/mcp-nest
[code-coverage-image]: https://codecov.io/gh/rekog-labs/mcp-nest/branch/main/graph/badge.svg

No reviews yet β€” be the first

Sign in to leave a review

Use Google, GitHub, or an email account so ratings stay tied to real people.

Email sign in

No reviews posted yet.