|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | + |
| 3 | +import { Loader2, PlugIcon, RefreshCwIcon } from "lucide-react"; |
| 4 | +import { API } from "@/core/network/api"; |
| 5 | +import { cn } from "@/utils/cn"; |
| 6 | +import { Button } from "../ui/button"; |
| 7 | +import { Popover, PopoverContent, PopoverTrigger } from "../ui/popover"; |
| 8 | +import { Tooltip } from "../ui/tooltip"; |
| 9 | +import { toast } from "../ui/use-toast"; |
| 10 | +import { useMCPStatus } from "./hooks"; |
| 11 | + |
| 12 | +/** |
| 13 | + * MCP Status indicator component |
| 14 | + * Shows a small icon with status color and a popover with detailed information |
| 15 | + */ |
| 16 | +export const MCPStatusIndicator: React.FC = () => { |
| 17 | + const { data: status, refetch, isFetching } = useMCPStatus(); |
| 18 | + |
| 19 | + const handleRefresh = async () => { |
| 20 | + try { |
| 21 | + await API.post<object, { success: boolean }>("/ai/mcp/refresh", {}); |
| 22 | + toast({ |
| 23 | + title: "MCP refreshed", |
| 24 | + description: "MCP server configuration has been refreshed", |
| 25 | + }); |
| 26 | + refetch(); |
| 27 | + } catch (error) { |
| 28 | + toast({ |
| 29 | + title: "Refresh failed", |
| 30 | + description: |
| 31 | + error instanceof Error ? error.message : "Failed to refresh MCP", |
| 32 | + variant: "danger", |
| 33 | + }); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + const servers = status?.servers || {}; |
| 38 | + const hasServers = Object.keys(servers).length > 0; |
| 39 | + |
| 40 | + return ( |
| 41 | + <Popover> |
| 42 | + <Tooltip content="MCP Status"> |
| 43 | + <PopoverTrigger asChild={true}> |
| 44 | + <Button variant="text" size="icon"> |
| 45 | + <PlugIcon |
| 46 | + className={cn( |
| 47 | + "h-4 w-4", |
| 48 | + status?.status === "ok" && "text-green-500", |
| 49 | + status?.status === "partial" && "text-yellow-500", |
| 50 | + status?.status === "error" && hasServers && "text-red-500", |
| 51 | + )} |
| 52 | + /> |
| 53 | + </Button> |
| 54 | + </PopoverTrigger> |
| 55 | + </Tooltip> |
| 56 | + <PopoverContent className="w-[320px]" align="start" side="right"> |
| 57 | + <div className="space-y-3"> |
| 58 | + <div className="flex items-center justify-between"> |
| 59 | + <h4 className="font-medium text-sm">MCP Server Status</h4> |
| 60 | + <Button |
| 61 | + variant="ghost" |
| 62 | + size="xs" |
| 63 | + onClick={handleRefresh} |
| 64 | + disabled={isFetching} |
| 65 | + > |
| 66 | + {isFetching ? ( |
| 67 | + <Loader2 className="h-3 w-3 animate-spin" /> |
| 68 | + ) : ( |
| 69 | + <RefreshCwIcon className="h-3 w-3" /> |
| 70 | + )} |
| 71 | + </Button> |
| 72 | + </div> |
| 73 | + {status && ( |
| 74 | + <div className="text-xs space-y-2"> |
| 75 | + {hasServers && ( |
| 76 | + <div className="flex justify-between items-center"> |
| 77 | + <span className="text-muted-foreground">Overall:</span> |
| 78 | + <McpStatusText status={status.status} /> |
| 79 | + </div> |
| 80 | + )} |
| 81 | + {status.error && ( |
| 82 | + <div className="text-xs text-red-500 bg-red-50 dark:bg-red-950/20 p-2 rounded"> |
| 83 | + {status.error} |
| 84 | + </div> |
| 85 | + )} |
| 86 | + {hasServers && ( |
| 87 | + <div className="space-y-1"> |
| 88 | + <div className="text-muted-foreground font-medium"> |
| 89 | + Servers: |
| 90 | + </div> |
| 91 | + {Object.entries(servers).map(([name, serverStatus]) => ( |
| 92 | + <div |
| 93 | + key={name} |
| 94 | + className="flex justify-between items-center pl-2" |
| 95 | + > |
| 96 | + <span className="text-muted-foreground truncate max-w-[180px]"> |
| 97 | + {name} |
| 98 | + </span> |
| 99 | + <McpStatusText status={serverStatus} /> |
| 100 | + </div> |
| 101 | + ))} |
| 102 | + </div> |
| 103 | + )} |
| 104 | + {!hasServers && ( |
| 105 | + <div className="text-muted-foreground text-center py-2"> |
| 106 | + No MCP servers configured. <br /> Configure under{" "} |
| 107 | + <b>Settings > AI > MCP</b> |
| 108 | + </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + </PopoverContent> |
| 114 | + </Popover> |
| 115 | + ); |
| 116 | +}; |
| 117 | + |
| 118 | +export const McpStatusText: React.FC<{ |
| 119 | + status: |
| 120 | + | "ok" |
| 121 | + | "partial" |
| 122 | + | "error" |
| 123 | + | "failed" |
| 124 | + | "disconnected" |
| 125 | + | "pending" |
| 126 | + | "connected"; |
| 127 | +}> = ({ status }) => { |
| 128 | + return ( |
| 129 | + <span |
| 130 | + className={cn( |
| 131 | + "text-xs font-medium", |
| 132 | + status === "ok" && "text-green-500", |
| 133 | + status === "partial" && "text-yellow-500", |
| 134 | + status === "error" && "text-red-500", |
| 135 | + status === "failed" && "text-red-500", |
| 136 | + status === "disconnected" && "text-gray-500", |
| 137 | + status === "pending" && "text-yellow-500", |
| 138 | + status === "connected" && "text-green-500", |
| 139 | + )} |
| 140 | + > |
| 141 | + {status} |
| 142 | + </span> |
| 143 | + ); |
| 144 | +}; |
0 commit comments