Skip to main content
Glama
Pongsapat1035

mcp-express-bolierplate

MCP Node.js Boilerplate

用于使用 Node.js + TypeScript 创建 MCP client 和 MCP server 的 Boilerplate,HTTP 端使用 Express,支持:

  • stdio — client 将 server 作为 child process 启动,适合在本地运行的 MCP host

  • Streamable HTTP — endpoint 位于 /mcp,可通过 Cloudflare Tunnel 对外提供 HTTPS

  • 用于 CRUD users 的 mock tools

  • 静态 resource users://all 和 resource template users://{id}

  • prompt summarize-users

  • 用于 discovery、调用 tool、读取 resource 和请求 prompt 的 CLI client

初始数据位于 src/data/users.json,在 server 启动时加载到内存中。通过 CRUD 进行的修改不会写回文件,重启进程后会重置。

Requirements

  • Node.js 20 或更高版本

  • npm

  • cloudflared 仅在需要 HTTPS tunnel 时使用

Related MCP server: MCP TypeScript Starter

安装

npm install

检查 build 和 test:

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 中的 Factory 被两种 transport 共用,因此 server 的功能没有差异。Tools、Resources 和 Prompts 调用统一的 UserService,而不是直接绑定 repository。

使用 Axios 调用 External API

项目在 src/lib/api-client.ts 中提供了共享的 Axios instance,包含 base URL、timeout 和可选的 Bearer token。可以 import 后在 tool 或 service 中使用:

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

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

在启动 server 时配置:

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

在 MCP tool 中使用的示例:

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 传入 absolute URL。避免记录 API_TOKEN,在生产环境部署时应将 token 存储在 secret manager 中。

以 stdio 方式运行

通常不需要单独启动 stdio server,因为 client 或 MCP host 会自行 spawn 进程。

运行 demo client,它会启动 server、discover capabilities、调用 tool、读取 resource 并请求 prompt:

npm run client:stdio -- demo

直接启动 server 以等待 MCP host:

npm run server:stdio

注意事项:stdio 使用 stdout 作为 JSON-RPC 通道,因此 server 的日志只能通过 stderr 输出,例如使用 console.error

MCP host 的配置示例,将 /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"
    }
  }
}

或者先 build 再使用 JavaScript,运行时无需依赖 tsx

npm run build
npm run start:stdio

build 后的配置:

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

以 Express HTTP 方式运行

Terminal 1 — 启动 server:

npm run server:http

默认值:

  • MCP endpoint:http://127.0.0.1:3000/mcp

  • health check:http://127.0.0.1:3000/health

Terminal 2 — 运行 HTTP client:

npm run client:http -- demo

可以通过 environment variables 修改 port 或 host:

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

用于 production build:

npm run build
npm run start:http

使用 Cloudflare Tunnel 开启 HTTPS

此示例中的 HTTPS 在 Cloudflare 处终止,Express server 仍然只在本地监听 HTTP。

在 macOS 上安装 cloudflared

brew install cloudflared

Terminal 1 — 启动 MCP HTTP server:

npm run server:http

Terminal 2 — 启动 Quick Tunnel:

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

cloudflared 会显示临时 URL,例如:

https://random-words.trycloudflare.com

因此外部 MCP endpoint 为:

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

Terminal 3 — 通过 HTTPS tunnel 测试:

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

Quick Tunnel 仅适用于 development,且 Cloudflare 表示不支持 SSE,因此此 boilerplate 将 response mode 设置为 auto,常规的 CRUD/discovery 命令可以返回 JSON,但不应使用 Quick Tunnel 测试需要 stream 的功能,例如长期 subscription。生产环境应使用 named tunnel、自有 hostname、authentication 和 authorization。

使用自定义 hostname 时,请将 hostname 添加到 allowlist:

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

多个 hostname 用逗号分隔:

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

localhost127.0.0.1::1*.trycloudflare.com 已默认允许用于 development。

MCP client 命令

client:stdioclient:http 使用相同的格式,只需更换 script 名称。

查看 tools:

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

查看 resources 或 prompts:

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

调用 CRUD tools:

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"}'

读取 resources:

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

请求 prompt:

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 server 进程,因此每次都会从原始 mock 数据开始。如果希望 CRUD 操作连续进行,请使用保持同一连接的 MCP host,或者启动 HTTP server 后通过 client:http 调用。

提供的 Tools、resources 和 prompt

类型

名称

功能

Tool

list-users

查看所有 users

Tool

get-user

按 ID 查看 user

Tool

create-user

创建 user

Tool

update-user

修改 user

Tool

delete-user

删除 user

Resource

users://all

所有 users 的 JSON snapshot

Resource template

users://{id}

单个 user 的 JSON,带 ID completion

Prompt

summarize-users

生成让模型总结 users 数据的提示文本

Environment variables

变量

默认值

用途

HOST

127.0.0.1

Express server 绑定地址

PORT

3000

Express server 端口

MCP_URL

http://127.0.0.1:3000/mcp

HTTP client endpoint

ALLOWED_HOSTS

添加 server 允许的自定义 Host/Origin

API_BASE_URL

未设置

Axios 调用的 upstream API 的 Base URL

API_TIMEOUT_MS

10000

Axios 请求超时时间,单位毫秒

API_TOKEN

未设置

Axios 自动附加的 Bearer token

示例值位于 .env.example 中。项目不会自动加载 .env 文件;请按上述示例导出变量或将其放在命令前面。

Security notes

  • 此示例没有 authentication 和 authorization,禁止将包含真实数据的 public endpoint 对外开放。

  • HostOrigin 的 validation 仅允许 localhost、TryCloudflare 以及 ALLOWED_HOSTS 中的值。

  • mock repository 位于内存中,有意不持久化数据。

  • 生产环境应添加 auth、rate limiting、audit logging、persistent database 以及适合实际系统的 TLS/trust-proxy 配置。

所有 Scripts

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