mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(ui): stop toolset editor background fan-out and keep tools on failed refetch
The toolset modal stays mounted when closed, so a finished search kept every server's tool query enabled and refocusing the window refetched all of them once stale. Fetch only while the editor is open, require both the live and debounced search before fanning out, and disable refetch on window focus for these queries. A failed refetch now keeps the tools that already loaded instead of flagging the server unreachable, and tools without a name are dropped. Trim comments that restated the code. (LIT-10012)
This commit is contained in:
parent
7bf210b308
commit
e77cf5b44c
5 changed files with 75 additions and 27 deletions
|
|
@ -1,9 +1,9 @@
|
|||
/* @vitest-environment jsdom */
|
||||
import React from "react";
|
||||
import { renderHook, waitFor } from "@testing-library/react";
|
||||
import { act, renderHook, waitFor } from "@testing-library/react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useMCPServerTools } from "./useMCPServerTools";
|
||||
import { mcpServerToolKeys, useMCPServerTools } from "./useMCPServerTools";
|
||||
import * as networking from "@/components/networking";
|
||||
|
||||
vi.mock("@/components/networking", () => ({
|
||||
|
|
@ -62,8 +62,12 @@ describe("useMCPServerTools", () => {
|
|||
expect(result.current.get("a")).toEqual({ tools: [], status: "loading" });
|
||||
});
|
||||
|
||||
it("normalizes a bare array response and tools without descriptions", async () => {
|
||||
vi.mocked(networking.listMCPTools).mockResolvedValue([{ tool_name: "legacy" }, "plain"]);
|
||||
it("normalizes a bare array response, tools without descriptions, and drops tools without a name", async () => {
|
||||
vi.mocked(networking.listMCPTools).mockResolvedValue([
|
||||
{ tool_name: "legacy" },
|
||||
"plain",
|
||||
{ description: "nameless" },
|
||||
]);
|
||||
|
||||
const { result } = renderHook(() => useMCPServerTools(["a"], new Set(["a"])), { wrapper });
|
||||
|
||||
|
|
@ -74,4 +78,25 @@ describe("useMCPServerTools", () => {
|
|||
]),
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps showing the loaded tools when a later refetch fails", async () => {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
const sharedWrapper = ({ children }: { children: React.ReactNode }) =>
|
||||
React.createElement(QueryClientProvider, { client: queryClient }, children);
|
||||
vi.mocked(networking.listMCPTools)
|
||||
.mockResolvedValueOnce(toolsFor("a"))
|
||||
.mockResolvedValueOnce({ tools: [], error: "http_502", message: "upstream down" });
|
||||
|
||||
const { result } = renderHook(() => useMCPServerTools(["a"], new Set(["a"])), { wrapper: sharedWrapper });
|
||||
await waitFor(() => expect(result.current.get("a")?.status).toBe("success"));
|
||||
|
||||
await act(() => queryClient.refetchQueries({ queryKey: mcpServerToolKeys.detail("a") }));
|
||||
|
||||
await waitFor(() => expect(queryClient.getQueryState(mcpServerToolKeys.detail("a"))?.status).toBe("error"));
|
||||
expect(networking.listMCPTools).toHaveBeenCalledTimes(2);
|
||||
expect(result.current.get("a")).toEqual({
|
||||
tools: [{ name: "a_tool", description: "from a" }],
|
||||
status: "success",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,8 +10,6 @@ export interface MCPServerToolEntry {
|
|||
description: string;
|
||||
}
|
||||
|
||||
// "idle" means the server has not been fetched yet, so callers can tell it apart from
|
||||
// a server that loaded and has no tools.
|
||||
export type MCPServerToolsStatus = "idle" | "loading" | "error" | "success";
|
||||
|
||||
export interface MCPServerToolsState {
|
||||
|
|
@ -34,22 +32,20 @@ export const fetchServerTools = async (accessToken: string, serverId: string): P
|
|||
throw new Error(result.message || result.error);
|
||||
}
|
||||
const toolList: RawTool[] = Array.isArray(result) ? result : result?.tools ?? [];
|
||||
return toolList.map(toEntry);
|
||||
return toolList.map(toEntry).filter((tool) => tool.name !== "");
|
||||
};
|
||||
|
||||
const toStatus = ({
|
||||
status,
|
||||
fetchStatus,
|
||||
}: Pick<QueryObserverResult, "status" | "fetchStatus">): MCPServerToolsStatus => {
|
||||
data,
|
||||
}: Pick<QueryObserverResult<MCPServerToolEntry[]>, "status" | "fetchStatus" | "data">): MCPServerToolsStatus => {
|
||||
// A failed refetch keeps the tools that already loaded on screen.
|
||||
if (status === "error" && data !== undefined) return "success";
|
||||
if (status !== "pending") return status;
|
||||
return fetchStatus === "idle" ? "idle" : "loading";
|
||||
};
|
||||
|
||||
/**
|
||||
* Tool catalogs for a set of MCP servers, one query per server so a slow or failing
|
||||
* server never blocks the others. Only servers in `enabledServerIds` are fetched;
|
||||
* the rest stay "idle" until they are enabled.
|
||||
*/
|
||||
export const useMCPServerTools = (
|
||||
serverIds: readonly string[],
|
||||
enabledServerIds: ReadonlySet<string>,
|
||||
|
|
@ -62,6 +58,7 @@ export const useMCPServerTools = (
|
|||
enabled: !!accessToken && enabledServerIds.has(serverId),
|
||||
staleTime: 5 * 60 * 1000,
|
||||
retry: false,
|
||||
refetchOnWindowFocus: false,
|
||||
})),
|
||||
combine: (results) =>
|
||||
new Map(
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import React from "react";
|
||||
import { fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import userEvent, { PointerEventsCheckLevel } from "@testing-library/user-event";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
|
|
@ -7,6 +7,7 @@ import { MCPToolsetsTab } from "./MCPToolsetsTab";
|
|||
import * as networking from "@/components/networking";
|
||||
import { useMCPToolsets } from "@/app/(dashboard)/hooks/mcpServers/useMCPToolsets";
|
||||
import { useMCPServers } from "@/app/(dashboard)/hooks/mcpServers/useMCPServers";
|
||||
import { mcpServerToolKeys } from "@/app/(dashboard)/hooks/mcpServers/useMCPServerTools";
|
||||
import { MCPToolset } from "@/components/mcp_tools/types";
|
||||
|
||||
vi.mock("@/components/networking", () => ({
|
||||
|
|
@ -223,11 +224,13 @@ describe("MCPToolsetsTab tool search and bulk selection", () => {
|
|||
vi.mocked(useMCPServers).mockReturnValue({ data: servers, isSuccess: true } as unknown as ReturnType<
|
||||
typeof useMCPServers
|
||||
>);
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } } });
|
||||
render(
|
||||
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false, gcTime: 0 } } })}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MCPToolsetsTab accessToken="sk-test" userRole="Admin" />
|
||||
</QueryClientProvider>,
|
||||
);
|
||||
return queryClient;
|
||||
};
|
||||
|
||||
const editToolset = (tools: MCPToolset["tools"]) =>
|
||||
|
|
@ -357,6 +360,23 @@ describe("MCPToolsetsTab tool search and bulk selection", () => {
|
|||
});
|
||||
});
|
||||
|
||||
it("stops fetching server tools once the editor is closed after a search", async () => {
|
||||
const user = setup();
|
||||
const queryClient = renderWithServers();
|
||||
|
||||
const dialog = await openCreate(user);
|
||||
await user.type(dialog.getByRole("textbox", { name: "Search tools or servers" }), "issue");
|
||||
expect(await dialog.findByRole("checkbox", { name: /^create_ticket/ })).toBeInTheDocument();
|
||||
await user.click(dialog.getByRole("button", { name: "Cancel" }));
|
||||
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
|
||||
const callsWhileOpen = vi.mocked(networking.listMCPTools).mock.calls.length;
|
||||
|
||||
await act(() => queryClient.invalidateQueries({ queryKey: mcpServerToolKeys.all }));
|
||||
await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
|
||||
expect(networking.listMCPTools).toHaveBeenCalledTimes(callsWhileOpen);
|
||||
});
|
||||
|
||||
it("clears the whole toolset with Clear all", async () => {
|
||||
const user = setup();
|
||||
renderWithServers([editToolset([{ server_id: "gh", tool_name: "create_issue" }])]);
|
||||
|
|
|
|||
|
|
@ -48,11 +48,24 @@ const toolsetSchema = z.object({
|
|||
|
||||
type ToolsetFormValues = z.infer<typeof toolsetSchema>;
|
||||
|
||||
// Typing starts a fetch of every server's tools; wait for a pause so each keystroke does not.
|
||||
const SEARCH_DEBOUNCE_WAIT_MS = 300;
|
||||
|
||||
const IDLE_SERVER_TOOLS: MCPServerToolsState = { tools: [], status: "idle" };
|
||||
|
||||
const NO_SERVERS: ReadonlySet<string> = new Set();
|
||||
|
||||
// The modal stays mounted while closed, so fetch only while it is open; otherwise a finished
|
||||
// search keeps every server's query live and refetches all of them in the background.
|
||||
const serversToFetch = (
|
||||
open: boolean,
|
||||
searching: boolean,
|
||||
serverIds: readonly string[],
|
||||
expandedServerIds: ReadonlySet<string>,
|
||||
): ReadonlySet<string> => {
|
||||
if (!open) return NO_SERVERS;
|
||||
return searching ? new Set(serverIds) : expandedServerIds;
|
||||
};
|
||||
|
||||
const checkboxStateProps = (state: SelectionState) => ({
|
||||
checked: state === "all",
|
||||
indeterminate: state === "some",
|
||||
|
|
@ -245,10 +258,10 @@ function CreateToolsetModal({ open, onClose, onSave, initialToolset }: CreateToo
|
|||
const [expandedServerIds, setExpandedServerIds] = useState<ReadonlySet<string>>(() => new Set());
|
||||
const { data: mcpServers = [], isSuccess: serversLoaded } = useMCPServers();
|
||||
const serverIds = React.useMemo(() => mcpServers.map((s) => s.server_id), [mcpServers]);
|
||||
// Searching needs every server's catalog; otherwise only expanded servers are fetched.
|
||||
const searching = search.trim() !== "" && debouncedSearch.trim() !== "";
|
||||
const fetchServerIds = React.useMemo(
|
||||
() => (debouncedSearch.trim() ? new Set(serverIds) : expandedServerIds),
|
||||
[debouncedSearch, serverIds, expandedServerIds],
|
||||
() => serversToFetch(open, searching, serverIds, expandedServerIds),
|
||||
[open, searching, serverIds, expandedServerIds],
|
||||
);
|
||||
const toolsByServer = useMCPServerTools(serverIds, fetchServerIds);
|
||||
const serverPrefixById = React.useMemo(
|
||||
|
|
|
|||
|
|
@ -11,16 +11,12 @@ const toolKey = (tool: MCPToolsetTool): string => `${tool.server_id}\u0000${tool
|
|||
|
||||
const normalize = (query: string): string => query.trim().toLowerCase();
|
||||
|
||||
/** Whether a non-empty search query matches any of a server's names (alias, server name). */
|
||||
export const matchesServerName = (query: string, serverNames: readonly string[]): boolean => {
|
||||
const q = normalize(query);
|
||||
return q !== "" && serverNames.some((name) => name.toLowerCase().includes(q));
|
||||
};
|
||||
|
||||
/**
|
||||
* The tools of one server that a search query shows. A query matching the server's
|
||||
* name shows every tool on it; otherwise a tool shows when its name or description matches.
|
||||
*/
|
||||
// A query that matches the server's name shows every tool on that server.
|
||||
export const filterServerTools = <T extends SearchableTool>(
|
||||
query: string,
|
||||
serverNames: readonly string[],
|
||||
|
|
@ -33,7 +29,6 @@ export const filterServerTools = <T extends SearchableTool>(
|
|||
return tools.filter((tool) => tool.name.toLowerCase().includes(q) || tool.description.toLowerCase().includes(q));
|
||||
};
|
||||
|
||||
/** Adds `tools` to the selection, keeping existing order and skipping tools already selected. */
|
||||
export const selectTools = (
|
||||
selected: readonly MCPToolsetTool[],
|
||||
tools: readonly MCPToolsetTool[],
|
||||
|
|
@ -48,7 +43,6 @@ export const selectTools = (
|
|||
return [...selected, ...added];
|
||||
};
|
||||
|
||||
/** Removes exactly `tools` from the selection; every other selected tool is kept. */
|
||||
export const deselectTools = (
|
||||
selected: readonly MCPToolsetTool[],
|
||||
tools: readonly MCPToolsetTool[],
|
||||
|
|
@ -63,7 +57,6 @@ export const toggleTool = (selected: readonly MCPToolsetTool[], tool: MCPToolset
|
|||
export const isToolSelected = (selected: readonly MCPToolsetTool[], tool: MCPToolsetTool): boolean =>
|
||||
selected.some((t) => toolKey(t) === toolKey(tool));
|
||||
|
||||
/** How much of `tools` is selected, for a tri-state "select all" checkbox. */
|
||||
export const selectionState = (
|
||||
selected: readonly MCPToolsetTool[],
|
||||
tools: readonly MCPToolsetTool[],
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue