Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(mcp): bound OAuth callback steps and log each phase to pinpoint hangs#5807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
74444ab
fix(mcp): bound OAuth callback steps and log each phase to pinpoint h…
waleedlatif1 6aa4c9c
fix(mcp): widen callback step bounds and drop resolved IP from fetch …
waleedlatif1 0785251
fix(mcp): tolerate non-thenable returns in callback timedStep helper
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -25,6 +25,46 @@ const logger = createLogger('McpOauthCallbackAPI') | ||
| export const dynamic = 'force-dynamic' | ||
| class OauthCallbackStepTimeout extends Error { | ||
| constructor(step: string, ms: number) { | ||
| super(`MCP OAuth callback step "${step}" did not settle within ${ms}ms`) | ||
| this.name = 'OauthCallbackStepTimeout' | ||
| } | ||
| } | ||
| /** | ||
| * Times and bounds one awaited step of the callback so a stalled operation | ||
| * surfaces as a labeled, logged error instead of hanging the request forever. | ||
| * The losing promise is not cancelled (a wedged DB/socket op can't be), so it | ||
| * settles in the background with its rejection swallowed; the point is that the | ||
| * request stops waiting on it and the logs name the exact step that stalled. | ||
| */ | ||
| async function timedStep<T>(step: string, ms: number, fn: () => Promise<T>): Promise<T> { | ||
| const start = Date.now() | ||
| logger.info(`OAuth callback step start: ${step}`) | ||
| const work = Promise.resolve(fn()) | ||
| work.catch(() => {}) | ||
| let timer: ReturnType<typeof setTimeout> | undefined | ||
| try { | ||
| const value = await Promise.race([ | ||
| work, | ||
| new Promise<never>((_, reject) => { | ||
| timer = setTimeout(() => reject(new OauthCallbackStepTimeout(step, ms)), ms) | ||
| timer.unref?.() | ||
| }), | ||
| ]) | ||
| logger.info(`OAuth callback step done: ${step} (${Date.now() - start}ms)`) | ||
| return value | ||
| } catch (error) { | ||
| logger.error(`OAuth callback step failed: ${step} (${Date.now() - start}ms)`, { | ||
| error: toError(error).message, | ||
| }) | ||
| throw error | ||
| } finally { | ||
| clearTimeout(timer) | ||
| } | ||
| } | ||
| function escapeHtml(value: string): string { | ||
| return value | ||
| .replace(/&/g, '&') | ||
| @@ -145,8 +185,9 @@ export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| serverId | ||
| ) | ||
| } | ||
| const serverUrl = server.url | ||
| try { | ||
| assertSafeOauthServerUrl(server.url) | ||
| assertSafeOauthServerUrl(serverUrl) | ||
| } catch { | ||
| return respond( | ||
| 'MCP OAuth requires https (or http://localhost for development).', | ||
| @@ -157,16 +198,22 @@ export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| } | ||
| // Burn state before token exchange so a replayed callback cannot reuse it. | ||
| await clearState(row.id, 'callback:burn-before-exchange') | ||
| await timedStep('clearState(burn)', 10_000, () => | ||
| clearState(row.id, 'callback:burn-before-exchange') | ||
| ) | ||
| const preregistered = await loadPreregisteredClient(server.id) | ||
| const preregistered = await timedStep('loadPreregisteredClient', 15_000, () => | ||
| loadPreregisteredClient(server.id) | ||
| ) | ||
| const provider = new SimMcpOauthProvider({ row, preregistered }) | ||
| let result: Awaited<ReturnType<typeof mcpAuthGuarded>> | ||
| try { | ||
| result = await mcpAuthGuarded(provider, { | ||
| serverUrl: server.url, | ||
| authorizationCode: code, | ||
| }) | ||
| result = await timedStep('mcpAuthGuarded', 120_000, () => | ||
| mcpAuthGuarded(provider, { | ||
| serverUrl, | ||
| authorizationCode: code, | ||
| }) | ||
| ) | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } catch (e) { | ||
| logger.error('Token exchange failed during MCP OAuth callback', e) | ||
| return respond( | ||
| @@ -176,7 +223,11 @@ export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| server.id | ||
| ) | ||
| } finally { | ||
| await clearVerifier(row.id) | ||
| await timedStep('clearVerifier', 10_000, () => clearVerifier(row.id)).catch((e) => | ||
| logger.error('Failed to clear PKCE verifier after MCP OAuth callback', { | ||
| error: toError(e).message, | ||
| }) | ||
| ) | ||
| } | ||
| if (result !== 'AUTHORIZED') { | ||
| @@ -185,7 +236,9 @@ export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| // forceRefresh: skip any stale cache from before re-auth. | ||
| await mcpService.discoverServerTools(session.user.id, server.id, server.workspaceId, true) | ||
| await timedStep('discoverServerTools', 60_000, () => | ||
| mcpService.discoverServerTools(session.user.id, server.id, server.workspaceId, true) | ||
| ) | ||
| } catch (e) { | ||
| logger.warn('Post-auth tools refresh failed', toError(e).message) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.