import { useEffect, useState } from "react";
import { assistantClientNames, mcpAuthorizationHandoffInstructions, mcpInvitation, mcpSetupSteps, type AssistantClient, type McpConnection } from "@paperclipai/shared";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Check, Copy, ExternalLink, Globe, Paperclip, Plug, Terminal } from "lucide-react";
import { publicMcpApi } from "@/api/publicMcp";
import { QueryErrorState, useQueryView } from "@/components/QueryView";
import { useCompany } from "@/context/CompanyContext";
import { useBreadcrumbs } from "@/context/BreadcrumbContext";
import { Link } from "@/lib/router";
import { copyTextToClipboard } from "@/lib/clipboard";
import { deriveInitials, Identity } from "@/components/Identity";
import { assistantConnectionDisplayName } from "./connection-owner";
import { AgentSetupPrompt } from "@/components/AgentSetupPrompt";
import { OpenCodeLogoIcon } from "@/components/OpenCodeLogoIcon";
import { CompanyPatternIcon } from "@/components/CompanyPatternIcon";
import { Button } from "@/components/ui/button";
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { ANALYTICS_MASK_CLASS } from "@/lib/analytics/privacy";
import { cn } from "@/lib/utils";
export const ASSISTANT_CONNECTION_PATH = "/apps/assistant-connection";
const connectionsKey = ["mcp-connections"];
type Assistant = AssistantClient;
const assistants = assistantClientNames;
function AssistantIcon({ assistant }: { assistant: Assistant }) {
if (assistant === "codex" || assistant === "claude") return ;
if (assistant === "opencode") return ;
const Icon = assistant === "browser" ? Globe : assistant === "headless" ? Terminal : Plug;
return ;
}
export function useAssistantConnections(poll = false) {
const { selectedCompanyId } = useCompany();
const query = useQuery({
queryKey: connectionsKey, queryFn: publicMcpApi.connections,
enabled: Boolean(selectedCompanyId), refetchInterval: poll ? 5000 : false,
});
const view = useQueryView(query);
return {
...query,
view,
/** Loaded rows are shown through a transient refetch failure (`stale`) as well as `ready`. */
loaded: view.kind === "ready" || view.kind === "stale",
rows: (query.data ?? []).filter(row => row.companyId === selectedCompanyId && !row.revokedAt && !row.scopes.includes("paperclip:agent")),
};
}
/** Inbound assistant access belongs beside the existing outbound connectors. */
export function AssistantConnectionCard({ onNavigate }: { onNavigate: (href: string) => void }) {
const connections = useAssistantConnections();
const active = connections.rows;
const action = !connections.loaded ? "Open" : active.length ? "Manage" : "Set up";
return
Use your Paperclip organization from Codex, Claude, OpenCode, or another assistant.
Checking your connection status…
} {connections.view.kind === "error" &&{value}
{error ? "Couldn’t copy. Select and copy the text above." : copied ? "Copied" : null}
Select an organization to connect an assistant.
; const serverUrl = setup.data?.serverUrl ?? ""; const invitation = serverUrl ? mcpInvitation(serverUrl, { id: selectedCompanyId, name: selectedCompany.name }) : ""; return{selectedCompany.name}
Connect your assistant to Paperclip. Review work, create tasks, and follow up using your account’s access to this organization.
Loading assistant setup…
} {setupView.kind === "error" && (setupView.errorKind === "auth" || setupView.errorKind === "not_found" ?{setupUnavailableCopy(setupView.errorKind)}
Paste this invitation into your assistant. It will help set up the connection and ask you to approve access in Paperclip.
{step.text}
{step.code &&{mcpAuthorizationHandoffInstructions}
}Choose {selectedCompany.name}, review access, then click Connect organization.
Creating tasks and adding comments may start agent work using the organization’s configured execution budget.
Loading connections…
} {revoke.error &&Couldn’t revoke this connection. Try again.
} {connections.view.kind === "error" &&No assistants connected to {selectedCompany.name} yet.
}{`Connected as you · ${row.scopes.includes("paperclip:write") ? "Read and write" : "Read only"}${row.scopes.includes("paperclip:configure") ? " · Configure agents, projects and skills" : ""}`}