A Cloudflare Worker template for building MCP (Model Context Protocol) servers with OAuth 2.1 + PIN authentication.
Works with Claude.ai web and Claude Code CLI out of the box.
- OAuth 2.1 + PIN: Secure access via PIN-protected authorization
- D1 Storage: OAuth tokens stored in Cloudflare D1
- MCP Protocol: JSON-RPC based tool calling
- Example Tool: Includes echo tool as a starting point
# Clone or copy this template
cd my-mcp-server
npm install
# Create D1 database
wrangler d1 create my-mcp-server
# Update wrangler.toml with your database_idwrangler d1 execute my-mcp-server --file=schema.sqlwrangler secret put AUTHORIZE_PIN
# Enter a secure PIN when promptedwrangler deploy- Go to Claude.ai Settings > Connectors
- Click "Add custom connector"
- Enter your worker URL:
https://my-mcp-server.YOUR-SUBDOMAIN.workers.dev - Enter your PIN when prompted
claude mcp add --transport http \
my-mcp-server https://my-mcp-server.YOUR-SUBDOMAIN.workers.dev/mcpEdit src/mcp.ts:
- Add your tool definition to
TOOLS:
const TOOLS = {
echo: { ... },
my_tool: {
name: 'my_tool',
description: 'What this tool does',
inputSchema: {
type: 'object',
properties: {
param1: { type: 'string', description: 'Parameter description' },
},
required: ['param1'],
},
},
};- Add the handler in the
tools/callswitch:
case 'my_tool': {
const param1 = args['param1'] as string;
result = await doSomething(param1, env);
break;
}Edit src/oauth.ts:
const SERVER_NAME = 'My MCP Server';
const SCOPES = ['my:read', 'my:write'];Add tables to schema.sql and types to src/types.ts.
Cloudflare Worker
├── OAuth 2.1 (DCR, PKCE, PIN auth)
└── D1 (token storage)
MIT