Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

3.3 KB
Raw Blame
'use client';

import { useThreadIsRunning } from '@assistant-ui/core/react';
import { useTrueForgeMcpAuth } from '@truefoundry/trueforge-assistant-ui-runtime';
import { useEffect, useRef, useState } from 'react';

import { useDraftCatalog } from '@/atoms/draft/DraftCatalogProvider.js';
import { useMCPAuth } from '@/hooks/useMcpAuth.js';
import { useOptionalCatalogServer } from '@/server/ServerContext.js';
import { useSlot } from '../theme/SlotsProvider.js';

type McpAuthPromptProps = {
  servers: NonNullable<ReturnType<typeof useTrueForgeMcpAuth>['pending']>['mcpServers'];
  onContinue: () => Promise<void>;
  readOnly: boolean;
};

function CatalogMcpAuthPrompt({ servers, onContinue, readOnly }: McpAuthPromptProps) {
  const McpAuthPrompt = useSlot('McpAuthPrompt');
  const { handleAuthorize } = useMCPAuth();
  const { refreshConnectors } = useDraftCatalog();
  const [connectedServerIds, setConnectedServerIds] = useState<ReadonlySet<string>>(() => new Set());
  const connectedServerIdsRef = useRef(connectedServerIds);
  const [isResuming, setIsResuming] = useState(false);
  const resumedRef = useRef(false);
  const promptGenerationRef = useRef(0);

  useEffect(
    () => () => {
      promptGenerationRef.current += 1;
    },
    [],
  );

  const startResume = () => {
    if (readOnly || resumedRef.current) return;
    resumedRef.current = true;
    setIsResuming(true);
    void onContinue().catch(() => {
      resumedRef.current = false;
      setIsResuming(false);
    });
  };

  const handleConnect = (serverId: string) => {
    const generation = promptGenerationRef.current;
    void handleAuthorize(serverId, isSuccess => {
      if (generation !== promptGenerationRef.current || !isSuccess) return;
      const nextConnectedServerIds = new Set([...connectedServerIdsRef.current, serverId]);
      connectedServerIdsRef.current = nextConnectedServerIds;
      setConnectedServerIds(nextConnectedServerIds);
      void refreshConnectors();
      if (servers.every(server => nextConnectedServerIds.has(server.id))) startResume();
    });
  };

  return (
    <McpAuthPrompt
      servers={servers}
      connectedServerIds={connectedServerIds}
      continueLoading={isResuming}
      onConnect={handleConnect}
      onContinue={startResume}
      readOnly={readOnly}
    />
  );
}

export function McpAuthContainer({ disabled = false }: { disabled?: boolean }) {
  const McpAuthPrompt = useSlot('McpAuthPrompt');
  const { pending, resume } = useTrueForgeMcpAuth();
  const isRunning = useThreadIsRunning();
  const catalog = useOptionalCatalogServer();

  if (!pending) return null;

  if (catalog) {
    const pendingServerKey = JSON.stringify(pending.mcpServers.map(server => server.id));
    return (
      <CatalogMcpAuthPrompt
        key={pendingServerKey}
        servers={pending.mcpServers}
        onContinue={resume}
        readOnly={isRunning || disabled}
      />
    );
  }

  const handleConnect = (serverId: string) => {
    const server = pending.mcpServers.find(s => s.id === serverId);
    if (server?.authUrl) {
      window.open(server.authUrl, '_blank', 'noopener,noreferrer');
    }
  };

  return (
    <McpAuthPrompt
      servers={pending.mcpServers}
      onConnect={handleConnect}
      onContinue={() => {
        if (!disabled) void resume();
      }}
      readOnly={isRunning || disabled}
    />
  );
}