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.
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
- Navigate to the project directory:
cd oauth_mcp_server
- Create a virtual environment (recommended):
python -m venv venv
# Activate on Windows
venv\Scripts\activate
# Activate on Linux/Mac
source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Configure environment variables:
- Copy
.envfile and update with your configuration - Set authentication credentials (TENANT_ID, CLIENT_ID, CLIENT_SECRET)
- Choose authentication method (CLIENT_CREDENTIALS or DEVICE_CODE)
- Copy
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_IDCLIENT_IDCLIENT_SECRETOAUTH2_SCOPE
Device Code Flow
Best for interactive use with user authentication.
AUTH_METHOD=DEVICE_CODE
Required environment variables:
TENANT_IDCLIENT_IDOAUTH2_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 runningget_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 flowget_azure_token_info- Get detailed token informationtest_azure_token- Test token by making authenticated API callcheck_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.login project directory - Format:
timestamp - logger_name - level - message - Configurable: Set
LOG_LEVELin.envfile
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:
- Use
oauth_mcp_serverto obtain authentication tokens - Pass tokens to
omada_mcp_serverfunctions asbearer_tokenparameter
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
.envfile - 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
- Add the tool function in
server.pywith@mcp.tool()decorator - Add type hints for parameters and return value
- Add comprehensive docstring
- 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:
AzureOAuth2Clientclassget_cached_token()functionget_token_from_device_code_file()functionget_azure_token()MCP toolget_azure_token_info()MCP tooltest_azure_token()MCP toolcheck_auth_config()MCP tool (nowcheck_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
- Check
.envfile has correct credentials - Verify
TENANT_ID,CLIENT_ID, andCLIENT_SECRETare set - Check
OAUTH2_SCOPEmatches your API configuration - Review logs in
oauth_mcp_server.log
Device Code Flow Times Out
- Default timeout is 5 minutes - complete authentication quickly
- Use
start_device_auth()+complete_device_auth()for better control - 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
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。