import { useState } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Dialog, DialogTrigger } from '@/components/ui/dialog' import { Plus, Loader2, RefreshCw } from 'lucide-react' import { DeleteDialog } from '@/components/ui/delete-dialog' import { AddMcpServerDialog } from './AddMcpServerDialog' import { McpServerCard } from './McpServerCard' import { useMcpServers } from '@/hooks/useMcpServers' import { useMutation, useQueryClient } from '@tanstack/react-query' interface McpServerConfig { type: 'local' | 'remote' enabled?: boolean command?: string[] url?: string environment?: Record timeout?: number } interface McpManagerProps { config: { name: string content: Record } | null onUpdate: (content: Record) => Promise onConfigUpdate?: (configName: string, content: Record) => Promise } export function McpManager({ config, onUpdate, onConfigUpdate }: McpManagerProps) { const [isAddDialogOpen, setIsAddDialogOpen] = useState(false) const [deleteConfirmServer, setDeleteConfirmServer] = useState<{ id: string; name: string } | null>(null) const [togglingServerId, setTogglingServerId] = useState(null) const queryClient = useQueryClient() const { status: mcpStatus, isLoading: isLoadingStatus, refetch: refetchStatus, connect, disconnect, authenticate, isToggling } = useMcpServers() const deleteServerMutation = useMutation({ mutationFn: async (serverId: string) => { if (!config) return const currentMcp = (config.content?.mcp as Record) || {} const { [serverId]: _, ...rest } = currentMcp void _ const updatedConfig = { ...config.content, mcp: rest, } await onUpdate(updatedConfig) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['opencode-config'] }) queryClient.invalidateQueries({ queryKey: ['mcp-status'] }) setDeleteConfirmServer(null) }, }) const mcpServers = config?.content?.mcp as Record || {} const isAnyOperationPending = deleteServerMutation.isPending || togglingServerId !== null || isToggling const handleToggleServer = async (serverId: string) => { const currentStatus = mcpStatus?.[serverId] if (!currentStatus) return setTogglingServerId(serverId) try { if (currentStatus.status === 'connected') { await disconnect(serverId) } else if (currentStatus.status === 'disabled') { await connect(serverId) } else if (currentStatus.status === 'needs_auth') { await authenticate(serverId) } else if (currentStatus.status === 'failed') { await connect(serverId) } } finally { setTogglingServerId(null) refetchStatus() } } const handleDeleteServer = () => { if (deleteConfirmServer) { deleteServerMutation.mutate(deleteConfirmServer.id) } } const getErrorMessage = (serverId: string): string | null => { const status = mcpStatus?.[serverId] if (!status) return null if (status.status === 'failed') return status.error if (status.status === 'needs_client_registration') return status.error return null } if (!config) { return (

Select a configuration to manage MCP servers.

) } return (
{isAnyOperationPending && (
{togglingServerId ? 'Updating MCP server...' : 'Processing...'} Please wait while we update your configuration
)}

MCP Servers

Manage Model Context Protocol servers for {config.name}

{Object.keys(mcpServers).length === 0 ? (

No MCP servers configured. Add your first server to get started.

) : (
{Object.entries(mcpServers).map(([serverId, serverConfig]) => { const status = mcpStatus?.[serverId] const isConnected = status?.status === 'connected' const errorMessage = getErrorMessage(serverId) return ( setDeleteConfirmServer({ id, name })} /> ) })}
)} setDeleteConfirmServer(null)} onConfirm={handleDeleteServer} onCancel={() => setDeleteConfirmServer(null)} title="Delete MCP Server" description="This will remove the MCP server configuration. This action cannot be undone." itemName={deleteConfirmServer?.name} isDeleting={deleteServerMutation.isPending} />
) }