import { useEffect, useState, useRef } from 'react'; import MCPCardComponent from '@/app/home/plugins/mcp-server/mcp-card/MCPCardComponent'; import { MCPCardVO } from '@/app/home/plugins/mcp-server/MCPCardVO'; import { useTranslation } from 'react-i18next'; import { MCPSessionStatus } from '@/app/infra/entities/api'; import { Hexagon } from 'lucide-react'; import { httpClient } from '@/app/infra/http/HttpClient'; export default function MCPComponent({ onEditServer, }: { askInstallServer?: (githubURL: string) => void; onEditServer?: (serverName: string) => void; }) { const { t } = useTranslation(); const [installedServers, setInstalledServers] = useState([]); const [loading, setLoading] = useState(false); const pollingIntervalRef = useRef(null); useEffect(() => { fetchInstalledServers(); return () => { // Cleanup: clear polling interval when component unmounts if (pollingIntervalRef.current) { clearInterval(pollingIntervalRef.current); } }; }, []); // Check if any enabled server is connecting and start/stop polling accordingly useEffect(() => { const hasConnecting = installedServers.some( (server) => server.enable && server.status === MCPSessionStatus.CONNECTING, ); if (hasConnecting && !pollingIntervalRef.current) { // Start polling every 3 seconds pollingIntervalRef.current = setInterval(() => { fetchInstalledServers(); }, 3000); } else if (!hasConnecting && pollingIntervalRef.current) { // Stop polling when no enabled server is connecting clearInterval(pollingIntervalRef.current); pollingIntervalRef.current = null; } return () => { if (pollingIntervalRef.current) { clearInterval(pollingIntervalRef.current); pollingIntervalRef.current = null; } }; }, [installedServers]); function fetchInstalledServers() { setLoading(true); httpClient .getMCPServers() .then((resp) => { const servers = resp.servers.map((server) => new MCPCardVO(server)); setInstalledServers(servers); setLoading(false); }) .catch((error) => { console.error('Failed to fetch MCP servers:', error); setLoading(false); }); } return (
{/* Server list */}
{loading ? (
{t('mcp.loading')}
) : installedServers.length === 0 ? (
{t('mcp.noServerInstalled')}
) : (
{installedServers.map((server, index) => (
{ if (onEditServer) { onEditServer(server.name); } }} onRefresh={fetchInstalledServers} />
))}
)}
); }