NestJS MCP Server Module
NestJS MCP サーバーモジュール
**モデル コンテキスト プロトコル (MCP)**を使用して、NestJS アプリケーションから AI 用のツール、リソース、プロンプトを簡単に公開できる NestJS モジュールです。
@rekog/mcp-nestを使用すると、NestJS で使い慣れた方法でツール、リソース、プロンプトを定義し、依存性注入の機能をフルに活用して、既存のコードベースを利用して複雑なエンタープライズ対応の MCP サーバーを構築できます。
特徴
🚀 すべてのトランスポート タイプをサポート:
ストリーミング可能なHTTP
HTTP+SSE
標準入出力
🔍 自動
tool、resource、prompt検出と登録💯 Zodベースのツール呼び出し検証
📊 進捗通知
🔒 ガードベースの認証
🌐 MCP リソース (ツール、リソース、プロンプト) 内の HTTP リクエスト情報へのアクセス
Related MCP server: SSE MCP Server
インストール
npm install @rekog/mcp-nest @modelcontextprotocol/sdk zodクイックスタート
1. モジュールのインポート
// 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 {}2. ツールとリソースを定義する
// 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',
},
],
};
}
}完了です!
上記の例は、MCPツール内でHTTPRequestヘッダーにアクセスする方法を示しています。これは、ユーザーの識別、クライアント固有のロジックの追加など、様々なユースケースに役立ちます。その他の例については、認証テストをご覧ください。
STDIO のクイックスタート
主な違いは、モジュールをインポートするときにtransportオプションを指定する必要があることです。
McpModule.forRoot({
name: 'playground-stdio-server',
version: '0.0.1',
transport: McpTransportType.STDIO,
});残りは通常通り、ツール、リソース、プロンプトを定義できます。STDIOトランスポートを使用したスタンドアロンのNestJSアプリケーションの例を以下に示します。
async function bootstrap() {
const app = await NestFactory.createApplicationContext(AppModule, {
logger: false,
});
return app.close();
}
void bootstrap();次に、MCP サーバーを MCP Stdio クライアントと共に使用するか (例を参照)、プロジェクトをビルドした後に次の MCP クライアント構成で使用することができます。
{
"mcpServers": {
"greeting": {
"command": "node",
"args": [
"<path to dist js file>",
]
}
}
}APIエンドポイント
HTTP + SSE トランスポートは 2 つのエンドポイントを公開します。
GET /sse: SSE 接続エンドポイント (構成されている場合はガードによって保護されます)POST /messages: ツール実行エンドポイント (設定によりガードで保護されます)
ストリーミング可能な HTTP トランスポートは、次のエンドポイントを公開します。
POST /mcp: すべてのMCP操作(ツール実行、リソースアクセスなど)のメインエンドポイント。ステートフルモードでは、セッションの作成と維持を行います。GET /mcp: リアルタイム更新と進捗状況通知のための Server-Sent Events (SSE) ストリームを確立します。ステートフルモードでのみ利用可能です。DELETE /mcp: MCPセッションを終了します。ステートフルモードでのみ使用できます。
ヒント
グローバル プレフィックス付きのモジュールを使用することもできますが、次のようにしてそれらのエンドポイントを除外することをお勧めします。
app.setGlobalPrefix('/api', { exclude: ['sse', 'messages', 'mcp'] });認証
標準の NestJS Guards を使用して MCP エンドポイントを保護できます。
1. ガードを作成する
CanActivateインターフェースを実装します。ガードはリクエストの検証(JWTやAPIキーのチェックなど)を処理し、必要に応じてユーザー情報をリクエストオブジェクトに添付する必要があります。
特別なことは何もありません。詳細については、NestJS のドキュメントを確認してください。
2. ガードをつける
McpModule.forRoot設定にガードを渡してください。ガードは/sseと/messages両方のエンドポイントに適用されます。
// 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 {}以上です!残りはNestJS Guardsと同じです。
遊び場
playgroundディレクトリには、MCPと@rekog/mcp-nest機能を簡単にテストするためのサンプルが含まれています。詳細はplayground/README.mdを参照してください。
構成
McpModule.forRoot()メソッドは、サーバーの設定に使用するMcpOptionsオブジェクトを受け取ります。使用可能なオプションは以下のとおりです。
オプション | 説明 | デフォルト |
| 必須。MCPサーバーの名前。 | - |
| 必須。MCPサーバーのバージョン。 | - |
| オプションのMCPサーバー機能をアドバタイズします。 @modelcontextprotocol/sdkを参照してください。 |
|
| サーバーと対話する方法に関するクライアントへのオプションの指示。 |
|
| 有効にするトランスポート タイプを指定します。 |
|
| SSE 接続のエンドポイント パス ( |
|
| メッセージを送信するためのエンドポイント パス ( |
|
| MCP 操作の基本エンドポイント パス ( |
|
| すべてのMCPエンドポイントのグローバルプレフィックス。既存のアプリケーションに統合する場合に便利です。 |
|
| 認証/承認のために MCP エンドポイントに適用する NestJS ガード配列。 |
|
| 生成された MCP コントローラーに適用する NestJS クラス デコレーターの配列。 |
|
|
|
|
| 接続を維持するために定期的な SSE ping メッセージを有効にするかどうか。 |
|
| SSE ping メッセージを送信する間隔 (ミリ秒単位)。 |
|
|
|
|
|
|
|
|
|
|
|
|
|
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/rekog-labs/MCP-Nest'
If you have feedback or need assistance with the MCP directory API, please join our Discord server