mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 19:08:59 -05:00
[GH-ISSUE #19809] MCP OAuth tokens not proactively refreshed, causing session loss after 1 hour #19004
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jamie-dit on GitHub (Dec 8, 2025).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/19809
Bug Description
MCP OAuth 2.1 tokens (e.g., Notion) expire after ~1 hour but are not proactively refreshed. The current refresh mechanism only triggers when
get_oauth_token()is called within 5 minutes of expiration. If the user isn't actively using the MCP tool during that window, the token expires, and the session is deleted on the next access attempt.Steps to Reproduce
Expected Behavior
The OAuth token should be automatically refreshed in the background before expiration using the stored
refresh_token, maintaining a valid session without user intervention.Actual Behavior
get_oauth_token()finds the expired session"Token refresh failed... deleting session"Logs
Root Cause Analysis
In
backend/open_webui/utils/oauth.py, theget_oauth_token()method has proactive refresh logic:However, this only runs on-demand when
get_oauth_token()is called. There is no background task to refresh tokens before they expire.The
oauth_sessiontable has an index onexpires_atbut no method to query sessions needing refresh.Proposed Solution
Add a periodic background task that:
refresh_tokenasyncio.create_task()in the lifespan handlerSimilar to the existing
periodic_usage_pool_cleanup()pattern.Environment
mcp.notion.com)Related Issues
@owui-terminator[bot] commented on GitHub (Dec 8, 2025):
🔍 Similar Issues Found
I found some existing issues that might be related to this one. Please check if any of these are duplicates or contain helpful solutions:
#19794 MCP OAuth 2.1: Not following WWW-Authenticate → Protected Resource → Authorization Server discovery chain
by jamie-dit • Dec 07, 2025
#18010 issue: MCP OAuth 2.1 flow doesn't match standard (missing code_challenge and resource_url)
by hsuyuming • Oct 02, 2025 •
bug#14119 feat: Enable per-user authentication in MCP for personalized tool access or consider implementing a proper MCP Client
by florianchappaz • May 21, 2025
💡 Tips:
This comment was generated automatically by a bot. Please react with a 👍 if this comment was helpful, or a 👎 if it was not.
@jamie-dit commented on GitHub (Dec 8, 2025):
Fix Submitted
A fix has been submitted in PR #19811.
Summary of Fix
The fix adds a background task that proactively refreshes OAuth tokens before they expire:
New method
get_expiring_sessions()- Queries OAuth sessions expiring within a specified time windowEnhanced
_perform_token_refresh()- Now handles unregistered MCP clients by discovering OAuth info from stored tool server configBackground task
periodic_oauth_token_refresh()- Runs every 5 minutes and refreshes tokens expiring within 10 minutesTesting Confirmed
The fix was tested with a Notion MCP integration:
07:42:44to08:23:05(refreshed for another hour)@tjbck commented on GitHub (Dec 8, 2025):
Please correct me if I'm wrong, but the current behaviour is working as intended: the access token will be renewed, whether proactively or on demand, as long as the refresh token remains valid. The main issue here seems to be the notion MCP server not providing clients with a lengthy enough refresh token, presumably for valid security reasons.