A production-ready example of a Model Context Protocol (MCP) server with OAuth 2.0 authentication, demonstrating best practices for secure, scalable MCP deployments.
This repository provides a complete implementation of an MCP server that:
- Shows how to add authentication to MCP servers using OAuth 2.0
- Demonstrates all MCP protocol features (tools, resources, prompts)
- Uses a scalable architecture suitable for production deployments
- Serves as a learning resource and starting template for your own MCP servers
The Model Context Protocol is an open standard that enables seamless integration between AI applications and external data sources, tools, and services.
For detailed installation instructions, see Installation below.
Get the server running in 5 minutes:
# 1. Prerequisites
brew install orbstack # macOS: Install OrbStack (skip if already installed)
orbctl start # macOS: Start OrbStack daemon
# OR install Docker Desktop and start it (Windows/Linux/macOS alternative)
# 2. Setup
git clone https://github.com/modelcontextprotocol/example-remote-server.git
cd example-remote-server
npm install # Install dependencies for all workspaces
# 3. Start services
docker compose up -d # Start Redis
npm run dev # Start both auth-server and mcp-server
# 4. Test with Inspector
npx -y @modelcontextprotocol/inspector
# Connect to http://localhost:3232/mcp
# (Make sure to include the http:// prefix!)
- Node.js >= 16
- npm or yarn
- Docker runtime (for Redis)
Choose one option:
macOS (Recommended: OrbStack)
brew install orbstack
# Start OrbStack daemon (required before using Docker commands)
orbctl start
# Or download from https://orbstack.dev/download
Windows/Linux: Docker Desktop
- Download from https://www.docker.com/products/docker-desktop
- Start Docker Desktop after installation
Alternative: Local Redis Installation
# macOS
brew install redis && brew services start redis
# Ubuntu/Debian
sudo apt-get install redis-server && sudo systemctl start redis
git clone https://github.com/modelcontextprotocol/example-remote-server.git
cd example-remote-server
npm install # Installs dependencies for all workspaces
# Ensure Docker/OrbStack is running first!
docker compose up -d
# Verify Redis is running
docker compose ps
# Start both servers concurrently
npm run dev
# Or start them individually:
npm run dev:auth-server # Auth server on http://localhost:3001
npm run dev:mcp-server # MCP server on http://localhost:3232
The easiest way to test the server:
# 1. Ensure servers are running (npm run dev)
# 2. Launch Inspector
npx -y @modelcontextprotocol/inspector
# 3. Connect to http://localhost:3232/mcp
# 4. Complete OAuth flow in the Auth tab
# 5. Test tools, resources, and prompts interactively
The examples/
directory contains runnable code demonstrating OAuth and MCP interactions:
client.js
: Complete Node.js example with OAuth flow and MCP operationscurl-examples.sh
: Shell script showing raw HTTP/curl usage
See examples/README.md for detailed usage.
- Cause: Token expired or servers not communicating
- Solution:
- Ensure both servers are running (
npm run dev
) - Check that Redis is running (
docker compose ps
) - Verify the token hasn't expired (tokens last 7 days)
- Ensure both servers are running (
- Cause: Incorrect URL format or servers not running
- Solution:
- Use the full URL with http:// prefix:
http://localhost:3232/mcp
- Ensure both auth and MCP servers are running (
npm run dev
)
- Use the full URL with http:// prefix:
- Cause: Using SSE transport with Streamable HTTP endpoint
- Solution:
- For SSE transport (legacy): Use
http://localhost:3232/sse
- For Streamable HTTP (recommended): Use
http://localhost:3232/mcp
- In MCP Inspector: Match transport type with the correct endpoint
- For SSE transport (legacy): Use
- Cause: Docker/OrbStack not running
- Solution:
- macOS with OrbStack:
orbctl start
(verify withorbctl status
) - Windows/Linux/macOS with Docker Desktop: Start Docker Desktop application
- macOS with OrbStack:
- Cause: Redis container not running
- Solution:
- Check Redis is running:
docker compose ps
- If not running:
docker compose up -d
- Ensure Docker/OrbStack is started first
- Check Redis is running:
- Cause: Another process using port 3001 or 3232
- Solution:
- Check for existing processes:
lsof -i :3232
orlsof -i :3001
- Kill existing processes or change PORT in .env files
- Check for existing processes:
- Cause: Auth server is not running or not reachable
- What happens: MCP server runs in degraded mode
- Splash page accessible with warning banner
- Health endpoint shows degraded status:
curl http://localhost:3232/health
- Protected MCP endpoints return 503 with helpful error message
- Solution:
- Start the auth server:
npm run dev
(starts both servers) - Or start manually:
cd auth-server && npm run dev
- Restart the MCP server to retry connection
- Start the auth server:
- Cause: Misconfiguration or servers not communicating
- Solution:
- Check server logs for error messages
- Ensure Redis is running:
docker compose ps
- Verify .env configuration in both server directories
- Check that both servers are running
- Tools: 7 demonstration tools including echo, add, long-running operations, LLM sampling, image handling, annotations, and resource references
- Resources: 100 example resources with pagination, templates, and subscription support
- Prompts: Simple and complex prompts with argument support and resource embedding
- Sampling: Integration with MCP sampling for LLM interactions
- Completions: Auto-completion support for prompt arguments
- Logging: Multi-level logging with configurable verbosity
- Notifications: Progress updates, resource updates, and stderr messages
- Streamable HTTP: Full implementation with GET/POST/DELETE support
- SSE Transport: Backwards-compatible Server-Sent Events
- Redis Integration: Pub/sub message routing and session state management
- Session Management: 5-minute TTL with automatic cleanup
- Horizontal Scaling: Any instance can handle any request
- OAuth 2.0: Complete authorization flow with PKCE support
- Separate Auth Server: Demonstrates integration with external OAuth providers
- Token Introspection: Validates tokens via RFC 7662
- Session Ownership: User isolation and access control
- Security Headers: CSP, HSTS, X-Frame-Options, and more
- Bearer Token Auth: Middleware for protected endpoints
This repository demonstrates a production-ready MCP deployment pattern with separate authorization and resource servers:
auth-server/ # OAuth 2.0 authorization server (demo only - replace in production)
└── src/ # Authorization endpoints and token management
mcp-server/ # MCP resource server (customize tools/resources/prompts)
└── src/ # MCP protocol implementation with external auth
scripts/ # Testing and deployment scripts
docs/ # Architecture and API documentation
examples/ # Example code and usage patterns
The architecture separates authentication concerns from MCP functionality, allowing you to integrate with commercial OAuth providers (Auth0, Okta, etc.).
Per the MCP Authorization specification, this implementation demonstrates a production-ready pattern with separate authorization and resource servers.
MCP servers often handle sensitive operations and data. This implementation shows how to:
- Authenticate users before granting access to MCP resources
- Integrate with existing identity providers (Auth0, Okta, Google)
- Implement user session isolation in multi-tenant environments
- Scale horizontally while maintaining security
sequenceDiagram
participant User
participant Client as Your App
participant Auth as Auth Server (:3001)
participant MCP as MCP Server (:3232)
User->>Client: Wants to use MCP
Client->>Auth: 1. Register & OAuth flow
Auth->>User: 2. Login page
User->>Auth: 3. Authenticate
Auth->>Client: 4. Access token
Client->>MCP: 5. MCP request + token
MCP->>Auth: 6. Validate token
Auth->>MCP: 7. Token valid ✓
MCP->>Client: 8. MCP response
Client->>User: 9. Results
This architecture pattern:
- Separates concerns: Auth server handles OAuth, MCP server handles protocol
- Enables integration: Can replace auth server with Auth0, Okta, etc.
- Scales independently: Auth and MCP servers can scale based on their load
- Follows standards: Uses OAuth 2.0 and token introspection (RFC 7662)
The /mock-upstream-idp
endpoints simulate what a real identity provider (Google, GitHub, corporate SSO) would do. In production, users would be redirected to their actual identity provider for login. This mock implementation helps you test the complete flow locally without external dependencies.
For detailed OAuth flow analysis including data storage and TTLs, see docs/oauth-flow.md. For alternative patterns like embedded OAuth, see docs/oauth-architecture-patterns.md.
Each server has its own .env
file:
Auth Server (auth-server/.env
):
AUTH_SERVER_URL=http://localhost:3001 # Auth server URL
AUTH_SERVER_PORT=3001 # Auth server port
BASE_URI=http://localhost:3232 # MCP server URL (for redirects)
REDIS_URL=redis://localhost:6379 # Redis connection
MCP Server (mcp-server/.env
):
BASE_URI=http://localhost:3232 # MCP server URL
PORT=3232 # MCP server port
AUTH_SERVER_URL=http://localhost:3001 # External auth server URL
REDIS_URL=redis://localhost:6379 # Redis connection
This is a reference implementation with demo tools, resources, and prompts. To adapt it for production:
- Replace MCP features: See Customization Guide for replacing demo functionality with your own
- Integrate OAuth provider: See OAuth Architecture Patterns for production authentication setup
# Start servers
npm run dev # Both servers concurrently
npm run dev:auth-server # Auth server only
npm run dev:mcp-server # MCP server only
npm run build # Build all workspaces
npm run start # Run both servers
npm run start:auth-server # Run auth server only
npm run start:mcp-server # Run MCP server only
npm test # All unit tests
npm run lint # Lint all code
npm run typecheck # Typecheck all code
npm run test:e2e # End-to-end test
The E2E script in scripts/test-e2e.sh
verifies the complete OAuth flow and all MCP features:
npm run test:e2e
The script:
- Automatically manages server lifecycle
- Tests OAuth 2.0 + PKCE flow
- Verifies all MCP features (tools, resources, prompts)
- Validates token introspection between servers
- Tests session management and ownership
Structured JSON logging with sanitized outputs:
- HTTP request/response logging
- Authentication events
- Session lifecycle events
- Redis operations
- Error tracking
# Monitor session ownership
redis-cli KEYS "session:*:owner"
# Watch real-time operations
redis-cli MONITOR | grep "session:"
# Check active sessions
redis-cli PUBSUB CHANNELS "mcp:shttp:toserver:*"
# Debug specific session
redis-cli GET "session:{sessionId}:owner"
- MCP Inspector for interactive debugging
- Comprehensive test suite
- Hot-reload development mode
- Source maps for debugging
- Redis monitoring commands
For a complete listing of all endpoints provided by each server, including OAuth authorization endpoints, MCP resource endpoints, and demo identity provider endpoints, see docs/endpoints.md.
The server is designed for horizontal scaling using Redis as the backbone:
- Redis Storage: All session state stored in Redis
- 5-minute TTL: Automatic session cleanup
- Session Ownership: User isolation via Redis keys
- Stateless Servers: Any instance can handle any request
- Pub/Sub Channels: Redis channels for message distribution
- Message Buffering: Reliable delivery for disconnected clients
- Connection State: Tracked via pub/sub subscription counts
- Automatic Cleanup: No explicit cleanup required
session:{sessionId}:owner # Session ownership
mcp:shttp:toserver:{sessionId} # Client→Server messages
mcp:shttp:toclient:{sessionId}:{requestId} # Server→Client responses
mcp:control:{sessionId} # Control messages
auth:client:{clientId} # OAuth client registrations
auth:pending:{authCode} # Pending authorizations
auth:installation:{accessToken} # Active MCP installations
auth:exch:{authCode} # Token exchanges
auth:refresh:{refreshToken} # Refresh tokens
Note: The auth:
prefix ensures complete namespace isolation between auth and MCP functions.
Modern transport supporting bidirectional communication over HTTP:
- Single endpoint for all operations
- Session management via headers
- Efficient message buffering
- Automatic reconnection support
Backwards-compatible transport using SSE:
- Separate endpoints for SSE streams and messages
- Session management via URL parameters
- Redis-backed message routing
- Real-time event delivery
- Authentication: OAuth 2.0 with bearer tokens
- Token Validation: Introspection via RFC 7662
- Authorization: User-based session ownership
- Session Isolation: Users can only access their own sessions
- Security Headers:
- Content Security Policy (CSP)
- Strict Transport Security (HSTS)
- X-Frame-Options
- X-Content-Type-Options
- Input Validation: Zod schemas for all inputs
- Error Handling: Sanitized error responses
- Always use HTTPS in production
- Configure proper CORS origins
- Use strong client secrets
- Enable all security headers
- Monitor session lifetimes
- Implement rate limiting
- Use structured logging
- Model Context Protocol Documentation
- MCP Authorization Specification
- MCP Specification
- MCP Concepts
- TypeScript SDK
- RFC 6749: OAuth 2.0 Framework
- RFC 7662: Token Introspection
- OAuth 2.1 Draft - Modern security best practices
- OAuth.net: End User Authentication
- The Resource Server - OAuth 2.0 Simplified
We welcome contributions!
- Fork the repository
- Create a feature branch
- Implement your changes
- Add tests for new functionality
- Ensure all tests pass
- Run linting and fix issues
- Submit a pull request
- TypeScript with strict mode
- ESLint configuration included
- Comprehensive type definitions
This project is licensed under the MIT License - see the LICENSE file for details.