| 1 | import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowPathIcon, |
| 4 | CircleStackIcon, |
| 5 | CommandLineIcon, |
| 6 | GlobeAltIcon, |
| 7 | InformationCircleIcon, |
| 8 | ShieldCheckIcon, |
| 9 | ShieldExclamationIcon, |
| 10 | WrenchScrewdriverIcon, |
| 11 | } from "@heroicons/react/24/outline"; |
| 12 | import { MCPServerStatus } from "core"; |
| 13 | import { Fragment, useContext, useMemo } from "react"; |
| 14 | import { useAuth } from "../../../../../context/Auth"; |
| 15 | import { IdeMessengerContext } from "../../../../../context/IdeMessenger"; |
| 16 | import { useAppDispatch, useAppSelector } from "../../../../../redux/hooks"; |
| 17 | import { updateConfig } from "../../../../../redux/slices/configSlice"; |
| 18 | import { selectCurrentOrg } from "../../../../../redux/slices/profilesSlice"; |
| 19 | import { fontSize } from "../../../../../util"; |
| 20 | import { ToolTip } from "../../../../gui/Tooltip"; |
| 21 | import { Button } from "../../../../ui"; |
| 22 | import EditBlockButton from "../../EditBlockButton"; |
| 23 | import { ExploreBlocksButton } from "../ExploreBlocksButton"; |
| 24 | |
| 25 | interface MCPServerStatusProps { |
| 26 | server: MCPServerStatus; |
| 27 | serverFromYaml?: NonNullable<ConfigYaml["mcpServers"]>[number]; |
| 28 | } |
| 29 | function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { |
| 30 | const ideMessenger = useContext(IdeMessengerContext); |
| 31 | const config = useAppSelector((store) => store.config.config); |
| 32 | const dispatch = useAppDispatch(); |
| 33 | |
| 34 | const updateMCPServerStatus = (status: MCPServerStatus["status"]) => { |
| 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, |
| 44 | } |
| 45 | : s, |
| 46 | ), |
| 47 | }), |
| 48 | ); |
| 49 | }; |
| 50 | |
| 51 | const onAuthenticate = async () => { |
| 52 | updateMCPServerStatus("authenticating"); |
| 53 | await ideMessenger.request("mcp/startAuthentication", server); |
| 54 | }; |
| 55 | |
| 56 | const onRemoveAuth = async () => { |
| 57 | updateMCPServerStatus("authenticating"); |
| 58 | await ideMessenger.request("mcp/removeAuthentication", server); |
| 59 | }; |
| 60 | |
| 61 | const onRefresh = async () => { |
| 62 | updateMCPServerStatus("connecting"); |
| 63 | ideMessenger.post("mcp/reloadServer", { |
| 64 | id: server.id, |
| 65 | }); |
| 66 | }; |
| 67 | |
| 68 | return ( |
| 69 | <div |
| 70 | style={{ |
| 71 | fontSize: fontSize(-2), |
| 72 | }} |
| 73 | className={`flex flex-row items-center justify-between gap-3 ${server.status === "authenticating" ? "my-0.5" : ""}`} |
| 74 | > |
| 75 | <div className="flex flex-row items-center gap-3"> |
| 76 | {/* Name and Status */} |
| 77 | <span className="m-0 font-semibold">{server.name}</span> |
| 78 | |
| 79 | {/* Error indicator if any */} |
| 80 | {server.errors.length ? ( |
| 81 | <ToolTip |
| 82 | clickable |
| 83 | delayHide={ |
| 84 | server.errors.some((error) => error.length > 150) ? 1500 : 0 |
| 85 | } |
| 86 | className="flex flex-col gap-0.5" |
| 87 | content={server.errors.map((error, idx) => ( |
| 88 | <Fragment key={idx}> |
| 89 | <div> |
| 90 | {error.length > 150 ? error.substring(0, 150) + "..." : error} |
| 91 | </div> |
| 92 | {error.length > 150 && ( |
| 93 | <Button |
| 94 | className="my-0" |
| 95 | size="sm" |
| 96 | variant="ghost" |
| 97 | onClick={() => |
| 98 | void ideMessenger.ide.showVirtualFile(server.name, error) |
| 99 | } |
| 100 | > |
| 101 | View full error |
| 102 | </Button> |
| 103 | )} |
| 104 | </Fragment> |
| 105 | ))} |
| 106 | > |
| 107 | <InformationCircleIcon |
| 108 | className={`h-3 w-3 ${server.status === "error" ? "text-red-500" : "text-yellow-500"}`} |
| 109 | /> |
| 110 | </ToolTip> |
| 111 | ) : null} |
| 112 | |
| 113 | {/* Tools, Prompts, Resources with counts */} |
| 114 | <div className="flex flex-row items-center gap-3"> |
| 115 | <ToolTip |
| 116 | className="flex flex-col gap-0.5" |
| 117 | content={ |
| 118 | <> |
| 119 | {server.tools.map((tool, idx) => ( |
| 120 | <code key={idx}>{tool.name}</code> |
| 121 | ))} |
| 122 | {server.tools.length === 0 && ( |
| 123 | <span className="text-lightgray">No tools</span> |
| 124 | )} |
| 125 | </> |
| 126 | } |
| 127 | > |
| 128 | <div className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"> |
| 129 | <WrenchScrewdriverIcon className="h-3 w-3" /> |
| 130 | <span className="text-xs">{server.tools.length}</span> |
| 131 | </div> |
| 132 | </ToolTip> |
| 133 | |
| 134 | <ToolTip |
| 135 | className="flex flex-col gap-0.5" |
| 136 | content={ |
| 137 | <> |
| 138 | {server.prompts.map((prompt, idx) => ( |
| 139 | <code key={idx}>{prompt.name}</code> |
| 140 | ))} |
| 141 | {server.prompts.length === 0 && ( |
| 142 | <span className="text-lightgray">No prompts</span> |
| 143 | )} |
| 144 | </> |
| 145 | } |
| 146 | > |
| 147 | <div className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"> |
| 148 | <CommandLineIcon className="h-3 w-3" /> |
| 149 | <span className="text-xs">{server.prompts.length}</span> |
| 150 | </div> |
| 151 | </ToolTip> |
| 152 | |
| 153 | <ToolTip |
| 154 | className="flex flex-col gap-0.5" |
| 155 | content={ |
| 156 | <> |
| 157 | {[...server.resources, ...server.resourceTemplates].map( |
| 158 | (resource, idx) => ( |
| 159 | <code key={idx}>{resource.name}</code> |
| 160 | ), |
| 161 | )} |
| 162 | {server.resources.length === 0 && ( |
| 163 | <span className="text-lightgray">No resources</span> |
| 164 | )} |
| 165 | </> |
| 166 | } |
| 167 | > |
| 168 | <div className="flex cursor-zoom-in items-center gap-1 hover:opacity-80"> |
| 169 | <CircleStackIcon className="h-3 w-3" /> |
| 170 | <span className="text-xs"> |
| 171 | {server.resources.length + server.resourceTemplates.length} |
| 172 | </span> |
| 173 | </div> |
| 174 | </ToolTip> |
| 175 | </div> |
| 176 | </div> |
| 177 | |
| 178 | <div className="flex items-center gap-2"> |
| 179 | {server.isProtectedResource && ( |
| 180 | <ToolTip |
| 181 | place="left" |
| 182 | hidden={server.status === "authenticating"} |
| 183 | content={server.status === "error" ? "Authenticate" : "Logout"} |
| 184 | > |
| 185 | <div className="text-lightgray flex cursor-pointer items-center hover:text-white hover:opacity-80"> |
| 186 | {server.status === "error" ? ( |
| 187 | <ShieldExclamationIcon |
| 188 | className="h-3 w-3" |
| 189 | onClick={onAuthenticate} |
| 190 | /> |
| 191 | ) : server.status === "authenticating" ? ( |
| 192 | <GlobeAltIcon className="animate-spin-slow h-3 w-3" /> |
| 193 | ) : ( |
| 194 | <ShieldCheckIcon className="h-3 w-3" onClick={onRemoveAuth} /> |
| 195 | )} |
| 196 | </div> |
| 197 | </ToolTip> |
| 198 | )} |
| 199 | <EditBlockButton |
| 200 | blockType={"mcpServers"} |
| 201 | block={serverFromYaml} |
| 202 | sourceFile={server.sourceFile} |
| 203 | /> |
| 204 | <div |
| 205 | className="text-lightgray flex cursor-pointer items-center hover:opacity-80" |
| 206 | onClick={onRefresh} |
| 207 | > |
| 208 | <ArrowPathIcon className="h-3 w-3" /> |
| 209 | </div> |
| 210 | |
| 211 | <div |
| 212 | className="hidden h-2 w-2 rounded-full sm:flex" |
| 213 | style={{ |
| 214 | backgroundColor: |
| 215 | server.status === "connected" |
| 216 | ? "#22c55e" // green-500 |
| 217 | : server.status === "connecting" |
| 218 | ? "#eab308" // yellow-500 |
| 219 | : server.status === "not-connected" |
| 220 | ? "#78716c" // stone-500 |
| 221 | : "#ef4444", // red-500 for error |
| 222 | }} |
| 223 | /> |
| 224 | </div> |
| 225 | </div> |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | function MCPSection() { |
| 230 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 231 | const servers = useAppSelector( |
| 232 | (store) => store.config.config.mcpServerStatuses, |
| 233 | ); |
| 234 | const { selectedProfile } = useAuth(); |
| 235 | const disableMcp = currentOrg?.policy?.allowMcpServers === false; |
| 236 | |
| 237 | const mergedBlocks = useMemo(() => { |
| 238 | const parsed = selectedProfile?.rawYaml |
| 239 | ? parseConfigYaml(selectedProfile?.rawYaml ?? "") |
| 240 | : undefined; |
| 241 | return (servers ?? []).map((doc, index) => ({ |
| 242 | block: doc, |
| 243 | blockFromYaml: parsed?.mcpServers?.[index], |
| 244 | })); |
| 245 | }, [servers, selectedProfile]); |
| 246 | |
| 247 | if (disableMcp) { |
| 248 | return ( |
| 249 | <div className="flex flex-col items-center justify-center p-2"> |
| 250 | <span className="text-description"> |
| 251 | MCP servers are disabled in your organization |
| 252 | </span> |
| 253 | </div> |
| 254 | ); |
| 255 | } |
| 256 | |
| 257 | return ( |
| 258 | <div className="flex flex-col gap-1"> |
| 259 | <div className="flex max-h-[170px] flex-col gap-1 overflow-y-auto overflow-x-hidden pr-2"> |
| 260 | {mergedBlocks.map(({ block, blockFromYaml }, idx) => ( |
| 261 | <MCPServerPreview |
| 262 | key={idx} |
| 263 | server={block} |
| 264 | serverFromYaml={blockFromYaml} |
| 265 | /> |
| 266 | ))} |
| 267 | </div> |
| 268 | <ExploreBlocksButton blockType="mcpServers" /> |
| 269 | </div> |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | export default MCPSection; |