Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

5.6 KB
Raw Blame
import { useState } from 'react';
import { QRCodeSVG } from 'qrcode.react';
import { QrCode, ExternalLink } from 'lucide-react';
import {
  Input,
  Button,
  OGDialog,
  OGDialogTitle,
  OGDialogContent,
  OGDialogDescription,
} from '@librechat/client';
import CopyButton from '~/components/Messages/Content/CopyButton';
import { useLocalize, useCopyToClipboard } from '~/hooks';
import { cn, openInNewTab } from '~/utils';

interface McpOAuthDialogProps {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  serverName: string;
  oauthUrl: string;
  /** The MCP server's icon, shown beside the title when the server provides one. */
  iconUrl?: string;
}

/**
 * Dedicated second dialog, opened ONLY when connecting an MCP server requires
 * OAuth. Offers three ways to finish: continue in this browser, copy the
 * authorization URL to open elsewhere, or reveal a QR code to scan on a phone.
 * Auto-closes once the server connects (the caller derives `open` from
 * connection state).
 */
export default function McpOAuthDialog({
  open,
  onOpenChange,
  serverName,
  oauthUrl,
  iconUrl,
}: McpOAuthDialogProps) {
  const localize = useLocalize();
  const [isCopying, setIsCopying] = useState(false);
  const [showQR, setShowQR] = useState(false);
  const [iconError, setIconError] = useState(false);
  const copyUrl = useCopyToClipboard({ text: oauthUrl });

  if (!oauthUrl) {
    return null;
  }

  return (
    <OGDialog open={open} onOpenChange={onOpenChange}>
      <OGDialogContent className="w-11/12 max-w-md overflow-x-hidden overflow-y-auto rounded-2xl">
        <div className="flex items-center gap-2">
          {iconUrl && !iconError && (
            <span
              className="bg-surface-fixed flex size-6 shrink-0 items-center justify-center overflow-hidden rounded-md"
              aria-hidden="true"
            >
              <img
                src={iconUrl}
                alt=""
                className="h-full w-full object-cover"
                loading="lazy"
                onError={() => setIconError(true)}
              />
            </span>
          )}
          <OGDialogTitle className="text-base leading-6 font-semibold">
            {localize('com_nav_mcp_connect_server', { 0: serverName })}
          </OGDialogTitle>
        </div>
        <OGDialogDescription className="text-sm">
          {localize('com_ui_mcp_oauth_description')}
        </OGDialogDescription>

        <div className="flex min-w-0 flex-col gap-3 p-1">
          {/* Auto-height reveal via grid-template-rows 0fr -> 1fr so the QR slides
           * open smoothly without a hardcoded height, matching MCPToolItem. */}
          <div
            className={cn(
              'grid transition-all duration-(--resize-dur) ease-(--resize-ease) motion-reduce:transition-none',
              showQR ? 'grid-rows-[1fr]' : 'grid-rows-[0fr]',
            )}
          >
            <div className="min-h-0 overflow-hidden">
              <div
                className={cn(
                  'flex flex-col items-center gap-2 pb-1 transition-opacity duration-200 ease-out motion-reduce:transition-none',
                  showQR ? 'opacity-100' : 'opacity-0',
                )}
              >
                <div className="bg-surface-qr max-w-full min-w-0 rounded-2xl p-4 shadow-lg">
                  {/* size is only the no-CSS fallback; the rem width is what renders,
                      so the code follows the dialog instead of staying at 180px. */}
                  <QRCodeSVG
                    value={oauthUrl}
                    size={180}
                    className="h-auto w-[11.25rem] max-w-full"
                    marginSize={2}
                    title={localize('com_ui_mcp_oauth_qr_code_description')}
                  />
                </div>
                <span className="text-text-secondary text-xs">
                  {localize('com_ui_mcp_oauth_scan_qr')}
                </span>
              </div>
            </div>
          </div>

          <div className="relative">
            <Input
              type="text"
              readOnly
              dir="ltr"
              value={oauthUrl}
              aria-label={localize('com_ui_copy_link')}
              onFocus={(event) => event.currentTarget.select()}
              className="text-text-secondary pr-10"
              data-testid="mcp-oauth-url"
            />
            <CopyButton
              iconOnly
              isCopied={isCopying}
              label={localize('com_ui_copy_link')}
              onClick={() => {
                if (!isCopying) {
                  copyUrl(setIsCopying);
                }
              }}
              className="absolute top-1/2 right-1 -translate-y-1/2"
            />
          </div>

          <div className="flex min-w-0 flex-wrap items-center gap-2">
            <Button
              type="button"
              variant="outline"
              size="icon"
              className="shrink-0"
              aria-expanded={showQR}
              aria-label={showQR ? localize('com_ui_hide_qr') : localize('com_ui_show_qr')}
              onClick={() => setShowQR((value) => !value)}
            >
              <QrCode className="size-4" aria-hidden="true" />
            </Button>
            <Button
              type="button"
              variant="submit"
              className="h-auto min-h-10 min-w-0 flex-1 basis-40 whitespace-normal"
              onClick={() => openInNewTab(oauthUrl)}
            >
              {localize('com_ui_continue_oauth')}
              <ExternalLink className="size-4 shrink-0" aria-hidden="true" />
            </Button>
          </div>
        </div>
      </OGDialogContent>
    </OGDialog>
  );
}