import type { OpenGeniBrowserClient as OpenGeniClient } from "@opengeni/sdk/browser"; import { useEffect, useState } from "react"; import { browserConnectWindowHost } from "../connect-window-host"; import { connectionAccountIdentityLabel } from "@opengeni/contracts/connection-account-label"; import { useOpenGeniText } from "../messages/runtime"; import { useMcpCapabilityConnect } from "./mcp-capability-connect-flow"; import { showsMcpServerLabel } from "./mcp-server-label"; import { SessionCapabilityFrame } from "./session-capability-frame"; export type SessionMcpCapabilityCardProps = { client: OpenGeniClient; workspaceId: string; sessionId: string; capabilityId: string; name: string; rationale?: string; /** Exact host route; no console-specific routing is assumed. */ returnUrl: string; onConfigured?: (() => void | Promise) | undefined; /** * Offer only a personal connection: the provider only ever connects * someone's own account (official Gmail, Slack's hosted MCP), or the person * may only connect their own account. No ownership choice. */ personalOnly?: boolean; }; export type McpConnectionCardProps = Omit & { /** Omit for connection management: no session tool selection is written. */ sessionId?: string; dialogOnly?: boolean; onClose?: (() => void) | undefined; /** The connect button's words, "Connect Gmail". Defaults to "Continue to ". */ connectLabel?: string; /** The line under the dialog title. Defaults to the kind and provider domain. */ dialogSubtitle?: string; /** The host's product copy for this connection, instead of the catalog description. */ description?: string; /** The host's logo for this connection, when the catalog has no asset. */ logoSrc?: string | null; /** The host's words for the ownership choice. */ ownershipCopy?: { legend: string; workspace: string; personal: string }; }; /** Native OAuth recommendation flow. Identity and endpoint come from the live * catalog, never from model-authored recommendation text. */ export function SessionMcpCapabilityCard(props: SessionMcpCapabilityCardProps) { return ; } export function McpConnectionCard(props: McpConnectionCardProps) { const [client, setClient] = useState(props.client); const [generation, setGeneration] = useState(0); if (client !== props.client) { setClient(props.client); setGeneration(generation + 1); } return ( ); } function ScopedCard({ client, workspaceId, sessionId, capabilityId, name, rationale = "", returnUrl, onConfigured, dialogOnly = false, onClose, personalOnly: configuredPersonalOnly = false, connectLabel, dialogSubtitle, description: hostDescription, logoSrc, ownershipCopy, }: McpConnectionCardProps) { const t = useOpenGeniText(); const [windows] = useState(() => browserConnectWindowHost(window)); const flow = useMcpCapabilityConnect({ client, workspaceId, sessionId, capabilityId, returnUrl, windows, onConfigured, dialogOnly, onClose, personalOnly: configuredPersonalOnly, }); const { item, personalOnly, busy, waiting, reconciling, connected, ownership, offHere } = flow; // Your own login, added in another workspace: one click turns it on here. const useOwnLoginHere = offHere !== null && !connected && item !== null; const ownLoginQuestion = useOwnLoginHere ? `${t("sessionMcpCapabilityCard.useYourLoginHereToo", { account: connectionAccountIdentityLabel( offHere.metadata, t("sessionMcpCapabilityCard.yourNameAccount", { name: item.name }), ), })} ${t("sessionMcpCapabilityCard.useYourLoginHereScope")}` : null; const [logo, setLogo] = useState(null); useEffect(() => { const path = item?.logoAssetPath; setLogo(null); if (!path?.startsWith("catalog-assets/")) return; const abort = new AbortController(); let objectUrl: string | null = null; // Embeds can require authentication even for the API's public catalog // asset route. Use the client's transport instead of an unauthenticated img. void client .downloadCatalogAsset(path, { signal: abort.signal }) .then((blob) => { if (abort.signal.aborted || !blob.type.startsWith("image/") || blob.size > 2_000_000) return; objectUrl = URL.createObjectURL(blob); setLogo(objectUrl); }) .catch(() => { /* Keep the provider's initials if its passive mark is unavailable. */ }); return () => { abort.abort(); if (objectUrl) URL.revokeObjectURL(objectUrl); }; }, [client, item?.logoAssetPath]); return (
{flow.error ?

{flow.error}

: null} {flow.notice ? (

{flow.notice}

) : null} {!item ? ( <>

{busy ? t("sessionMcpCapabilityCard.loadingConnectionDetails") : t("sessionMcpCapabilityCard.connectionDetailsAreUnavailable")}

) : ( <>

{hostDescription || item.description || rationale}

{busy ? (

{waiting ? t("sessionMcpCapabilityCard.finishSigningInWithName", { name: item.name }) : reconciling ? t("sessionMcpCapabilityCard.finishingYourConnection") : t("sessionMcpCapabilityCard.preparingYourConnection")}

{waiting ? ( ) : null}
) : ownLoginQuestion ? ( <>

{ownLoginQuestion}

) : !connected ? ( <> {!item.connectionRef && !personalOnly ? (
{ownershipCopy?.legend ?? t("sessionMcpCapabilityCard.whoCanUseThisConnection")}
) : null}

{personalOnly && ownership === "personal" ? t("sessionMcpCapabilityCard.connectsYourOwnAccountOnly") : ownershipCopy ? ownership === "workspace" ? ownershipCopy.workspace : ownershipCopy.personal : ownership === "workspace" ? sessionId ? t("sessionMcpCapabilityCard.thisConnectionWillBeAvailable") : t("sessionMcpCapabilityCard.thisConnectionWillBeAvailable2") : t("sessionMcpCapabilityCard.thisConnectionBelongsToYou")}

) : null} {connected && !busy ? ( <>

{sessionId ? t("sessionMcpCapabilityCard.yourAccountIsConnectedEnable") : t("sessionMcpCapabilityCard.yourAccountIsConnected")}

) : null} )}
); }