Skip to content

fix(mcp): clean up stale OAuth cache when auth type changes (#60313)#71106

Open
DavidMetcalfe wants to merge 1 commit into
NousResearch:mainfrom
DavidMetcalfe:fix/60313-mcp-oauth-stale-cache-cleanup
Open

fix(mcp): clean up stale OAuth cache when auth type changes (#60313)#71106
DavidMetcalfe wants to merge 1 commit into
NousResearch:mainfrom
DavidMetcalfe:fix/60313-mcp-oauth-stale-cache-cleanup

Conversation

@DavidMetcalfe

@DavidMetcalfe DavidMetcalfe commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

When a user switches an MCP server from auth: oauth to headers-based auth (or removes auth entirely), the cached mcp-tokens/<name>.client.json files are left as orphaned disk cruft. These files don't independently trigger OAuth (the flow is correctly gated by self._auth_type == "oauth" in MCPServerTask._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 in try/except OSError so permission errors never block server startup.

Changes

  • tools/mcp_tool.py: In the else branch of if self._auth_type == "oauth", call HermesTokenStorage.remove() to clean up any stale cache
  • tests/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 exist

Testing

$ python -m pytest tests/tools/test_mcp_oauth.py -q
107 passed in 3.22s

Notes

Closes #60313

…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.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint tool/mcp MCP client and OAuth area/config Config system, migrations, profiles P2 Medium — degraded but workaround exists labels Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P2 Medium — degraded but workaround exists tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dual config.yaml sources cause confusing MCP OAuth behavior

2 participants