| 1 | import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowPathIcon, |
| 4 | ChevronDownIcon, |
| 5 | CircleStackIcon, |
| 6 | CommandLineIcon, |
| 7 | EllipsisVerticalIcon, |
| 8 | GlobeAltIcon, |
| 9 | PlayCircleIcon, |
| 10 | StopCircleIcon, |
| 11 | UserCircleIcon, |
| 12 | WrenchScrewdriverIcon, |
| 13 | } from "@heroicons/react/24/outline"; |
| 14 | import { MCPConnectionStatus, MCPServerStatus } from "core"; |
| 15 | import { useContext, useEffect, useMemo, useState } from "react"; |
| 16 | import Alert from "../../../components/gui/Alert"; |
| 17 | import { ToolTip } from "../../../components/gui/Tooltip"; |
| 18 | import EditBlockButton from "../../../components/mainInput/Lump/EditBlockButton"; |
| 19 | import { |
| 20 | Button, |
| 21 | Card, |
| 22 | EmptyState, |
| 23 | Listbox, |
| 24 | ListboxButton, |
| 25 | ListboxOption, |
| 26 | ListboxOptions, |
| 27 | } from "../../../components/ui"; |
| 28 | import { useAuth } from "../../../context/Auth"; |
| 29 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 30 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 31 | import { updateConfig } from "../../../redux/slices/configSlice"; |
| 32 | import { selectCurrentOrg } from "../../../redux/slices/profilesSlice"; |
| 33 | import { ConfigHeader } from "../components/ConfigHeader"; |
| 34 | import { ToolPolicies } from "../components/ToolPolicies"; |
| 35 | |
| 36 | interface MCPServerStatusProps { |
| 37 | server: MCPServerStatus; |
| 38 | serverFromYaml?: NonNullable<ConfigYaml["mcpServers"]>[number]; |
| 39 | } |
| 40 | |
| 41 | const ServerStatusTooltip: Record<MCPConnectionStatus, string> = { |
| 42 | connected: "Active", |
| 43 | connecting: "Connecting", |
| 44 | "not-connected": "Inactive", |
| 45 | authenticating: "Authenticating", |
| 46 | error: "Error", |
| 47 | }; |
| 48 | |
| 49 | const ServerStatusColor: Record<MCPConnectionStatus, string> = { |
| 50 | connected: "bg-success", |
| 51 | connecting: "bg-warning", |
| 52 | "not-connected": "bg-description-muted", |
| 53 | authenticating: "bg-warning", |
| 54 | error: "bg-error", |
| 55 | }; |
| 56 | |
| 57 | function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { |
| 58 | const [expandedSections, setExpandedSections] = useState<{ |
| 59 | [key: string]: boolean; |
| 60 | }>({}); |
| 61 | const [disconnectedMCPServers, setDisconnectedMCPServers] = useState< |
| 62 | string[] |
| 63 | >([]); |
| 64 | const ideMessenger = useContext(IdeMessengerContext); |
| 65 | const config = useAppSelector((store) => store.config.config); |
| 66 | const dispatch = useAppDispatch(); |
| 67 | const updateMCPServerStatus = (status: MCPServerStatus["status"]) => { |
| 68 | // optimistic config update |
| 69 | dispatch( |
| 70 | updateConfig({ |
| 71 | ...config, |
| 72 | mcpServerStatuses: config.mcpServerStatuses.map((s) => |
| 73 | s.id === server.id |
| 74 | ? { |
| 75 | ...s, |
| 76 | status, |
| 77 | } |
| 78 | : s, |
| 79 | ), |
| 80 | }), |
| 81 | ); |
| 82 | }; |
| 83 | |
| 84 | const onAuthenticate = async () => { |
| 85 | if ("url" in server) { |
| 86 | updateMCPServerStatus("authenticating"); |
| 87 | await ideMessenger.request("mcp/startAuthentication", { |
| 88 | serverId: server.id, |
| 89 | serverUrl: server.url, |
| 90 | }); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | const onRemoveAuth = async () => { |
| 95 | if ("url" in server) { |
| 96 | updateMCPServerStatus("authenticating"); |
| 97 | await ideMessenger.request("mcp/removeAuthentication", { |
| 98 | serverId: server.id, |
| 99 | serverUrl: server.url, |
| 100 | }); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | const onRefresh = async () => { |
| 105 | updateMCPServerStatus("connecting"); |
| 106 | await ideMessenger.request("mcp/reloadServer", { |
| 107 | id: server.id, |
| 108 | }); |
| 109 | await fetchDisconectedMCPServers(); |
| 110 | }; |
| 111 | |
| 112 | const onDisconnect = async () => { |
| 113 | updateMCPServerStatus("not-connected"); |
| 114 | setDisconnectedMCPServers((prev) => [...prev, server.id]); |
| 115 | dispatch( |
| 116 | updateConfig({ |
| 117 | ...config, |
| 118 | tools: config.tools.filter((tool) => tool.group !== server.id), |
| 119 | }), |
| 120 | ); |
| 121 | await ideMessenger.request("mcp/disconnectServer", { |
| 122 | id: server.id, |
| 123 | }); |
| 124 | await fetchDisconectedMCPServers(); |
| 125 | }; |
| 126 | |
| 127 | const fetchDisconectedMCPServers = async () => { |
| 128 | const disconnectedServersData = await ideMessenger.request( |
| 129 | "mcp/getDisconnectedServers", |
| 130 | undefined, |
| 131 | ); |
| 132 | if (disconnectedServersData.status === "success") { |
| 133 | setDisconnectedMCPServers(disconnectedServersData.content); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | const toggleSection = (section: string) => { |
| 138 | setExpandedSections((prev) => ({ |
| 139 | ...prev, |
| 140 | [section]: !prev[section], |
| 141 | })); |
| 142 | }; |
| 143 | |
| 144 | useEffect(() => void fetchDisconectedMCPServers(), []); |
| 145 | |
| 146 | const ResourceRow = ({ |
| 147 | title, |
| 148 | items, |
| 149 | icon, |
| 150 | sectionKey, |
| 151 | }: { |
| 152 | title: string; |
| 153 | items: |
| 154 | | MCPServerStatus["tools"] |
| 155 | | MCPServerStatus["prompts"] |
| 156 | | MCPServerStatus["resources"] |
| 157 | | MCPServerStatus["resourceTemplates"]; |
| 158 | icon: React.ReactNode; |
| 159 | sectionKey: string; |
| 160 | }) => { |
| 161 | const isExpanded = expandedSections[sectionKey]; |
| 162 | const hasItems = items.length > 0; |
| 163 | |
| 164 | return ( |
| 165 | <div> |
| 166 | <div |
| 167 | className="-mx-2 flex cursor-pointer items-center justify-between rounded px-2 py-2 hover:bg-gray-50 hover:bg-opacity-5" |
| 168 | onClick={() => toggleSection(sectionKey)} |
| 169 | > |
| 170 | <div className="flex items-center gap-3"> |
| 171 | <ChevronDownIcon |
| 172 | className={`text-description h-3 w-3 transition-transform ${isExpanded ? "rotate-180" : ""}`} |
| 173 | /> |
| 174 | <div className="flex items-center gap-2"> |
| 175 | {icon} |
| 176 | <span className="text-sm">{title}</span> |
| 177 | <div className="flex h-5 w-5 items-center justify-center rounded-full bg-gray-600 text-xs font-medium text-white"> |
| 178 | {items.length} |
| 179 | </div> |
| 180 | </div> |
| 181 | </div> |
| 182 | </div> |
| 183 | |
| 184 | {isExpanded && ( |
| 185 | <div className="mb-3 ml-6 mt-2"> |
| 186 | {hasItems ? ( |
| 187 | <div className="space-y-1"> |
| 188 | {items.map((item, idx) => ( |
| 189 | <div |
| 190 | key={idx} |
| 191 | className="text-description rounded bg-gray-50 bg-opacity-5 px-2 py-1 text-xs" |
| 192 | > |
| 193 | <code>{item.name}</code> |
| 194 | {item.description && ( |
| 195 | <div className="mt-1 text-xs text-gray-500"> |
| 196 | {item.description} |
| 197 | </div> |
| 198 | )} |
| 199 | </div> |
| 200 | ))} |
| 201 | </div> |
| 202 | ) : ( |
| 203 | <div className="text-xs italic text-gray-500"> |
| 204 | No {title.toLowerCase()} available |
| 205 | </div> |
| 206 | )} |
| 207 | </div> |
| 208 | )} |
| 209 | </div> |
| 210 | ); |
| 211 | }; |
| 212 | |
| 213 | return ( |
| 214 | <div> |
| 215 | <div className="flex items-center justify-between py-1"> |
| 216 | <div className="flex items-center gap-3"> |
| 217 | <div className="flex-1"> |
| 218 | <div className="flex items-center gap-3"> |
| 219 | <h3 className="my-0 text-sm font-medium">{server.name}</h3> |
| 220 | <ToolTip content={ServerStatusTooltip[server.status] ?? "Error"}> |
| 221 | <div |
| 222 | className={`h-2 w-2 flex-shrink-0 rounded-full ${ServerStatusColor[server.status] ?? "bg-error"}`} |
| 223 | /> |
| 224 | </ToolTip> |
| 225 | </div> |
| 226 | </div> |
| 227 | </div> |
| 228 | |
| 229 | <div className="-mr-2.5 flex items-center gap-1"> |
| 230 | {server.isProtectedResource && |
| 231 | "url" in server && |
| 232 | server.status !== "connected" && ( |
| 233 | <ToolTip |
| 234 | content={ |
| 235 | server.status === "error" |
| 236 | ? "Authenticate" |
| 237 | : server.status === "authenticating" |
| 238 | ? "Authenticating..." |
| 239 | : "Remove authentication" |
| 240 | } |
| 241 | > |
| 242 | <Button |
| 243 | onClick={ |
| 244 | server.status === "error" |
| 245 | ? onAuthenticate |
| 246 | : server.status === "authenticating" |
| 247 | ? undefined |
| 248 | : onRemoveAuth |
| 249 | } |
| 250 | variant="ghost" |
| 251 | size="sm" |
| 252 | className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0 pt-0.5" |
| 253 | disabled={server.status === "authenticating"} |
| 254 | > |
| 255 | {server.status === "authenticating" ? ( |
| 256 | <GlobeAltIcon className="animate-spin-slow h-4 w-4 flex-shrink-0" /> |
| 257 | ) : ( |
| 258 | <UserCircleIcon className="h-4 w-4 flex-shrink-0" /> |
| 259 | )} |
| 260 | </Button> |
| 261 | </ToolTip> |
| 262 | )} |
| 263 | <Listbox> |
| 264 | <ListboxButton> |
| 265 | <EllipsisVerticalIcon className="h-4 w-4 flex-shrink-0" /> |
| 266 | </ListboxButton> |
| 267 | <ListboxOptions className="min-w-fit" anchor="bottom end"> |
| 268 | {server.isProtectedResource && server.status === "connected" && ( |
| 269 | <ListboxOption |
| 270 | value="remove auth" |
| 271 | onClick={onRemoveAuth} |
| 272 | className="justify-start gap-x-1.5" |
| 273 | > |
| 274 | <UserCircleIcon className="h-4 w-4 flex-shrink-0" /> Logout |
| 275 | </ListboxOption> |
| 276 | )} |
| 277 | |
| 278 | <ListboxOption |
| 279 | value="edit mcp" |
| 280 | className="justify-start gap-x-1.5" |
| 281 | > |
| 282 | <EditBlockButton |
| 283 | blockType={"mcpServers"} |
| 284 | block={serverFromYaml} |
| 285 | sourceFile={server.sourceFile} |
| 286 | className="h-3.5 w-3.5 text-inherit" |
| 287 | /> |
| 288 | Edit |
| 289 | </ListboxOption> |
| 290 | |
| 291 | {!disconnectedMCPServers.includes(server.name) && ( |
| 292 | <ListboxOption |
| 293 | value="disconnect" |
| 294 | onClick={onDisconnect} |
| 295 | className="justify-start gap-x-1.5" |
| 296 | > |
| 297 | <StopCircleIcon className="h-4 w-4 flex-shrink-0" />{" "} |
| 298 | Disconnect |
| 299 | </ListboxOption> |
| 300 | )} |
| 301 | |
| 302 | <ListboxOption |
| 303 | value="reconnect" |
| 304 | onClick={onRefresh} |
| 305 | className="justify-start gap-x-1.5" |
| 306 | > |
| 307 | {disconnectedMCPServers.includes(server.name) ? ( |
| 308 | <PlayCircleIcon className="h-4 w-4 flex-shrink-0" /> |
| 309 | ) : ( |
| 310 | <ArrowPathIcon className="h-4 w-4 flex-shrink-0" /> |
| 311 | )} |
| 312 | Reload |
| 313 | </ListboxOption> |
| 314 | </ListboxOptions> |
| 315 | </Listbox> |
| 316 | </div> |
| 317 | </div> |
| 318 | |
| 319 | {/* Individual resource rows */} |
| 320 | <div className="mt-1"> |
| 321 | <ResourceRow |
| 322 | title="Tools" |
| 323 | items={server.tools} |
| 324 | icon={ |
| 325 | <WrenchScrewdriverIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 326 | } |
| 327 | sectionKey={`${server.id}-tools`} |
| 328 | /> |
| 329 | |
| 330 | <ResourceRow |
| 331 | title="Prompts" |
| 332 | items={server.prompts} |
| 333 | icon={ |
| 334 | <CommandLineIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 335 | } |
| 336 | sectionKey={`${server.id}-prompts`} |
| 337 | /> |
| 338 | |
| 339 | <ResourceRow |
| 340 | title="Resources" |
| 341 | items={[...server.resources, ...server.resourceTemplates]} |
| 342 | icon={ |
| 343 | <CircleStackIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 344 | } |
| 345 | sectionKey={`${server.id}-resources`} |
| 346 | /> |
| 347 | </div> |
| 348 | |
| 349 | {/* Error display below expandable section */} |
| 350 | {server.errors && server.errors.length > 0 && ( |
| 351 | <div className="mt-3 space-y-2"> |
| 352 | {server.errors.map((error, errorIndex) => ( |
| 353 | <Alert |
| 354 | key={errorIndex} |
| 355 | type="error" |
| 356 | size="sm" |
| 357 | className="cursor-pointer transition-all hover:underline" |
| 358 | onClick={() => |
| 359 | void ideMessenger.ide.showVirtualFile(server.name, error) |
| 360 | } |
| 361 | > |
| 362 | <span className="text-xs"> |
| 363 | {error.length > 150 ? error.substring(0, 150) + "..." : error} |
| 364 | </span> |
| 365 | </Alert> |
| 366 | ))} |
| 367 | </div> |
| 368 | )} |
| 369 | |
| 370 | {server.infos && server.infos.length > 0 && ( |
| 371 | <div className="mt-3 space-y-2"> |
| 372 | {server.infos.map((info, infoIndex) => ( |
| 373 | <Alert |
| 374 | key={infoIndex} |
| 375 | type="info" |
| 376 | size="sm" |
| 377 | className="transition-all" |
| 378 | onClick={() => |
| 379 | void ideMessenger.ide.showVirtualFile(server.name, info) |
| 380 | } |
| 381 | > |
| 382 | <span |
| 383 | className="text-xs" |
| 384 | dangerouslySetInnerHTML={{ __html: info }} |
| 385 | /> |
| 386 | </Alert> |
| 387 | ))} |
| 388 | </div> |
| 389 | )} |
| 390 | </div> |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | function McpSubsection() { |
| 395 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 396 | const mode = useAppSelector((store) => store.session.mode); |
| 397 | const servers = useAppSelector( |
| 398 | (store) => store.config.config.mcpServerStatuses, |
| 399 | ); |
| 400 | const { selectedProfile } = useAuth(); |
| 401 | const ideMessenger = useContext(IdeMessengerContext); |
| 402 | const disableMcp = currentOrg?.policy?.allowMcpServers === false; |
| 403 | const isLocal = selectedProfile?.profileType === "local"; |
| 404 | |
| 405 | const mergedBlocks = useMemo(() => { |
| 406 | const parsed = selectedProfile?.rawYaml |
| 407 | ? parseConfigYaml(selectedProfile?.rawYaml ?? "") |
| 408 | : undefined; |
| 409 | |
| 410 | // Create a map of YAML servers keyed by name for stable matching |
| 411 | const yamlServersByName = new Map( |
| 412 | parsed?.mcpServers |
| 413 | ?.filter( |
| 414 | (server): server is NonNullable<typeof server> & { name: string } => |
| 415 | server != null && "name" in server, |
| 416 | ) |
| 417 | .map((server) => [server.name, server]) ?? [], |
| 418 | ); |
| 419 | |
| 420 | return (servers ?? []).map((doc: MCPServerStatus) => ({ |
| 421 | block: doc, |
| 422 | blockFromYaml: yamlServersByName.get(doc.name), |
| 423 | })); |
| 424 | }, [servers, selectedProfile]); |
| 425 | |
| 426 | const handleAddMcpServer = () => { |
| 427 | if (isLocal) { |
| 428 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 429 | blockType: "mcpServers", |
| 430 | }); |
| 431 | } else { |
| 432 | void ideMessenger.request("controlPlane/openUrl", { |
| 433 | path: "?type=mcpServers", |
| 434 | orgSlug: undefined, |
| 435 | }); |
| 436 | } |
| 437 | }; |
| 438 | |
| 439 | return ( |
| 440 | <div> |
| 441 | <ConfigHeader |
| 442 | title="MCP Servers" |
| 443 | variant="sm" |
| 444 | onAddClick={!disableMcp ? handleAddMcpServer : undefined} |
| 445 | addButtonTooltip="Add MCP server" |
| 446 | /> |
| 447 | {disableMcp ? ( |
| 448 | <Card> |
| 449 | <EmptyState message="MCP servers are disabled in your organization" /> |
| 450 | </Card> |
| 451 | ) : mode === "chat" ? ( |
| 452 | <Alert type="info" size="sm"> |
| 453 | <span className="text-2xs italic"> |
| 454 | All MCPs are disabled in Chat, switch to Plan or Agent mode to use |
| 455 | MCPs |
| 456 | </span> |
| 457 | </Alert> |
| 458 | ) : mergedBlocks.length > 0 ? ( |
| 459 | mergedBlocks.map(({ block, blockFromYaml }, index) => ( |
| 460 | <div key={block.id}> |
| 461 | <Card> |
| 462 | <MCPServerPreview server={block} serverFromYaml={blockFromYaml} /> |
| 463 | </Card> |
| 464 | {index < mergedBlocks.length - 1 && <div className="mb-4" />} |
| 465 | </div> |
| 466 | )) |
| 467 | ) : ( |
| 468 | <Card> |
| 469 | <EmptyState message="No MCP servers configured. Click the + button to add your first server." /> |
| 470 | </Card> |
| 471 | )} |
| 472 | </div> |
| 473 | ); |
| 474 | } |
| 475 | |
| 476 | function ToolPoliciesSubsection() { |
| 477 | return ( |
| 478 | <div> |
| 479 | <ConfigHeader title="Tool Policies" variant="sm" /> |
| 480 | <ToolPolicies /> |
| 481 | </div> |
| 482 | ); |
| 483 | } |
| 484 | |
| 485 | export function ToolsSection() { |
| 486 | const availableTools = useAppSelector((state) => state.config.config.tools); |
| 487 | |
| 488 | const hasTools = availableTools && availableTools.length > 0; |
| 489 | |
| 490 | return ( |
| 491 | <> |
| 492 | <ConfigHeader title="Tools" /> |
| 493 | <div className="space-y-6"> |
| 494 | <McpSubsection /> |
| 495 | {hasTools && <ToolPoliciesSubsection />} |
| 496 | </div> |
| 497 | </> |
| 498 | ); |
| 499 | } |