Overview
createMCPServer is the main entry point for building an MCP server. It wires together your tool definitions, storage backends, authentication strategy, and HTTP routing into a single object that Cloudflare Workers can export directly.
Cloudflare Workers is the primary supported runtime. The returned
MCPServer
object exposes a fetch method that matches the Workers module export
interface, so you can use export default directly.Parameters
Configuration object for the server.
Return value
Returns anMCPServer object:
fetch method is called by the Workers runtime on every incoming request. It reads environment variables from env, initializes storage (KV with memory fallback), parses the active auth strategy, and routes the request to the appropriate handler.
Usage
Basic server
With multiple tools
Environment variables
createMCPServer reads all configuration from the Worker’s env object at request time. The following bindings and variables are used:
| Variable | Required | Description |
|---|---|---|
TOKENS | Yes | Cloudflare KV namespace binding for token and session storage |
RS_TOKENS_ENC_KEY | Yes (production) | Base64url-encoded 32-byte AES-256-GCM encryption key |
AUTH_STRATEGY | No | oauth | bearer | api_key | custom | none (inferred if unset) |
RS_TOKENS_ENC_KEY is not set in production, tokens are stored in KV unencrypted and a warning is logged. In local development this is acceptable.
Storage
createMCPServer sets up token and session storage automatically based on your environment:
- If a
TOKENSKV namespace binding is present, it uses Cloudflare KV with an in-memory fallback. - If
RS_TOKENS_ENC_KEYis set, all stored values are encrypted with AES-256-GCM. - If
TOKENSis missing, the server returns503 Service Unavailableon every request.