Govindmallurwar/mcp-oauth2-server
If you are the rightful owner of mcp-oauth2-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
The MCP OAuth2 Server is a protocol server designed to handle OAuth2 authentication and authorization processes efficiently.
š OAuth2 Authentication for MCP Server
A secure Model Context Protocol (MCP) server with OAuth 2.1 authentication that provides web search capabilities through the Tavily API. Built with FastAPI and designed for enterprise-grade security.
š Features
- š OAuth 2.1 Authentication - Enterprise-grade security using Scalekit authorization server
- š Web Search Integration - Powered by Tavily API for real-time web search capabilities
- ā” FastAPI Performance - High-performance async server with automatic API documentation
- š”ļø MCP Compliance - Full compliance with Model Context Protocol authorization specification
- šÆ Scope-based Authorization - Fine-grained access control with custom scopes
- š Token Validation - Robust token validation with audience checking
- š Security Best Practices - PKCE support, proper error handling, and secure defaults
šļø Architecture
This MCP server implements a secure architecture with:
- OAuth 2.1 Flow: Complete authorization flow with Scalekit
- MCP Protocol: Standardized communication between LLMs and tools
- Web Search Tool: Tavily-powered search functionality
- Middleware Security: Request-level authentication and authorization
š Prerequisites
- Python 3.11+ - Required for modern async features
- Scalekit Account - Sign up here
- Tavily API Key - Get your key here
š ļø Installation
1. Clone the Repository
git clone https://github.dev/Govindmallurwar/mcp-oauth2-server.git
cd mcp-oauth2-server
2. Install Dependencies
# Using pip
pip install -e .
# Or using uv (recommended)
uv sync
3. Environment Configuration
Create a .env
file with your configuration:
# Scalekit Configuration
SCALEKIT_ENVIRONMENT_URL=https://your-company.scalekit.com
SCALEKIT_CLIENT_ID=your_client_id
SCALEKIT_CLIENT_SECRET=your_client_secret
SCALEKIT_AUDIENCE_NAME=your_audience_name
# Server Configuration
PORT=10000
# Tavily API Configuration
TAVILY_API_KEY=your_tavily_api_key_here
4. Scalekit Setup
- Navigate to MCP servers in your Scalekit dashboard
- Click Add MCP server
- Configure:
- Server name: "Web Search MCP Server"
- Resource identifier: Your server URL
- Allow dynamic client registration: ā Enable
- Access token lifetime: 300-3600 seconds
- Scopes:
search:read
š Quick Start
Run the Server
python src/server.py
The server will start on http://localhost:10001
š API Documentation
OAuth Discovery Endpoints
GET /.well-known/oauth-protected-resource/mcp
- MCP resource metadata
MCP Endpoints
POST /tools/call
- Execute web search tool (requires authentication)
Authentication Flow
- Discovery: Client discovers authorization server via metadata endpoint
- Registration: Dynamic client registration with Scalekit
- Authorization: OAuth 2.1 flow with PKCE
- Token Usage: Bearer token in Authorization header
- Validation: Server validates token and checks scopes
š§ Configuration
Environment Variables
Variable | Description | Required |
---|---|---|
SCALEKIT_ENVIRONMENT_URL | Your Scalekit environment URL | ā |
SCALEKIT_CLIENT_ID | Scalekit client ID | ā |
SCALEKIT_CLIENT_SECRET | Scalekit client secret | ā |
SCALEKIT_AUDIENCE_NAME | Token audience identifier | ā |
PORT | Server port (default: 10001) | ā |
TAVILY_API_KEY | Tavily API key for web search | ā |
Security Settings
The server implements several security features:
- Token Audience Validation: Ensures tokens are issued for this specific server
- Scope-based Authorization: Controls access to web search functionality
- PKCE Support: Protects against authorization code interception
- Proper Error Responses: Returns WWW-Authenticate headers as per MCP spec
š Troubleshooting
Common Issues
Issue | Solution |
---|---|
Token validation fails | Check SCALEKIT_AUDIENCE_NAME matches Scalekit config |
Client registration fails | Verify Scalekit environment URL and credentials |
Web search not working | Ensure Tavily API key is valid and has quota |
CORS errors | Configure allow_origins in production |
Debug Mode
Enable debug logging:
import logging
logging.basicConfig(level=logging.DEBUG)
Health Checks
# Check OAuth metadata
curl http://localhost:10000/.well-known/oauth-protected-resource/mcp
š Project Structure
mcp-oauth2-server/
āāā src/
ā āāā server.py # Main FastAPI server
ā āāā auth.py # OAuth middleware
ā āāā tavily_mcp.py # MCP server implementation
ā āāā config.py # Configuration management
āāā docs/
āāā pyproject.toml # Project dependencies
āāā .env # Environment with placeholder
āāā README.md # This file
š License
This project is licensed under the MIT License - see the file for details.