import { useReducer, useCallback, useEffect, useRef, useState, type ReactNode } from "react";import { useAtomSet } from "@effect/atom-react"; import { useScope } from "@executor-js/react/api/scope-context";import { Button } from "@executor-js/react/components/button";import { CardStack, CardStackContent, CardStackEntry, CardStackEntryActions, CardStackEntryContent, CardStackEntryDescription, CardStackEntryField, CardStackEntryMedia, CardStackEntryTitle,} from "@executor-js/react/components/card-stack";import { FieldError, FieldLabel } from "@executor-js/react/components/field";import { FilterTabs } from "@executor-js/react/components/filter-tabs";import { FloatActions } from "@executor-js/react/components/float-actions";import { Input } from "@executor-js/react/components/input";import { Label } from "@executor-js/react/components/label";import { Badge } from "@executor-js/react/components/badge";import { Skeleton } from "@executor-js/react/components/skeleton";import { SourceFavicon } from "@executor-js/react/components/source-favicon";import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner";import { Textarea } from "@executor-js/react/components/textarea";import { HeadersList } from "@executor-js/react/plugins/headers-list";import { emptyHttpCredentials, httpCredentialsValid, HttpCredentialsEditor, serializeHttpCredentials, type SecretBackedValue,} from "@executor-js/react/plugins/http-credentials";import { type HeaderState } from "@executor-js/react/plugins/secret-header-auth";import { displayNameFromUrl, slugifyNamespace, SourceIdentityFields, useSourceIdentity,} from "@executor-js/react/plugins/source-identity";import { useSecretPickerSecrets } from "@executor-js/react/plugins/use-secret-picker-secrets";import { oauthCallbackUrl, oauthConnectionId, useOAuthPopupFlow, type OAuthCompletionPayload,} from "@executor-js/react/plugins/oauth-sign-in"; type RemoteAuthMode = "none" | "header" | "oauth2";import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys";import { usePendingSources } from "@executor-js/react/api/optimistic";import { probeMcpEndpoint, addMcpSource } from "./atoms";import { mcpPresets, type McpPreset } from "../sdk/presets"; // ---------------------------------------------------------------------------// Preset lookup// --------------------------------------------------------------------------- function findPreset(id: string | undefined): McpPreset | undefined { if (!id) return undefined; return mcpPresets.find((p) => p.id === id);} // ---------------------------------------------------------------------------// State machine (remote flow)// --------------------------------------------------------------------------- type OAuthTokens = OAuthCompletionPayload; type ProbeResult = { connected: boolean; requiresOAuth: boolean; name: string; namespace: string; toolCount: number | null; serverName: string | null;}; type PlainHeader = { name: string; value: string;}; type State = | { step: "url"; url: string } | { step: "probing"; url: string } | { step: "probed"; url: string; probe: ProbeResult } | { step: "oauth-starting"; url: string; probe: ProbeResult } | { step: "oauth-waiting"; url: string; probe: ProbeResult; sessionId: string; } | { step: "oauth-done"; url: string; probe: ProbeResult; tokens: OAuthTokens } | { step: "adding"; url: string; probe: ProbeResult; tokens: OAuthTokens | null; } | { step: "error"; url: string; probe: ProbeResult | null; tokens: OAuthTokens | null; error: string; }; type Action = | { type: "set-url"; url: string } | { type: "probe-start" } | { type: "probe-ok"; probe: ProbeResult } | { type: "probe-fail"; error: string } | { type: "oauth-start" } | { type: "oauth-waiting"; sessionId: string } | { type: "oauth-ok"; tokens: OAuthTokens } | { type: "oauth-fail"; error: string } | { type: "oauth-cancelled" } | { type: "add-start" } | { type: "add-fail"; error: string } | { type: "retry" }; const init: State = { step: "url", url: "" }; function reducer(state: State, action: Action): State { switch (action.type) { case "set-url": return { step: "url", url: action.url }; case "probe-start": return { step: "probing", url: state.url }; case "probe-ok": return { step: "probed", url: state.url, probe: action.probe }; case "probe-fail": return { step: "error", url: state.url, probe: null, tokens: null, error: action.error, }; case "oauth-start": if (state.step !== "probed" && state.step !== "error") return state; return { step: "oauth-starting", url: state.url, probe: state.step === "probed" ? state.probe : state.probe!, }; case "oauth-waiting": if (state.step !== "oauth-starting") return state; return { step: "oauth-waiting", url: state.url, probe: state.probe, sessionId: action.sessionId, }; case "oauth-ok": if (state.step !== "oauth-waiting") return state; return { step: "oauth-done", url: state.url, probe: state.probe, tokens: action.tokens, }; case "oauth-fail": if (state.step !== "oauth-starting" && state.step !== "oauth-waiting") return state; return { step: "error", url: state.url, probe: state.probe, tokens: null, error: action.error, }; case "oauth-cancelled": if (state.step !== "oauth-waiting") return state; return { step: "probed", url: state.url, probe: state.probe }; case "add-start": { const tokens = state.step === "oauth-done" ? state.tokens : state.step === "probed" ? null : null; const probe = "probe" in state ? state.probe : null; if (!probe) return state; return { step: "adding", url: state.url, probe, tokens }; } case "add-fail": if (state.step !== "adding") return state; return { step: "error", url: state.url, probe: state.probe, tokens: state.tokens, error: action.error, }; case "retry": { if (state.step !== "error") return state; return state.probe ? state.tokens ? { step: "oauth-done", url: state.url, probe: state.probe, tokens: state.tokens, } : { step: "probed", url: state.url, probe: state.probe } : { step: "url", url: state.url }; } default: return state; }} // ---------------------------------------------------------------------------// Component// --------------------------------------------------------------------------- export default function AddMcpSource(props: { onComplete: () => void; onCancel: () => void; initialUrl?: string; initialPreset?: string; /** Whether the stdio transport is enabled on the server. */ allowStdio?: boolean;}) { const allowStdio = props.allowStdio ?? false; const rawPreset = findPreset(props.initialPreset); // Drop stdio presets when stdio is disabled — the caller should have // already filtered these out, but defence-in-depth. const preset = rawPreset?.transport === "stdio" && !allowStdio ? undefined : rawPreset; const isStdioPreset = preset?.transport === "stdio"; const [transport, setTransport] = useState<"remote" | "stdio">( isStdioPreset && allowStdio ? "stdio" : "remote", ); // --- Stdio state --- const [stdioCommand, setStdioCommand] = useState(isStdioPreset ? preset.command : ""); const [stdioArgs, setStdioArgs] = useState( isStdioPreset && preset.args ? preset.args.join(" ") : "", ); const [stdioEnv, setStdioEnv] = useState(""); const stdioIdentity = useSourceIdentity({ fallbackName: isStdioPreset ? preset.name : stdioCommand, }); const [stdioAdding, setStdioAdding] = useState(false); const [stdioError, setStdioError] = useState<string | null>(null); // --- Remote state --- const remoteUrl = !isStdioPreset && preset?.transport === undefined && preset?.url ? preset.url : (props.initialUrl ?? ""); const [state, dispatch] = useReducer( reducer, remoteUrl ? { step: "url" as const, url: remoteUrl } : init, ); const scopeId = useScope(); const doProbe = useAtomSet(probeMcpEndpoint, { mode: "promise" }); const doAdd = useAtomSet(addMcpSource, { mode: "promise" }); const { beginAdd } = usePendingSources(); const secretList = useSecretPickerSecrets(); const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ popupName: "mcp-oauth", popupBlockedMessage: "OAuth popup was blocked", startErrorMessage: "Failed to start OAuth", }); const [remoteAuthMode, setRemoteAuthMode] = useState<RemoteAuthMode>("none"); const [remoteAuthHeaders, setRemoteAuthHeaders] = useState<HeaderState[]>([ { name: "Authorization", prefix: "Bearer ", presetKey: "bearer", secretId: null, }, ]); const [remoteHeaders, setRemoteHeaders] = useState<PlainHeader[]>([]); const [remoteCredentials, setRemoteCredentials] = useState(() => emptyHttpCredentials()); const probe = "probe" in state ? state.probe : null; const tokens = "tokens" in state ? state.tokens : null; const remoteIdentity = useSourceIdentity({ fallbackName: probe?.serverName ?? probe?.name ?? displayNameFromUrl(state.url) ?? "", }); const isProbing = state.step === "probing"; const isAdding = state.step === "adding"; const isOAuthBusy = state.step === "oauth-starting" || state.step === "oauth-waiting" || oauth.busy; const canUseNone = probe?.requiresOAuth !== true; const remoteAuthHeader = remoteAuthHeaders[0]; const headerAuthComplete = Boolean(remoteAuthHeader?.name.trim() && remoteAuthHeader?.secretId); const remoteHeadersComplete = remoteHeaders.every( (header) => header.name.trim() && header.value.trim(), ); const remoteCredentialsComplete = httpCredentialsValid(remoteCredentials); // OAuth is "ready to save" even without tokens — the source is stored // with a stable connectionId pointer, and each user completes their // own sign-in via McpSignInButton on the source detail page (per-user // scope shadowing means each user's tokens land at their own scope). const authReady = remoteAuthMode === "none" ? canUseNone : remoteAuthMode === "header" ? headerAuthComplete : true; const canAdd = Boolean(probe) && authReady && remoteHeadersComplete && remoteCredentialsComplete && !isAdding && !isOAuthBusy; // Probe failures are shown inline on the URL field; other failures // (OAuth start, add source) render in the bottom error block. const probeError = state.step === "error" && state.probe === null ? state.error : null; const otherError = state.step === "error" && state.probe !== null ? state.error : null; // ---- Remote actions ---- const handleProbe = useCallback(async () => { dispatch({ type: "probe-start" }); try { const { headers, queryParams } = serializeHttpCredentials(remoteCredentials); const result = await doProbe({ params: { scopeId }, payload: { endpoint: state.url.trim(), ...(Object.keys(headers).length > 0 ? { headers } : {}), ...(Object.keys(queryParams).length > 0 ? { queryParams } : {}), }, }); setRemoteAuthMode(result.requiresOAuth ? "oauth2" : "none"); dispatch({ type: "probe-ok", probe: result }); } catch (e) { dispatch({ type: "probe-fail", error: e instanceof Error ? e.message : "Failed to connect", }); } }, [state.url, scopeId, doProbe, remoteCredentials]); // Keep the latest handleProbe in a ref so the debounced effect can call it // without depending on its identity (which changes every render). const handleProbeRef = useRef(handleProbe); handleProbeRef.current = handleProbe; // Auto-probe whenever the URL changes (debounced) while we're on the // remote transport and not already probing/probed. useEffect(() => { if (transport !== "remote") return; if (state.step !== "url") return; const trimmed = state.url.trim(); if (!trimmed) return; const handle = setTimeout(() => { handleProbeRef.current(); }, 400); return () => clearTimeout(handle); }, [transport, state.step, state.url, remoteCredentials]); const handleRemoteCredentialsChange = useCallback( (next: typeof remoteCredentials) => { setRemoteCredentials(next); if (state.step === "error" || state.step === "probed" || state.step === "oauth-done") { dispatch({ type: "set-url", url: state.url }); } }, [state], ); const handleOAuth = useCallback(async () => { dispatch({ type: "oauth-start" }); const namespaceSlug = slugifyNamespace(remoteIdentity.namespace) || slugifyNamespace(probe?.namespace ?? "") || "mcp"; const { headers, queryParams } = serializeHttpCredentials(remoteCredentials); await oauth.start({ payload: { endpoint: state.url.trim(), ...(Object.keys(headers).length > 0 ? { headers } : {}), ...(Object.keys(queryParams).length > 0 ? { queryParams } : {}), redirectUrl: oauthCallbackUrl(), connectionId: oauthConnectionId({ pluginId: "mcp", namespace: namespaceSlug, }), strategy: { kind: "dynamic-dcr" }, pluginId: "mcp", identityLabel: `${remoteIdentity.name.trim() || probe?.serverName || probe?.name || "MCP"} OAuth`, }, onSuccess: (result) => { dispatch({ type: "oauth-ok", tokens: { connectionId: result.connectionId, expiresAt: result.expiresAt, scope: result.scope, }, }); }, onAuthorizationStarted: (result) => dispatch({ type: "oauth-waiting", sessionId: result.sessionId }), onError: (error) => dispatch({ type: "oauth-fail", error }), }); }, [state.url, remoteIdentity, probe, remoteCredentials, oauth]); const handleCancelOAuth = useCallback(() => { oauth.cancel(); dispatch({ type: "oauth-cancelled" }); }, [oauth]); const handleAddRemote = useCallback(async () => { if (!probe) return; dispatch({ type: "add-start" }); const headerAuth = remoteAuthHeaders[0]; // For oauth2 sources saved without completing the flow, use the // same stable connectionId the handleOAuth path would have used. // This pins the source's auth pointer, so when a per-user sign-in // runs later (via McpSignInButton) it mints the connection at the // user scope against the same id — innermost-wins shadowing then // resolves tokens per-user at invoke time. const deferredOAuthConnectionId = oauthConnectionId({ pluginId: "mcp", namespace: slugifyNamespace(remoteIdentity.namespace) || slugifyNamespace(probe.namespace ?? "") || "mcp", }); const auth = remoteAuthMode === "header" && headerAuth?.secretId ? { kind: "header" as const, headerName: headerAuth.name.trim(), secretId: headerAuth.secretId, ...(headerAuth.prefix ? { prefix: headerAuth.prefix } : {}), } : remoteAuthMode === "oauth2" ? { kind: "oauth2" as const, connectionId: tokens?.connectionId ?? deferredOAuthConnectionId, } : { kind: "none" as const }; const headers = Object.fromEntries( remoteHeaders .map((header) => [header.name.trim(), header.value.trim()] as const) .filter(([name, value]) => name && value), ); const credentials = serializeHttpCredentials(remoteCredentials); const remoteRequestHeaders: Record<string, SecretBackedValue> = { ...headers, ...credentials.headers, }; const displayName = remoteIdentity.name.trim() || probe.serverName || probe.name; const slugNamespace = slugifyNamespace(remoteIdentity.namespace); const placeholderId = slugNamespace || `pending:${crypto.randomUUID()}`; const placeholder = beginAdd({ id: placeholderId, name: displayName, kind: "mcp", url: state.url.trim(), }); try { await doAdd({ params: { scopeId }, payload: { transport: "remote" as const, name: displayName, namespace: slugNamespace || undefined, endpoint: state.url.trim(), auth, ...(Object.keys(remoteRequestHeaders).length > 0 ? { headers: remoteRequestHeaders } : {}), ...(Object.keys(credentials.queryParams).length > 0 ? { queryParams: credentials.queryParams } : {}), }, reactivityKeys: sourceWriteKeys, }); props.onComplete(); } catch (e) { dispatch({ type: "add-fail", error: e instanceof Error ? e.message : "Failed to add source", }); } finally {Lines 1–500 of 1067. Load more
import { useReducer, useCallback, useEffect, useRef, useState, type ReactNode } from "react"; import { useAtomSet } from "@effect/atom-react"; import { useScope } from "@executor-js/react/api/scope-context"; import { Button } from "@executor-js/react/components/button"; import { CardStack, CardStackContent, CardStackEntry, CardStackEntryActions, CardStackEntryContent, CardStackEntryDescription, CardStackEntryField, CardStackEntryMedia, CardStackEntryTitle, } from "@executor-js/react/components/card-stack"; import { FieldError, FieldLabel } from "@executor-js/react/components/field"; import { FilterTabs } from "@executor-js/react/components/filter-tabs"; import { FloatActions } from "@executor-js/react/components/float-actions"; import { Input } from "@executor-js/react/components/input"; import { Label } from "@executor-js/react/components/label"; import { Badge } from "@executor-js/react/components/badge"; import { Skeleton } from "@executor-js/react/components/skeleton"; import { SourceFavicon } from "@executor-js/react/components/source-favicon"; import { IOSSpinner, Spinner } from "@executor-js/react/components/spinner"; import { Textarea } from "@executor-js/react/components/textarea"; import { HeadersList } from "@executor-js/react/plugins/headers-list"; import { emptyHttpCredentials, httpCredentialsValid, HttpCredentialsEditor, serializeHttpCredentials, type SecretBackedValue, } from "@executor-js/react/plugins/http-credentials"; import { type HeaderState } from "@executor-js/react/plugins/secret-header-auth"; import { displayNameFromUrl, slugifyNamespace, SourceIdentityFields, useSourceIdentity, } from "@executor-js/react/plugins/source-identity"; import { useSecretPickerSecrets } from "@executor-js/react/plugins/use-secret-picker-secrets"; import { oauthCallbackUrl, oauthConnectionId, useOAuthPopupFlow, type OAuthCompletionPayload, } from "@executor-js/react/plugins/oauth-sign-in"; type RemoteAuthMode = "none" | "header" | "oauth2"; import { sourceWriteKeys } from "@executor-js/react/api/reactivity-keys"; import { usePendingSources } from "@executor-js/react/api/optimistic"; import { probeMcpEndpoint, addMcpSource } from "./atoms"; import { mcpPresets, type McpPreset } from "../sdk/presets"; // --------------------------------------------------------------------------- // Preset lookup // --------------------------------------------------------------------------- function findPreset(id: string | undefined): McpPreset | undefined { if (!id) return undefined; return mcpPresets.find((p) => p.id === id); } // --------------------------------------------------------------------------- // State machine (remote flow) // --------------------------------------------------------------------------- type OAuthTokens = OAuthCompletionPayload; type ProbeResult = { connected: boolean; requiresOAuth: boolean; name: string; namespace: string; toolCount: number | null; serverName: string | null; }; type PlainHeader = { name: string; value: string; }; type State = | { step: "url"; url: string } | { step: "probing"; url: string } | { step: "probed"; url: string; probe: ProbeResult } | { step: "oauth-starting"; url: string; probe: ProbeResult } | { step: "oauth-waiting"; url: string; probe: ProbeResult; sessionId: string; } | { step: "oauth-done"; url: string; probe: ProbeResult; tokens: OAuthTokens } | { step: "adding"; url: string; probe: ProbeResult; tokens: OAuthTokens | null; } | { step: "error"; url: string; probe: ProbeResult | null; tokens: OAuthTokens | null; error: string; }; type Action = | { type: "set-url"; url: string } | { type: "probe-start" } | { type: "probe-ok"; probe: ProbeResult } | { type: "probe-fail"; error: string } | { type: "oauth-start" } | { type: "oauth-waiting"; sessionId: string } | { type: "oauth-ok"; tokens: OAuthTokens } | { type: "oauth-fail"; error: string } | { type: "oauth-cancelled" } | { type: "add-start" } | { type: "add-fail"; error: string } | { type: "retry" }; const init: State = { step: "url", url: "" }; function reducer(state: State, action: Action): State { switch (action.type) { case "set-url": return { step: "url", url: action.url }; case "probe-start": return { step: "probing", url: state.url }; case "probe-ok": return { step: "probed", url: state.url, probe: action.probe }; case "probe-fail": return { step: "error", url: state.url, probe: null, tokens: null, error: action.error, }; case "oauth-start": if (state.step !== "probed" && state.step !== "error") return state; return { step: "oauth-starting", url: state.url, probe: state.step === "probed" ? state.probe : state.probe!, }; case "oauth-waiting": if (state.step !== "oauth-starting") return state; return { step: "oauth-waiting", url: state.url, probe: state.probe, sessionId: action.sessionId, }; case "oauth-ok": if (state.step !== "oauth-waiting") return state; return { step: "oauth-done", url: state.url, probe: state.probe, tokens: action.tokens, }; case "oauth-fail": if (state.step !== "oauth-starting" && state.step !== "oauth-waiting") return state; return { step: "error", url: state.url, probe: state.probe, tokens: null, error: action.error, }; case "oauth-cancelled": if (state.step !== "oauth-waiting") return state; return { step: "probed", url: state.url, probe: state.probe }; case "add-start": { const tokens = state.step === "oauth-done" ? state.tokens : state.step === "probed" ? null : null; const probe = "probe" in state ? state.probe : null; if (!probe) return state; return { step: "adding", url: state.url, probe, tokens }; } case "add-fail": if (state.step !== "adding") return state; return { step: "error", url: state.url, probe: state.probe, tokens: state.tokens, error: action.error, }; case "retry": { if (state.step !== "error") return state; return state.probe ? state.tokens ? { step: "oauth-done", url: state.url, probe: state.probe, tokens: state.tokens, } : { step: "probed", url: state.url, probe: state.probe } : { step: "url", url: state.url }; } default: return state; } } // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- export default function AddMcpSource(props: { onComplete: () => void; onCancel: () => void; initialUrl?: string; initialPreset?: string; /** Whether the stdio transport is enabled on the server. */ allowStdio?: boolean; }) { const allowStdio = props.allowStdio ?? false; const rawPreset = findPreset(props.initialPreset); // Drop stdio presets when stdio is disabled — the caller should have // already filtered these out, but defence-in-depth. const preset = rawPreset?.transport === "stdio" && !allowStdio ? undefined : rawPreset; const isStdioPreset = preset?.transport === "stdio"; const [transport, setTransport] = useState<"remote" | "stdio">( isStdioPreset && allowStdio ? "stdio" : "remote", ); // --- Stdio state --- const [stdioCommand, setStdioCommand] = useState(isStdioPreset ? preset.command : ""); const [stdioArgs, setStdioArgs] = useState( isStdioPreset && preset.args ? preset.args.join(" ") : "", ); const [stdioEnv, setStdioEnv] = useState(""); const stdioIdentity = useSourceIdentity({ fallbackName: isStdioPreset ? preset.name : stdioCommand, }); const [stdioAdding, setStdioAdding] = useState(false); const [stdioError, setStdioError] = useState<string | null>(null); // --- Remote state --- const remoteUrl = !isStdioPreset && preset?.transport === undefined && preset?.url ? preset.url : (props.initialUrl ?? ""); const [state, dispatch] = useReducer( reducer, remoteUrl ? { step: "url" as const, url: remoteUrl } : init, ); const scopeId = useScope(); const doProbe = useAtomSet(probeMcpEndpoint, { mode: "promise" }); const doAdd = useAtomSet(addMcpSource, { mode: "promise" }); const { beginAdd } = usePendingSources(); const secretList = useSecretPickerSecrets(); const oauth = useOAuthPopupFlow<OAuthCompletionPayload>({ popupName: "mcp-oauth", popupBlockedMessage: "OAuth popup was blocked", startErrorMessage: "Failed to start OAuth", }); const [remoteAuthMode, setRemoteAuthMode] = useState<RemoteAuthMode>("none"); const [remoteAuthHeaders, setRemoteAuthHeaders] = useState<HeaderState[]>([ { name: "Authorization", prefix: "Bearer ", presetKey: "bearer", secretId: null, }, ]); const [remoteHeaders, setRemoteHeaders] = useState<PlainHeader[]>([]); const [remoteCredentials, setRemoteCredentials] = useState(() => emptyHttpCredentials()); const probe = "probe" in state ? state.probe : null; const tokens = "tokens" in state ? state.tokens : null; const remoteIdentity = useSourceIdentity({ fallbackName: probe?.serverName ?? probe?.name ?? displayNameFromUrl(state.url) ?? "", }); const isProbing = state.step === "probing"; const isAdding = state.step === "adding"; const isOAuthBusy = state.step === "oauth-starting" || state.step === "oauth-waiting" || oauth.busy; const canUseNone = probe?.requiresOAuth !== true; const remoteAuthHeader = remoteAuthHeaders[0]; const headerAuthComplete = Boolean(remoteAuthHeader?.name.trim() && remoteAuthHeader?.secretId); const remoteHeadersComplete = remoteHeaders.every( (header) => header.name.trim() && header.value.trim(), ); const remoteCredentialsComplete = httpCredentialsValid(remoteCredentials); // OAuth is "ready to save" even without tokens — the source is stored // with a stable connectionId pointer, and each user completes their // own sign-in via McpSignInButton on the source detail page (per-user // scope shadowing means each user's tokens land at their own scope). const authReady = remoteAuthMode === "none" ? canUseNone : remoteAuthMode === "header" ? headerAuthComplete : true; const canAdd = Boolean(probe) && authReady && remoteHeadersComplete && remoteCredentialsComplete && !isAdding && !isOAuthBusy; // Probe failures are shown inline on the URL field; other failures // (OAuth start, add source) render in the bottom error block. const probeError = state.step === "error" && state.probe === null ? state.error : null; const otherError = state.step === "error" && state.probe !== null ? state.error : null; // ---- Remote actions ---- const handleProbe = useCallback(async () => { dispatch({ type: "probe-start" }); try { const { headers, queryParams } = serializeHttpCredentials(remoteCredentials); const result = await doProbe({ params: { scopeId }, payload: { endpoint: state.url.trim(), ...(Object.keys(headers).length > 0 ? { headers } : {}), ...(Object.keys(queryParams).length > 0 ? { queryParams } : {}), }, }); setRemoteAuthMode(result.requiresOAuth ? "oauth2" : "none"); dispatch({ type: "probe-ok", probe: result }); } catch (e) { dispatch({ type: "probe-fail", error: e instanceof Error ? e.message : "Failed to connect", }); } }, [state.url, scopeId, doProbe, remoteCredentials]); // Keep the latest handleProbe in a ref so the debounced effect can call it // without depending on its identity (which changes every render). const handleProbeRef = useRef(handleProbe); handleProbeRef.current = handleProbe; // Auto-probe whenever the URL changes (debounced) while we're on the // remote transport and not already probing/probed. useEffect(() => { if (transport !== "remote") return; if (state.step !== "url") return; const trimmed = state.url.trim(); if (!trimmed) return; const handle = setTimeout(() => { handleProbeRef.current(); }, 400); return () => clearTimeout(handle); }, [transport, state.step, state.url, remoteCredentials]); const handleRemoteCredentialsChange = useCallback( (next: typeof remoteCredentials) => { setRemoteCredentials(next); if (state.step === "error" || state.step === "probed" || state.step === "oauth-done") { dispatch({ type: "set-url", url: state.url }); } }, [state], ); const handleOAuth = useCallback(async () => { dispatch({ type: "oauth-start" }); const namespaceSlug = slugifyNamespace(remoteIdentity.namespace) || slugifyNamespace(probe?.namespace ?? "") || "mcp"; const { headers, queryParams } = serializeHttpCredentials(remoteCredentials); await oauth.start({ payload: { endpoint: state.url.trim(), ...(Object.keys(headers).length > 0 ? { headers } : {}), ...(Object.keys(queryParams).length > 0 ? { queryParams } : {}), redirectUrl: oauthCallbackUrl(), connectionId: oauthConnectionId({ pluginId: "mcp", namespace: namespaceSlug, }), strategy: { kind: "dynamic-dcr" }, pluginId: "mcp", identityLabel: `${remoteIdentity.name.trim() || probe?.serverName || probe?.name || "MCP"} OAuth`, }, onSuccess: (result) => { dispatch({ type: "oauth-ok", tokens: { connectionId: result.connectionId, expiresAt: result.expiresAt, scope: result.scope, }, }); }, onAuthorizationStarted: (result) => dispatch({ type: "oauth-waiting", sessionId: result.sessionId }), onError: (error) => dispatch({ type: "oauth-fail", error }), }); }, [state.url, remoteIdentity, probe, remoteCredentials, oauth]); const handleCancelOAuth = useCallback(() => { oauth.cancel(); dispatch({ type: "oauth-cancelled" }); }, [oauth]); const handleAddRemote = useCallback(async () => { if (!probe) return; dispatch({ type: "add-start" }); const headerAuth = remoteAuthHeaders[0]; // For oauth2 sources saved without completing the flow, use the // same stable connectionId the handleOAuth path would have used. // This pins the source's auth pointer, so when a per-user sign-in // runs later (via McpSignInButton) it mints the connection at the // user scope against the same id — innermost-wins shadowing then // resolves tokens per-user at invoke time. const deferredOAuthConnectionId = oauthConnectionId({ pluginId: "mcp", namespace: slugifyNamespace(remoteIdentity.namespace) || slugifyNamespace(probe.namespace ?? "") || "mcp", }); const auth = remoteAuthMode === "header" && headerAuth?.secretId ? { kind: "header" as const, headerName: headerAuth.name.trim(), secretId: headerAuth.secretId, ...(headerAuth.prefix ? { prefix: headerAuth.prefix } : {}), } : remoteAuthMode === "oauth2" ? { kind: "oauth2" as const, connectionId: tokens?.connectionId ?? deferredOAuthConnectionId, } : { kind: "none" as const }; const headers = Object.fromEntries( remoteHeaders .map((header) => [header.name.trim(), header.value.trim()] as const) .filter(([name, value]) => name && value), ); const credentials = serializeHttpCredentials(remoteCredentials); const remoteRequestHeaders: Record<string, SecretBackedValue> = { ...headers, ...credentials.headers, }; const displayName = remoteIdentity.name.trim() || probe.serverName || probe.name; const slugNamespace = slugifyNamespace(remoteIdentity.namespace); const placeholderId = slugNamespace || `pending:${crypto.randomUUID()}`; const placeholder = beginAdd({ id: placeholderId, name: displayName, kind: "mcp", url: state.url.trim(), }); try { await doAdd({ params: { scopeId }, payload: { transport: "remote" as const, name: displayName, namespace: slugNamespace || undefined, endpoint: state.url.trim(), auth, ...(Object.keys(remoteRequestHeaders).length > 0 ? { headers: remoteRequestHeaders } : {}), ...(Object.keys(credentials.queryParams).length > 0 ? { queryParams: credentials.queryParams } : {}), }, reactivityKeys: sourceWriteKeys, }); props.onComplete(); } catch (e) { dispatch({ type: "add-fail", error: e instanceof Error ? e.message : "Failed to add source", }); } finally {