-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(site): auto-select MCP server after successful OAuth #28155
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2817903
fix(site): enable MCP server after OAuth
ibetitsmike 0715721
fix(site): gate MCP auto-select on the initiating OAuth popup
ibetitsmike e0e0b5e
fix(site): keep MCP OAuth flow correlation past popup close
ibetitsmike 386b8b7
refactor(site/src/pages/AgentsPage): extract MCP OAuth popup flow int…
ibetitsmike c079e1d
fix(site/src/pages/AgentsPage): drop comment restating the OAuth flow…
ibetitsmike 1810f0d
fix(site/src/pages/AgentsPage/hooks): drop comments restating hook in…
ibetitsmike 6ed0789
fix(site/src/pages/AgentsPage): use JSDoc for exported hook and corre…
ibetitsmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import { useEffect, useEffectEvent, useRef, useState } from "react"; | ||
| import { mcpServerOAuth2ConnectPath } from "#/api/api"; | ||
|
|
||
| type UseMCPOAuthFlowOptions = { | ||
| organizationId?: string; | ||
| // Called for every same-origin completion message, regardless of | ||
| // which flow (if any) produced it, so callers can refresh server | ||
| // state. | ||
| onAuthComplete?: (serverId: string) => void; | ||
| // Called only for a completion posted by the initiating popup for | ||
| // the initiating server. | ||
| onFlowSuccess: (serverId: string) => void; | ||
| }; | ||
|
|
||
| type MCPOAuthFlow = { | ||
| connectingServerId: string | null; | ||
| connect: (serverId: string) => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Runs the MCP server OAuth2 popup flow: opens the consent popup, | ||
| * listens for the completion message coderd's callback page posts to | ||
| * the opener, and reports success only for the initiating popup and | ||
| * server. | ||
| */ | ||
| export const useMCPOAuthFlow = ({ | ||
| organizationId, | ||
| onAuthComplete, | ||
| onFlowSuccess, | ||
| }: UseMCPOAuthFlowOptions): MCPOAuthFlow => { | ||
| const [connectingServerId, setConnectingServerId] = useState<string | null>( | ||
| null, | ||
| ); | ||
| // Correlates a completion message with the initiating flow. | ||
| // Retained after popup close: the callback page posts before | ||
| // closing, and the close poll can run before the queued message is | ||
| // dispatched. | ||
| const flowRef = useRef<{ popup: Window; serverID: string } | null>(null); | ||
|
|
||
| const handleAuthComplete = useEffectEvent( | ||
| (serverID: string, source: MessageEventSource | null) => { | ||
| onAuthComplete?.(serverID); | ||
| // Only a message from the initiating popup for the initiating | ||
| // server counts as flow success. | ||
| const flow = flowRef.current; | ||
| if (!flow || source !== flow.popup || serverID !== flow.serverID) { | ||
| return; | ||
| } | ||
| flowRef.current = null; | ||
| setConnectingServerId(null); | ||
| onFlowSuccess(serverID); | ||
| }, | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| const handler = (event: MessageEvent) => { | ||
| if (event.origin !== location.origin) return; | ||
| if ( | ||
| event.data?.type === "mcp-oauth2-complete" && | ||
| typeof event.data.serverID === "string" | ||
| ) { | ||
| handleAuthComplete(event.data.serverID, event.source); | ||
| } | ||
| }; | ||
| window.addEventListener("message", handler); | ||
| return () => window.removeEventListener("message", handler); | ||
| }, []); | ||
|
|
||
| // Clear only the connecting indicator when the popup closes; the | ||
| // flow ref stays so a completion message posted before close still | ||
| // correlates. | ||
| useEffect(() => { | ||
| if (!connectingServerId || !flowRef.current) return; | ||
| const interval = setInterval(() => { | ||
| if (flowRef.current?.popup.closed) { | ||
| setConnectingServerId(null); | ||
| } | ||
| }, 500); | ||
| return () => { | ||
| clearInterval(interval); | ||
| const popup = flowRef.current?.popup; | ||
| if (popup && !popup.closed) { | ||
| popup.close(); | ||
| flowRef.current = null; | ||
| } | ||
| }; | ||
| }, [connectingServerId]); | ||
|
|
||
| const connect = (serverId: string) => { | ||
| if (!organizationId) { | ||
| return; | ||
| } | ||
| const connectUrl = mcpServerOAuth2ConnectPath(organizationId, serverId); | ||
| const popup = window.open(connectUrl, "_blank", "width=900,height=600"); | ||
| // A blocked popup (window.open returns null) must not enter the | ||
| // connecting state; nothing would ever clear it. | ||
| flowRef.current = popup ? { popup, serverID: serverId } : null; | ||
| setConnectingServerId(popup ? serverId : null); | ||
| }; | ||
|
|
||
| return { connectingServerId, connect }; | ||
| }; |
Oops, something went wrong.
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.