Skip to main content
Glama
Pongsapat1035

mcp-express-bolierplate

MCP Node.js ボイラープレート

Node.js + TypeScript で MCP クライアントと MCP サーバーを作成するためのボイラープレートです。HTTP 側は Express を使用し、以下に対応しています。

  • stdio — クライアントがサーバーを子プロセスとして起動。ローカルで動作する MCP ホストに適しています

  • Streamable HTTP — エンドポイントは /mcp にあり、Cloudflare Tunnel で HTTPS として公開可能

  • users の CRUD 用モックツール

  • 静的リソース users://all とリソーステンプレート users://{id}

  • プロンプト summarize-users

  • discovery、ツール呼び出し、リソース読み取り、プロンプト要求のための CLI クライアント

初期データは src/data/users.json にあり、サーバー起動時にメモリへ読み込まれます。CRUD による変更はファイルに書き戻されず、プロセス再起動時にリセットされます。

要件

  • Node.js 20 以上

  • npm

  • cloudflared(HTTPS トンネルが必要な場合のみ)

Related MCP server: MCP TypeScript Starter

インストール

npm install

ビルドとテストの確認:

npm run check

主要な構造

src/
├── client/
│   └── client.ts          # MCP CLI client ใช้ได้ทั้ง stdio และ HTTP
├── data/
│   └── users.json         # mock seed data
├── lib/
│   └── api-client.ts      # shared Axios instance สำหรับ upstream APIs
├── services/
│   └── user-service.ts    # business logic กลางสำหรับ MCP capabilities
└── server/
    ├── mcp.ts             # ประกอบ server และ capability registrations
    ├── tools/
    │   └── user-tools.ts
    ├── resources/
    │   └── user-resources.ts
    ├── prompts/
    │   └── user-prompts.ts
    ├── schemas/
    │   └── user.ts        # shared MCP output schema
    ├── repository.ts      # in-memory CRUD repository
    ├── stdio.ts           # stdio entry point
    └── http.ts            # Express + Streamable HTTP entry point
scripts/
└── build.mjs              # compile TypeScript และ copy mock JSON ไป dist

mcp.ts のファクトリは両方のトランスポートで共有されるため、サーバーの機能に違いはありません。Tools、Resources、Prompts はリポジトリに直接バインドするのではなく、共通の UserService を呼び出します。

Axios で外部 API を呼び出す

プロジェクトには src/lib/api-client.ts に共有の Axios インスタンスがあり、base URL、タイムアウト、オプションの Bearer トークンが設定されています。ツールやサービスでインポートして使用できます:

import { apiClient } from "../../lib/api-client.js";

const response = await apiClient.get("/users");
console.log(response.data);

サーバー起動時の設定:

API_BASE_URL=https://api.example.com \
API_TIMEOUT_MS=10000 \
API_TOKEN=your-token \
npm run server:http

MCP ツールでの使用例:

server.registerTool(
  "list-upstream-users",
  {
    description: "List users from the configured upstream API",
    inputSchema: z.object({}),
  },
  async () => {
    const { data } = await apiClient.get("/users");
    return {
      content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
      structuredContent: { users: data },
    };
  },
);

API_BASE_URL を設定しない場合でも、Axios に絶対 URL を直接渡すことができます。API_TOKEN をログに出力しないようにし、本番環境ではトークンをシークレットマネージャーに保存してください。

stdio での実行方法

通常、stdio サーバーを個別に起動する必要はありません。クライアントまたは MCP ホストがプロセスを自ら spawn するためです。

デモクライアントを実行(サーバーを起動し、capabilities の discovery、ツール呼び出し、リソース読み取り、プロンプト要求を行います):

npm run client:stdio -- demo

MCP ホストを待ち受けるためにサーバーを直接起動:

npm run server:stdio

注意: stdio は stdout を JSON-RPC のチャネルとして使用するため、サーバーのログは console.error などの stderr 経由でのみ書き込む必要があります。

MCP ホストの設定例(/absolute/path/to/mcp-boilerplate を実際のパスに置き換えてください):

{
  "mcpServers": {
    "mock-users": {
      "command": "node",
      "args": [
        "--import",
        "tsx",
        "/absolute/path/to/mcp-boilerplate/src/server/stdio.ts"
      ],
      "cwd": "/absolute/path/to/mcp-boilerplate"
    }
  }
}

または、事前にビルドして実行時に tsx に依存せず JavaScript を使用:

npm run build
npm run start:stdio

ビルド後の設定:

{
  "mcpServers": {
    "mock-users": {
      "command": "node",
      "args": [
        "/absolute/path/to/mcp-boilerplate/dist/server/stdio.js"
      ],
      "cwd": "/absolute/path/to/mcp-boilerplate"
    }
  }
}

Express HTTP での実行方法

ターミナル 1 — サーバーを起動:

npm run server:http

デフォルト値:

  • MCP エンドポイント: http://127.0.0.1:3000/mcp

  • ヘルスチェック: http://127.0.0.1:3000/health

ターミナル 2 — HTTP クライアントを実行:

npm run client:http -- demo

環境変数でポートやホストを変更できます:

HOST=127.0.0.1 PORT=4000 npm run server:http
MCP_URL=http://127.0.0.1:4000/mcp npm run client:http -- demo

本番ビルドの場合:

npm run build
npm run start:http

Cloudflare Tunnel で HTTPS を有効にする

この例では HTTPS は Cloudflare で終端され、Express サーバーはローカルのみで HTTP を待ち受けます。

macOS で cloudflared をインストール:

brew install cloudflared

ターミナル 1 — MCP HTTP サーバーを起動:

npm run server:http

ターミナル 2 — Quick Tunnel を起動:

cloudflared tunnel --url http://127.0.0.1:3000

