mcp-worker-template

mcp-worker-template

An MCP server template for Cloudflare Workers with zero runtime dependencies, providing a hand-rolled Streamable HTTP transport that lets you easily define and deploy tools with static bearer token auth.

Category
访问服务器

README

mcp-worker-template

An MCP server on Cloudflare Workers with zero runtime dependencies. No SDK, no framework — a hand-rolled Streamable HTTP transport in ~190 lines you can read in one sitting, plus one file where your tools live.

git clone https://github.com/AayushCharde/mcp-worker-template my-mcp-server
cd my-mcp-server && npm install && cp .dev.vars.example .dev.vars
npm run dev   # → http://localhost:8787/mcp

Test it:

curl -s http://localhost:8787/mcp \
  -H 'Authorization: Bearer dev-token-change-me' \
  -H 'Content-Type: application/json' \
  -d '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"echo","arguments":{"message":"hello"}}}'

Connect it to Claude Code:

claude mcp add --transport http my-server http://localhost:8787/mcp \
  --header "Authorization: Bearer dev-token-change-me"

Why no SDK?

Most MCP server examples pull in the official SDK, an HTTP framework, and a session layer. For a tools-only server, the protocol surface is four methods: initialize, tools/list, tools/call, ping. That's small and stable enough to own outright:

  • Zero runtime dependencies — nothing to update, nothing to audit, no bundle bloat. The Worker is two source files.
  • Nothing is magic — when a client misbehaves, you read your own 190-line transport, not a stack trace through someone else's abstraction.
  • Stateless by design — no sessions, no SSE stream, no Durable Objects. Every request is self-contained, which is exactly the shape Cloudflare Workers want.

The trade-off: no server→client notifications, resources, or prompts. If you need those, use the official SDK — this template is for the (very common) case where you just want tools.

Adding a tool

Everything you touch is in src/tools.ts. A tool is a name, a description, a JSON Schema for its input, and a run function:

{
  name: 'get_weather',
  description: 'Current weather for a city.',
  inputSchema: {
    type: 'object',
    properties: { city: { type: 'string' } },
    required: ['city'],
    additionalProperties: false
  },
  async run(args, { env }) {
    const res = await fetch(`https://api.example.com/weather?q=${args.city}`);
    if (!res.ok) throw new ToolError(`Weather API returned ${res.status}`);
    return res.json();
  }
}

Throw ToolError for failures the client should see verbatim; any other exception is masked as a generic internal error so implementation details never leak. Need bindings (KV, D1, a database URL)? Add them to Environment in src/index.ts — they arrive in every tool via ctx.env.

Auth model — read this before deploying

Auth is a single static bearer token (MCP_BEARER_TOKEN), compared in constant time. That token is the entire trust boundary: anyone who has it can call every tool. This is the right shape for a personal server or an internal integration; it is not multi-tenant. If different users need different permissions, you need OAuth (see the MCP authorization spec) — at which point the official SDK starts earning its keep.

openssl rand -hex 32                      # generate a real token
npx wrangler secret put MCP_BEARER_TOKEN  # set it in production

Deploy

npm run check     # wrangler types + tsc --noEmit
npm run deploy    # → https://my-mcp-server.<your-subdomain>.workers.dev/mcp

Rename the worker in wrangler.jsonc first.

Transport details (for the curious)

The spec-relevant behavior, all in src/index.ts:

Behavior Implementation
Protocol versions Negotiates 2025-06-18 / 2025-03-26 / 2024-11-05 — echoes the client's version if supported, else offers the newest
Notifications No response body; HTTP 202 Accepted
JSON-RPC batches Accepted; responses filtered of notification slots
GET /mcp 405 Method Not Allowed — stateless server, no SSE stream to offer
Unknown method -32601; unknown tool -32602; parse failure -32700
CORS Permissive by default (*) — tighten Access-Control-Allow-Origin if a browser client will hold your token

Real-world example

This transport was extracted from Junto, a keyboard-first task tracker whose MCP server lets Claude list, create, and update tasks in a live Postgres database — see apps/mcp there for what a production instance of this pattern looks like (per-request DB clients, workspace scoping, activity logging).

License

MIT © Aayush Charde

推荐服务器

Baidu Map

Baidu Map

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

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

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

官方
精选
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

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

官方
精选
本地
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

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

官方
精选
本地
TypeScript
VeyraX

VeyraX

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

官方
精选
本地
Kagi MCP Server

Kagi MCP Server

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

官方
精选
Python
graphlit-mcp-server

graphlit-mcp-server

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

官方
精选
TypeScript
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 模型以安全和受控的方式获取实时的网络信息。

官方
精选