feat: better mcp connection status (#7830)

* add tooltips for showing mcp connection status

* add type

* explicit message to clarify no mcps are available in chat mode

* ability to disconnect mcp server

* move buttons inside a kebab menu

* use a listbox instead

* remove server and mcp suffixes
change reconnect to reload

* add anchor
This commit is contained in:
Aditya Mitra 2025-09-24 21:31:14 +05:30 • committed by GitHub
parent e84f20ec20
commit 0d3b7d60a3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 180 additions and 43 deletions

View file

@ -85,6 +85,7 @@ class MCPConnection {
}
async disconnect() {
this.status = "not-connected";
this.abortController.abort();
await this.client.close();
await this.transport.close();

View file

@ -13,6 +13,7 @@ export class MCPManagerSingleton {
public onConnectionsRefreshed?: () => void;
public connections: Map<string, MCPConnection> = 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();

View file

@ -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;

View file

@ -148,6 +148,8 @@ export type ToCoreFromIdeOrWebviewProtocol = {
},
void,
];
"mcp/disconnectServer": [{ id: string }, void];
"mcp/getDisconnectedServers": [undefined, string[]];
"mcp/getPrompt": [
{
serverName: string;

View file

@ -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",

View file

@ -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 "";
}

View file

@ -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<ConfigYaml["mcpServers"]>[number];
}
const ServerStatusTooltip: Record<MCPConnectionStatus, string> = {
connected: "Active",
connecting: "Connecting",
"not-connected": "Inactive",
authenticating: "Authenticating",
error: "Error",
};
const ServerStatusColor: Record<MCPConnectionStatus, string> = {
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) {
<div className="flex-1">
<div className="flex items-center gap-3">
<h3 className="my-0 text-sm font-medium">{server.name}</h3>
<div
className={`h-2 w-2 flex-shrink-0 rounded-full ${
server.status === "connected"
? "bg-success"
: server.status === "connecting"
? "bg-warning"
: server.status === "not-connected"
? "bg-description-muted"
: "bg-error"
}`}
/>
<ToolTip content={ServerStatusTooltip[server.status] ?? "Error"}>
<div
className={`h-2 w-2 flex-shrink-0 rounded-full ${ServerStatusColor[server.status] ?? "bg-error"}`}
/>
</ToolTip>
</div>
</div>
</div>
<div className="-mr-2.5 flex items-center gap-1">
{server.isProtectedResource && (
{server.isProtectedResource && server.status !== "connected" && (
<ToolTip
content={
server.status === "error"
@ -182,7 +237,7 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) {
}
variant="ghost"
size="sm"
className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0"
className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0 pt-0.5"
disabled={server.status === "authenticating"}
>
{server.status === "authenticating" ? (
@ -193,27 +248,59 @@ function MCPServerPreview({ server, serverFromYaml }: MCPServerStatusProps) {
</Button>
</ToolTip>
)}
<Listbox>
<ListboxButton>
<EllipsisVerticalIcon className="h-4 w-4 flex-shrink-0" />
</ListboxButton>
<ListboxOptions className="min-w-fit" anchor="bottom end">
{server.isProtectedResource && server.status === "connected" && (
<ListboxOption
value="remove auth"
onClick={onRemoveAuth}
className="justify-start gap-x-1.5"
>
<UserCircleIcon className="h-4 w-4 flex-shrink-0" /> Logout
</ListboxOption>
)}
<ToolTip content="Edit server configuration">
<div>
<EditBlockButton
blockType={"mcpServers"}
block={serverFromYaml}
sourceFile={server.sourceFile}
/>
</div>
</ToolTip>
<ListboxOption
value="edit mcp"
className="justify-start gap-x-1.5"
>
<EditBlockButton
blockType={"mcpServers"}
block={serverFromYaml}
sourceFile={server.sourceFile}
className="h-3.5 w-3.5 text-inherit"
/>
Edit
</ListboxOption>
<ToolTip content="Refresh server">
<Button
onClick={onRefresh}
variant="ghost"
size="sm"
className="text-description-muted hover:enabled:text-foreground my-0 h-6 w-6 p-0"
>
<ArrowPathIcon className="h-4 w-4 flex-shrink-0" />
</Button>
</ToolTip>
{!disconnectedMCPServers.includes(server.name) && (
<ListboxOption
value="disconnect"
onClick={onDisconnect}
className="justify-start gap-x-1.5"
>
<StopCircleIcon className="h-4 w-4 flex-shrink-0" />{" "}
Disconnect
</ListboxOption>
)}
<ListboxOption
value="reconnect"
onClick={onRefresh}
className="justify-start gap-x-1.5"
>
{disconnectedMCPServers.includes(server.name) ? (
<PlayCircleIcon className="h-4 w-4 flex-shrink-0" />
) : (
<ArrowPathIcon className="h-4 w-4 flex-shrink-0" />
)}
Reload
</ListboxOption>
</ListboxOptions>
</Listbox>
</div>
</div>
@ -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() {
<Card>
<EmptyState message="MCP servers are disabled in your organization" />
</Card>
) : mode === "chat" ? (
<Alert type="info" size="sm">
<span className="text-2xs italic">
All MCPs are disabled in Chat, switch to Plan or Agent mode to use
MCPs
</span>
</Alert>
) : mergedBlocks.length > 0 ? (
mergedBlocks.map(({ block, blockFromYaml }, index) => (
<div key={block.id}>