mcpField
N
NestJS MCP Server Module

NestJS MCP Server Module

ローカル
github.com/rahosung開発ツール081 週間前に更新

NestJS MCP Server Module について

NestJS MCP Server Moduleは、SSE経由でMCPサーバーを構築するNestJSモジュールです。ツールとリソースの自動登録に加え、リクエスト検証、進捗通知、NestJS Guardによる認証に対応します。

主な機能

  1. 01ストリーミングとツール実行のSSEトランスポート
  2. 02ツールとリソースの自動登録
  3. 03Zodによるリクエスト検証
  4. 04進捗通知
  5. 05NestJS Guardによる認証

活用シーン

  1. 01NestJSアプリでのMCPサーバー構築
  2. 02SSE経由でのツールとリソースの公開
  3. 03NestJS Guardを使ったMCPエンドポイントの保護

MCP 対応

クライアント向けにMCPサーバーを提供し、`/sse`でSSE接続、`/messages`でツールを実行します。検出されたツールとリソースは自動登録されます。

設定

ローカルサーバーの設定はどのクライアントでも共通です。コピーしてクライアントの MCP 設定ファイルに貼り付けてください。

インストール方法は未掲載です

このサーバーのパッケージやリモートエンドポイントはまだ取得できていません。プロジェクトの README の手順に従ってください。

リポジトリ

ツール

ツール一覧はまだ取得されていません

ローカルサーバーのツールはマシン上で起動して初めて取得できます。メンテナーはまだツール一覧を提供していません。

詳細

NestJS MCP サーバーモジュール

Server-Sent Events(SSE)トランスポートを使用して MCP(Model Context Protocol)サーバーを作成するための NestJS モジュールです。

README の画像

機能

  • 🚀 ストリーミングとツール実行に対応した SSE トランスポート
  • 🔍 tool と resource の自動検出と登録
  • 💯 Zod ベースのリクエスト検証
  • 📊 進行状況の通知
  • 🔒 ガードベースの認証

インストール

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 { Injectable } from '@nestjs/common';
import { Tool, 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) {
    const greeting = `Hello, ${name}!`;

    const totalSteps = 5;
    for (let i = 0; i < totalSteps; i++) {
      await new Promise((resolve) => setTimeout(resolve, 500));

      // 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',
        },
      ],
    };
  }
}

これで完了です!

API エンドポイント

  • GET /sse:SSE 接続エンドポイント(設定されている場合、ガードで保護されます)
  • POST /messages:ツール実行エンドポイント(設定されている場合、ガードで保護されます)
ヒント

グローバルプレフィックスを使用することもできますが、次のようにこれらのエンドポイントを除外する方法をおすすめします。

app.setGlobalPrefix('/api', { exclude: ['sse', 'messages'] });

認証

標準的な NestJS ガードを使用して 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 ガードと同じです。

コメント

MCP は初めてですか?

プロトコルの概要から最初のサーバーの導入まで、よくある疑問にまとめて答えます。

AI アプリケーションが外部のツールやデータソースに統一的な方法で接続するためのオープンなプロトコルです。クライアントはモデルとの対話を担い、MCP サーバーはツール・リソース・プロンプトテンプレートを提供します。標準メッセージでやり取りするため、1 つのサーバーを複数のクライアントで再利用できます。