cloudflared は一時的な URL を表示します。例:

https://random-words.trycloudflare.com

したがって、外部の MCP エンドポイントは次のようになります:

https://random-words.trycloudflare.com/mcp

ターミナル 3 — HTTPS トンネル経由でテスト:

MCP_URL=https://random-words.trycloudflare.com/mcp npm run client:http -- demo

Quick Tunnel は開発専用であり、Cloudflare は SSE をサポートしていないと明記しています。そのため、このボイラープレートでは response mode を auto に設定しており、一般的な CRUD/discovery コマンドは JSON で応答しますが、長期間のサブスクリプションなどストリーミングが必要な機能のテストに Quick Tunnel を使用すべきではありません。本番環境では、named tunnel、独自のホスト名、認証、認可を使用してください。

カスタムホスト名を使用する場合は、ホスト名を allowlist に追加します:

ALLOWED_HOSTS=mcp.example.com npm run server:http

複数のホスト名はカンマで区切ります:

ALLOWED_HOSTS=mcp.example.com,mcp-staging.example.com npm run server:http

localhost127.0.0.1::1*.trycloudflare.com は開発用にすでに許可されています。

MCP クライアントのコマンド

client:stdioclient:http の両方で同じ形式を使用し、スクリプト名のみを変更します。

ツールを表示:

npm run client:stdio -- list-tools
npm run client:http -- list-tools

リソースまたはプロンプトを表示:

npm run client:stdio -- list-resources
npm run client:stdio -- list-prompts

CRUD ツールを呼び出し:

npm run client:stdio -- call list-users '{}'
npm run client:stdio -- call get-user '{"id":"1"}'
npm run client:stdio -- call create-user '{"name":"Margaret Hamilton","email":"margaret@example.com","role":"developer"}'
npm run client:stdio -- call update-user '{"id":"1","role":"viewer"}'
npm run client:stdio -- call delete-user '{"id":"3"}'

リソースを読み取り:

npm run client:stdio -- read users://all
npm run client:stdio -- read users://1

プロンプトを要求:

npm run client:stdio -- prompt summarize-users '{"tone":"detailed"}'

他の HTTP URL の場合は MCP_URL を設定:

MCP_URL=https://mcp.example.com/mcp npm run client:http -- call list-users '{}'

stdio に関する注意: 各 CLI コマンドは新しいサーバープロセスを spawn するため、毎回元のモックデータから開始されます。CRUD を連続して実行するには、接続を維持する MCP ホストを使用するか、HTTP サーバーを起動して client:http 経由で呼び出してください。

提供されるツール、リソース、プロンプト

種類

名前

機能

Tool

list-users

すべてのユーザーを表示

Tool

get-user

ID でユーザーを表示

Tool

create-user

ユーザーを作成

Tool

update-user

ユーザーを編集

Tool

delete-user

ユーザーを削除

Resource

users://all

すべてのユーザーの JSON スナップショット

Resource template

users://{id}

個別ユーザーの JSON(ID 補完付き)

Prompt

summarize-users

モデルがユーザーデータを要約するためのテキストを生成

環境変数

変数

デフォルト

用途

HOST

127.0.0.1

Express サーバーのバインドアドレス

PORT

3000

Express サーバーのポート

MCP_URL

http://127.0.0.1:3000/mcp

HTTP クライアントのエンドポイント

ALLOWED_HOSTS

サーバーが受け入れるカスタム Host/Origin を追加

API_BASE_URL

未設定

Axios が呼び出すアップストリーム API のベース URL

API_TIMEOUT_MS

10000

Axios リクエストのタイムアウト(ミリ秒)

API_TOKEN

未設定

Axios が自動的に付与する Bearer トークン

値の例は .env.example にあります。プロジェクトは .env ファイルを自動ロードしません。上記の例のように変数を export するか、コマンドの前に指定してください。

セキュリティに関する注意

  • この例には認証と認可がありません。実際のデータを含む公開エンドポイントを開かないでください。

  • HostOrigin の検証は、localhost、TryCloudflare、および ALLOWED_HOSTS の値のみを許可します。

  • モックリポジトリはメモリ内にあり、意図的にデータを永続化しません。

  • 本番環境では、認証、レート制限、監査ログ、永続データベース、および実際のシステムに適した TLS/trust-proxy 設定を追加してください。

すべてのスクリプト

npm run dev:stdio       # stdio server พร้อม watch mode
npm run dev:http        # Express HTTP server พร้อม watch mode
npm run server:stdio    # stdio server จาก TypeScript
npm run server:http     # Express HTTP server จาก TypeScript
npm run client:stdio -- demo
npm run client:http -- demo
npm run build
npm run start:stdio     # รัน dist หลัง build
npm run start:http      # รัน dist หลัง build
npm test
npm run check

参照: MCP TypeScript SDKCloudflare Quick Tunnels

F
license - not found
Not graded
quality - not tested
C
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A simple MCP server that exposes a createUser tool to add users to a local JSON file via stdio transport.
    247
    1
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A sample MCP server that exposes tools, resources, and prompts for managing users and todos, supporting both stdio and Streamable HTTP transports.
  • A
    license
    Not graded
    quality
    D
    maintenance
    Enables creating MCP (Model Context Protocol) servers with zero boilerplate, full TypeScript support, and multiple transports (stdio and HTTP).
    10
    1
    MIT

View all related MCP servers

Related MCP Connectors

  • An MCP server that let you interact with Cycloid.io Internal Development Portal and Platform

  • A basic MCP server to operate on the Postman API.

  • A MCP server built for developers enabling Git based project management with project and personal…

View all MCP Connectors

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/Pongsapat1035/mcp-express-bolierplate'

If you have feedback or need assistance with the MCP directory API, please join our Discord server