From 3fd62b77a1372e2dd080be2ecf646f4a5f346288 Mon Sep 17 00:00:00 2001 From: Chris Scott Date: Thu, 25 Dec 2025 10:38:51 -0500 Subject: [PATCH] Add RepoMcpDialog component for MCP server management --- .../src/components/repo/RepoMcpDialog.tsx | 209 ++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 frontend/src/components/repo/RepoMcpDialog.tsx diff --git a/frontend/src/components/repo/RepoMcpDialog.tsx b/frontend/src/components/repo/RepoMcpDialog.tsx new file mode 100644 index 00000000..8283d10b --- /dev/null +++ b/frontend/src/components/repo/RepoMcpDialog.tsx @@ -0,0 +1,209 @@ +import { useState, useEffect, useCallback } from 'react' +import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog' +import { Switch } from '@/components/ui/switch' +import { Badge } from '@/components/ui/badge' +import { Loader2, Plug, XCircle, AlertCircle } from 'lucide-react' +import { mcpApi, type McpStatus } from '@/api/mcp' +import { useMutation } from '@tanstack/react-query' +import { showToast } from '@/lib/toast' + +interface McpServerConfig { + type: 'local' | 'remote' + enabled?: boolean + command?: string[] + url?: string + environment?: Record + timeout?: number +} + +interface RepoMcpDialogProps { + open: boolean + onOpenChange: (open: boolean) => void + config: { + content: Record + } | null + directory: string | undefined +} + +function getServerDisplayName(serverId: string): string { + const name = serverId.replace(/[-_]/g, ' ') + return name.charAt(0).toUpperCase() + name.slice(1) +} + +function getServerDescription(serverConfig: McpServerConfig): string { + if (serverConfig.type === 'local' && serverConfig.command) { + const command = serverConfig.command.join(' ') + if (command.includes('filesystem')) return 'File system access' + if (command.includes('git')) return 'Git repository operations' + if (command.includes('sqlite')) return 'SQLite database access' + if (command.includes('postgres')) return 'PostgreSQL database access' + if (command.includes('brave-search')) return 'Web search via Brave' + if (command.includes('github')) return 'GitHub repository access' + if (command.includes('slack')) return 'Slack integration' + if (command.includes('puppeteer')) return 'Web automation' + if (command.includes('fetch')) return 'HTTP requests' + if (command.includes('memory')) return 'Persistent memory' + return `Local: ${command}` + } else if (serverConfig.type === 'remote' && serverConfig.url) { + return `${serverConfig.url}` + } + return 'MCP server' +} + +function getStatusBadge(status?: McpStatus) { + if (!status) return null + + switch (status.status) { + case 'connected': + return Connected + case 'disabled': + return Disabled + case 'failed': + return ( + + + Failed + + ) + case 'needs_auth': + return ( + + Needs Auth + + ) + default: + return Unknown + } +} + +export function RepoMcpDialog({ open, onOpenChange, config, directory }: RepoMcpDialogProps) { + const [localStatus, setLocalStatus] = useState>({}) + const [isLoadingStatus, setIsLoadingStatus] = useState(false) + + const mcpServers = config?.content?.mcp as Record | undefined || {} + const serverIds = Object.keys(mcpServers) + + const fetchStatus = useCallback(async () => { + if (!directory || serverIds.length === 0) return + + setIsLoadingStatus(true) + try { + const status = await mcpApi.getStatusFor(directory) + setLocalStatus(status) + } catch (error) { + console.error('Failed to fetch MCP status:', error) + } finally { + setIsLoadingStatus(false) + } + }, [directory, serverIds.length]) + + const toggleMutation = useMutation({ + mutationFn: async ({ serverId, enable }: { serverId: string; enable: boolean }) => { + if (!directory) throw new Error('No directory provided') + + const currentStatus = localStatus[serverId] + + if (enable) { + if (currentStatus?.status === 'needs_auth') { + await mcpApi.authenticateDirectory(serverId, directory) + } else { + await mcpApi.connectDirectory(serverId, directory) + } + } else { + await mcpApi.disconnectDirectory(serverId, directory) + } + }, + onSuccess: async () => { + showToast.success('MCP server updated for this location') + await fetchStatus() + }, + onError: (error) => { + showToast.error(error instanceof Error ? error.message : 'Failed to update MCP server') + }, + }) + + useEffect(() => { + if (open && directory) { + fetchStatus() + } + }, [open, directory, serverIds.length, fetchStatus]) + + if (!directory) return null + + return ( + + + +
+ + MCP for This Location +
+

+ Toggle MCP servers for this repository +

+
+ +
+ {serverIds.length === 0 ? ( +
+ +

+ No MCP servers configured globally +

+

+ Add them in Settings first +

+
+ ) : isLoadingStatus ? ( +
+ +
+ ) : ( +
+ {serverIds.map((serverId) => { + const serverConfig = mcpServers[serverId] + const status = localStatus[serverId] + const isConnected = status?.status === 'connected' + const failed = status?.status === 'failed' + + return ( +
+
+
+

+ {getServerDisplayName(serverId)} +

+ {getStatusBadge(status)} +
+

+ {getServerDescription(serverConfig)} +

+ {failed && status.status === 'failed' && ( +
+ + {status.error} +
+ )} +
+ + { + toggleMutation.mutate({ serverId, enable: enabled }) + }} + onClick={(e) => e.stopPropagation()} + /> +
+ ) + })} +
+ )} +
+
+
+ ) +}