Skip to content

Commit 8f62d4a

Browse files
authored
fix(mcp): register OAuth callback before opening browser (#9646)
1 parent 733226d commit 8f62d4a

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packages/opencode/src/mcp/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,11 @@ export namespace MCP {
795795
// The SDK has already added the state parameter to the authorization URL
796796
// We just need to open the browser
797797
log.info("opening browser for oauth", { mcpName, url: authorizationUrl, state: oauthState })
798+
799+
// Register the callback BEFORE opening the browser to avoid race condition
800+
// when the IdP has an active SSO session and redirects immediately
801+
const callbackPromise = McpOAuthCallback.waitForCallback(oauthState)
802+
798803
try {
799804
const subprocess = await open(authorizationUrl)
800805
// The open package spawns a detached process and returns immediately.
@@ -822,8 +827,8 @@ export namespace MCP {
822827
Bus.publish(BrowserOpenFailed, { mcpName, url: authorizationUrl })
823828
}
824829

825-
// Wait for callback using the OAuth state parameter
826-
const code = await McpOAuthCallback.waitForCallback(oauthState)
830+
// Wait for callback using the already-registered promise
831+
const code = await callbackPromise
827832

828833
// Validate and clear the state
829834
const storedState = await McpAuth.getOAuthState(mcpName)

0 commit comments

Comments
 (0)