Fix critical OAuth token expiration bug causing 401 errors after 1 hour#1
Open
jb-xl-rnd wants to merge 1 commit intobengineer19:mainfrom
Open
Fix critical OAuth token expiration bug causing 401 errors after 1 hour#1jb-xl-rnd wants to merge 1 commit intobengineer19:mainfrom
jb-xl-rnd wants to merge 1 commit intobengineer19:mainfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
.envfilesTechnical Details
datetimeandthreadingimports for token lifecycle management_get_headers()to useTokenManager.get_token()instead of static token variableImpact
✅ 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.