mcp-nestjs
A NestJS module for building Model Context Protocol (MCP) servers using decorators to expose services as tools, resources, and prompts. It features auto-discovery, a built-in playground UI, and support for multiple transports including SSE and Stdio.
README
mcp-nestjs
NestJS module for building Model Context Protocol (MCP) servers. Expose your NestJS services as MCP tools, resources, and prompts using decorators — with auto-discovery, multiple transports, and a built-in playground UI.
Features
- Decorator-driven —
@McpTool(),@McpResource(),@McpPrompt(),@McpGuard(),@McpToolGroup() - Auto-discovery — decorated methods on any provider or controller are registered automatically
- Schema adapters — auto-detect Zod, Joi, class-validator, or inline schemas (zero config)
- Multiple transports — SSE, Streamable HTTP, and Stdio (JSON-RPC 2.0)
- Built-in playground — interactive UI at
/mcp-playgroundto browse and test tools - Guards — per-tool or per-class authorization
- Session management — configurable timeout, cleanup, and max sessions
- No SDK dependency — custom JSON-RPC 2.0 implementation
Installation
npm install mcp-nestjs
Peer dependencies (your NestJS app already has these):
npm install @nestjs/common @nestjs/core reflect-metadata rxjs
Optional schema libraries (auto-detected):
npm install zod # for Zod schemas
npm install joi # for Joi schemas
npm install class-validator class-transformer # for DTO schemas
Quick Start
// app.module.ts
import { Module } from '@nestjs/common';
import { McpModule } from 'mcp-nestjs';
import { GreetingService } from './greeting.service';
@Module({
imports: [
McpModule.forRoot({
name: 'my-mcp-server',
version: '1.0.0',
transports: { sse: { enabled: true } },
playground: true,
}),
],
providers: [GreetingService],
})
export class AppModule {}
// greeting.service.ts
import { Injectable } from '@nestjs/common';
import { McpTool } from 'mcp-nestjs';
@Injectable()
export class GreetingService {
@McpTool({
description: 'Say hello to someone',
schema: {
name: { type: 'string', description: 'Name to greet' },
},
})
async greet(args: { name: string }) {
return { message: `Hello, ${args.name}!` };
}
}
Start your app and visit http://localhost:3000/mcp-playground.
Module Configuration
McpModule.forRoot(options)
McpModule.forRoot({
name: 'my-server', // Server name (required)
version: '1.0.0', // Server version (required)
transports: {
sse: { enabled: true, path: '/sse' }, // SSE transport
http: { enabled: true, path: '/mcp' }, // Streamable HTTP transport
stdio: { enabled: false }, // Stdio transport
},
session: {
timeout: 30 * 60 * 1000, // Session timeout (default: 30min)
cleanupInterval: 5 * 60 * 1000, // Cleanup interval (default: 5min)
maxSessions: 1000, // Max concurrent sessions
},
playground: true, // Enable playground UI
tools: [], // Manual tool registrations
guards: [], // Global guards
})
McpModule.forRootAsync(options)
McpModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
name: config.get('MCP_SERVER_NAME'),
version: config.get('MCP_VERSION'),
transports: { sse: { enabled: true } },
playground: true,
}),
inject: [ConfigService],
})
McpModule.forFeature(providers)
Register additional providers in feature modules:
McpModule.forFeature([MyFeatureService])
Decorators
@McpTool(options) — Method Decorator
Marks a method as an MCP tool.
@McpTool({
name?: string, // Override tool name (default: [group_]methodName)
description: string, // Required description
schema?: ZodType | JoiSchema | Record<string, InlinePropertyDef>, // Input schema
transform?: 'auto' | 'raw' | ((result) => ToolResult), // Response mode
excludeProperties?: string[], // Hide fields from schema
requiredProperties?: string[], // Override required fields
})
Schema options:
// Inline (always available)
@McpTool({
description: 'Search users',
schema: {
query: { type: 'string', description: 'Search term' },
limit: { type: 'number', default: 10, required: false },
role: { type: 'string', enum: ['admin', 'user'] },
tags: { type: 'array', items: { type: 'string' }, required: false },
},
})
// Zod (if installed)
import { z } from 'zod';
@McpTool({
description: 'Create user',
schema: z.object({
name: z.string(),
email: z.string(),
role: z.enum(['admin', 'user']).optional(),
}),
})
// Joi (if installed)
import Joi from 'joi';
@McpTool({
description: 'Create user',
schema: Joi.object({
name: Joi.string().required(),
email: Joi.string().required(),
}),
})
Transform modes:
// 'auto' (default) — return value auto-wrapped in ToolResult
@McpTool({ description: 'Get data' })
async getData() {
return { key: 'value' }; // → { content: [{ type: 'text', text: '{"key":"value"}' }] }
}
// 'raw' — you return a ToolResult directly
@McpTool({ description: 'Get data', transform: 'raw' })
async getData(): Promise<ToolResult> {
return { content: [{ type: 'text', text: 'hello' }] };
}
// Custom function
@McpTool({
description: 'Get user',
transform: (user) => ({
content: [{ type: 'text', text: `User: ${user.name}` }],
}),
})
@McpToolGroup(prefix?) — Class Decorator
Prefixes all tool names in a class:
@McpToolGroup('files') // tools: files_list, files_read, files_write
@Injectable()
export class FilesService {
@McpTool({ description: 'List files' })
async list() { ... }
@McpTool({ description: 'Read file' })
async read(args: { path: string }) { ... }
}
@McpToolGroup() // Auto-derive from @Controller path
@McpResource(options) — Method Decorator
Exposes data as MCP resources:
// Static resource
@McpResource({
uri: 'config://app',
name: 'app_config',
description: 'Application config',
mimeType: 'application/json',
})
async getConfig() {
return {
contents: [{
uri: 'config://app',
mimeType: 'application/json',
text: JSON.stringify({ debug: true }),
}],
};
}
// Parameterized resource (URI template)
@McpResource({
uriTemplate: 'users://{userId}',
name: 'user_detail',
mimeType: 'application/json',
})
async getUser(uri: string) {
const id = uri.match(/users:\/\/(.+)/)?.[1];
return { contents: [{ uri, text: JSON.stringify(user) }] };
}
@McpPrompt(options) — Method Decorator
Reusable prompt templates:
@McpPrompt({
name: 'code_review',
description: 'Generate a code review prompt',
schema: {
code: { type: 'string', description: 'Code to review' },
language: { type: 'string', description: 'Programming language' },
},
})
async codeReview(args: { code: string; language: string }) {
return {
messages: [{
role: 'user',
content: {
type: 'text',
text: `Review this ${args.language} code:\n\`\`\`\n${args.code}\n\`\`\``,
},
}],
};
}
@McpGuard(...guards) — Class or Method Decorator
Per-tool or per-class authorization:
import { IMcpGuard, McpExecutionContext } from 'mcp-nestjs';
export class ApiKeyGuard implements IMcpGuard {
canActivate(context: McpExecutionContext): boolean {
const args = context.getArgs();
return args.apiKey === 'secret';
}
}
// Class-level: all tools require auth
@McpGuard(ApiKeyGuard)
@Injectable()
export class AdminService { ... }
// Method-level: only this tool requires auth
@McpGuard(RateLimitGuard)
@McpTool({ description: 'Sensitive operation' })
async sensitiveOp() { ... }
McpExecutionContext provides:
getSessionId()— current session IDgetArgs()— tool/prompt argumentsgetRequest()— raw transport requestgetToolName()— tool/resource/prompt namegetType()—'tool' | 'resource' | 'prompt'
Manual Tool Registration
// Via forRoot options
McpModule.forRoot({
...config,
tools: [{
definition: {
name: 'echo',
description: 'Echo input',
inputSchema: {
type: 'object',
properties: { message: { type: 'string' } },
required: ['message'],
},
},
handler: async (args) => ({
content: [{ type: 'text', text: args.message }],
}),
}],
})
// Via ToolRegistryService at runtime
@Injectable()
export class DynamicTools implements OnModuleInit {
constructor(private registry: ToolRegistryService) {}
onModuleInit() {
this.registry.registerTool({ definition: {...}, handler: async (args) => {...} });
}
}
Transports
SSE (Server-Sent Events)
GET /sse → Opens SSE stream, returns session endpoint
POST /messages?sessionId=X → Send JSON-RPC requests
Streamable HTTP
POST /mcp → Single JSON-RPC request/response
Session via `mcp-session-id` header
Stdio
Reads JSON-RPC from stdin, writes responses to stdout. For CLI subprocess spawning.
Playground
Enable with playground: true in module options. Visit /mcp-playground to:
- Browse all registered tools, resources, and prompts
- View input schemas with types, required fields, and descriptions
- Auto-generated forms for each tool
- Execute tools and see results with timing
- Read resources by URI
- Test prompts with arguments
Examples
See the examples/ directory for complete working examples:
| Example | What it shows |
|---|---|
01-basic |
Minimal setup — one service, two tools |
02-inline-schema |
All inline schema options — enums, arrays, defaults, optionals |
03-zod-schema |
Zod schema integration |
04-tool-groups |
@McpToolGroup prefix namespacing |
05-resources |
Static URIs and URI templates |
06-prompts |
Reusable prompt templates |
07-guards |
API key auth, rate limiting, logging guards |
08-manual-tools |
forRoot({ tools }) + runtime ToolRegistryService |
09-multiple-transports |
SSE + HTTP + Stdio with session config |
10-async-config |
forRootAsync() with injected ConfigService |
11-full-app |
Everything combined |
Run any example:
npx ts-node -r reflect-metadata examples/01-basic/main.ts
License
MIT
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。
mcp-server-qdrant
这个仓库展示了如何为向量搜索引擎 Qdrant 创建一个 MCP (Managed Control Plane) 服务器的示例。