mirror of
https://github.com/open-webui/open-webui.git
synced 2026-08-30 17:25:30 -05:00
[GH-ISSUE #24618] bug: MCP OAuth sessions wiped after every chat completion (get_system_oauth_token fallback) #107355
Reference in New Issue
Block a user
Originally created by @verdier on GitHub (May 12, 2026).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/24618
Bug
When a user has SSO login (e.g. Microsoft Entra) and at least one MCP server with
auth_type: oauth_2.1, the MCP OAuth session is deleted from the database at every chat completion. The next tool invocation then fails with 401.Steps to reproduce
https://coda.io/apis/mcp.oauth_sessionrow is created withprovider='mcp:<server_id>'.oauth_sessionrow is gone. Subsequent tool calls return401 Unauthorized.Root cause
backend/open_webui/utils/middleware.py::get_system_oauth_token()is called on every chat completion (via the__oauth_token__context value). When nooauth_session_idcookie is present, it falls back to the most recent OAuth session of the user, regardless of provider:When
bestis an MCP session (e.g.mcp:coda), it gets handed to the SSOoauth_manager, which doesn't know anymcp:*provider._perform_token_refreshthen logs:…and the session is deleted (
OAuthSessions.delete_session_by_id). The MCPoauth_client_manager(which does know how to refreshmcp:*tokens) is never consulted in this code path.Suggested fix
Skip MCP-provider sessions in the SSO fallback — MCP token refresh is already handled separately by
oauth_client_manager.get_oauth_token()inprocess_chat_payload:Happy to send a PR if useful.
Version
OpenWebUI 0.9.5 (
ghcr.io/open-webui/open-webui:lateston 2026-05-12).@owui-terminator[bot] commented on GitHub (May 12, 2026):
⚠️ Missing Issue Title Prefix
@verdier, your issue title is missing a categorising prefix (e.g.
bug:,feat:,docs:).Please update the title to lead with one of:
Example:
bug: Login fails when the password contains special characters@owui-terminator[bot] commented on GitHub (May 12, 2026):
🔍 Related Issues Found
I found some existing issues that might be related. Please check if any of these are duplicates or contain helpful solutions:
🟣 #24039 feat: Allow Clearing of Oauth Sessions for MCP Connections
This feature request is directly about managing/deleting MCP OAuth sessions. It confirms MCP sessions are stored in
oauth_sessions, which is the same table being incorrectly deleted in the new bug.by anengineerdude
🟣 #20291 issue: MCP Atlassian OAuth token refresh fails with "Constructor parameter should be str" in v0.6.43
This bug is also about MCP OAuth token refresh failing and the session being deleted after refresh attempts. While the error differs, it is closely related because both involve MCP OAuth sessions being removed when refresh logic misroutes.
by rolandscho ·
bug🟢 #24615 issue: Older OAuth session deletion not being reflected in the frontend when OAUTH_MAX_SESSIONS_PER_USER is set to 1
This is about OAuth session deletion behavior when the max-session limit is enforced. It is not the same root cause, but it is adjacent because it deals with automatic deletion of OAuth sessions and the resulting session-state mismatch.
by shubhankar-ncp ·
bug💡 If your issue is a duplicate, please close it and add any additional details to the existing issue instead.
This comment was generated automatically. React with 👍 if helpful, 👎 if not.
@los93sol commented on GitHub (May 13, 2026):
Ran into the same issue and applied the fix mentioned, worked perfectly for me
@IVVI0927 commented on GitHub (May 17, 2026):
Hi @verdier, thanks for the detailed root-cause analysis and the suggested fix.
My plan is to apply your suggested filter in
get_system_oauth_token()inbackend/open_webui/utils/middleware.py— skipping sessions whose provider starts withmcp:so they aren't passed to the SSO oauth_manager and incorrectly deleted.Will submit a PR shortly against the
devbranch. Happy to adjust based on feedback.@IVVI0927 commented on GitHub (May 17, 2026):
Hi @Sean-Kenneth-Doherty, thanks for the regression-test target — adopted it.
Quick note on test strategy: since importing
middleware.pypulls in the full DB/config initialization, my test file inlines the filter logic (with a# keep in sync with middleware.pycomment) rather than importingget_system_oauth_tokendirectly.Trade-off: future edits to the real function won't automatically propagate to the test, but it keeps the test fast and avoids touching test infrastructure outside the scope of this fix. Happy to switch to a fixture-based approach if maintainers prefer.
The test covers:
oauth_managernot calledprovider=Nonesessions → preserved (covers the(s.provider or '')guard)