From 389ef42ac0b7967caddcae54809d7f6f1ffa749e Mon Sep 17 00:00:00 2001 From: Yassin Kortam Date: Sat, 5 Sep 2026 14:04:13 -0700 Subject: [PATCH] fix(ui): keep MCP tool allowlists for team servers granted indirectly (#35153) * fix(ui): filter team MCP tool allowlists against the effective server set Saving a team filtered mcp_tool_permissions down to the directly selected servers. A server reached through an access group or a toolset is never in that list, so any save dropped its entry, including a save that only changed the team alias. Because the resolver unions tool-permission keys into the entitled server set and treats a missing entry as "no restriction from this level", the team kept the server and lost the tool allowlist on it Filtering on the direct list alone cannot get this right in either direction. Keeping every entry a level did not directly select leaves a removed access group's server reachable through its own stale entry, which breaks revocation. Dropping on deselection alone widens a server that an access group still supplies The save handler now resolves the effective server set with resolveEffectiveMcpServers and keeps an entry only when something other than the entry itself still grants that server: a direct selection, a selected access group, or a selected toolset. Unified access group ids are added when that selection is untouched, since the loaded server list is then still accurate When the server or toolset list cannot be resolved, every entry is kept and the admin is told the allowlists were saved unchanged. Pruning on incomplete knowledge is the direction that silently widens, so it only happens when the editor can show the server became unreachable. A failed lookup and a changed access group selection are separate cases in a tagged union, so the notice names what actually happened instead of describing the intentional one as a failure, and both hooks gate the filter symmetrically so a save fired before toolsets settle cannot resolve against an empty toolset list Resolves LIT-4961 * fix(ui): resolve team MCP grants from access group metadata and refuse unsafe saves Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): resolve team access group grants from team info when the access group list is role-gated Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): match every selected access group by id instead of by count Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): reload team access group grants at save time Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(ui): keep frontend lint budget within limit Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(ui): cover a standing allowlist no group grant covers at load or save Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(ui): keep MCP grant inputs in named variables for the lint budget Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../src/components/team/TeamInfo.test.tsx | 254 +++++++++++++++++- .../src/components/team/TeamInfo.tsx | 87 ++++-- 2 files changed, 310 insertions(+), 31 deletions(-) diff --git a/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx b/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx index f5afb636245..041ba4b548d 100644 --- a/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx +++ b/ui/litellm-dashboard/src/components/team/TeamInfo.test.tsx @@ -9,6 +9,7 @@ import type { EffectiveMcpServer } from "../mcp_server_management/effectiveMcpSe import type { MCPServer } from "../mcp_tools/types"; import TeamInfoView, { grantedMcpServerIds, + type McpGrantInput, retainedMcpToolPermissions, standingToolPermissionServerIds, type TeamData, @@ -191,6 +192,14 @@ vi.mock("@/app/(dashboard)/hooks/accessGroups/useAccessGroups", () => ({ }), })); +vi.mock("@/components/common_components/AccessGroupSelector", () => ({ + default: ({ value, onChange }: { value?: string[]; onChange?: (next: string[]) => void }) => ( + + ), +})); + vi.mock("@/app/(dashboard)/hooks/keys/useKeys", () => ({ useKeys: vi.fn().mockReturnValue({ data: { keys: [], total_count: 0, current_page: 1, total_pages: 1 }, @@ -2286,13 +2295,96 @@ describe("TeamInfo MCP permission retention", () => { source: kind === "accessGroup" ? { kind, name: "ops_readonly" } : { kind }, }) as EffectiveMcpServer; - it("retains permissions for directly and indirectly granted servers", () => { - const resolution = grantedMcpServerIds( - [effective("direct", "direct"), effective("inherited", "toolPermission")], - ["ag-1"], - [{ access_group_id: "ag-1", access_mcp_server_ids: ["inherited"] }], - new Set(), + const UNIFIED_SERVER = server("unified-server", "wiki"); + const UNIFIED_GROUPS = [ + { access_group_id: "ag-1", access_group_name: "Group 1", access_mcp_server_ids: ["unified-server"] }, + { access_group_id: "ag-2", access_group_name: "Group 2", access_mcp_server_ids: [] }, + ]; + + const unifiedTeam = (toolPermissions: Record, serverIds: string[] = []) => { + const teamData = { + models: ["gpt-4"], + access_group_ids: ["ag-1", "ag-2"], + access_group_mcp_server_ids: serverIds, + object_permission: { + mcp_servers: [], + mcp_access_groups: [], + mcp_toolsets: [], + mcp_tool_permissions: toolPermissions, + }, + }; + return createMockTeamData(teamData); + }; + + const renderMcpEditor = async ( + user: ReturnType, + { + initialTeam = unifiedTeam({ wiki: ["read_page"] }, ["unified-server"]), + freshTeam = initialTeam, + accessGroups = [], + }: { + initialTeam?: ReturnType; + freshTeam?: ReturnType; + accessGroups?: typeof UNIFIED_GROUPS; + } = {}, + ) => { + mockUseMCPServers.mockReturnValue({ data: [UNIFIED_SERVER], isLoading: false, isError: false } as any); + mockUseMCPToolsets.mockReturnValue({ data: [], isLoading: false, isError: false } as any); + mockUseAccessGroups.mockReturnValue({ data: accessGroups, isLoading: false, isError: false } as any); + vi.mocked(networking.teamInfoCall).mockResolvedValueOnce(initialTeam).mockResolvedValue(freshTeam); + vi.mocked(networking.teamUpdateCall).mockResolvedValue({ data: {}, team_id: "123" } as any); + + renderWithProviders( + , ); + await waitFor(() => expect(screen.queryAllByText("Test Team").length).toBeGreaterThan(0)); + await user.click(screen.getByRole("tab", { name: "Settings" })); + await user.click(await screen.findByRole("button", { name: /edit settings/i })); + await screen.findByLabelText("Team Name"); + }; + + const saveMcpEditor = async (user: ReturnType) => { + await user.click(screen.getByRole("button", { name: /save changes/i })); + await waitFor(() => expect(networking.teamUpdateCall).toHaveBeenCalled()); + const calls = vi.mocked(networking.teamUpdateCall).mock.calls; + const [, payload] = calls[calls.length - 1]; + return payload.object_permission.mcp_tool_permissions; + }; + + const refuseMcpSave = async (user: ReturnType, reason: RegExp) => { + const errorToast = vi.spyOn(toast, "fromError").mockImplementation(() => {}); + await user.click(screen.getByRole("button", { name: /save changes/i })); + await waitFor(() => expect(errorToast).toHaveBeenCalledWith(expect.stringMatching(reason))); + expect(networking.teamUpdateCall).not.toHaveBeenCalled(); + errorToast.mockRestore(); + }; + + const resolveGrants = (overrides: Partial) => { + const input: McpGrantInput = { + effectiveServers: [effective("server-1", "direct")], + selectedAccessGroupIds: ["ag-1"], + accessGroups: [], + standingServerIds: new Set(), + loadTeamGroups: vi.fn(), + ...overrides, + }; + return grantedMcpServerIds(input); + }; + + it("retains permissions for directly and indirectly granted servers", async () => { + const resolution = await resolveGrants({ + effectiveServers: [effective("direct", "direct"), effective("inherited", "toolPermission")], + accessGroups: [{ access_group_id: "ag-1", access_mcp_server_ids: ["inherited"] }], + }); expect(resolution.kind).toBe("resolved"); if (resolution.kind !== "resolved") return; @@ -2323,8 +2415,69 @@ describe("TeamInfo MCP permission retention", () => { }); }); - it("refuses an unresolved selected access group", () => { - expect(grantedMcpServerIds([effective("server-1", "direct")], ["missing"], [], new Set())).toEqual({ + it("does not reload the team when the access group list covers the selection", async () => { + const loadTeamGroups = vi.fn(); + await expect( + resolveGrants({ + accessGroups: [{ access_group_id: "ag-1", access_mcp_server_ids: ["group-server"] }], + loadTeamGroups, + }), + ).resolves.toEqual({ + kind: "resolved", + serverIds: new Set(["server-1", "group-server"]), + }); + expect(loadTeamGroups).not.toHaveBeenCalled(); + }); + + it("falls back to the team's loaded access group servers when the list is unavailable and the selection is unchanged", async () => { + expect( + await resolveGrants({ + loadTeamGroups: vi.fn().mockResolvedValue({ ids: ["ag-1"], serverIds: ["group-server"] }), + }), + ).toEqual({ + kind: "resolved", + serverIds: new Set(["server-1", "group-server"]), + }); + }); + + it("adds standing tool-permission grants to the reloaded team grants", async () => { + expect( + await resolveGrants({ + effectiveServers: [effective("server-1", "direct"), effective("standing", "toolPermission")], + standingServerIds: new Set(["standing"]), + loadTeamGroups: vi.fn().mockResolvedValue({ ids: ["ag-1"], serverIds: ["group-server"] }), + }), + ).toEqual({ + kind: "resolved", + serverIds: new Set(["server-1", "group-server", "standing"]), + }); + }); + + it("is unresolvable when the list is unavailable and the selection changed", async () => { + expect( + await resolveGrants({ + loadTeamGroups: vi.fn().mockResolvedValue({ ids: ["ag-1", "ag-2"], serverIds: ["group-server"] }), + }), + ).toEqual({ + kind: "unresolvable", + reason: expect.stringMatching(/access groups could not be loaded/), + }); + }); + + it("is unresolvable when the team reload fails", async () => { + expect(await resolveGrants({ loadTeamGroups: vi.fn().mockRejectedValue(new Error("boom")) })).toEqual({ + kind: "unresolvable", + reason: expect.stringMatching(/access groups could not be reloaded/), + }); + }); + + it("refuses an unresolved selected access group", async () => { + expect( + await resolveGrants({ + selectedAccessGroupIds: ["missing"], + loadTeamGroups: vi.fn().mockResolvedValue({ ids: [], serverIds: [] }), + }), + ).toEqual({ kind: "unresolvable", reason: expect.stringMatching(/access groups could not be loaded/), }); @@ -2354,22 +2507,101 @@ describe("TeamInfo MCP permission retention", () => { }); }); + it("keeps the group server allowlist when the access group list is unavailable and the selection is unchanged", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user); + + await user.clear(screen.getByLabelText("Team Name")); + await user.type(screen.getByLabelText("Team Name"), "Renamed Team"); + + expect(await saveMcpEditor(user)).toEqual({ wiki: ["read_page"] }); + }); + + it("refuses a save when the access group list is unavailable and the selection changed", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user); + + await user.click(screen.getByRole("button", { name: "remove first unified access group" })); + await refuseMcpSave(user, /access groups could not be loaded/); + }); + + it("keeps the group server allowlist when a selected access group is missing from the list", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user, { + accessGroups: [UNIFIED_GROUPS[1]], + initialTeam: unifiedTeam({ wiki: ["read_page"] }, ["unified-server"]), + }); + + expect(await saveMcpEditor(user)).toEqual({ wiki: ["read_page"] }); + }); + + it("uses current group grants when a server was revoked after the page loaded", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user, { + initialTeam: unifiedTeam({ wiki: ["read_page"] }, ["unified-server"]), + freshTeam: unifiedTeam({ wiki: ["read_page"] }), + }); + + expect(await saveMcpEditor(user)).toEqual({}); + }); + + it("uses current group grants when a server was granted after the page loaded", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user, { + initialTeam: unifiedTeam({ wiki: ["read_page"] }), + freshTeam: unifiedTeam({ wiki: ["read_page"] }, ["unified-server"]), + }); + + expect(await saveMcpEditor(user)).toEqual({ wiki: ["read_page"] }); + }); + + it("keeps a standing allowlist that no group grant covers at load or at save", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user, { + initialTeam: unifiedTeam({ wiki: ["read_page"] }), + freshTeam: unifiedTeam({ wiki: ["read_page"] }), + }); + + expect(await saveMcpEditor(user)).toEqual({ wiki: ["read_page"] }); + }); + + it("refuses a save when the access group list is unavailable and the team reload fails", async () => { + const user = userEvent.setup({ delay: null }); + await renderMcpEditor(user); + vi.mocked(networking.teamInfoCall).mockRejectedValueOnce(new Error("boom")); + + await refuseMcpSave(user, /access groups could not be reloaded/); + }); + it("identifies standing tool-permission grants not covered by loaded access groups", () => { expect( standingToolPermissionServerIds( [effective("a", "toolPermission"), effective("b", "toolPermission"), effective("c", "direct")], ["ag-1"], [{ access_group_id: "ag-1", access_mcp_server_ids: ["b"] }], + [], ), ).toEqual(new Set(["a"])); }); - it("includes standing tool-permission grants in the resolved server ids", () => { - expect(grantedMcpServerIds([effective("x", "toolPermission")], [], [], new Set(["x"]))).toEqual({ + it("does not treat a server granted by the team's loaded access groups as standing", () => { + expect( + standingToolPermissionServerIds( + [effective("a", "toolPermission"), effective("b", "toolPermission")], + ["ag-1"], + [], + ["b"], + ), + ).toEqual(new Set(["a"])); + }); + + it("includes standing tool-permission grants in the resolved server ids", async () => { + const standingOnly = { effectiveServers: [effective("x", "toolPermission")], selectedAccessGroupIds: [] }; + expect(await resolveGrants({ ...standingOnly, standingServerIds: new Set(["x"]) })).toEqual({ kind: "resolved", serverIds: new Set(["x"]), }); - expect(grantedMcpServerIds([effective("x", "toolPermission")], [], [], new Set())).toEqual({ + expect(await resolveGrants(standingOnly)).toEqual({ kind: "resolved", serverIds: new Set(), }); diff --git a/ui/litellm-dashboard/src/components/team/TeamInfo.tsx b/ui/litellm-dashboard/src/components/team/TeamInfo.tsx index 3ca58b4006b..efd69a79fc0 100644 --- a/ui/litellm-dashboard/src/components/team/TeamInfo.tsx +++ b/ui/litellm-dashboard/src/components/team/TeamInfo.tsx @@ -131,16 +131,29 @@ export type McpGrantResolution = | { readonly kind: "resolved"; readonly serverIds: ReadonlySet } | { readonly kind: "unresolvable"; readonly reason: string }; +export type TeamAccessGroupGrants = { + readonly ids: readonly string[]; + readonly serverIds: readonly string[]; +}; + +const sameIdSelection = (a: readonly string[], b: readonly string[]): boolean => { + const aSet = new Set(a); + const bSet = new Set(b); + return aSet.size === bSet.size && [...aSet].every((id) => bSet.has(id)); +}; + export const standingToolPermissionServerIds = ( loadedEffectiveServers: readonly EffectiveMcpServer[], loadedAccessGroupIds: readonly string[], accessGroups: readonly Pick[], + loadedTeamGroupServerIds: readonly string[], ): ReadonlySet => { - const loadedUnifiedServerIds = new Set( - accessGroups + const loadedUnifiedServerIds = new Set([ + ...accessGroups .filter((group) => loadedAccessGroupIds.includes(group.access_group_id)) .flatMap((group) => group.access_mcp_server_ids), - ); + ...loadedTeamGroupServerIds, + ]); return new Set( loadedEffectiveServers .filter(({ source, server }) => source.kind === "toolPermission" && !loadedUnifiedServerIds.has(server.server_id)) @@ -148,21 +161,46 @@ export const standingToolPermissionServerIds = ( ); }; -export const grantedMcpServerIds = ( - effectiveServers: readonly EffectiveMcpServer[], - selectedAccessGroupIds: readonly string[], - accessGroups: readonly Pick[], - standingServerIds: ReadonlySet, -): McpGrantResolution => { +export type McpGrantInput = { + readonly effectiveServers: readonly EffectiveMcpServer[]; + readonly selectedAccessGroupIds: readonly string[]; + readonly accessGroups: readonly Pick[]; + readonly standingServerIds: ReadonlySet; + readonly loadTeamGroups: () => Promise; +}; + +export const grantedMcpServerIds = async ({ + effectiveServers, + selectedAccessGroupIds, + accessGroups, + standingServerIds, + loadTeamGroups, +}: McpGrantInput): Promise => { const selectedGroups = accessGroups.filter((group) => selectedAccessGroupIds.includes(group.access_group_id)); - if (selectedAccessGroupIds.some((id) => !selectedGroups.some((group) => group.access_group_id === id))) { - return { kind: "unresolvable", reason: "the team's access groups could not be loaded" }; - } const direct = effectiveServers .filter(({ source }) => source.kind !== "toolPermission") .map(({ server }) => server.server_id); - const viaUnifiedGroups = selectedGroups.flatMap((group) => group.access_mcp_server_ids); - return { kind: "resolved", serverIds: new Set([...direct, ...viaUnifiedGroups, ...standingServerIds]) }; + if (selectedAccessGroupIds.every((id) => selectedGroups.some((group) => group.access_group_id === id))) { + return { + kind: "resolved", + serverIds: new Set([ + ...direct, + ...selectedGroups.flatMap((group) => group.access_mcp_server_ids), + ...standingServerIds, + ]), + }; + } + const loadedTeamGroups = await loadTeamGroups().catch(() => null); + if (loadedTeamGroups === null) { + return { kind: "unresolvable", reason: "the team's access groups could not be reloaded" }; + } + if (sameIdSelection(selectedAccessGroupIds, loadedTeamGroups.ids)) { + return { + kind: "resolved", + serverIds: new Set([...direct, ...loadedTeamGroups.serverIds, ...standingServerIds]), + }; + } + return { kind: "unresolvable", reason: "the team's access groups could not be loaded" }; }; export const retainedMcpToolPermissions = ( @@ -947,16 +985,25 @@ const TeamInfoView: React.FC = ({ loadedEffectiveMcpServers, info.access_group_ids ?? [], allAccessGroups, + info.access_group_mcp_server_ids ?? [], ); + const mcpGrantInput: McpGrantInput = { + effectiveServers: resolveEffectiveMcpServers(effectiveMcpInput), + selectedAccessGroupIds: values.access_group_ids || [], + accessGroups: allAccessGroups, + standingServerIds, + loadTeamGroups: async () => { + const teamInfo = await teamInfoCall(accessToken, teamId); + return { + ids: teamInfo.team_info.access_group_ids ?? [], + serverIds: teamInfo.team_info.access_group_mcp_server_ids ?? [], + }; + }, + }; const mcpResolution: McpGrantResolution = mcpLookupFailure !== null ? { kind: "unresolvable", reason: mcpLookupFailure } - : grantedMcpServerIds( - resolveEffectiveMcpServers(effectiveMcpInput), - values.access_group_ids || [], - allAccessGroups, - standingServerIds, - ); + : await grantedMcpServerIds(mcpGrantInput); if (mcpResolution.kind === "unresolvable" && Object.keys(submittedToolPermissions).length > 0) { toast.fromError(mcpUnresolvableSaveError(mcpResolution.reason)); return;