Skip to main content
Glama
NestJS MCP Server
by kaz29

NestJS MCP Server

NestJSを使用したModel Context Protocol (MCP)サーバーの実装サンプルです。 DDD/クリーンアーキテクチャを適用しやすい構造で、今後のMCP開発のベースとして利用できます。

🎯 特徴

  • NestJS: エンタープライズグレードのフレームワーク

  • デコレーターベース: @Toolデコレーターで簡潔にツールを定義

  • レイヤー分離: サービス層とツール層を分離(DDD適用可能)

  • 依存性注入: テスタブルで保守性の高い設計

  • Streamable HTTP: 最新のMCP仕様に対応

  • Zod: 型安全なパラメータバリデーション

Related MCP server: MCP Redirect Server

🌿 ブランチ構成

ブランチ

内容

main(このブランチ)

入門向け。時刻・計算・メモのシンプルなツールでMCPの基本を学ぶ

four-keys-example

実践向け。GitHub APIを使った Four Keys (DORA Metrics) の実装

git checkout four-keys-example
npm install  # GitHub API関連の依存パッケージが追加されるため再インストールが必要

📦 提供する機能

  1. get_current_time - 現在の日時を取得

  2. calculate - 四則演算(加算、減算、乗算、除算)

  3. save_note - メモの保存

  4. get_note - メモの取得

  5. list_notes - 保存されたメモの一覧表示

🚀 セットアップ

1. 依存関係のインストール

npm install

2. ビルド

npm run build

3. サーバーの起動

npm start

サーバーが起動すると、以下のように表示されます:

🚀 NestJS MCP Server running on http://localhost:3000
📡 MCP Endpoint: http://localhost:3000/mcp

4. テスト実行

npm test          # 単体テスト
npm run test:e2e  # サーバー起動中にMCPクライアントから疎通確認

🏗️ プロジェクト構造

src/
├── main.ts                          # エントリーポイント
├── app.module.ts                    # ルートモジュール
├── mcp/                             # MCPモジュール
│   ├── mcp.module.ts               # MCP設定・プロバイダー登録
│   ├── tools/                       # ツール層(MCPインターフェース)
│   │   ├── time.tool.ts            # 時刻ツール
│   │   ├── calculator.tool.ts      # 計算ツール
│   │   └── notes.tool.ts           # メモツール
│   └── services/                    # サービス層(ビジネスロジック)
│       ├── time.service.ts
│       ├── calculator.service.ts
│       └── notes.service.ts
└── domain/                          # ドメイン層(将来の拡張用)
    └── notes/
        └── (エンティティ、リポジトリなど)

📝 実装例

ツール定義(デコレーターベース)

// src/mcp/tools/time.tool.ts
import { Injectable } from '@nestjs/common';
import { Tool } from '@rekog/mcp-nest';
import { z } from 'zod';
import { TimeService } from '../services/time.service';

@Injectable()
export class TimeTool {
  constructor(private readonly timeService: TimeService) {}

  @Tool({
    name: 'get_current_time',
    description: '現在の日時を取得します',
    parameters: z.object({}),
  })
  async getCurrentTime() {
    const time = this.timeService.getCurrentTime();
    return `現在の日時: ${time}`;
  }
}

サービス層(ビジネスロジック)

// src/mcp/services/time.service.ts
import { Injectable } from '@nestjs/common';

@Injectable()
export class TimeService {
  getCurrentTime(): string {
    const now = new Date();
    return now.toLocaleString('ja-JP', { timeZone: 'Asia/Tokyo' });
  }
}

モジュール設定

// src/mcp/mcp.module.ts
import { Module } from '@nestjs/common';
import { TimeService } from './services/time.service';
import { TimeTool } from './tools/time.tool';

@Module({
  providers: [
    // サービス層
    TimeService,
    // ツール層
    TimeTool,
  ],
})
export class McpToolsModule {}

🔧 開発

開発モード(ホットリロード)

npm run start:dev

ビルドのみ

npm run build

🎓 学習ポイント

1. レイヤー分離

  • ツール層: MCPプロトコルとのインターフェース(@Toolデコレーター)

  • サービス層: ビジネスロジック(@Injectable())

  • ドメイン層: エンティティ、リポジトリ(将来の拡張)

2. 依存性注入

constructor(private readonly timeService: TimeService) {}

コンストラクタインジェクションで疎結合を実現し、テストが容易。

3. 型安全なパラメータ

parameters: z.object({
  operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
  a: z.number(),
  b: z.number(),
})

Zodによる実行時バリデーションとTypeScript型推論。

4. MCPモジュール設定

McpModule.forRoot({
  name: 'nestjs-mcp-server',
  version: '1.0.0',
  transport: McpTransportType.STREAMABLE_HTTP,
  mcpEndpoint: '/mcp',
})

🌟 今後の拡張例

データベース統合

// TypeORM
@Module({
  imports: [
    TypeOrmModule.forRoot({...}),
    TypeOrmModule.forFeature([Note]),
  ],
})

// Prisma
@Injectable()
export class NotesService {
  constructor(private prisma: PrismaService) {}
}

外部API統合

@Module({
  imports: [HttpModule],
})

@Injectable()
export class ExternalApiService {
  constructor(private http: HttpService) {}
}

認証・認可

@Tool({...})
@UseGuards(AuthGuard)
async protectedTool() {...}

📚 参考リンク

📄 ライセンス

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      Not graded
      quality
      D
      maintenance
      A TypeScript implementation of a Model Context Protocol server that provides a frictionless framework for developers to build and deploy AI tools and prompts, focusing on developer experience with zero boilerplate and automatic tool registration.
      2,530 npm
      14
      MIT
    • F
      license
      Not graded
      quality
      D
      maintenance
      A Model Context Protocol server built with NestJS that provides OAuth 2.1 authentication with GitHub and exposes MCP tools through Server-Sent Events transport. Enables secure, real-time communication with JWT-based protection and dependency injection.
      -
    • A
      license
      Not graded
      quality
      D
      maintenance
      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.
      12 npm
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      A minimal NestJS MCP server skeleton for experimenting with the Model Context Protocol. Currently a placeholder with no implemented tools or resources.
      19 npm
      MIT