fix(core): refresh MCP OAuth token usage after re-auth#26312
fix(core): refresh MCP OAuth token usage after re-auth#26312sahilkirad wants to merge 5 commits intogoogle-gemini:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses an issue where MCP OAuth tokens would become stale, requiring a full CLI restart to refresh. By introducing a dynamic token provider, the system now resolves tokens at the time of authentication, ensuring that refreshed or updated credentials are used immediately. This change improves the robustness of the Gemini CLI's authentication flow for MCP servers. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
🛑 Action Required: Evaluation ApprovalSteering changes have been detected in this PR. To prevent regressions, a maintainer must approve the evaluation run before this PR can be merged. Maintainers:
Once approved, the evaluation results will be posted here automatically. |
There was a problem hiding this comment.
Code Review
This pull request implements dynamic OAuth token retrieval for MCP clients by introducing the DynamicStoredOAuthProvider class. This allows tokens to be looked up or refreshed at request time rather than being fixed at transport creation. The createTransport function was updated to utilize this dynamic provider when OAuth is enabled or stored credentials are detected. Feedback was provided regarding the efficiency of the tokens() method in DynamicStoredOAuthProvider, specifically noting that instantiating storage and provider classes within the method leads to redundant disk I/O and should be refactored to use instance-scoped fields.
…(core): align MCP authProvider assertions with tokens() API
|
Hi maintainers, all requested MCP OAuth fixes are pushed and tests were updated accordingly. |
scidomino
left a comment
There was a problem hiding this comment.
Gemini spotted two issues:
-
Severe Performance Hit on HTTP Transports: The MCP SDK’s
StreamableHTTPClientTransportcallsauthProvider.tokens()for every single RPC request (inside_commonHeaders()).DynamicStoredOAuthProvider.tokens()delegates tooauthProvider.getValidToken().getValidToken()callstokenStorage.getCredentials(), which reads and parses the JSON file from the disk (or queries the system Keychain if using encrypted storage) synchronously/asynchronously.- Impact: This means every single MCP message sent via HTTP will perform a disk read or a slow Keychain lookup.
- Recommendation:
DynamicStoredOAuthProvidershould cache theaccessTokenandexpiresAtin memory, and only re-fetch/refresh when the token is expired (e.g., adding a 5-minute buffer).
-
Redundant Storage Reads on Fallback: When
oauth.enabledis falsy in the config, thetokens()method manually retrieves theclientIdby callingthis.tokenStorage.getCredentials(this.serverName). It then passes thisclientIdtothis.oauthProvider.getValidToken(...), which internally callsgetCredentials(...)again. This results in two back-to-back disk/keychain reads for a single request.- Recommendation: Can be fixed by reusing the retrieved credentials or by caching in memory (as per issue #1).
|
Okay @scidomino sir will do those changes |
|
Hello @scidomino sir i have fixed the issues:
Validated locally:
|
Summary
Fixes MCP OAuth token reuse after token refresh/re-auth.
Previously, transport auth could continue using a stale access token until CLI restart.
This change makes token retrieval dynamic so refreshed/stored tokens are used without restarting Gemini CLI.
Closes #18895
Details
oauth.enabledservers, token is fetched viagetValidToken(...)at auth time.oauth.enabled), token is also resolved dynamically.Related issues
Validation
Ran successfully:
npm run build --workspace @google/gemini-cli-corenpm run test --workspace @google/gemini-cli-core -- src/tools/mcp-client.test.tsNotes