import { Badge } from '@/components/ui/badge'
import { Switch } from '@/components/ui/switch'
import { Button } from '@/components/ui/button'
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
import { DropdownMenuSeparator } from '@/components/ui/dropdown-menu'
import { XCircle, AlertCircle, Key, MoreVertical, Shield, Trash2, RefreshCw } from 'lucide-react'
import type { McpStatus, McpServerConfig } from '@/api/mcp'
interface McpServerCardProps {
serverId: string
serverConfig: McpServerConfig
status?: McpStatus
isConnected: boolean
errorMessage: string | null
isAnyOperationPending: boolean
togglingServerId: string | null
isRemovingAuth: boolean
onToggleServer: (serverId: string) => void
onAuthenticate?: (serverId: string) => void
onRemoveAuth?: (serverId: string) => void
onDeleteServer: (serverId: string, serverName: string) => void
}
function getStatusBadge(status: McpStatus) {
switch (status.status) {
case 'connected':
return Connected
case 'disabled':
return Disabled
case 'failed':
return (
Failed
)
case 'needs_auth':
return (
Auth Required
)
case 'needs_client_registration':
return (
Registration Required
)
default:
return Unknown
}
}
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: ${command}`
} else if (serverConfig.type === 'remote' && serverConfig.url) {
return `Remote server: ${serverConfig.url}`
}
return 'MCP server'
}
export function McpServerCard({
serverId,
serverConfig,
status,
isConnected,
errorMessage,
isAnyOperationPending,
togglingServerId,
isRemovingAuth,
onToggleServer,
onAuthenticate,
onRemoveAuth,
onDeleteServer
}: McpServerCardProps) {
const needsAuth = status?.status === 'needs_auth'
const isRemote = serverConfig.type === 'remote'
const hasOAuthConfig = isRemote && !!serverConfig.oauth
const hasOAuthError = status?.status === 'failed' && isRemote && /oauth|auth.*state/i.test(status.error)
const isOAuthServer = hasOAuthConfig || hasOAuthError || (needsAuth && isRemote)
const connectedWithOAuth = isOAuthServer && isConnected
const showAuthButton = needsAuth || (isOAuthServer && status?.status === 'failed')
const displayName = getServerDisplayName(serverId)
return (
{displayName}
{connectedWithOAuth && (
)}
{status ? getStatusBadge(status) : (
Loading...
)}
{getServerDescription(serverConfig)}
{errorMessage && (
{errorMessage}
)}
{showAuthButton && onAuthenticate ? (
) : (
onToggleServer(serverId)}
disabled={isAnyOperationPending || togglingServerId === serverId}
/>
)}
{showAuthButton && onAuthenticate && (
setTimeout(() => onAuthenticate(serverId), 0)}>
Authenticate
)}
{connectedWithOAuth && onAuthenticate && (
setTimeout(() => onAuthenticate(serverId), 0)}>
Re-authenticate
)}
{connectedWithOAuth && onRemoveAuth && (
setTimeout(() => onRemoveAuth(serverId), 0)}
disabled={isRemovingAuth}
>
{isRemovingAuth ? 'Removing...' : 'Remove Auth'}
)}
{(showAuthButton || connectedWithOAuth) && }
{
setTimeout(() => onDeleteServer(serverId, displayName), 0)
}}
className="text-red-600"
>
Delete Server
)
}