"use client"; import { motion, AnimatePresence } from "framer-motion"; import { useState, useEffect } from "react"; import { Globe, Brain, Database, Package, Loader2, ChevronDown } from "lucide-react"; import type { Node } from "@xyflow/react"; import { toast } from "sonner"; import { useQuery } from "convex/react"; import { api } from "@/convex/_generated/api"; import { useUser } from "@clerk/nextjs"; interface MCPPanelProps { node: Node | null; onClose: () => void; onUpdate: (nodeId: string, data: any) => void; mode?: 'configure' | 'add-to-agent'; onAddToAgent?: (mcpConfig: any) => void; onOpenSettings?: () => void; } export default function MCPPanel({ node, onClose, onUpdate, mode = 'configure', onAddToAgent, onOpenSettings }: MCPPanelProps) { const { user } = useUser(); const nodeData = node?.data as any; // Fetch enabled MCP servers from central registry const mcpServers = useQuery(api.mcpServers.getEnabledMCPs, user?.id ? { userId: user.id } : "skip" ); // Store only the selected server ID const [selectedServerId, setSelectedServerId] = useState(() => { return nodeData?.mcpServerId || null; }); const [showDetails, setShowDetails] = useState(false); const selectedServer = mcpServers?.find(s => s._id === selectedServerId); // Auto-save selected server ID (only in configure mode) useEffect(() => { if (!node || mode === 'add-to-agent') return; const timeoutId = setTimeout(() => { try { onUpdate(node.id, { mcpServerId: selectedServerId, }); } catch (error) { console.error('Error saving MCP server selection:', error); toast.error('Failed to save MCP server selection', { description: error instanceof Error ? error.message : 'Unable to save changes', }); } }, 500); return () => clearTimeout(timeoutId); }, [selectedServerId, node, onUpdate, mode]); const getCategoryIcon = (category: string) => { switch (category) { case 'web': return ; case 'ai': return ; case 'data': return ; default: return ; } }; return ( {(node || mode === 'add-to-agent') && ( {/* Header */}

{mode === 'add-to-agent' ? 'Add MCP to Agent' : 'Tools Node'}

Select tools from an MCP server to invoke them in your agent

{/* Configuration */}
{/* Server Selector */}
{!mcpServers || mcpServers.length === 0 ? (

No servers available in your MCP registry

) : (
{selectedServer && (
{/* Server Info Card */}
{getCategoryIcon(selectedServer.category)}

{selectedServer.name}

{/* {selectedServer.name === 'Firecrawl' && selectedServer.isOfficial && ( API Key Required )} */}
{selectedServer.description && (

{selectedServer.description}

)} {/* {selectedServer.name === 'Firecrawl' && selectedServer.isOfficial && ( Get API key here → )} */}
Category: {selectedServer.category} {selectedServer.connectionStatus === 'connected' && ( Connected )}
{/* Show/Hide Tools Button */} {selectedServer.tools && selectedServer.tools.length > 0 && (
{/* Tools List */} {showDetails && (
{selectedServer.tools.map((tool: string) => (
{tool}
))}
)}
)}
)}
)}
{/* Add New Server Link */}

Need to add a new MCP server?

)}
); }