A complete boilerplate for building remote Model Context Protocol (MCP) servers on Cloudflare Workers with custom OAuth authentication and PostgreSQL database integration.
This boilerplate provides everything you need to build production-ready MCP servers:
- 🔐 Complete OAuth 2.1 Provider - Custom OAuth implementation with user registration/login
- 🗄️ PostgreSQL Integration - Full database schema with user management and OAuth tokens
- ⚡ Cloudflare Workers - Serverless deployment with global edge distribution
- 🛠️ MCP Tools Framework - Modular tool system with user context
- 🔌 Custom Routes Framework - Easy-to-use system for adding REST API endpoints
- 🎨 Beautiful UI - Responsive login/registration pages with customizable consent screen
- 🔒 Security First - JWT tokens, bcrypt hashing, PKCE support
- 📱 Mobile Ready - Works on desktop, web, and mobile MCP clients
- 🔧 Developer Friendly - TypeScript, hot reload, comprehensive error handling
- add - Basic math operations
- userInfo - Get current user information
- personalGreeting - Personalized user greetings
- generateImage - AI image generation
- getUserStats - User statistics and analytics
Everything starts from src/index.ts - this is your main configuration file where you can easily:
The OAuth consent screen is fully customizable with pure HTML, CSS, and JavaScript located in src/auth/views/consent.ts. Features include:
- 🌙 Dark/Light Mode Toggle - Automatic system preference detection with manual override
- 📱 Responsive Design - Mobile-first design with Tailwind CSS
- 🎨 Custom Branding - Easy logo, colors, and styling customization
- ⚡ Real-time Updates - JavaScript-powered consent flow with loading states
- 🔒 Security Features - CSRF protection and secure form handling
Customize the consent screen:
The consent screen includes:
- Application info display with logos
- User profile information
- Granular permission scopes
- Terms of Service and Privacy Policy links
- Beautiful animations and transitions
- Error handling and loading states
This boilerplate includes a powerful custom routes framework that makes it easy to add REST API endpoints alongside your MCP tools:
- 🔒 Automatic Authentication - Protected routes get user context automatically
- 📝 Type Safety - Full TypeScript support with proper typing
- 🌐 HTTP Methods - Support for GET, POST, PUT, DELETE, PATCH
- 📊 Request Handling - Easy access to headers, body, query params
- 🔄 Response Helpers - JSON, HTML, redirect, and custom responses
- ⚡ Performance - Built on Hono framework for maximum speed
This makes it easy to create complete applications with both MCP tools and traditional REST APIs!
Before you start, ensure you have:
- Node.js 18+ - Download here
- Cloudflare Account - Sign up free
- PostgreSQL Database - See database options below
- Git - For cloning the repository
Choose one of these PostgreSQL hosting options:
- Go to neon.tech and create account
- Create a new project
- Copy the connection string from the dashboard
- Go to supabase.com and create account
- Create a new project
- Go to Settings → Database and copy the connection string
- Go to railway.app and create account
- Create a new PostgreSQL database
- Copy the connection string from the Connect tab
This boilerplate is optimized to work on Cloudflare but doesn't rely on Cloudflare systems - you can run it on your own servers with any hosting provider.
Cloudflare Hyperdrive dramatically improves database performance by connection pooling and global caching. To enable Hyperdrive:
-
Create a Hyperdrive configuration in your Cloudflare dashboard:
# Via Wrangler CLI wrangler hyperdrive create my-mcp-db --connection-string="postgresql://username:password@host:port/database" -
Update wrangler.toml to reference your Hyperdrive:
[[hyperdrive]] binding = "HYPERDRIVE" id = "your-hyperdrive-id" -
Use Hyperdrive in your environment:
# In .dev.vars or production secrets DATABASE_URL="postgresql://username:password@host:port/database" # Hyperdrive will automatically optimize this connection
Don't want to use Cloudflare? Simply change your deployment environment:
- Vercel: Use @vercel/node runtime with PostgreSQL
- Railway: Deploy directly with built-in PostgreSQL
- AWS Lambda: Use with RDS or Aurora Serverless
- Google Cloud Run: Deploy with Cloud SQL
- Self-hosted: Deploy with Docker and any PostgreSQL instance
The codebase uses standard PostgreSQL connections and OAuth2, making it platform-agnostic. Just update your:
- Database connection string
- Deployment configuration
- Environment variables
No Cloudflare-specific dependencies are required for core functionality.
Add your configuration:
🔒 Security Note: Generate strong secrets:
- Open your browser: Go to http://localhost:8787
- Register account: Create a new user account
- Test login: Login with your credentials
- Check tools: Visit http://localhost:8787/up to see available tools
- Set production secrets:
- Deploy to Cloudflare:
- Initialize production database:
- Update OAuth settings: Update any OAuth client redirect URIs to use your production URL.
- In Cloudflare dashboard, go to Workers & Pages
- Select your worker
- Go to Settings → Triggers
- Add custom domain
- Create tool file: src/tools/myTool.ts
- Export from index: Add to src/tools/index.ts
- Register in main: Add to src/index.ts
Add to your Claude Desktop config (~/Library/Application Support/Claude/claude_desktop_config.json on macOS):
- POST /mcp - Main MCP endpoint (requires OAuth)
- GET /up - Health check and server info
- POST /init-db - Initialize database (run once)
- Fork the repository
- Create a feature branch: git checkout -b feature/amazing-feature
- Make your changes
- Add tests if applicable
- Commit changes: git commit -m 'Add amazing feature'
- Push to branch: git push origin feature/amazing-feature
- Open a Pull Request
- Follow TypeScript best practices
- Add JSDoc comments for public APIs
- Update README for new features
- Test OAuth flows thoroughly
- Ensure database migrations are safe
MIT License - see LICENSE file for details.
- Model Context Protocol - The protocol this server implements
- Cloudflare Workers - Serverless platform
- Hono - Web framework for Cloudflare Workers
- @node-oauth/oauth2-server - OAuth2 implementation
Need help? Open an issue on GitHub or check the troubleshooting section above.