refactor(McpOAuthClientProvider): add TOKEN_EXPIRY_BUFFER

This commit is contained in:
Elliott de Launay 2026-03-26 22:49:29 -04:00
parent 5aeed4f3c0
commit 1f8209ac2a
No known key found for this signature in database
GPG key ID: BB899BED766D1806
3 changed files with 4 additions and 5 deletions

View file

@ -36,6 +36,7 @@ import { GlobalFileNames } from "../../shared/globalFileNames"
import { UnauthorizedError } from "@modelcontextprotocol/sdk/client/auth.js"
import { fileExistsAtPath } from "../../utils/fs"
import { TOKEN_EXPIRY_BUFFER_MS } from "./constants"
import { SecretStorageService } from "./SecretStorageService"
import { McpOAuthClientProvider } from "./McpOAuthClientProvider"
import { arePathsEqual, getWorkspacePath } from "../../utils/path"
@ -1053,8 +1054,6 @@ export class McpHub {
return
}
const TOKEN_EXPIRY_BUFFER_MS = 5 * 60 * 1000
// Check if another window already saved valid tokens
const existing = await this.secretStorage.getOAuthData(serverUrl)
if (existing && Date.now() < existing.expires_at - TOKEN_EXPIRY_BUFFER_MS) {
@ -1163,8 +1162,6 @@ export class McpHub {
this._oauthWatchers.delete(watcherKey)
}
const TOKEN_EXPIRY_BUFFER_MS = 5 * 60 * 1000
// Called when SecretStorage fires onDidChange for this server's key.
// Runs in all VS Code windows the instant tokens are saved — no polling delay.
const onTokensChanged = async () => {

View file

@ -9,6 +9,7 @@ import type {
OAuthTokens,
} from "@modelcontextprotocol/sdk/shared/auth.js"
import { TOKEN_EXPIRY_BUFFER_MS } from "./constants"
import { SecretStorageService } from "./SecretStorageService"
import { startCallbackServer, stopCallbackServer } from "./utils/callbackServer"
import { fetchOAuthAuthServerMetadata } from "./utils/oauth"
@ -265,7 +266,7 @@ export class McpOAuthClientProvider implements OAuthClientProvider {
if (!data) return undefined
// If the access token is still valid (with 5m buffer), return it.
if (Date.now() < data.expires_at - 5 * 60 * 1000) {
if (Date.now() < data.expires_at - TOKEN_EXPIRY_BUFFER_MS) {
return data.tokens
}

View file

@ -0,0 +1 @@
export const TOKEN_EXPIRY_BUFFER_MS = 5 * 60 * 1000 // 5 minutes