OAuth MCP Server

OAuth MCP Server

An MCP server for OAuth 2.0 authentication supporting Device Code and Client Credentials flows, enabling secure token management for MCP applications.

Category
访问服务器

README

OAuth MCP Server

A Model Context Protocol (MCP) server for OAuth 2.0 authentication operations.

Overview

This server provides comprehensive OAuth 2.0 authentication support for MCP applications, implementing both:

  • OAuth 2.0 Device Authorization Grant Flow (RFC 8628) - User-delegated authentication
  • OAuth 2.0 Client Credentials Flow (RFC 6749) - App-only authentication

Features

  • Dual Authentication Flows - Support for both Device Code and Client Credentials flows
  • Token Management - Automatic token caching and expiry handling
  • MCP Integration - Compatible with Claude Desktop and other MCP clients
  • Comprehensive Logging - File and console logging with configurable levels
  • Secure Configuration - Environment-based configuration via .env file

Prerequisites

  • Python 3.8 or higher
  • pip (Python package manager)
  • Azure AD tenant (or other OAuth 2.0 provider)

Installation

  1. Navigate to the project directory:
cd oauth_mcp_server
  1. Create a virtual environment (recommended):
python -m venv venv

# Activate on Windows
venv\Scripts\activate

# Activate on Linux/Mac
source venv/bin/activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure environment variables:
    • Copy .env file and update with your configuration
    • Set authentication credentials (TENANT_ID, CLIENT_ID, CLIENT_SECRET)
    • Choose authentication method (CLIENT_CREDENTIALS or DEVICE_CODE)

Configuration

.env File

# =============================================================================
# OAuth MCP Server Configuration
# =============================================================================

# Logging Configuration
LOG_LEVEL=DEBUG
LOG_FILE=oauth_mcp_server.log

# Azure OAuth2 Authentication Configuration
TENANT_ID=your-tenant-id-here
CLIENT_ID=your-client-id-here
CLIENT_SECRET=your-client-secret-here
ACCESS_TOKEN_URL=https://login.microsoftonline.com/{TENANT_ID}/oauth2/v2.0/token

# OAuth2 Scope
OAUTH2_SCOPE=api://your-api-id/.default

# Authentication Method (CLIENT_CREDENTIALS or DEVICE_CODE)
AUTH_METHOD=CLIENT_CREDENTIALS

Authentication Methods

Client Credentials Flow

Best for server-to-server automation with no user interaction.

AUTH_METHOD=CLIENT_CREDENTIALS

Required environment variables:

  • TENANT_ID
  • CLIENT_ID
  • CLIENT_SECRET
  • OAUTH2_SCOPE

Device Code Flow

Best for interactive use with user authentication.

AUTH_METHOD=DEVICE_CODE

Required environment variables:

  • TENANT_ID
  • CLIENT_ID
  • OAUTH2_SCOPE

Claude Desktop Configuration

Add to your Claude Desktop config file:

Windows: %APPDATA%\Claude\claude_desktop_config.json

{
  "mcpServers": {
    "oauth-server": {
      "command": "C:\\Users\\YourUser\\AppData\\Local\\Programs\\Python\\Python313\\python.exe",
      "args": ["C:\\Users\\YourUser\\Documents\\Code\\oauth_mcp_server\\server.py"],
      "env": {
        "PYTHONPATH": "C:\\Users\\YourUser\\Documents\\Code\\oauth_mcp_server"
      }
    }
  }
}

Available Tools

Utility Functions

  • ping - Health check endpoint to verify server is running
  • get_server_info - Get server information and configuration

Device Code Flow Functions

  • start_device_auth - Initiate device authentication flow (Step 1)
  • complete_device_auth - Complete authentication and retrieve token (Step 2)
  • device_auth_flow - One-step device authentication (blocking)
  • read_bearer_token - Read saved bearer token from file

Client Credentials Flow Functions

  • get_azure_token - Get bearer token using Client Credentials flow
  • get_azure_token_info - Get detailed token information
  • test_azure_token - Test token by making authenticated API call
  • check_auth_config - Check and validate authentication configuration

Usage Examples

Using Client Credentials Flow

# In Claude Desktop, tokens are acquired automatically
# Just call the function and it handles authentication

# Get a token
token = get_azure_token()

# Get token details
info = get_azure_token_info()

# Test the token
result = test_azure_token()

Using Device Code Flow

# Step 1: Start device authentication
auth_info = start_device_auth()
# Follow the displayed instructions to complete authentication in browser

# Step 2: Complete authentication after user signs in
token_info = complete_device_auth()
# Token is returned in the response

# Use the token in subsequent API calls
bearer_token = token_info['access_token']

Running the Server

Standalone

python server.py

Via Claude Desktop

The server will start automatically when Claude Desktop launches.

Logging

Logs are written to both file and console:

  • Default log file: oauth_mcp_server.log in project directory
  • Format: timestamp - logger_name - level - message
  • Configurable: Set LOG_LEVEL in .env file

Project Structure

oauth_mcp_server/
├── server.py           # Main MCP server implementation
├── .env               # Environment configuration (not in git)
├── .gitignore        # Git ignore rules
├── README.md         # This file
├── test_token.bat    # Token testing utility (Windows)
└── requirements.txt  # Python dependencies

Integration with Omada MCP Server

This server is designed to work alongside the omada_mcp_server. The OAuth token acquisition functions have been migrated from omada_mcp_server to this dedicated OAuth server.

Workflow:

  1. Use oauth_mcp_server to obtain authentication tokens
  2. Pass tokens to omada_mcp_server functions as bearer_token parameter

Example:

# Get token from oauth_mcp_server
token = get_azure_token()

# Use token with omada_mcp_server
get_pending_approvals(
    impersonate_user='user@domain.com',
    bearer_token=token
)

Security Notes

  • Never commit .env file - contains sensitive credentials
  • Log files (.log) are excluded from git
  • Store all secrets in environment variables
  • Tokens are cached in memory only - not saved to disk by default
  • Tokens are automatically refreshed when expired (Client Credentials flow)

Testing

Test Token Acquisition (Windows)

test_token.bat

This script uses curl to test OAuth token acquisition directly.

Development

Adding New Tools

  1. Add the tool function in server.py with @mcp.tool() decorator
  2. Add type hints for parameters and return value
  3. Add comprehensive docstring
  4. Test the tool using Claude Desktop or MCP Inspector

Migration Notes

OAuth token functions have been migrated from omada_mcp_server to this dedicated OAuth server:

Migrated Functions:

  • AzureOAuth2Client class
  • get_cached_token() function
  • get_token_from_device_code_file() function
  • get_azure_token() MCP tool
  • get_azure_token_info() MCP tool
  • test_azure_token() MCP tool
  • check_auth_config() MCP tool (now check_auth_config())

Preserved in omada_mcp_server:

  • _prepare_graphql_request() - now requires bearer_token parameter
  • _execute_graphql_request() - now requires bearer_token parameter

Troubleshooting

Token Acquisition Fails

  1. Check .env file has correct credentials
  2. Verify TENANT_ID, CLIENT_ID, and CLIENT_SECRET are set
  3. Check OAUTH2_SCOPE matches your API configuration
  4. Review logs in oauth_mcp_server.log

Device Code Flow Times Out

  1. Default timeout is 5 minutes - complete authentication quickly
  2. Use start_device_auth() + complete_device_auth() for better control
  3. Check network connectivity to Azure endpoints

License

[Add your license information here]

Support

For issues or questions, please open an issue on GitHub or contact the development team.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选