fix: defer OAuth token acquisition to first use; no browser on transient refresh errors - #732
fix: defer OAuth token acquisition to first use; no browser on transient refresh errors#732zereight wants to merge 1 commit into
Conversation
…ent refresh errors - Construct the OAuth client synchronously at startup (no network); the token resolves lazily on the first tool call via ensureOAuthToken. - ensureOAuthToken delivers the token even when the cached token is still valid (fast path requires a held token AND a valid stored token); token-script setups resolve exactly once. - Refresh failures open the browser only for 400/401 invalid_grant/invalid_token; transient/config errors propagate to the caller. - 10s timeout on the refresh request so boot-time hangs fail fast. Supersedes #495. Thanks to @medyas for the original fix.
|
Warning Review limit reachedNext included review available in 21 minutes. View limit detailsLimit details: You’ve used all 4 included reviews currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Team Run ID: 📒 Files selected for processing (5)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@cursor please review this PR |
|
PR #732 리뷰 올렸다. 코어 픽 자체는 맞다. #495에서 막혔던 “디스크 토큰이 유효하면 머지 전에 손볼 건 두 개다. 차단할 버그는 못 봤다. 문서 불일치랑 first-use race는 경고로 남겼다. 자신감 8/10. |
Review방향은 맞다. #495에서 막혔던 cached-token 구멍( 로컬에서 Warning1. 이 PR은 “브라우저는 서버 시작이 아니라 첫 tool call”이 핵심 행동 변경인데, 셋업 가이드는 아직 이렇게 적혀 있다:
2. first-use
만료 토큰 + 첫 사용에서 tool call이 겹치면:
stdio는 보통 한 번에 하나라 운 좋게 안 터질 수 있다. 그래도 lock 패턴이 바로 옆에 있는데 안 가져온 건 구멍이다. 간단한 in-flight Promise면 된다. // 둘 다 currentToken=null, disk token expired
await Promise.all([
ensureOAuthToken(client, null, onToken),
ensureOAuthToken(client, null, onToken),
]);
// 두 번째 refresh가 invalid_grant → startOAuthFlow()Note
잘한 점
판정: 코어 픽은 merge 해도 된다. 머지 전에 |


Problem
When
GITLAB_USE_OAUTH=true, the server acquires the OAuth token eagerly and synchronously at startup (runServer→initializeOAuthClient→getAccessToken), blocking the MCP handshake on a network call to GitLab.For a self-hosted GitLab (often behind a VPN), the network frequently isn't ready at the instant the MCP client spawns the server. The refresh
fetchthen hangs past the client's connection timeout, and on any refresh error the code falls through to the interactivestartOAuthFlow()— opening a browser on every cold start.Fix
createGitLabOAuthClient. The token resolves lazily on the first tool call (every call already passes throughensureValidOAuthToken), by which point the network is ready.initializeOAuthClientis kept and now delegates.ensureOAuthToken(client, currentToken, onToken): the fast path requires a held token AND a valid stored token, so the first call after lazy startup populates the token even when the on-disk token is still valid. Token-script setups resolve exactly once.AbortSignal.timeouton the refresh request.isAuthInvalidTokenResponse(status, body)returns true only for 400/401 withinvalid_grant/invalid_token. Transient and config errors propagate to the caller without a popup.Tests
test/oauth-startup.test.ts(14 cases): 8 classifier cases + 6ensureOAuthTokencases (valid-cache delivery without network via closed-port URL, no re-delivery fast path, refresh + persist against a loopback/oauth/tokenmock, transient-failure propagation, token-script single resolution). Wired into the pure-unit group inscripts/run-mock-tests.sh.npm run buildclean; fullnpm run test:mockgreen;test-auth-retry19/19 andoauth-device-flow22/22 still green.Behavior change
First tool call (instead of startup) opens the browser when no stored token exists; a transient refresh failure fails that call instead of popping a browser. Documented in
docs/configuration/environment-variables.md.Supersedes #495. Thanks to @medyas for the original fix — this PR carries it forward with the cached-token fix and regression tests from the review.