mcp-oauth2-server

Govindmallurwar/mcp-oauth2-server

3.1

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

Python FastAPI MCP OAuth2.1

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

šŸ› ļø 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

  1. Navigate to MCP servers in your Scalekit dashboard
  2. Click Add MCP server
  3. 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

  1. Discovery: Client discovers authorization server via metadata endpoint
  2. Registration: Dynamic client registration with Scalekit
  3. Authorization: OAuth 2.1 flow with PKCE
  4. Token Usage: Bearer token in Authorization header
  5. Validation: Server validates token and checks scopes

šŸ”§ Configuration

Environment Variables

VariableDescriptionRequired
SCALEKIT_ENVIRONMENT_URLYour Scalekit environment URLāœ…
SCALEKIT_CLIENT_IDScalekit client IDāœ…
SCALEKIT_CLIENT_SECRETScalekit client secretāœ…
SCALEKIT_AUDIENCE_NAMEToken audience identifierāœ…
PORTServer port (default: 10001)āŒ
TAVILY_API_KEYTavily 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

IssueSolution
Token validation failsCheck SCALEKIT_AUDIENCE_NAME matches Scalekit config
Client registration failsVerify Scalekit environment URL and credentials
Web search not workingEnsure Tavily API key is valid and has quota
CORS errorsConfigure 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.