import { useState } from 'react' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription, DialogFooter } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Alert, AlertDescription } from '@/components/ui/alert' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Loader2, ExternalLink, Key, XCircle } from 'lucide-react' import type { McpAuthStartResponse } from '@/api/mcp' interface McpOAuthDialogProps { open: boolean onOpenChange: (open: boolean) => void serverName: string onAutoAuth: () => Promise onStartAuth: () => Promise onCompleteAuth: (code: string) => Promise directory?: string } export function McpOAuthDialog({ open, onOpenChange, serverName, onAutoAuth, onStartAuth, onCompleteAuth, directory }: McpOAuthDialogProps) { const [step, setStep] = useState<'method' | 'auto' | 'manual' | 'enter_code'>('method') const [loading, setLoading] = useState(false) const [authUrl, setAuthUrl] = useState(null) const [authCode, setAuthCode] = useState('') const [error, setError] = useState(null) const handleAutoAuth = async () => { setLoading(true) setError(null) setStep('auto') try { await onAutoAuth() onOpenChange(false) resetState() } catch (err) { setError(err instanceof Error ? err.message : 'Failed to authenticate') setStep('method') } finally { setLoading(false) } } const handleStartManualAuth = async () => { setLoading(true) setError(null) try { const result = await onStartAuth() setAuthUrl(result.authorizationUrl) setStep('manual') } catch (err) { setError(err instanceof Error ? err.message : 'Failed to start authentication') } finally { setLoading(false) } } const handleCompleteAuth = async () => { if (!authCode.trim()) return setLoading(true) setError(null) try { await onCompleteAuth(authCode.trim()) onOpenChange(false) resetState() } catch (err) { setError(err instanceof Error ? err.message : 'Failed to complete authentication') } finally { setLoading(false) } } const handleOpenAuthUrl = () => { if (authUrl) { window.open(authUrl, '_blank') setStep('enter_code') } } const resetState = () => { setStep('method') setAuthUrl(null) setAuthCode('') setError(null) setLoading(false) } const handleOpenChange = (newOpen: boolean) => { if (!newOpen) { resetState() } onOpenChange(newOpen) } const scopes = directory ? 'this location' : 'global' return ( Connect {serverName} Authenticate to use this MCP server for {scopes}
{error && ( {error} )} {step === 'method' && (

Choose an authentication method:

)} {step === 'auto' && (

Waiting for browser authorization...

)} {step === 'manual' && authUrl && (

Open the authorization link in your browser to begin the flow. Then paste the code below.

{authUrl}
)} {step === 'enter_code' && (

Paste the authorization code from the browser:

setAuthCode(e.target.value)} placeholder="Paste code here..." disabled={loading} />
)}
{step === 'enter_code' && ( <> )} {(step === 'method' || step === 'manual') && ( )}
) }