fix(mcp): clean up stale OAuth cache when auth type changes (#60313)#71106
Open
DavidMetcalfe wants to merge 1 commit into
Open
fix(mcp): clean up stale OAuth cache when auth type changes (#60313)#71106DavidMetcalfe wants to merge 1 commit into
DavidMetcalfe wants to merge 1 commit into
Conversation
…arch#60313) When a user switches an MCP server from auth: oauth to headers or no-auth, the cached mcp-tokens/<name>.client.json files are left behind as orphaned disk cruft. While these files don't independently trigger OAuth (the flow is correctly gated by _auth_type == 'oauth'), their presence confuses debugging because they suggest OAuth is still in use. This adds a best-effort cleanup step in MCPServerTask._connect(): when _auth_type != 'oauth', remove any stale OAuth cache files. The cleanup is wrapped in try/except OSError so permission errors never block server startup.
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.
Summary
When a user switches an MCP server from
auth: oauthtoheaders-based auth (or removes auth entirely), the cachedmcp-tokens/<name>.client.jsonfiles are left as orphaned disk cruft. These files don't independently trigger OAuth (the flow is correctly gated byself._auth_type == "oauth"inMCPServerTask._connect()), but their presence confuses debugging — as reported in #60313.What this does
Adds a best-effort cleanup step in
MCPServerTask._connect(). When_auth_type != "oauth", any stale OAuth cache files (tokens, client info, metadata) are removed. The cleanup is wrapped intry/except OSErrorso permission errors never block server startup.Changes
tools/mcp_tool.py: In theelsebranch ofif self._auth_type == "oauth", callHermesTokenStorage.remove()to clean up any stale cachetests/tools/test_mcp_oauth.py: Two new tests — one verifying cleanup removes all three cache files, one verifying cleanup is a no-op when no files existTesting
Notes
auth: oauthconfig fieldCloses #60313