mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-06 10:58:17 -05:00
[PR #19811] [CLOSED] fix: Add proactive OAuth token refresh for MCP sessions #64207
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?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/19811
Author: @jamie-dit
Created: 12/8/2025
Status: ❌ Closed
Base:
dev← Head:fix/mcp-oauth-proactive-refresh📝 Commits (1)
0fe0fcffix: Add proactive OAuth token refresh for MCP sessions📊 Changes
3 files changed (+200 additions, -17 deletions)
View changed files
📝
backend/open_webui/main.py(+10 -0)📝
backend/open_webui/models/oauth_sessions.py(+35 -0)📝
backend/open_webui/utils/oauth.py(+155 -17)📄 Description
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions to discuss your idea/fix with the community before creating a pull request, and describe your changes before submitting a pull request.
Before submitting, make sure you've checked the following:
devbranch.fix:prefix.Changelog Entry
Description
MCP OAuth 2.1 tokens (e.g., Notion) expire after ~1 hour but were not being 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, requiring re-authentication.This PR adds a background task that proactively refreshes OAuth tokens before they expire.
Added
get_expiring_sessions(minutes=10)method inOAuthSessionTableto query sessions expiring within a specified time windowperiodic_oauth_token_refresh()background task that runs every 5 minutes and refreshes tokens expiring within 10 minutesChanged
OAuthClientManager._perform_token_refresh()to handle unregistered MCP clients by:TOOL_SERVER_CONNECTIONS)oauth_client_infoto get client_id, client_secret, and token_endpointFixed
Additional Information
oauth_client_infostored in tool server config to discover token endpoint and credentialsTesting Steps
Screenshots
Background task starting:
Token refresh successful:
Token expiration updated (before/after):
Expires: 2025-12-08 07:42:44(was about to expire)Expires: 2025-12-08 08:23:05(refreshed for another hour)Contributor License Agreement
By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.