Skip to content

πŸ™Œ OpenHands: AI-Driven Development

1.6 KB
Raw Blame
import { useMutation, useQueryClient } from "@tanstack/react-query";
import SettingsService from "#/api/settings-service/settings-service.api";
import { MCPServerConfig } from "#/types/mcp-server";
import { SETTINGS_QUERY_KEYS } from "#/hooks/query/query-keys";
import {
  mcpServerRemoved,
  updateEveryAgentProfile,
} from "#/hooks/mutation/sync-agent-profiles";
import { clearMcpServerHealth } from "#/api/mcp-health/mcp-health-store";
import { getMcpServerHealthKey } from "#/utils/mcp-server-health-key";

/**
 * Delete an installed MCP server.
 *
 * The UI server id is the canonical settings map key, so deletion does not
 * read, match, or reconstruct any other catalog entries.
 */
// @spec MCP-001 β€” Sparse mutations preserve sibling servers
// @spec MCP-003 β€” Settings map keys are stable MCP identities
export function useDeleteMcpServer() {
  const queryClient = useQueryClient();

  return useMutation({
    // Every caller reports a failure itself; skip the global error toast.
    meta: { disableToast: true },
    mutationFn: async (target: MCPServerConfig): Promise<void> => {
      await SettingsService.deleteMcpServer(target.id);
      await updateEveryAgentProfile(queryClient, mcpServerRemoved(target.id));
    },
    onSuccess: (_data, target) => {
      // Drop the deleted server's health entry so a later server that
      // happens to produce the same key starts "unchecked" instead of
      // inheriting this one's verdict.
      clearMcpServerHealth(getMcpServerHealthKey(target));
      queryClient.invalidateQueries({
        queryKey: SETTINGS_QUERY_KEYS.personal(),
      });
    },
  });
}