diff --git a/server/src/__tests__/google-workspace-mcp-transport.test.ts b/server/src/__tests__/google-workspace-mcp-transport.test.ts index 622b1fd0edb..260506cd0e3 100644 --- a/server/src/__tests__/google-workspace-mcp-transport.test.ts +++ b/server/src/__tests__/google-workspace-mcp-transport.test.ts @@ -4,7 +4,7 @@ import { and, eq } from "drizzle-orm"; import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; import { agents, companies, companyMemberships, createDb, heartbeatRuns, issues, issueThreadInteractions, - toolAccessAuditEvents, toolActionRequests, toolCallEvents, toolCatalogEntries, toolInvocations, + toolAccessAuditEvents, toolActionRequests, toolCallEvents, toolCatalogEntries, toolInvocations, toolPolicies, toolProfileEntries, } from "@paperclipai/db"; import { GOOGLE_WORKSPACE_DEFAULT_SELECTION, GOOGLE_WORKSPACE_TOOL_TRANSPORT, googleWorkspaceServiceToolNames, @@ -596,16 +596,19 @@ const support = await getEmbeddedPostgresTestSupport(); expect(removed.every((row) => row.status === "disabled")).toBe(true); const held = rows.filter((row) => row.status === "quarantined"); expect(held.length).toBeGreaterThan(0); - const choices = (enabled: string[]) => f.service.finishGalleryAppConnection(f.company.id, f.connectionId, { - enabledCatalogEntryIds: enabled, askFirstCatalogEntryIds: [], + const choices = (enabled: string[], askFirst: string[] = []) => f.service.finishGalleryAppConnection(f.company.id, f.connectionId, { + enabledCatalogEntryIds: enabled, askFirstCatalogEntryIds: askFirst, reviewedCatalogEntryIds: [...held, ...removed].map((row) => row.id), access: { agentIds: [f.agent.id] }, }, f.actor); await expect(choices([...held, blocked!].map((row) => row.id))) .rejects.toMatchObject({ status: 400, message: "Disabled actions cannot be enabled" }); - await choices([...held, ...removed].map((row) => row.id)); + await choices([...held, removed[0]!].map((row) => row.id), [removed[1]!.id]); const after = await byName(f.connectionId); + const askFirstPolicies = (await db.select().from(toolPolicies).where(eq(toolPolicies.companyId, f.company.id))) + .filter((policy) => policy.enabled && policy.policyType === "require_approval"); + expect(askFirstPolicies.map((policy) => (policy.selectors as { catalogEntryId?: string }).catalogEntryId)).not.toContain(removed[1]!.id); expect(after.get("gmail__list_threads")).toMatchObject({ status: "disabled", quarantineReason: "mcp_tool_removed" }); expect(after.get("gmail__search_messages")).toMatchObject({ status: "disabled", quarantineReason: "mcp_tool_removed" }); const offered = (await f.listed()).map((tool) => tool.upstreamToolName); @@ -613,6 +616,55 @@ const support = await getEmbeddedPostgresTestSupport(); expect(offered).not.toContain("gmail__list_threads"); expect(offered).not.toContain("gmail__search_messages"); expect(offered).not.toContain("gmail__get_draft"); + + forgetMcpHttpSessions(f.connectionId); + await f.service.refreshCatalog(f.connectionId, f.actor); + const relisted = await byName(f.connectionId); + expect(relisted.get("gmail__list_threads")?.status).toBe("active"); + expect(relisted.get("gmail__search_messages")?.status).toBe("active"); + const offeredAgain = (await f.listed()).map((tool) => tool.upstreamToolName); + expect(offeredAgain).toContain("gmail__get_message"); + expect(offeredAgain).not.toContain("gmail__list_threads"); + expect(offeredAgain).not.toContain("gmail__search_messages"); + }); + + it("does not keep a removed action on when a save keeps the existing access", async () => { + const f = await savedOnRest(); + vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockReturnValue([]); + UNLISTED.add("list_threads"); + try { + await backOnGoogle(f); + } finally { + vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockRestore(); + UNLISTED.clear(); + } + const rows = await savedRows(f.connectionId); + const removed = rows.filter((row) => row.quarantineReason === "mcp_tool_removed"); + expect(names(removed)).toEqual(["gmail__list_threads"]); + const held = rows.filter((row) => row.status === "quarantined"); + const [draftRow] = rows.filter((row) => row.toolName === "gmail__get_draft"); + await db.update(toolCatalogEntries).set({ status: "disabled", quarantineReason: "mcp_tool_removed" }).where(eq(toolCatalogEntries.id, draftRow!.id)); + const excluded = await db.update(toolProfileEntries).set({ effect: "exclude" }) + .where(eq(toolProfileEntries.catalogEntryId, draftRow!.id)).returning(); + expect(excluded).toHaveLength(1); + await f.service.finishGalleryAppConnection(f.company.id, f.connectionId, { + enabledCatalogEntryIds: held.map((row) => row.id), askFirstCatalogEntryIds: [removed[0]!.id], + reviewedCatalogEntryIds: [...held, ...removed].map((row) => row.id), access: { agentIds: [f.agent.id] }, + preserveExistingAccess: true, + }, f.actor); + const kept = await db.select().from(toolProfileEntries).where(eq(toolProfileEntries.profileId, excluded[0]!.profileId)); + expect(kept.filter((entry) => entry.catalogEntryId === draftRow!.id).map((entry) => entry.effect)).toEqual(["exclude"]); + expect(kept.some((entry) => entry.catalogEntryId === removed[0]!.id)).toBe(false); + const askFirstPolicies = (await db.select().from(toolPolicies).where(eq(toolPolicies.companyId, f.company.id))) + .filter((policy) => policy.enabled && policy.policyType === "require_approval"); + expect(askFirstPolicies.map((policy) => (policy.selectors as { catalogEntryId?: string }).catalogEntryId)).not.toContain(removed[0]!.id); + + forgetMcpHttpSessions(f.connectionId); + await f.service.refreshCatalog(f.connectionId, f.actor); + expect((await byName(f.connectionId)).get("gmail__list_threads")?.status).toBe("active"); + const offered = (await f.listed()).map((tool) => tool.upstreamToolName); + expect(offered).toContain("gmail__get_message"); + expect(offered).not.toContain("gmail__list_threads"); }); it("keeps an action that a switch before this fix held for review held, because the hold replaced the saved REST definition", async () => { diff --git a/server/src/services/tool-access.ts b/server/src/services/tool-access.ts index 0470e962756..ec71687d15c 100644 --- a/server/src/services/tool-access.ts +++ b/server/src/services/tool-access.ts @@ -99,7 +99,7 @@ import { defaultConnectionAgentInstructions, paperclipPeerAgentInstructions, pap import { isBrowserUseConnection } from "./browser-use-client.js"; import { COGNEE_STDIO_TEMPLATE } from "./cognee-connection.js"; -import { isCatalogEntryBlocked, isCatalogEntryRemovedByProvider, isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared"; +import { isCatalogEntryBlocked, isCatalogEntryOffered, isCatalogEntryRemovedByProvider, isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared"; import { githubBotRequest } from "./chat-github-client.js"; import { syncConnectionCredentialBindings } from "./connection-credential-bindings.js"; @@ -3438,7 +3438,7 @@ export function toolAccessService( if (input.access !== "all_agents") await assertAgentsInCompany(companyId, input.access.agentIds); - const entries: CreateToolProfileEntryForProfile[] = enabledRows.map( + const entries: CreateToolProfileEntryForProfile[] = enabledRows.filter(isCatalogEntryOffered).map( (entry) => ({ selectorType: "catalog_entry", effect: "include", @@ -3540,8 +3540,19 @@ export function toolAccessService( priority: prior.priority, metadata: prior.metadata, }); + const priorCatalogEntryIds = [...new Set(priorEntries.flatMap((prior) => prior.catalogEntryId ? [prior.catalogEntryId] : []))]; + const removedPriorCatalogEntryIds = new Set( + (priorCatalogEntryIds.length > 0 + ? await tx + .select({ id: toolCatalogEntries.id, status: toolCatalogEntries.status, quarantineReason: toolCatalogEntries.quarantineReason }) + .from(toolCatalogEntries) + .where(and(eq(toolCatalogEntries.companyId, companyId), inArray(toolCatalogEntries.id, priorCatalogEntryIds))) + : [] + ).filter(isCatalogEntryRemovedByProvider).map((entry) => entry.id), + ); for (const prior of priorEntries) if ( + !(prior.effect === "include" && prior.catalogEntryId && removedPriorCatalogEntryIds.has(prior.catalogEntryId)) && !entries.some( (entry) => entry.catalogEntryId && @@ -3729,7 +3740,7 @@ export function toolAccessService( { companyId, connection, - askFirstEntries: askFirstRows, + askFirstEntries: askFirstRows.filter(isCatalogEntryOffered), disableStale: !input.preserveExistingAccess, actor, },