Skip to content

fix(mcp): stop OAuth callback server after authentication completes#23567

Closed
hchangjae wants to merge 1 commit intoanomalyco:devfrom
hchangjae:fix/oauth-callback-stop-after-auth
Closed

fix(mcp): stop OAuth callback server after authentication completes#23567
hchangjae wants to merge 1 commit intoanomalyco:devfrom
hchangjae:fix/oauth-callback-stop-after-auth

Conversation

@hchangjae
Copy link
Copy Markdown

Issue for this PR

Closes #23562

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

The OAuth callback server (port 19876) stays running after authenticate() completes. When multiple TUI instances are running, the first instance holds the port and subsequent instances can't receive their own OAuth callbacks — their state is registered in their own process-local pendingAuths Map, but the callback arrives at the first instance's server where pendingStates=[], resulting in CSRF errors.

This adds stopIfIdle() to McpOAuthCallback that stops the server only when no pending auth flows remain, and calls it after authenticate() resolves (both success and state-mismatch paths). This is safe for concurrent auth flows — the server only stops when all pending auths are resolved.

How did you verify your code works?

  • Verified stopIfIdle() only calls stop() when pendingAuths.size === 0
  • Confirmed the callback server lifecycle: ensureRunning() → auth flow → stopIfIdle() → port released
  • Traced all code paths in authenticate() to ensure cleanup on both success and error
  • Existing oauth-callback.test.ts tests use McpOAuthCallback.stop() in afterEach — same pattern

Checklist

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

@hchangjae hchangjae force-pushed the fix/oauth-callback-stop-after-auth branch 3 times, most recently from 552c126 to ed62476 Compare April 20, 2026 14:44
@hchangjae hchangjae force-pushed the fix/oauth-callback-stop-after-auth branch from ed62476 to cfcdb47 Compare April 20, 2026 14:47
@hchangjae
Copy link
Copy Markdown
Author

Recreating with cleaner commit.

@hchangjae hchangjae closed this Apr 20, 2026
@hchangjae hchangjae deleted the fix/oauth-callback-stop-after-auth branch April 20, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: OAuth callback server not stopped after authentication, causing cross-instance CSRF failures

1 participant