fix(core): refresh MCP OAuth tokens with the stored client ID#28481
fix(core): refresh MCP OAuth tokens with the stored client ID#28481ParthivNaresh wants to merge 4 commits into
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 failed to refresh for servers configured via discovery and dynamic client registration. Previously, the refresh process would fail when the client ID was missing from the configuration, leading to the deletion of valid credentials. The changes ensure that the system falls back to the stored client ID during the refresh flow and correctly persists it, maintaining a stable connection for dynamically registered 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 the 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 counterproductive. 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
|
|
📊 PR Size: size/M
|
There was a problem hiding this comment.
Code Review
This pull request updates the MCPOAuthProvider to fall back to the stored client ID (persisted during dynamic client registration) when refreshing an expired token if the static configuration does not provide one. It also adds corresponding unit tests to verify this fallback behavior and ensure that the configured client ID is preferred over the stored one when both are present. I have no feedback to provide on these changes.
Summary
Fixes MCP OAuth token refresh for servers configured via OAuth discovery + dynamic client registration (
gemini mcp add --transport http ...with nooauthblock). Refresh failed locally before any network I/O, and the failure deleted the stored credentials, forcing re-auth on every reconnect.Details
The connection path (
DynamicStoredOAuthProvider) passes an empty OAuth config for discovery-configured servers.getValidTokenWithMetadataresolved the client ID from stored credentials for its guard but passed the raw config torefreshAccessToken, which throws whenconfig.clientIdis missing. The catch block then deleted the (recoverable) credentials.getValidTokenhad the same gap and silently returned null.Fix: resolve
config.clientId ?? credentials.clientId, pass it into the refresh call, and persist it when saving the refreshed token, in both paths.Same approach as #27889, which was closed by inactivity.
Related Issues
Fixes #27745
How to Validate
The two "stored client ID" tests fail without the fix (no refresh request sent, credentials deleted) and pass with it.
End-to-end: add an HTTP MCP server with no oauth block, /mcp auth it, then reconnect while the token is within the 5-minute refresh buffer (short-lived tokens, e.g. expires_in: 300, hit this immediately). Without the fix: "Failed to refresh auth token." and Disconnected. With the fix: the refresh grant is sent with the registered client ID and the server stays Connected. Verified against a real DCR-enabled server.
Pre-Merge Checklist