Cloudflare
coleam00/remote-mcp-server-with-auth · 294 stars · TypeScript · MIT
MCP server Template for a remote MCP server with GitHub OAuth - following best practices for building MCP servers so you can take this as a starting point for any MCP server you want to build!
Install
The repo has no one-line install. Follow its README.
Files
Cloudflare Remote PostgreSQL Database MCP Server + GitHub OAuth
This is a Model Context Protocol (MCP) server that enables you to chat with your PostgreSQL database, deployable as a remote MCP server with GitHub OAuth through Cloudflare. This is production ready MCP.
Key Features
- 🗄️ Database Integration with Lifespan: Direct PostgreSQL database connection for all MCP tool calls
- 🛠️ Modular, Single Purpose Tools: Following best practices around MCP tools and their descriptions
- 🔐 Role-Based Access: GitHub username-based permissions for database write operations
- 📊 Schema Discovery: Automatic table and column information retrieval
- 🛡️ SQL Injection Protection: Built-in validation and sanitization
- 📈 Monitoring: Optional Sentry integration for production monitoring
- ☁️ Cloud Native: Powered by Cloudflare Workers for global scale
Modular Architecture
This MCP server uses a clean, modular architecture that makes it easy to extend and maintain:
src/tools/- Individual tool implementations in separate filesregisterAllTools()- Centralized tool registration system- Extensible Design - Add new tools by creating files in
tools/and registering them
This architecture allows you to easily add new database operations, external API integrations, or any other MCP tools while keeping the codebase organized and maintainable.
Transport Protocols
This MCP server supports both modern and legacy transport protocols:
/mcp- Streamable HTTP (recommended): Uses a single endpoint with bidirectional communication, automatic connection upgrades, and better resilience for network interruptions/sse- Server-Sent Events (legacy): Uses separate endpoints for requests/responses, maintained for backward compatibility
For new implementations, use the /mcp endpoint as it provides better performance and reliability.
How It Works
The MCP server provides three main tools for database interaction:
listTables- Get database schema and table information (all authenticated users)queryDatabase- Execute read-only SQL queries (all authenticated users)executeDatabase- Execute write operations like INSERT/UPDATE/DELETE (privileged users only)
Authentication Flow: Users authenticate via GitHub OAuth → Server validates permissions → Tools become available based on user's GitHub username.
Security Model:
- All authenticated GitHub users can read data
- Only specific GitHub usernames can write/modify data
- SQL injection protection and query validation built-in
Simple Example First
Want to see a basic MCP server before diving into the full database implementation? Check out src/simple-math.ts - a minimal MCP server with a single calculate tool that performs basic math operations (add, subtract, multiply, divide). This example demonstrates the core MCP components: server setup, tool definition with Zod schemas, and dual transport support (/mcp and /sse endpoints). You can run it locally with wrangler dev --config wrangler-simple.jsonc and test at http://localhost:8789/mcp.
Prerequisites
- Node.js installed on your machine
- A Cloudflare account (free tier works)
- A GitHub account for OAuth setup
- A PostgreSQL database (local or hosted)
Getting Started
Step 1: Install Wrangler CLI
Install Wrangler globally to manage your Cloudflare Workers:
npm install -g wrangler
Step 2: Authenticate with Cloudflare
Log in to your Cloudflare account:
wrangler login
This will open a browser window where you can authenticate with your Cloudflare account.
Step 3: Clone and Setup
Clone the repo directly & install dependencies: npm install.
Environment Variables Setup
Before running the MCP server, you need to configure several environment variables for authentication and database access.
Create Environment Variables File
- Create your
.dev.varsfile from the example:
cp .dev.vars.example .dev.vars
- Configure all required environment variables in
.dev.vars:
# GitHub OAuth (for authentication)
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
COOKIE_ENCRYPTION_KEY=your_random_encryption_key
# Database Connection
DATABASE_URL=postgresql://username:password@localhost:5432/database_name
# Optional: Sentry monitoring
SENTRY_DSN=https://your-sentry-dsn@sentry.io/project-id
NODE_ENV=development
Getting GitHub OAuth Credentials
- Create a GitHub OAuth App for local development:
- Go to GitHub Developer Settings
- Click "New OAuth App"
- Application name:
MCP Server (Local Development) - Homepage URL:
http://localhost:8792 - Authorization callback URL:
http://localhost:8792/callback - Click "Register application"
- Copy your credentials:
- Copy the Client ID and paste it as
GITHUB_CLIENT_IDin.dev.vars - Click "Generate a new client secret", copy it, and paste as
GITHUB_CLIENT_SECRETin.dev.vars
Generate Encryption Key
Generate a secure random encryption key for cookie encryption:
openssl rand -hex 32
Copy the output and paste it as COOKIE_ENCRYPTION_KEY in .dev.vars.
Database Setup
- Set up PostgreSQL using a hosted service like:
- Update the DATABASE_URL in
.dev.varswith your connection string:
DATABASE_URL=postgresql://username:password@host:5432/database_name
#### Connection String Examples:
- Local:
postgresql://myuser:mypass@localhost:5432/mydb - Supabase:
postgresql://postgres:your-password@db.your-project.supabase.co:5432/postgres
Database Schema Setup
The MCP server works with any PostgreSQL database schema. It will automatically discover:
- All tables in the
publicschema - Column names, types, and constraints
- Primary keys and indexes
Testing the Connection: Once you have your database set up, you can test it by asking the MCP server "What tables are available in the database?" and then querying those tables to explore your data.
Local Development & Testing
Run the server locally:
wrangler dev
This makes the server available at http://localhost:8792
Testing with MCP Inspector
Use the MCP Inspector to test your server:
- Install and run Inspector:
npx @modelcontextprotocol/inspector@latest
- Connect to your local server:
- Preferred: Enter URL:
http://localhost:8792/mcp(streamable HTTP transport - newer, more robust) - Alternative: Enter URL:
http://localhost:8792/sse(SSE transport - legacy support) - Click "Connect"
- Follow the OAuth prompts to authenticate with GitHub
- Once connected, you'll see the available tools
- Test the tools:
- Use
listTablesto see your database structure - Use
queryDatabaseto run SELECT queries - Use
executeDatabase(if you have write access) for INSERT/UPDATE/DELETE operations
Production Deployment
#### Set up a KV namespace
- Create the KV namespace:
wrangler kv namespace create "OAUTH_KV"
- Update the
wrangler.jsoncfile with the KV ID (replace )
#### Deploy Deploy the MCP server to make it available on your workers.dev domain
wrangler deploy
Create environment variables in production
Create a new GitHub OAuth App:
- For the Homepage URL, specify
https://mcp-github-oauth.<your-subdomain>.workers.dev - For the Authorization callback URL, specify
https://mcp-github-oauth.<your-subdomain>.workers.dev/callback - Note your Client ID and generate a Client secret.
- Set all required secrets via Wrangler:
wrangler secret put GITHUB_CLIENT_ID
wrangler secret put GITHUB_CLIENT_SECRET
wrangler secret put COOKIE_ENCRYPTION_KEY # use: openssl rand -hex 32
wrangler secret put DATABASE_URL
wrangler secret put SENTRY_DSN # optional (more on Sentry setup below)
#### Test
Test the remote server using Inspector:
npx @modelcontextprotocol/inspector@latest
Enter https://mcp-github-oauth.<your-subdomain>.workers.dev/mcp (preferred) or https://mcp-github-oauth.<your-subdomain>.workers.dev/sse (legacy) and hit connect. Once you go through the authentication flow, you'll see the Tools working:
You now have a remote MCP server deployed!
Database Tools & Access Control
Available Tools
Facts
- Kind
- MCP server
- Repo
- coleam00/remote-mcp-server-with-auth
- Group
- Uncategorized
- Stars
- 294
- License
- MIT
- Language
- TypeScript
- Last push
- 2025-07-11
- Forks
- 143
- 1Everythingmodelcontextprotocol/serversThis MCP server attempts to exercise all the features of the MCP protocol. It is not intended to be a useful server, but rather a test server for builders of MCP clients. It implements prompts, tools, resources, sampling, and more to showcase MCP capabilities.85.8k
- 2Fetchmodelcontextprotocol/serversA Model Context Protocol server that provides web content fetching capabilities. This server enables LLMs to retrieve and process content from web pages, converting HTML to markdown for easier consumption.85.8k
- 3Gitmodelcontextprotocol/serversA Model Context Protocol server for Git repository interaction and automation. This server provides tools to read, search, and manipulate Git repositories via Large Language Models.85.8k
- 4Memorymodelcontextprotocol/serversA basic implementation of persistent memory using a local knowledge graph. This lets Claude remember information about the user across chats.85.8k
- 5Sequential Thinkingmodelcontextprotocol/serversAn MCP server implementation that provides a tool for dynamic and reflective problem-solving through a structured thinking process.85.8k
- 6Timemodelcontextprotocol/serversA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.85.8k