fix(core): refresh OAuth-backed HTTP MCP sessions in chat#23493
fix(core): refresh OAuth-backed HTTP MCP sessions in chat#23493nivbrook wants to merge 4 commits intogoogle-gemini:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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 issues where OAuth-backed HTTP MCP sessions would fail after access tokens expired or due to transient network disconnects during chat interactions. The changes introduce a robust authentication provider that can refresh tokens and modify error handling to gracefully manage temporary HTTP transport issues. Additionally, a retry logic for tool calls after authentication failures has been added, significantly enhancing the stability and user experience of MCP tools within chat sessions. 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
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of expired OAuth tokens in long-lived MCP chat sessions. The introduction of the StoredOAuthMcpProvider is a solid architectural improvement that enables token refreshing for HTTP-based MCP servers. The addition of tool-call retry logic for recoverable authentication errors and the more nuanced handling of transient transport errors significantly improve the robustness and user experience of MCP interactions. The code is well-structured and the accompanying tests are thorough. I've identified one potential high-severity issue in the new token handling logic that could lead to a refresh loop, for which I've provided a specific comment and suggestion.
Fixes #18895
Related: #23296
Summary
Stored OAuth-backed HTTP MCP servers could reconnect and refresh successfully on a fresh connection path, but tool calls inside an already-running chat session could still fail after the access token expired.
This change fixes two parts of that behavior:
MCP ERROReventsIt also retries one MCP tool call after a recoverable auth-expiry failure so the first in-chat tool call after expiry can succeed without requiring a manual reconnect.
Background
The user-visible failure looked like this:
/mcp auth <server>succeedsMCP ERRORgemini mcp listcan refresh successfully immediately afterwardThat pointed to a transport/auth wiring problem in long-lived chat sessions rather than a refresh-token problem in the MCP server.
During debugging, the remaining chat-visible error after refresh wiring was traced to Gemini surfacing a background Streamable HTTP SSE disconnect such as:
SSE stream disconnected: TypeError: terminatedThe MCP SDK treats that as part of its reconnect behavior, but Gemini was treating any such transport error as fatal.
Changes
Validation
Passed:
npm run typecheck --workspace=@google/gemini-cli-corenpm test --workspace=@google/gemini-cli-core -- src/mcp/stored-oauth-provider.test.ts src/tools/mcp-client.test.tsAlso ran broader checks locally:
typecheckpassedlint:cipassed in local environmenttest:cihit unrelated existing failures outside the MCP codepath:src/tools/ls.test.tssrc/utils/googleQuotaErrors.test.ts