GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mcp): refresh stored OAuth tokens before they expire by aNewMoose · Pull Request #62200 · anomalyco/opencode

Conversation

@aNewMoose

@aNewMoose aNewMoose commented Nov 5, 2026 •
edited by opencodejr Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Issue for this PR

Fixes #62199

Type of change

  • Bug fix

What does this PR do?

A stored remote-MCP OAuth credential is now refreshed within five minutes of expiry, before the next MCP request. The transport still receives the raw OAuthClientProvider, so 403 insufficient_scope step-up, finishAuth and DPoP behavior is unchanged. The refresh runs under the credential lock, and a per-attempt provider copy pins saveTokens to the refresh token that attempt sent, so a late response cannot overwrite a newer row. A rejected refresh keeps the credential row and ends in needs_auth instead of deleting the sign-in.

The per-request fetch wrapper only applies to the configured MCP endpoint; OAuth token and discovery requests bypass it. DELETE teardown skips the proactive refresh so it cannot delay the 1 s termination cap.

How did you verify your code works?

packages/core: the 38 tests in test/mcp-oauth.test.ts (new and updated OAuth tests), the full bun run test (the four failures are pre-existing shell/git failures reproduced on the base commit), bun run typecheck, and the repository bun run lint. The red-without-fix proof reverts the wrapper (the proactive-refresh tests fail) or the no-delete invalidation (the invalidation test and the rejected-refresh test fail), then passes after restore. The built packages/core dist and a staged opencode v2.2.5-ldmcprefresh.1 binary were produced. The isolated LaunchDarkly built-dist call and the installed-binary isolated call are not run in this session: they need a separate isolated LaunchDarkly consent and, for the installed-binary call, model auth. A follow-up verification records them as not_run.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #60269, #60612

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@aNewMoose
fix(mcp): refresh stored OAuth tokens before they expire
029780e
Stored remote-MCP OAuth credentials were refreshed only after a transport 401. Some servers (LaunchDarkly) return HTTP 200 with a token_expired tool result, so a live session never refreshed and needed a manual login.

Refresh a stored credential within five minutes of expiry through a per-request fetch wrapper on the MCP endpoint. The raw OAuthClientProvider remains the transport auth provider, preserving 403 step-up, finishAuth and DPoP. A rejected refresh retains the credential row and ends in needs_auth; a stale save cannot overwrite a newer row.

Co-Authored-By: ollama-cloud/deepseek-v4.1-flash:1017
@aNewMoose
aNewMoose marked this pull request as ready for review November 6, 2026 00:08
opencodejr[bot]
opencodejr Bot reviewed Nov 6, 2026
View reviewed changes

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a stored credential that is already expired, the base sends the expired token on initialize and never refreshes. With this PR, one refresh_token grant runs first and the new token is used. I checked this at the core level by running the PR's test against both checkouts; I did not run the issue's TUI reproduction. The PR's OAuth tests pass, and the package type-checks with tsc -b.

Public API: unchanged. The new McpOAuth.Auth wrapper is internal to packages/core.

Concerns:

  • When a proactive refresh fails because the authorization server returns 5xx, times out or can't be reached, the request now fails or ends in needs_auth, even though the stored access token may still be valid for up to five minutes (inline comment).
  • The tests are large for what they check: 745 added lines for 178 lines of source. Some depend on timing (Bun.sleep(50), Bun.sleep(20), a token delay). Could you cut them down to the core cases (refresh before initialize, a single shared refresh under concurrency, a rejected refresh keeps the row, no refresh on DELETE) and drop the sleeps?
  • #60944 and #61762 change the same refresh and lock code, so expect conflicts with whichever lands first.

Related: #60269, #60612

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/core/src/mcp/oauth.ts
saveCodeVerifier: async () => {},
},
})
const refresh = async (fetchFn: FetchLike): Promise<string | undefined> => {

@opencodejr opencodejr Bot Nov 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs before every MCP request in the last five minutes before expiry. The failure cases worry me. On a 5xx, timeout or network error from the token endpoint, the SDK's auth() doesn't throw. It falls back to startAuthorization → redirectToAuthorization, and with no redirect that throws UnauthorizedError. So a short authorization-server outage makes the request fail and can mark the server needs_auth, even though current.access is still valid. Before this PR, that request would have gone through.

It also repeats on every following request: each one waits up to REFRESH_TIMEOUT_MS and then fails the same way.

Could you fall back to the stored access token while it hasn't expired yet when the refresh fails without an explicit rejection (invalid_grant), and only surface the failure once the token has actually expired?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot mentioned this pull request Nov 6, 2026
📊 AI CLI 工具社区动态日报 2026-11-06 litang9/big_model_radar#660
Open
@opencodejr opencodejr Bot mentioned this pull request Nov 6, 2026
mcp: expired OAuth token leaves server failed on HTTP 403 instead of refreshing or prompting sign-in #62372
Closed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@opencodejr opencodejr[bot] opencodejr[bot] left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

MCP: stored OAuth token is never refreshed when the server reports expiry in a 200 tool result

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@aNewMoose
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.