import { openExternalUrl } from "@getpaseo/plugin/client"; import { copyText } from "@getpaseo/plugin/client/ui"; import { useState, type ReactNode } from "react"; import { Text, View } from "react-native"; import { MCP_HEADER_PATTERN, MCP_NAME_PATTERN, mcpNameFromUrl } from "../shared/mcp"; import type { McpServerView, SignInView } from "../shared/rpc"; import { errorMessage, mcpStatusView, plural } from "./format"; import { Empty, Figures, Frame, Grid, StatusChip } from "./frame"; import { Actions, Button, Code, ConfirmButton, Field, Meter, Note, Pill, Section, Stat, SwitchRow, Toggle, figures, kicker, mono, text, tint, toneColor, type Theme, type Tone, } from "./kit"; import { mcpLoadState, type PanelTabProps } from "./panel"; import { useAddMcpServer, useMcpChoices, useMcpServers, useMcpSignIn, useRemoveMcpServer, useStatus } from "./queries"; /** MCP servers: registered in the Docker Sandboxes screen, chosen per repository in the workspace panel. */ const ISSUES = "https://github.com/drungrin/paseo-sbx/issues"; const AUTH_LABEL: Record = { oauth: "Signs in with OAuth", "api-key": "API key", none: "No sign-in", }; /** A prefilled issue, with paths under a home folder shortened so no user name is posted. */ function issueUrl(problem: string, sbxVersion: string | null): string { const redacted = problem.replace(/\/(home|Users)\/[^/\s]+/g, "~"); const title = `MCP state not recognized with sbx ${sbxVersion ?? "(unknown version)"}`; const body = [ "paseo-sbx could not read what sbx reports about MCP servers.", "", `sbx: ${sbxVersion ?? "unknown"}`, "", "```", redacted, "```", ].join("\n"); return `${ISSUES}/new?title=${encodeURIComponent(title)}&body=${encodeURIComponent(body)}`; } /** * sbx keeps no public record of what a local sandbox's gateway loaded, so the plugin reads sbx's * own; this says when that record (or sbx's output) changed shape, and how to report it. */ export function FormatHint({ problem, theme }: { problem: string; theme: Theme }) { const status = useStatus(); const version = status.data?.sbx.version ?? null; return (
{`${version ? `sbx ${version}` : "This sbx"} keeps its MCP records in a shape this plugin version does not know, so it cannot tell which servers are loaded where. Turning servers on and off still works. Report it, so the plugin can follow:`}
); } export function McpServersTab({ theme, compact, header, stateHome, }: { theme: Theme; compact: boolean; header: { status: ReactNode; actions: ReactNode }; stateHome: string | null; }) { const servers = useMcpServers(); const choices = useMcpChoices(); const signIn = useMcpSignIn(); const remove = useRemoveMcpServer(); const [error, setError] = useState(null); const global = choices.settings.status === "ready" ? choices.settings.values.mcpGlobal : []; const data = servers.data; const ready = data?.servers.filter((server) => server.status === "ready").length ?? 0; function attempt(action: Promise) { setError(null); action.catch((caught) => setError(errorMessage(caught))); } return ( {servers.error ? (
{errorMessage(servers.error)}
) : null} {data?.unrecognized ? : null} {signIn.view ? ( signIn.finish.mutate(address)} onRetry={() => signIn.start.mutate(signIn.view?.server ?? "")} onClose={signIn.close} /> ) : null} {signIn.start.error ? ( {errorMessage(signIn.start.error)} ) : null} {!data ? ( servers.error ? null : ( Reading the MCP servers… ) ) : data.servers.length === 0 ? ( ) : ( <> global.includes(server.name)).length)} detail="The rest load where a repository turns them on" /> server.name} render={(server) => ( attempt(choices.setGlobal(server.name, on))} onSignIn={() => signIn.start.mutate(server.name)} onRemove={() => attempt(remove.mutateAsync(server.name).then(() => choices.forget(server.name)))} /> )} /> )} {error ? ( {error} ) : null} { if (everywhere) await choices.setGlobal(server.name, true); if (server.status === "needs-auth") signIn.start.mutate(server.name); }} /> ); } function ServerCard({ server, sandboxes, global, stateHome, theme, signingIn, removing, onGlobal, onSignIn, onRemove, }: { server: McpServerView; sandboxes: number; global: boolean; stateHome: string | null; theme: Theme; signingIn: boolean; removing: boolean; onGlobal(on: boolean): void; onSignIn(): void; onRemove(): void; }) { const status = mcpStatusView(server); const loaded = server.loadedIn.length; return (
} > {server.url ? ( {server.url} ) : null} Loaded {sandboxes === 0 ? "No sandboxes yet" : `${loaded} of ${plural(sandboxes, "sandbox", "sandboxes")}`} {server.status === "needs-secret" && server.nextStep ? ( Its API key is not stored. Store it on the Paseo host: ) : null} {server.status === "needs-auth" ? ( Until you sign in, agents see only its sign-in tool, which cannot finish without you. ) : null} {server.auth === "oauth" ? (
); } function Step({ number, title, theme, children }: { number: number; title: string; theme: Theme; children: ReactNode }) { const accent = toneColor(theme, "accent"); return ( {number} {title} {children} ); } const SIGN_IN_CHIP: Record = { waiting: { label: "Waiting for you", tone: "accent", moving: true }, done: { label: "Signed in", tone: "success", moving: false }, failed: { label: "Failed", tone: "danger", moving: false }, cancelled: { label: "Cancelled", tone: "neutral", moving: false }, }; /** * sbx's sign-in, on any device: open the provider's page there, then paste back the address it * sends the browser to, which only the Paseo host can open. On the host's own browser it ends alone. */ function SignInCard({ view, theme, finishing, finishError, onFinish, onRetry, onClose, }: { view: SignInView; theme: Theme; finishing: boolean; finishError: string | null; onFinish(address: string): void; onRetry(): void; onClose(): void; }) { const [address, setAddress] = useState(""); const [copied, setCopied] = useState(false); const chip = SIGN_IN_CHIP[view.status]; const waiting = view.status === "waiting"; const tone: Tone = view.status === "done" ? "success" : view.status === "failed" ? "danger" : "accent"; return (
} > {waiting && view.url ? ( <> {view.url}
); } function AddServer({ theme, compact, onAdded, }: { theme: Theme; compact: boolean; onAdded(server: McpServerView, everywhere: boolean): Promise; }) { const add = useAddMcpServer(); const [url, setUrl] = useState(""); const [name, setName] = useState(null); const [auth, setAuth] = useState<"oauth" | "api-key">("oauth"); const [header, setHeader] = useState("Authorization"); const [key, setKey] = useState(""); const [everywhere, setEverywhere] = useState(true); const [error, setError] = useState(null); // Named after the address until you type a name of your own. const shownName = name ?? mcpNameFromUrl(url); const nameError = shownName && !MCP_NAME_PATTERN.test(shownName) ? "Letters, digits, dots, dashes and underscores" : null; const headerError = header && !MCP_HEADER_PATTERN.test(header) ? "Letters, digits and dashes" : null; const complete = url.trim() !== "" && shownName !== "" && !nameError && (auth === "oauth" || (key.trim() !== "" && header !== "" && !headerError)); async function submit() { if (!complete || add.isPending) return; setError(null); try { const server = await add.mutateAsync({ name: shownName, url: url.trim(), apiKey: auth === "api-key" ? { header: header.trim(), value: key } : null, }); setUrl(""); setName(null); setKey(""); await onAdded(server, everywhere); } catch (caught) { setError(errorMessage(caught)); } } const row = { flexDirection: compact ? ("column" as const) : ("row" as const), gap: 12 }; return (
Signs in with theme={theme} label="Signs in with" options={[ { value: "oauth", label: "Sign-in, if it asks" }, { value: "api-key", label: "API key" }, ]} value={auth} onChange={setAuth} /> {auth === "oauth" ? "A server that asks for a sign-in shows its page right after you add it." : "sbx keeps the key on this host and adds it to every request: it never enters a sandbox."} {auth === "api-key" ? ( void submit()} /> ) : null}
); } /** One server's choice for the repository: the switch, where it stands in the sandbox, and whose choice it is. */ function ChoiceCard({ server, data, choice, global, ready, theme, onChoose, }: { server: McpServerView; data: PanelTabProps["data"]; choice: boolean | undefined; global: boolean; ready: boolean; theme: Theme; onChoose(on: boolean | null): void; }) { const on = choice ?? global; const state = mcpLoadState(data, server.name, on); const status = mcpStatusView(server); return (
} > {server.url ? ( {server.url} ) : null} onChoose(next)} hint={ {state.label} } /> {choice !== undefined ? (
); } export function McpTab({ data, theme, compact, header }: PanelTabProps) { const servers = useMcpServers(); const choices = useMcpChoices(); const [error, setError] = useState(null); const values = choices.settings.status === "ready" ? choices.settings.values : null; const own = values?.repos[data.repoKey]?.mcp ?? {}; const list = servers.data?.servers ?? null; function choose(name: string, on: boolean | null) { setError(null); choices.setRepo(data.repoKey, name, on).catch((caught) => setError(errorMessage(caught))); } return ( {data.mcp.unreadable ? : null} {servers.error ? (
{errorMessage(servers.error)}
) : null} {!list ? ( servers.error ? null : ( Reading the MCP servers… ) ) : list.length === 0 ? ( ) : ( server.name} render={(server) => ( choose(server.name, on)} /> )} /> )} {error ? ( {error} ) : null} ); }