MCP Server Boilerplate

MCP Server Boilerplate

A production-grade TypeScript starter for building Model Context Protocol servers, supporting stdio and Streamable HTTP transports with modular tools, resources, and prompts.

Category
访问服务器

README

MCP Server Boilerplate

A production-grade starting point for building Model Context Protocol (MCP) servers in native TypeScript. Clean enough for a weekend project, hardened enough for an enterprise deployment.

  • ⚡️ Native TypeScript, strict, ESM (NodeNext)
  • 🔌 Two transports out of the box — stdio (local clients) and Streamable HTTP (remote/hosted) with session management
  • 🧱 Modular structure — one file per tool / resource / prompt, aggregated automatically
  • ✅ Zod-validated tool I/O and environment config
  • 🪵 Structured logging (pino) to stderr only, so stdio JSON-RPC stays clean
  • 🛡️ DNS-rebinding protection (Host-header validation) + CORS + optional bearer auth
  • 🧪 Vitest with fast in-memory client↔server tests
  • 🧹 ESLint (type-checked) + Prettier + Husky pre-commit hooks
  • 🐳 Multi-stage Dockerfile for the HTTP transport
  • 🤖 Claude Code tooling — CLAUDE.md, skills, and slash commands baked in

Requirements

  • Node.js >= 22.13 (required by pnpm 11)
  • pnpm >= 11 (corepack enable will provide it)

Quick start

pnpm install
cp .env.example .env

# Local (stdio) development with hot reload
pnpm dev

# Remote (Streamable HTTP) development with hot reload
pnpm dev:http

Then build and run the compiled server:

pnpm build
pnpm start        # stdio
pnpm start:http   # Streamable HTTP on http://127.0.0.1:3000/mcp

Transports

The transport is chosen by the --transport CLI flag (which overrides the MCP_TRANSPORT env var), defaulting to stdio:

  • stdio — the client spawns the server and speaks JSON-RPC over stdin/stdout. Use this for Claude Desktop, IDEs, and other local integrations.
  • Streamable HTTP — an Express server at POST/GET/DELETE /mcp with per-session transports and a GET /healthz liveness endpoint. Use this for remote/hosted deployments.

Connect from Claude Desktop (stdio)

{
  "mcpServers": {
    "mcp-boilerplate": {
      "command": "node",
      "args": ["/absolute/path/to/dist/index.js"],
    },
  },
}

Authentication (HTTP)

The HTTP transport is unauthenticated by default (protected by ALLOWED_HOSTS). To require a bearer token, set AUTH_ENABLED=true and provide AUTH_TOKENS:

AUTH_ENABLED=true AUTH_TOKENS=my-secret-token pnpm start:http
# Requests to /mcp without "Authorization: Bearer my-secret-token" get 401.

The bundled verifier in src/core/auth.ts checks a static token allowlist — replace verifyAccessToken with real JWT verification or OAuth token introspection for production. The validated AuthInfo is available to tool handlers via extra.authInfo.

Configuration

All configuration is environment-based and validated at startup (see .env.example):

Variable Default Description
MCP_TRANSPORT stdio stdio or http (CLI --transport wins)
HOST 127.0.0.1 HTTP bind address
PORT 3000 HTTP port
ALLOWED_HOSTS 127.0.0.1,localhost Allowed Host hostnames (port-agnostic). Empty disables protection.
CORS_ORIGINS (empty) Comma-separated CORS origins for browser clients. Empty disables CORS.
AUTH_ENABLED false Require a bearer token on /mcp (HTTP transport).
AUTH_TOKENS (empty) Comma-separated tokens accepted by the example verifier.
AUTH_REQUIRED_SCOPES (empty) Comma-separated scopes every token must carry.
AUTH_RESOURCE_METADATA_URL (empty) Protected Resource Metadata URL advertised in 401 responses.
LOG_LEVEL info fatal…trace/silent
NODE_ENV development production emits plain JSON logs

Project structure

src/
  index.ts          Entrypoint: transport resolution + graceful shutdown
  server.ts         createServer(): builds McpServer, registers everything
  config/env.ts     Zod-validated environment configuration
  core/             logger (stderr), errors (AppError + toToolError), pkg (name/version)
  transports/       stdio.ts, http.ts (Express + Streamable HTTP)
  tools/            echo.tool.ts + index.ts aggregator
  resources/        system-info.resource.ts + index.ts aggregator
  prompts/          summarize.prompt.ts + index.ts aggregator
tests/              Vitest, in-memory client↔server (helpers/connect.ts)

Adding features

Adding a feature is always: new file + one import line in the folder's index.ts.

# With Claude Code:
/new-tool weather "get the forecast for a city"
/new-resource changelog "expose CHANGELOG.md"
/new-prompt review "ask the model to review a diff"

Or by hand — copy src/tools/echo.tool.ts, define Zod schemas, register it in src/tools/index.ts, and add a test. See CLAUDE.md for the full conventions.

Testing & inspecting

pnpm test            # run once
pnpm test:watch      # watch mode
pnpm test:coverage   # coverage report
pnpm inspector       # launch the MCP Inspector against the server over stdio

Docker (HTTP transport)

docker build -t mcp-boilerplate .
docker run --rm -p 3000:3000 \
  -e ALLOWED_HOSTS=localhost,127.0.0.1 \
  mcp-boilerplate

# or with Docker Compose (includes a healthcheck):
docker compose up --build

Commits & releases

  • Conventional Commits are enforced by a commit-msg hook (commitlint). Use feat:, fix:, chore:, docs:, etc. — e.g. feat(tools): add weather tool.
  • Versioning uses changesets: pnpm changeset to record a change, pnpm changeset:version to bump + update the changelog, pnpm changeset:release to publish.

Claude Code tooling

This repo ships agent tooling under .claude/ and CLAUDE.md:

  • karpathy-guidelines skill — behavioral guardrails (think first, stay simple, surgical changes, verify) vendored from multica-ai/andrej-karpathy-skills.
  • mcp-add-tool / mcp-add-resource / mcp-add-prompt skills — encode this repo's exact patterns so new features stay consistent.
  • /new-tool / /new-resource / /new-prompt slash commands.
  • settings.json — a permission allowlist for the common project commands.

Scripts

Script Description
pnpm dev / pnpm dev:http Watch-mode dev (stdio / HTTP)
pnpm build Transpile src/ → dist/ with tsup
pnpm start / pnpm start:http Run the built server
pnpm typecheck tsc --noEmit
pnpm lint / pnpm lint:fix ESLint (type-checked)
pnpm format / pnpm format:check Prettier
pnpm test / pnpm test:watch / pnpm test:coverage Vitest
pnpm inspector MCP Inspector over stdio

License

MIT

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选