| 1 | import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowPathIcon, |
| 4 | CircleStackIcon, |
| 5 | CommandLineIcon, |
| 6 | InformationCircleIcon, |
| 7 | WrenchScrewdriverIcon, |
| 8 | } from "@heroicons/react/24/outline"; |
| 9 | import { MCPServerStatus } from "core"; |
| 10 | import { useContext, useMemo } from "react"; |
| 11 | import { useAuth } from "../../../../../context/Auth"; |
| 12 | import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; |
| 13 | import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; |
| 14 | import { updateConfig } from "../../../../../redux/slices/configSlice"; |
| 15 | import { fontSize } from "../../../../../util"; |
| 16 | import { ToolTip } from "../../../../gui/Tooltip"; |
| 17 | import EditBlockButton from "../../EditBlockButton"; |
| 18 | import { ExploreBlocksButton } from "../ExploreBlocksButton"; |
| 19 | |
| 20 | interface MCPServerStatusProps { |
| 21 | server: MCPServerStatus; |
| 22 | serverFromYaml?: NonNullable<ConfigYaml["mcpServers"]>[number]; |
| 23 | } |
| 24 | function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { |
| 25 | const ideMessenger = useContext(IdeMessengerContext); |
| 26 | const config = useAppSelector((store) => store.config.config); |
| 27 | const dispatch = useAppDispatch(); |
| 28 | const toolsTooltipId = `${server.id}-tools`; |
| 29 | const promptsTooltipId = `${server.id}-prompts`; |
| 30 | const resourcesTooltipId = `${server.id}-resources`; |
| 31 | const errorsTooltipId = `${server.id}-errors`; |
| 32 | |
| 33 | async function onRefresh() { |
| 34 | // optimistic config update |
| 35 | dispatch( |
| 36 | updateConfig({ |
| 37 | ...config, |
| 38 | mcpServerStatuses: config.mcpServerStatuses.map((s) => |
| 39 | s.id === server.id |
| 40 | ? { |
| 41 | ...s, |
| 42 | status: "connecting", |
| 43 | } |
| 44 | : s, |
| 45 | ), |
| 46 | }), |
| 47 | ); |
| 48 | ideMessenger.post("mcp/reloadServer", { |
| 49 | id: server.id, |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | return ( |
| 54 | <div |
| 55 | style={{ |
| 56 | fontSize: fontSize(-2), |
| 57 | }} |
| 58 | className="flex flex-row items-center justify-between gap-3" |
| 59 | > |
| 60 | <div className="flex flex-row items-center gap-3"> |
| 61 | {/* Name and Status */} |
| 62 | <span className="m-0 font-semibold">{server.name}</span> |
| 63 | |
| 64 | {/* Error indicator if any */} |
| 65 | {server.errors.length ? ( |
| 66 | <> |
| 67 | <InformationCircleIcon |
| 68 | className={`h-3 w-3 ${server.status === "error" ? "text-red-500" : "text-yellow-500"}`} |
| 69 | data-tooltip-id={errorsTooltipId} |
| 70 | /> |
| 71 | <ToolTip id={errorsTooltipId} className="flex flex-col gap-0.5"> |
| 72 | {server.errors.map((error, idx) => ( |
| 73 | <code key={idx}>{error}</code> |
| 74 | ))} |
| 75 | </ToolTip> |
| 76 | </> |
| 77 | ) : null} |
| 78 | |
| 79 | {/* Tools, Prompts, Resources with counts */} |
| 80 | <div className="flex flex-row items-center gap-3"> |
| 81 | <div |
| 82 | className="flex cursor-zoom-in items-center gap-1 hover:opacity-80" |
| 83 | data-tooltip-id={toolsTooltipId} |
| 84 | > |
| 85 | <WrenchScrewdriverIcon className="h-3 w-3" /> |
| 86 | <span className="text-xs">{server.tools.length}</span> |
| 87 | <ToolTip id={toolsTooltipId} className="flex flex-col gap-0.5"> |
| 88 | {server.tools.map((tool, idx) => ( |
| 89 | <code key={idx}>{tool.name}</code> |
| 90 | ))} |
| 91 | {server.tools.length === 0 && ( |
| 92 | <span className="text-lightgray">No tools</span> |
| 93 | )} |
| 94 | </ToolTip> |
| 95 | </div> |
| 96 | <div |
| 97 | className="flex cursor-zoom-in items-center gap-1 hover:opacity-80" |
| 98 | data-tooltip-id={promptsTooltipId} |
| 99 | > |
| 100 | <CommandLineIcon className="h-3 w-3" /> |
| 101 | <span className="text-xs">{server.prompts.length}</span> |
| 102 | <ToolTip id={promptsTooltipId} className="flex flex-col gap-0.5"> |
| 103 | {server.prompts.map((prompt, idx) => ( |
| 104 | <code key={idx}>{prompt.name}</code> |
| 105 | ))} |
| 106 | {server.prompts.length === 0 && ( |
| 107 | <span className="text-lightgray">No prompts</span> |
| 108 | )} |
| 109 | </ToolTip> |
| 110 | </div> |
| 111 | <div |
| 112 | className="flex cursor-zoom-in items-center gap-1 hover:opacity-80" |
| 113 | data-tooltip-id={resourcesTooltipId} |
| 114 | > |
| 115 | <CircleStackIcon className="h-3 w-3" /> |
| 116 | <span className="text-xs"> |
| 117 | {server.resources.length + server.resourceTemplates.length} |
| 118 | </span> |
| 119 | <ToolTip id={resourcesTooltipId} className="flex flex-col gap-0.5"> |
| 120 | {[...server.resources, ...server.resourceTemplates].map( |
| 121 | (resource, idx) => ( |
| 122 | <code key={idx}>{resource.name}</code> |
| 123 | ), |
| 124 | )} |
| 125 | {server.resources.length === 0 && ( |
| 126 | <span className="text-lightgray">No resources</span> |
| 127 | )} |
| 128 | </ToolTip> |
| 129 | </div> |
| 130 | </div> |
| 131 | </div> |
| 132 | |
| 133 | {/* Refresh button */} |
| 134 | <div className="flex items-center gap-2"> |
| 135 | <EditBlockButton |
| 136 | blockType={"mcpServers"} |
| 137 | block={serverFromYaml} |
| 138 | sourceFile={server.sourceFile} |
| 139 | /> |
| 140 | <div |
| 141 | className="text-lightgray flex cursor-pointer items-center hover:opacity-80" |
| 142 | onClick={onRefresh} |
| 143 | > |
| 144 | <ArrowPathIcon className="h-3 w-3" /> |
| 145 | </div> |
| 146 | |
| 147 | <div |
| 148 | className="hidden h-2 w-2 rounded-full sm:flex" |
| 149 | style={{ |
| 150 | backgroundColor: |
| 151 | server.status === "connected" |
| 152 | ? "#22c55e" // green-500 |
| 153 | : server.status === "connecting" |
| 154 | ? "#eab308" // yellow-500 |
| 155 | : server.status === "not-connected" |
| 156 | ? "#78716c" // stone-500 |
| 157 | : "#ef4444", // red-500 for error |
| 158 | }} |
| 159 | /> |
| 160 | </div> |
| 161 | </div> |
| 162 | ); |
| 163 | } |
| 164 | |
| 165 | function MCPSection() { |
| 166 | const servers = useAppSelector( |
| 167 | (store) => store.config.config.mcpServerStatuses, |
| 168 | ); |
| 169 | const { selectedProfile } = useAuth(); |
| 170 | |
| 171 | const mergedBlocks = useMemo(() => { |
| 172 | const parsed = selectedProfile?.rawYaml |
| 173 | ? parseConfigYaml(selectedProfile?.rawYaml ?? "") |
| 174 | : undefined; |
| 175 | return (servers ?? []).map((doc, index) => ({ |
| 176 | block: doc, |
| 177 | blockFromYaml: parsed?.mcpServers?.[index], |
| 178 | })); |
| 179 | }, [servers, selectedProfile]); |
| 180 | |
| 181 | return ( |
| 182 | <div className="flex flex-col gap-1"> |
| 183 | <div className="flex max-h-[170px] flex-col gap-1 overflow-y-auto overflow-x-hidden pr-2"> |
| 184 | {mergedBlocks.map(({ block, blockFromYaml }, idx) => ( |
| 185 | <MCPServerPreview |
| 186 | key={idx} |
| 187 | server={block} |
| 188 | serverFromYaml={blockFromYaml} |
| 189 | /> |
| 190 | ))} |
| 191 | </div> |
| 192 | <ExploreBlocksButton blockType="mcpServers" /> |
| 193 | </div> |
| 194 | ); |
| 195 | } |
| 196 | |
| 197 | export default MCPSection; |