| 1 | import { ConfigYaml, parseConfigYaml } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowPathIcon, |
| 4 | ChevronDownIcon, |
| 5 | CircleStackIcon, |
| 6 | CommandLineIcon, |
| 7 | GlobeAltIcon, |
| 8 | PlusIcon, |
| 9 | UserCircleIcon, |
| 10 | WrenchScrewdriverIcon, |
| 11 | } from "@heroicons/react/24/outline"; |
| 12 | import { MCPServerStatus } from "core"; |
| 13 | import { useContext, useMemo, useState } from "react"; |
| 14 | import Alert from "../../../components/gui/Alert"; |
| 15 | import { ToolTip } from "../../../components/gui/Tooltip"; |
| 16 | import EditBlockButton from "../../../components/mainInput/Lump/EditBlockButton"; |
| 17 | import { Button, Card, EmptyState } from "../../../components/ui"; |
| 18 | import { useAuth } from "../../../context/Auth"; |
| 19 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 20 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 21 | import { updateConfig } from "../../../redux/slices/configSlice"; |
| 22 | import { selectCurrentOrg } from "../../../redux/slices/profilesSlice"; |
| 23 | import { ConfigHeader } from "../components/ConfigHeader"; |
| 24 | import { ToolPolicies } from "../components/ToolPolicies"; |
| 25 | |
| 26 | interface MCPServerStatusProps { |
| 27 | server: MCPServerStatus; |
| 28 | serverFromYaml?: NonNullable<ConfigYaml["mcpServers"]>[number]; |
| 29 | } |
| 30 | |
| 31 | function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { |
| 32 | const [expandedSections, setExpandedSections] = useState<{ |
| 33 | [key: string]: boolean; |
| 34 | }>({}); |
| 35 | const ideMessenger = useContext(IdeMessengerContext); |
| 36 | const config = useAppSelector((store) => store.config.config); |
| 37 | const dispatch = useAppDispatch(); |
| 38 | |
| 39 | const updateMCPServerStatus = (status: MCPServerStatus["status"]) => { |
| 40 | // optimistic config update |
| 41 | dispatch( |
| 42 | updateConfig({ |
| 43 | ...config, |
| 44 | mcpServerStatuses: config.mcpServerStatuses.map((s) => |
| 45 | s.id === server.id |
| 46 | ? { |
| 47 | ...s, |
| 48 | status, |
| 49 | } |
| 50 | : s, |
| 51 | ), |
| 52 | }), |
| 53 | ); |
| 54 | }; |
| 55 | |
| 56 | const onAuthenticate = async () => { |
| 57 | updateMCPServerStatus("authenticating"); |
| 58 | await ideMessenger.request("mcp/startAuthentication", server); |
| 59 | }; |
| 60 | |
| 61 | const onRemoveAuth = async () => { |
| 62 | updateMCPServerStatus("authenticating"); |
| 63 | await ideMessenger.request("mcp/removeAuthentication", server); |
| 64 | }; |
| 65 | |
| 66 | const onRefresh = async () => { |
| 67 | updateMCPServerStatus("connecting"); |
| 68 | ideMessenger.post("mcp/reloadServer", { |
| 69 | id: server.id, |
| 70 | }); |
| 71 | }; |
| 72 | |
| 73 | const toggleSection = (section: string) => { |
| 74 | setExpandedSections((prev) => ({ |
| 75 | ...prev, |
| 76 | [section]: !prev[section], |
| 77 | })); |
| 78 | }; |
| 79 | |
| 80 | const ResourceRow = ({ |
| 81 | title, |
| 82 | items, |
| 83 | icon, |
| 84 | sectionKey, |
| 85 | }: { |
| 86 | title: string; |
| 87 | items: any[]; |
| 88 | icon: React.ReactNode; |
| 89 | sectionKey: string; |
| 90 | }) => { |
| 91 | const isExpanded = expandedSections[sectionKey]; |
| 92 | const hasItems = items.length > 0; |
| 93 | |
| 94 | return ( |
| 95 | <div> |
| 96 | <div |
| 97 | className="-mx-2 flex cursor-pointer items-center justify-between rounded px-2 py-2 hover:bg-gray-50 hover:bg-opacity-5" |
| 98 | onClick={() => toggleSection(sectionKey)} |
| 99 | > |
| 100 | <div className="flex items-center gap-3"> |
| 101 | <ChevronDownIcon |
| 102 | className={`text-description h-3 w-3 transition-transform ${isExpanded ? "rotate-180" : ""}`} |
| 103 | /> |
| 104 | <div className="flex items-center gap-2"> |
| 105 | {icon} |
| 106 | <span className="text-sm">{title}</span> |
| 107 | <div className="flex h-5 w-5 items-center justify-center rounded-full bg-gray-600 text-xs font-medium text-white"> |
| 108 | {items.length} |
| 109 | </div> |
| 110 | </div> |
| 111 | </div> |
| 112 | </div> |
| 113 | |
| 114 | {isExpanded && ( |
| 115 | <div className="mb-3 ml-6 mt-2"> |
| 116 | {hasItems ? ( |
| 117 | <div className="space-y-1"> |
| 118 | {items.map((item, idx) => ( |
| 119 | <div |
| 120 | key={idx} |
| 121 | className="text-description rounded bg-gray-50 bg-opacity-5 px-2 py-1 text-xs" |
| 122 | > |
| 123 | <code>{item.name}</code> |
| 124 | {item.description && ( |
| 125 | <div className="mt-1 text-xs text-gray-500"> |
| 126 | {item.description} |
| 127 | </div> |
| 128 | )} |
| 129 | </div> |
| 130 | ))} |
| 131 | </div> |
| 132 | ) : ( |
| 133 | <div className="text-xs italic text-gray-500"> |
| 134 | No {title.toLowerCase()} available |
| 135 | </div> |
| 136 | )} |
| 137 | </div> |
| 138 | )} |
| 139 | </div> |
| 140 | ); |
| 141 | }; |
| 142 | |
| 143 | return ( |
| 144 | <div> |
| 145 | <div className="flex items-center justify-between py-1"> |
| 146 | <div className="flex items-center gap-3"> |
| 147 | <div className="flex-1"> |
| 148 | <div className="flex items-center gap-3"> |
| 149 | <h3 className="my-0 text-sm font-medium">{server.name}</h3> |
| 150 | <div |
| 151 | className={`h-2 w-2 flex-shrink-0 rounded-full ${ |
| 152 | server.status === "connected" |
| 153 | ? "bg-success" |
| 154 | : server.status === "connecting" |
| 155 | ? "bg-warning" |
| 156 | : server.status === "not-connected" |
| 157 | ? "bg-description-muted" |
| 158 | : "bg-error" |
| 159 | }`} |
| 160 | /> |
| 161 | </div> |
| 162 | </div> |
| 163 | </div> |
| 164 | |
| 165 | <div className="-mr-2.5 flex items-center gap-1"> |
| 166 | {server.isProtectedResource && ( |
| 167 | <ToolTip |
| 168 | content={ |
| 169 | server.status === "error" |
| 170 | ? "Authenticate" |
| 171 | : server.status === "authenticating" |
| 172 | ? "Authenticating..." |
| 173 | : "Remove authentication" |
| 174 | } |
| 175 | > |
| 176 | <Button |
| 177 | onClick={ |
| 178 | server.status === "error" |
| 179 | ? onAuthenticate |
| 180 | : server.status === "authenticating" |
| 181 | ? undefined |
| 182 | : onRemoveAuth |
| 183 | } |
| 184 | variant="ghost" |
| 185 | size="sm" |
| 186 | className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0" |
| 187 | disabled={server.status === "authenticating"} |
| 188 | > |
| 189 | {server.status === "authenticating" ? ( |
| 190 | <GlobeAltIcon className="animate-spin-slow h-4 w-4 flex-shrink-0" /> |
| 191 | ) : ( |
| 192 | <UserCircleIcon className="h-4 w-4 flex-shrink-0" /> |
| 193 | )} |
| 194 | </Button> |
| 195 | </ToolTip> |
| 196 | )} |
| 197 | |
| 198 | <ToolTip content="Edit server configuration"> |
| 199 | <div> |
| 200 | <EditBlockButton |
| 201 | blockType={"mcpServers"} |
| 202 | block={serverFromYaml} |
| 203 | sourceFile={server.sourceFile} |
| 204 | /> |
| 205 | </div> |
| 206 | </ToolTip> |
| 207 | |
| 208 | <ToolTip content="Refresh server"> |
| 209 | <Button |
| 210 | onClick={onRefresh} |
| 211 | variant="ghost" |
| 212 | size="sm" |
| 213 | className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0" |
| 214 | > |
| 215 | <ArrowPathIcon className="h-4 w-4 flex-shrink-0" /> |
| 216 | </Button> |
| 217 | </ToolTip> |
| 218 | </div> |
| 219 | </div> |
| 220 | |
| 221 | {/* Individual resource rows */} |
| 222 | <div className="mt-1"> |
| 223 | <ResourceRow |
| 224 | title="Tools" |
| 225 | items={server.tools} |
| 226 | icon={ |
| 227 | <WrenchScrewdriverIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 228 | } |
| 229 | sectionKey={`${server.id}-tools`} |
| 230 | /> |
| 231 | |
| 232 | <ResourceRow |
| 233 | title="Prompts" |
| 234 | items={server.prompts} |
| 235 | icon={ |
| 236 | <CommandLineIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 237 | } |
| 238 | sectionKey={`${server.id}-prompts`} |
| 239 | /> |
| 240 | |
| 241 | <ResourceRow |
| 242 | title="Resources" |
| 243 | items={[...server.resources, ...server.resourceTemplates]} |
| 244 | icon={ |
| 245 | <CircleStackIcon className="text-description h-4 w-4 flex-shrink-0" /> |
| 246 | } |
| 247 | sectionKey={`${server.id}-resources`} |
| 248 | /> |
| 249 | </div> |
| 250 | |
| 251 | {/* Error display below expandable section */} |
| 252 | {server.errors && server.errors.length > 0 && ( |
| 253 | <div className="mt-3 space-y-2"> |
| 254 | {server.errors.map((error, errorIndex) => ( |
| 255 | <Alert |
| 256 | key={errorIndex} |
| 257 | type="error" |
| 258 | size="sm" |
| 259 | className="cursor-pointer transition-all hover:underline" |
| 260 | onClick={() => |
| 261 | void ideMessenger.ide.showVirtualFile(server.name, error) |
| 262 | } |
| 263 | > |
| 264 | <span className="text-xs"> |
| 265 | {error.length > 150 ? error.substring(0, 150) + "..." : error} |
| 266 | </span> |
| 267 | </Alert> |
| 268 | ))} |
| 269 | </div> |
| 270 | )} |
| 271 | </div> |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | function McpSubsection() { |
| 276 | const currentOrg = useAppSelector(selectCurrentOrg); |
| 277 | const servers = useAppSelector( |
| 278 | (store) => store.config.config.mcpServerStatuses, |
| 279 | ); |
| 280 | const { selectedProfile } = useAuth(); |
| 281 | const ideMessenger = useContext(IdeMessengerContext); |
| 282 | const disableMcp = currentOrg?.policy?.allowMcpServers === false; |
| 283 | const isLocal = selectedProfile?.profileType === "local"; |
| 284 | |
| 285 | const mergedBlocks = useMemo(() => { |
| 286 | const parsed = selectedProfile?.rawYaml |
| 287 | ? parseConfigYaml(selectedProfile?.rawYaml ?? "") |
| 288 | : undefined; |
| 289 | |
| 290 | // Create a map of YAML servers keyed by name for stable matching |
| 291 | const yamlServersByName = new Map( |
| 292 | parsed?.mcpServers |
| 293 | ?.filter( |
| 294 | (server): server is NonNullable<typeof server> & { name: string } => |
| 295 | server != null && "name" in server, |
| 296 | ) |
| 297 | .map((server) => [server.name, server]) ?? [], |
| 298 | ); |
| 299 | |
| 300 | return (servers ?? []).map((doc) => ({ |
| 301 | block: doc, |
| 302 | blockFromYaml: yamlServersByName.get(doc.name), |
| 303 | })); |
| 304 | }, [servers, selectedProfile]); |
| 305 | |
| 306 | const handleAddMcpServer = () => { |
| 307 | if (isLocal) { |
| 308 | void ideMessenger.request("config/addLocalWorkspaceBlock", { |
| 309 | blockType: "mcpServers", |
| 310 | }); |
| 311 | } else { |
| 312 | void ideMessenger.request("controlPlane/openUrl", { |
| 313 | path: "?type=mcpServers", |
| 314 | orgSlug: undefined, |
| 315 | }); |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | return ( |
| 320 | <div> |
| 321 | <ConfigHeader |
| 322 | title="MCP Servers" |
| 323 | variant="sm" |
| 324 | onAddClick={!disableMcp ? handleAddMcpServer : undefined} |
| 325 | addButtonTooltip="Add MCP server" |
| 326 | /> |
| 327 | {disableMcp ? ( |
| 328 | <Card> |
| 329 | <EmptyState message="MCP servers are disabled in your organization" /> |
| 330 | </Card> |
| 331 | ) : mergedBlocks.length > 0 ? ( |
| 332 | mergedBlocks.map(({ block, blockFromYaml }, index) => ( |
| 333 | <div key={block.id}> |
| 334 | <Card> |
| 335 | <MCPServerPreview server={block} serverFromYaml={blockFromYaml} /> |
| 336 | </Card> |
| 337 | {index < mergedBlocks.length - 1 && <div className="mb-4" />} |
| 338 | </div> |
| 339 | )) |
| 340 | ) : ( |
| 341 | <Card> |
| 342 | <EmptyState message="No MCP servers configured. Click the + button to add your first server." /> |
| 343 | </Card> |
| 344 | )} |
| 345 | </div> |
| 346 | ); |
| 347 | } |
| 348 | |
| 349 | function ToolPoliciesSubsection() { |
| 350 | return ( |
| 351 | <div> |
| 352 | <ConfigHeader title="Tool Policies" variant="sm" /> |
| 353 | <ToolPolicies /> |
| 354 | </div> |
| 355 | ); |
| 356 | } |
| 357 | |
| 358 | export function ToolsSection() { |
| 359 | const availableTools = useAppSelector((state) => state.config.config.tools); |
| 360 | |
| 361 | const hasTools = availableTools && availableTools.length > 0; |
| 362 | |
| 363 | return ( |
| 364 | <> |
| 365 | <ConfigHeader title="Tools" /> |
| 366 | <div className="space-y-6"> |
| 367 | <McpSubsection /> |
| 368 | {hasTools && <ToolPoliciesSubsection />} |
| 369 | </div> |
| 370 | </> |
| 371 | ); |
| 372 | } |