diff --git a/core/context/mcp/MCPConnection.ts b/core/context/mcp/MCPConnection.ts index bdd3279a0..a7978b2d2 100644 --- a/core/context/mcp/MCPConnection.ts +++ b/core/context/mcp/MCPConnection.ts @@ -85,6 +85,7 @@ class MCPConnection { } async disconnect() { + this.status = "not-connected"; this.abortController.abort(); await this.client.close(); await this.transport.close(); diff --git a/core/context/mcp/MCPManagerSingleton.ts b/core/context/mcp/MCPManagerSingleton.ts index c9dc41929..9dbfc28b8 100644 --- a/core/context/mcp/MCPManagerSingleton.ts +++ b/core/context/mcp/MCPManagerSingleton.ts @@ -13,6 +13,7 @@ export class MCPManagerSingleton { public onConnectionsRefreshed?: () => void; public connections: Map = new Map(); + private disconnectedServers: string[] = []; private abortController: AbortController = new AbortController(); @@ -25,6 +26,18 @@ export class MCPManagerSingleton { return MCPManagerSingleton.instance; } + addDisconnectedServer(serverId: string) { + this.disconnectedServers.push(serverId); + } + removeDisconnectedServer(serverId: string) { + this.disconnectedServers = this.disconnectedServers.filter( + (server) => server !== serverId, + ); + } + getDisconnectedServers() { + return this.disconnectedServers; + } + createConnection(id: string, options: MCPOptions): MCPConnection { if (this.connections.has(id)) { return this.connections.get(id)!; @@ -163,9 +176,19 @@ export class MCPManagerSingleton { }), (async () => { await Promise.all( - Array.from(this.connections.values()).map(async (connection) => { - await connection.connectClient(force, this.abortController.signal); - }), + Array.from(this.connections.values()) + .filter( + (connection) => + !this.disconnectedServers.some( + (s) => s === connection.options.id, + ), + ) + .map(async (connection) => { + await connection.connectClient( + force, + this.abortController.signal, + ); + }), ); if (this.onConnectionsRefreshed) { this.onConnectionsRefreshed(); diff --git a/core/core.ts b/core/core.ts index 8fd24c15a..6249107bd 100644 --- a/core/core.ts +++ b/core/core.ts @@ -492,6 +492,20 @@ export class Core { on("mcp/reloadServer", async (msg) => { await MCPManagerSingleton.getInstance().refreshConnection(msg.data.id); + MCPManagerSingleton.getInstance().removeDisconnectedServer(msg.data.id); + }); + on("mcp/disconnectServer", async (msg) => { + const mcpConnection = MCPManagerSingleton.getInstance().getConnection( + msg.data.id, + ); + if (!mcpConnection) + throw new Error(`MCP connection with id ${msg.data.id} not found`); + MCPManagerSingleton.getInstance().addDisconnectedServer(msg.data.id); + await mcpConnection.disconnect(); + await this.configHandler.refreshAll("MCP Servers disconnected"); + }); + on("mcp/getDisconnectedServers", async (_msg) => { + return MCPManagerSingleton.getInstance().getDisconnectedServers(); }); on("mcp/getPrompt", async (msg) => { const { serverName, promptName, args } = msg.data; diff --git a/core/protocol/core.ts b/core/protocol/core.ts index 82ef174a7..eb7be1bf6 100644 --- a/core/protocol/core.ts +++ b/core/protocol/core.ts @@ -148,6 +148,8 @@ export type ToCoreFromIdeOrWebviewProtocol = { }, void, ]; + "mcp/disconnectServer": [{ id: string }, void]; + "mcp/getDisconnectedServers": [undefined, string[]]; "mcp/getPrompt": [ { serverName: string; diff --git a/core/protocol/passThrough.ts b/core/protocol/passThrough.ts index 60a971c36..f02727a37 100644 --- a/core/protocol/passThrough.ts +++ b/core/protocol/passThrough.ts @@ -32,6 +32,8 @@ export const WEBVIEW_TO_CORE_PASS_THROUGH: (keyof ToCoreFromWebviewProtocol)[] = "mcp/getPrompt", "mcp/startAuthentication", "mcp/removeAuthentication", + "mcp/disconnectServer", + "mcp/getDisconnectedServers", "context/getContextItems", "context/getSymbolsForFiles", "context/loadSubmenuItems", diff --git a/gui/src/pages/config/hooks/useToolPolicies.ts b/gui/src/pages/config/hooks/useToolPolicies.ts index b33ec798d..6f9421197 100644 --- a/gui/src/pages/config/hooks/useToolPolicies.ts +++ b/gui/src/pages/config/hooks/useToolPolicies.ts @@ -45,7 +45,7 @@ export function useToolPolicies() { case "chat": return "All tools disabled in Chat, switch to Plan or Agent mode to use tools"; case "plan": - return "Read-only/MCP tools available in Plan mode"; + return "Read-only tools available in Plan mode"; default: return ""; } diff --git a/gui/src/pages/config/sections/ToolsSection.tsx b/gui/src/pages/config/sections/ToolsSection.tsx index c94c97a9e..c94a46fe0 100644 --- a/gui/src/pages/config/sections/ToolsSection.tsx +++ b/gui/src/pages/config/sections/ToolsSection.tsx @@ -4,16 +4,27 @@ import { ChevronDownIcon, CircleStackIcon, CommandLineIcon, + EllipsisVerticalIcon, GlobeAltIcon, + PlayCircleIcon, + StopCircleIcon, UserCircleIcon, WrenchScrewdriverIcon, } from "@heroicons/react/24/outline"; -import { MCPServerStatus } from "core"; -import { useContext, useMemo, useState } from "react"; +import { MCPConnectionStatus, MCPServerStatus } from "core"; +import { useContext, useEffect, useMemo, useState } from "react"; import Alert from "../../../components/gui/Alert"; import { ToolTip } from "../../../components/gui/Tooltip"; import EditBlockButton from "../../../components/mainInput/Lump/EditBlockButton"; -import { Button, Card, EmptyState } from "../../../components/ui"; +import { + Button, + Card, + EmptyState, + Listbox, + ListboxButton, + ListboxOption, + ListboxOptions, +} from "../../../components/ui"; import { useAuth } from "../../../context/Auth"; import { IdeMessengerContext } from "../../../context/IdeMessenger"; import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; @@ -27,14 +38,32 @@ interface MCPServerStatusProps { serverFromYaml?: NonNullable[number]; } +const ServerStatusTooltip: Record = { + connected: "Active", + connecting: "Connecting", + "not-connected": "Inactive", + authenticating: "Authenticating", + error: "Error", +}; + +const ServerStatusColor: Record = { + connected: "bg-success", + connecting: "bg-warning", + "not-connected": "bg-description-muted", + authenticating: "bg-warning", + error: "bg-error", +}; + function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { const [expandedSections, setExpandedSections] = useState<{ [key: string]: boolean; }>({}); + const [disconnectedMCPServers, setDisconnectedMCPServers] = useState< + string[] + >([]); const ideMessenger = useContext(IdeMessengerContext); const config = useAppSelector((store) => store.config.config); const dispatch = useAppDispatch(); - const updateMCPServerStatus = (status: MCPServerStatus["status"]) => { // optimistic config update dispatch( @@ -64,9 +93,35 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { const onRefresh = async () => { updateMCPServerStatus("connecting"); - ideMessenger.post("mcp/reloadServer", { + await ideMessenger.request("mcp/reloadServer", { id: server.id, }); + await fetchDisconectedMCPServers(); + }; + + const onDisconnect = async () => { + updateMCPServerStatus("not-connected"); + setDisconnectedMCPServers((prev) => [...prev, server.id]); + dispatch( + updateConfig({ + ...config, + tools: config.tools.filter((tool) => tool.group !== server.id), + }), + ); + await ideMessenger.request("mcp/disconnectServer", { + id: server.id, + }); + await fetchDisconectedMCPServers(); + }; + + const fetchDisconectedMCPServers = async () => { + const disconnectedServersData = await ideMessenger.request( + "mcp/getDisconnectedServers", + undefined, + ); + if (disconnectedServersData.status === "success") { + setDisconnectedMCPServers(disconnectedServersData.content); + } }; const toggleSection = (section: string) => { @@ -76,6 +131,8 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { })); }; + useEffect(() => void fetchDisconectedMCPServers(), []); + const ResourceRow = ({ title, items, @@ -83,7 +140,11 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { sectionKey, }: { title: string; - items: any[]; + items: + | MCPServerStatus["tools"] + | MCPServerStatus["prompts"] + | MCPServerStatus["resources"] + | MCPServerStatus["resourceTemplates"]; icon: React.ReactNode; sectionKey: string; }) => { @@ -146,23 +207,17 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) {

{server.name}

-
+ +
+
- {server.isProtectedResource && ( + {server.isProtectedResource && server.status !== "connected" && ( {server.status === "authenticating" ? ( @@ -193,27 +248,59 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { )} + + + + + + {server.isProtectedResource && server.status === "connected" && ( + + Logout + + )} - -
- -
-
+ + + Edit + - - - + {!disconnectedMCPServers.includes(server.name) && ( + + {" "} + Disconnect + + )} + + + {disconnectedMCPServers.includes(server.name) ? ( + + ) : ( + + )} + Reload + +
+
@@ -294,6 +381,7 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) { function McpSubsection() { const currentOrg = useAppSelector(selectCurrentOrg); + const mode = useAppSelector((store) => store.session.mode); const servers = useAppSelector( (store) => store.config.config.mcpServerStatuses, ); @@ -317,7 +405,7 @@ function McpSubsection() { .map((server) => [server.name, server]) ?? [], ); - return (servers ?? []).map((doc) => ({ + return (servers ?? []).map((doc: MCPServerStatus) => ({ block: doc, blockFromYaml: yamlServersByName.get(doc.name), })); @@ -348,6 +436,13 @@ function McpSubsection() { + ) : mode === "chat" ? ( + + + All MCPs are disabled in Chat, switch to Plan or Agent mode to use + MCPs + + ) : mergedBlocks.length > 0 ? ( mergedBlocks.map(({ block, blockFromYaml }, index) => (