From dc92b8e3681690b488f4433af2e2ed140567ef2a Mon Sep 17 00:00:00 2001 From: ryan-crabbe-berri Date: Wed, 16 Sep 2026 11:21:59 -0700 Subject: [PATCH] fix(ui): address review on mcp url state A stale edit OAuth state no longer overrides the server in the URL and is dropped when its server is gone. The empty state now says no server matches when filters hide every server. Admin tab gates share one flag. Tests cover every page tab value, clearing detail keys on Back, the tool result tag, malformed stored OAuth state and the http client tab. --- .../_components/mcp_connect.test.tsx | 10 ++- .../_components/mcp_servers.test.tsx | 89 ++++++++++++++++++- .../mcp-servers/_components/mcp_servers.tsx | 80 ++++++++--------- .../_components/mcp_tools.test.tsx | 50 ++++++++--- .../_components/useMcpServersUrlState.ts | 19 +++- 5 files changed, 183 insertions(+), 65 deletions(-) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_connect.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_connect.test.tsx index e21c3769f06..834aa24d85d 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_connect.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_connect.test.tsx @@ -30,10 +30,14 @@ describe("MCPConnect (tab mount contract)", () => { }); describe("MCPConnect client tab URL state", () => { - it("opens the client tab named in the URL", () => { - renderWithProviders(, { searchParams: "?connect_client=cursor" }); + it.each([ + ["litellm", "LiteLLM Proxy"], + ["cursor", "Cursor"], + ["http", "Streamable HTTP"], + ])("opens the %s client tab named in the URL", (value, name) => { + renderWithProviders(, { searchParams: `?connect_client=${value}` }); - expect(screen.getByRole("tab", { name: "Cursor" })).toHaveAttribute("aria-selected", "true"); + expect(screen.getByRole("tab", { name })).toHaveAttribute("aria-selected", "true"); }); it("writes the client tab the user picks and drops the default", async () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.test.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.test.tsx index 439e3c0a5d1..5d9dcba4778 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.test.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.test.tsx @@ -298,6 +298,19 @@ describe("MCPServers", () => { vi.mocked(networking.fetchMCPServers).mockResolvedValue(urlServers); }); + it.each([ + ["toolsets", "Toolsets"], + ["connect", "Connect"], + ["semantic-filter", "Semantic Filter"], + ["tool-search", "Tool Search"], + ["network", "Network Settings"], + ["submitted", "Submitted MCPs"], + ])("opens the %s tab from the URL for an admin", (value, name) => { + renderPage(`?tab=${value}`); + + expect(screen.getByRole("tab", { name })).toHaveAttribute("aria-selected", "true"); + }); + it("opens the tab named in the URL and writes the tab the user picks", async () => { const onUrlUpdate = renderPage("?tab=toolsets"); @@ -384,6 +397,20 @@ describe("MCPServers", () => { expect(shownServerNames()).toEqual(["Charlie"]); }); + it("says no server matches when a URL filter excludes every server", async () => { + renderPage("?team=unknown-team"); + + expect(await screen.findByText("No servers match the current filters or search.")).toBeInTheDocument(); + expect(screen.queryByText(/No MCP servers configured/)).not.toBeInTheDocument(); + }); + + it("says no servers are configured when the proxy has none", async () => { + vi.mocked(networking.fetchMCPServers).mockResolvedValue([]); + renderPage("?team=team-a"); + + expect(await screen.findByText(/No MCP servers configured/)).toBeInTheDocument(); + }); + it("opens the server in the URL even when the filters hide it", async () => { renderPage("?server=bravo&team=team-a"); @@ -422,6 +449,30 @@ describe("MCPServers", () => { expect(closed?.options.history).toBe("push"); }); + it("clears the server tab, tool and tool search on Back but keeps the filters", async () => { + const onUrlUpdate = renderPage("?server=charlie&server_tab=tools&tool=search_docs&tool_search=se&team=team-a"); + + await userEvent.click(await screen.findByRole("button", { name: "Back to All Servers" })); + + expect(await screen.findByText("Alpha")).toBeInTheDocument(); + const closed = lastUrl(onUrlUpdate); + for (const key of ["server", "server_tab", "tool", "tool_search"]) { + expect(closed?.searchParams.has(key)).toBe(false); + } + expect(closed?.searchParams.get("team")).toBe("team-a"); + }); + + it("opens a clicked server on its overview tab", async () => { + const onUrlUpdate = renderPage("?server=missing&server_tab=settings"); + + await screen.findByText("Alpha"); + await userEvent.click(cardFor("Alpha")); + + expect(await screen.findByText("viewing Alpha")).toBeInTheDocument(); + expect(lastUrl(onUrlUpdate)?.searchParams.get("server")).toBe("alpha"); + expect(lastUrl(onUrlUpdate)?.searchParams.has("server_tab")).toBe(false); + }); + it("opens the env vars modal for fill_env_vars once and drops the key", async () => { const onUrlUpdate = renderPageKeepingMountWrites("?fill_env_vars=bravo&team=team-a"); @@ -444,6 +495,12 @@ describe("MCPServers", () => { expect(lastUrl(onUrlUpdate)?.searchParams.get("server")).toBe("alpha"); expect(lastUrl(onUrlUpdate)?.searchParams.get("server_tab")).toBe("tools"); expect(window.sessionStorage.getItem(TOOLS_OAUTH_UI_STATE_KEY)).toBeNull(); + + await userEvent.click(screen.getByRole("button", { name: "Back to All Servers" })); + + expect(await screen.findByText("Alpha")).toBeInTheDocument(); + expect(screen.queryByRole("region", { name: "server detail" })).not.toBeInTheDocument(); + expect(lastUrl(onUrlUpdate)?.searchParams.has("server")).toBe(false); }); it("returns to the editable settings of the server after an edit OAuth redirect", async () => { @@ -455,15 +512,43 @@ describe("MCPServers", () => { expect(screen.getByText("settings editable")).toBeInTheDocument(); expect(lastUrl(onUrlUpdate)?.searchParams.get("server")).toBe("charlie"); expect(lastUrl(onUrlUpdate)?.searchParams.get("server_tab")).toBe("settings"); + expect(window.sessionStorage.getItem(EDIT_OAUTH_UI_STATE_KEY)).not.toBeNull(); }); - it("ignores a stored OAuth state without a server id", async () => { - setSecureItem(TOOLS_OAUTH_UI_STATE_KEY, JSON.stringify({ serverId: "" })); + it("keeps the server in the URL over a stale edit OAuth state and drops that state", async () => { + setSecureItem(EDIT_OAUTH_UI_STATE_KEY, JSON.stringify({ serverId: "charlie" })); + + const onUrlUpdate = renderPageKeepingMountWrites("?server=bravo"); + + expect(await screen.findByText("viewing Bravo")).toBeInTheDocument(); + expect(screen.getByText("settings read only")).toBeInTheDocument(); + expect(onUrlUpdate.mock.calls.some(([update]) => update.searchParams.get("server") === "charlie")).toBe(false); + expect(window.sessionStorage.getItem(EDIT_OAUTH_UI_STATE_KEY)).toBeNull(); + }); + + it("drops an edit OAuth state whose server no longer exists", async () => { + setSecureItem(EDIT_OAUTH_UI_STATE_KEY, JSON.stringify({ serverId: "deleted" })); renderPageKeepingMountWrites(""); await screen.findByText("Alpha"); + await waitFor(() => expect(window.sessionStorage.getItem(EDIT_OAUTH_UI_STATE_KEY)).toBeNull()); expect(screen.queryByRole("region", { name: "server detail" })).not.toBeInTheDocument(); }); + + it.each([ + ["not json"], + [JSON.stringify({ serverId: 7 })], + [JSON.stringify({})], + [JSON.stringify({ serverId: "" })], + ])("ignores the malformed stored OAuth state %s", async (stored) => { + setSecureItem(TOOLS_OAUTH_UI_STATE_KEY, stored); + + const onUrlUpdate = renderPageKeepingMountWrites(""); + + await screen.findByText("Alpha"); + expect(screen.queryByRole("region", { name: "server detail" })).not.toBeInTheDocument(); + expect(onUrlUpdate.mock.calls.some(([update]) => update.searchParams.has("server"))).toBe(false); + }); }); }); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.tsx b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.tsx index b02308a6663..baa7284e143 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.tsx +++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/mcp_servers.tsx @@ -146,7 +146,7 @@ const MCPServers: React.FC = ({ accessToken, userRole, userID }) closeServer, envVarsDeepLinkId, clearEnvVarsDeepLink, - } = useMcpServersUrlState(isAdmin, serversWithHealth); + } = useMcpServersUrlState(isAdmin, serversWithHealth, mcpServers !== undefined); const [serverIdToDelete, setServerToDelete] = useState(null); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [isModalVisible, setModalVisible] = useState(false); @@ -368,7 +368,7 @@ const MCPServers: React.FC = ({ accessToken, userRole, userID })

Configure and manage your MCP servers

- {isAdminRole(userRole) && ( + {isAdmin && ( <> )} - {!isAdminRole(userRole) && ( + {!isAdmin && ( ; + }; + renderWithProviders( + <> + {viewer(M2M)} + + , + { searchParams: "?tool=search_docs" }, + ); + + await userEvent.click(await screen.findByRole("button", { name: "Call Tool" })); + expect(await screen.findByText("Tool executed successfully")).toBeInTheDocument(); + + await userEvent.click(screen.getByRole("button", { name: "go to fetch_page" })); + + expect(await screen.findByText("Ready to Call Tool")).toBeInTheDocument(); + expect(screen.queryByText("Tool executed successfully")).not.toBeInTheDocument(); + }); + it("clears the previous result when the selected tool is picked again", async () => { renderViewer(M2M, "?tool=search_docs"); diff --git a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/useMcpServersUrlState.ts b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/useMcpServersUrlState.ts index 97ce401e9c9..c2e9edcbab6 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/useMcpServersUrlState.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/mcp-servers/_components/useMcpServersUrlState.ts @@ -68,20 +68,33 @@ const readOAuthRestore = (): OAuthRestore | null => { const toSortKey = (value: string | null): McpServerSortKey | null => MCP_SERVER_SORT_KEYS.find((key) => key === value) ?? null; -export function useMcpServersUrlState(isAdmin: boolean, servers: readonly MCPServer[]) { +export function useMcpServersUrlState(isAdmin: boolean, servers: readonly MCPServer[], serversLoaded: boolean) { const [tab, setTab] = useUrlTab(isAdmin ? ADMIN_PAGE_TABS : USER_PAGE_TABS, "servers"); const [filters, setFilters] = useQueryStates(listParsers); const [detail, setDetail] = useQueryStates(detailParsers); const [fillEnvVars, setFillEnvVars] = useQueryState("fill_env_vars", parseAsString); const [envVarsDeepLinkId, setEnvVarsDeepLinkId] = useState(fillEnvVars); - const [oauthRestore] = useState(readOAuthRestore); + const [oauthRestore] = useState(() => { + const stored = readOAuthRestore(); + return stored && (detail.server ?? stored.serverId) === stored.serverId ? stored : null; + }); const [editServer, setEditServer] = useState(oauthRestore?.tab === "settings"); + const restoredServerMissing = + oauthRestore !== null && serversLoaded && !servers.some((server) => server.server_id === oauthRestore.serverId); useEffect(() => { clearStorage(TOOLS_OAUTH_UI_STATE_KEY); - if (oauthRestore) void setDetail({ server: oauthRestore.serverId, server_tab: oauthRestore.tab }); + if (!oauthRestore) { + clearStorage(EDIT_OAUTH_UI_STATE_KEY); + return; + } + void setDetail({ server: oauthRestore.serverId, server_tab: oauthRestore.tab }); }, [oauthRestore, setDetail]); + useEffect(() => { + if (restoredServerMissing) clearStorage(EDIT_OAUTH_UI_STATE_KEY); + }, [restoredServerMissing]); + useEffect(() => { if (fillEnvVars !== null) void setFillEnvVars(null); }, [fillEnvVars, setFillEnvVars]);