Implementation Reference: The authentication logic described in this page is implemented in apps/backend/src/middleware/api-key-oauth.middleware.ts
.
Authentication Scenarios
MetaMCP supports four different authentication configurations, each with specific behaviors:1. Both API Key and OAuth Disabled
Configuration:enable_api_key_auth: false
, enable_oauth: false
Behavior:
- All requests pass through without authentication
- No authentication headers required
- Suitable for public endpoints that don’t require authentication
2. API Key Only (OAuth Disabled)
Configuration:enable_api_key_auth: true
, enable_oauth: false
Behavior:
- Requires valid API key via
X-API-Key
header or query parameter - Critical Issue: If API key is missing or invalid, some MCP clients (like Inspector) may attempt OAuth flow
- This can cause infinite refresh loops and 429 rate limit errors
- Infinite token refresh attempts
- 429 “Too Many Requests” errors
- Inspector becoming unusable
3. Both API Key and OAuth Enabled
Configuration:enable_api_key_auth: true
, enable_oauth: true
Behavior:
- Accepts both API keys and OAuth bearer tokens
- API key takes precedence if provided
- Falls back to OAuth if no API key is provided
- Most flexible configuration
-
No Token Provided: Initiates OAuth flow
-
Valid API Key: Passes through
-
Valid OAuth Token: Passes through
-
Invalid Credentials: Returns 401 with rate limiting
4. OAuth Only (API Key Disabled)
Configuration:enable_api_key_auth: false
, enable_oauth: true
Behavior:
- Requires OAuth bearer token
- No API key support
- Clean OAuth-only authentication
Common Issues and Solutions
Issue 1: Infinite OAuth Refresh Loop
Symptoms:- MCP Inspector continuously refreshes tokens
- 429 “Too Many Requests” errors
- Inspector becomes unresponsive
-
Enable OAuth in endpoint configuration:
-
Configure MCP client to use API keys only:
- Update client configuration to use
X-API-Key
header - Disable OAuth flow in client settings
- Update client configuration to use
-
Use OAuth-only configuration:
Issue 2: 429 Rate Limit Errors
Symptoms:- “Too many failed authentication attempts” errors
- Temporary lockout from authentication attempts
- Wait for rate limit to reset (1 minute)
- If you have enabled Auth, make sure you never pass in a wrong API key.
Issue 3: Access Denied (403) Errors
Symptoms:- “Access denied” errors even with valid credentials
- “Public API keys cannot access private endpoints” messages
- For Private Endpoints: Use API key owned by endpoint creator
- For Public Endpoints: Any valid API key or OAuth token works
- Check endpoint ownership:
Issue 4: Wrong Authentication Method
Symptoms:- “Authentication required via API key” when using OAuth
- “Authentication required via OAuth bearer token” when using API key
-
Check endpoint configuration:
-
Use correct authentication method:
- API key:
X-API-Key
header orapi_key
query parameter - OAuth:
Authorization: Bearer
header
- API key: