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