fix(mcp): invalidate OAuth tokens when the configured client changes (port of cline/cline#12983) - #84905
Merged
Merged
Conversation
Port from cline/cline#12983 (the 'invalidate tokens when OAuth client changes' invariant): tokens are minted for a specific client_id, so after a user edits oauth.client_id / oauth.client_secret in config.yaml the old tokens can only fail with invalid_client. Pre-registered clients are deliberately exempt from the invalid_client auto-poison path, so the stale tokens wedged every request until ~/.hermes/mcp-tokens/<server>.* was wiped by hand. _maybe_preregister_client() now compares the on-disk client.json identity against the incoming config identity before overwriting it and discards tokens.json + meta.json on a mismatch (with a log line pointing at hermes mcp login). Unchanged identity is a strict no-op. Proven live on main with an isolated-HERMES_HOME E2E probe; regression tests sabotage-verified (fail without the wiring line).
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
Editing a server's
oauth.client_id/oauth.client_secretin config.yaml now discards the OAuth tokens minted under the previous client identity, instead of leaving stale tokens that fail every request withinvalid_clientuntil the user manually wipes~/.hermes/mcp-tokens/<server>.*.Port of the "invalidate tokens when OAuth client changes" invariant from cline/cline#12983 (their pre-registered-OAuth-clients-for-remote-MCP feature — hermes already ships pre-registered clients, but not this half).
Root cause:
_maybe_preregister_client()unconditionally overwroteclient.jsonwith the config-supplied identity and never touched the token files. Pre-registered clients are deliberately exempt from theinvalid_clientauto-poison path (_maybe_flag_poisoned_client— a config-supplied identity can't be healed by re-registration), so nothing ever recovered: the stale tokens wedged the server permanently.Changes
tools/mcp_oauth.py: new_invalidate_tokens_on_client_change()— compares the on-diskclient.jsonidentity (client_id + client_secret) against the incoming config identity before the pre-register overwrite; on mismatch removestokens.json+meta.json(with a warning log pointing athermes mcp login). Unchanged identity is a strict no-op; missing/dynamic-registrationclient.jsonis a no-op (nothing provably stale).tests/tools/test_mcp_oauth.py:TestInvalidateTokensOnClientChange— 6 tests covering changed client_id, changed secret, unchanged identity (preserved), no prior client info (no-op), and both end-to-end_maybe_preregister_clientflows.Validation
HERMES_HOME, real_maybe_preregister_client, client-a → client-b)access_tokenminted under client-a still on diskclient.json= client-b, warning loggedtests/tools/test_mcp_oauth.pytests/tools/test_mcp_oauth_manager.pyAdjacent open PRs checked for overlap: #35965/#37840 (force browser login for preregistered clients — orthogonal: login UX, not token staleness), #30391 (refresh-rejection cleanup — reactive path; this PR is the proactive config-edit path), #16711 (redirect-URI persistence). None invalidate tokens on config-identity change.
Infographic
Opened by the weekly Cline PR scout (cron). Awaiting review — not merged.