import { MCPCardVO } from '@/app/home/plugins/mcp-server/MCPCardVO'; import { useState, useEffect } from 'react'; import { httpClient } from '@/app/infra/http/HttpClient'; import { Switch } from '@/components/ui/switch'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { toast } from 'sonner'; import { useTranslation } from 'react-i18next'; import { RefreshCcw, Wrench, Ban, AlertCircle, Loader2, Link, } from 'lucide-react'; import { MCPSessionStatus } from '@/app/infra/entities/api'; export default function MCPCardComponent({ cardVO, onCardClick, onRefresh, }: { cardVO: MCPCardVO; onCardClick: () => void; onRefresh: () => void; }) { const { t } = useTranslation(); const [enabled, setEnabled] = useState(cardVO.enable); const [switchEnable, setSwitchEnable] = useState(true); const [testing, setTesting] = useState(false); const [toolsCount, setToolsCount] = useState(cardVO.tools); const [status, setStatus] = useState(cardVO.status); useEffect(() => { setStatus(cardVO.status); setToolsCount(cardVO.tools); setEnabled(cardVO.enable); }, [cardVO.status, cardVO.tools, cardVO.enable]); function handleEnable(checked: boolean) { setSwitchEnable(false); httpClient .toggleMCPServer(cardVO.name, checked) .then(() => { setEnabled(checked); toast.success(t('mcp.saveSuccess')); onRefresh(); setSwitchEnable(true); }) .catch((err) => { toast.error(t('mcp.modifyFailed') + err.msg); setSwitchEnable(true); }); } function handleTest(e: React.MouseEvent) { e.stopPropagation(); setTesting(true); httpClient .testMCPServer(cardVO.name, {}) .then((resp) => { const taskId = resp.task_id; const interval = setInterval(() => { httpClient.getAsyncTask(taskId).then((taskResp) => { if (taskResp.runtime.done) { clearInterval(interval); setTesting(false); if (taskResp.runtime.exception) { toast.error( t('mcp.refreshFailed') + taskResp.runtime.exception, ); } else { toast.success(t('mcp.refreshSuccess')); } // Refresh to get updated runtime_info onRefresh(); } }); }, 1000); }) .catch((err) => { toast.error(t('mcp.refreshFailed') + err.msg); setTesting(false); }); } return (