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