File tree Expand file tree Collapse file tree 1 file changed +7
-2
lines changed
packages/opencode/src/mcp Expand file tree Collapse file tree 1 file changed +7
-2
lines changed Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments