Skip to content

Fix critical OAuth token expiration bug causing 401 errors after 1 hour#1

Open
jb-xl-rnd wants to merge 1 commit intobengineer19:mainfrom
jb-xl-rnd:fix/oauth-token-expiration
Open

Fix critical OAuth token expiration bug causing 401 errors after 1 hour#1
jb-xl-rnd wants to merge 1 commit intobengineer19:mainfrom
jb-xl-rnd:fix/oauth-token-expiration

Conversation

@jb-xl-rnd
Copy link

Problem

The server currently fails after ~1 hour when the OAuth2 access token expires, causing all API requests to return 401 Unauthorized errors. This makes the server unusable for any session longer than the token lifetime (typically 3600 seconds).

Solution

This PR implements automatic OAuth token refresh with the following features:

  • TokenManager class - Handles token lifecycle management
  • Automatic refresh - Tokens are refreshed 5 minutes before expiration to prevent race conditions
  • Thread-safe - Uses mutex locks for concurrent access safety
  • Zero configuration - No changes required to existing setup or .env files

Technical Details

  • DigiKey OAuth2 tokens expire after 3600 seconds (1 hour)
  • TokenManager checks expiration before each API call
  • Added datetime and threading imports for token lifecycle management
  • Updated _get_headers() to use TokenManager.get_token() instead of static token variable

Impact

✅ Server can now run reliably 24/7 without authentication failures
✅ No user intervention required for token management
✅ Fully backwards compatible - no breaking changes

Testing

Verified with multiple API endpoints (keyword_search, product_details, etc.) over extended runtime periods. Confirmed automatic token refresh works correctly without interrupting service.


Note: I've also extended this server with additional features (3 new endpoints, context optimization, enhanced error handling) in my fork at https://github.com/jb-xl-rnd/digikey_mcp if you're interested. This PR contains only the critical bug fix to keep it focused and easy to review.

PROBLEM:
The server fails after ~1 hour when the OAuth2 access token expires,
causing all API requests to return 401 Unauthorized errors. This makes
the server unusable for any session longer than the token lifetime.

SOLUTION:
- Added TokenManager class to handle automatic token refresh
- Tokens are refreshed 5 minutes before expiration to prevent race conditions
- Thread-safe implementation using mutex locks
- Automatic token refresh on every API call if needed

IMPACT:
- Server can now run reliably 24/7 without authentication failures
- No user intervention required for token management
- Backwards compatible - no configuration changes needed

TECHNICAL DETAILS:
- DigiKey OAuth2 tokens expire after 3600 seconds (1 hour)
- TokenManager checks expiration before each API call
- Added datetime and threading imports for token lifecycle management
- Updated _get_headers() to use TokenManager.get_token() instead of static token

TESTING:
Verified with multiple API endpoints over extended runtime periods.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant