diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 09acd480368..87b4a095d07 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -3035,6 +3035,7 @@ export { configureRailwaySshSchema, type ConfigureRailwaySsh, type RailwaySshSet export * from "./announcements.js"; export { REMOTE_MCP_CONNECTOR_METHODS, isRemoteMcpConnectorId, isRemoteMcpConnectorMethod, type RemoteMcpConnectorId } from "./remote-mcp-connectors.js"; +export { isCatalogEntryBlocked, isCatalogEntryOffered, isCatalogEntryRemovedByProvider } from "./tool-catalog-availability.js"; export { isRetiredComposioConnection, RETIRED_COMPOSIO_MESSAGE } from "./retired-composio.js"; diff --git a/packages/shared/src/tool-catalog-availability.test.ts b/packages/shared/src/tool-catalog-availability.test.ts new file mode 100644 index 00000000000..74d3374cee0 --- /dev/null +++ b/packages/shared/src/tool-catalog-availability.test.ts @@ -0,0 +1,17 @@ +import { describe, expect, it } from "vitest"; +import { isCatalogEntryBlocked, isCatalogEntryOffered, isCatalogEntryRemovedByProvider } from "./tool-catalog-availability.js"; + +describe("tool catalog availability", () => { + it("tells offered, provider-removed and blocked actions apart", () => { + const cases = [ + [{ status: "active" }, true, false, false], + [{ status: "quarantined", quarantineReason: "pending_review" }, true, false, false], + [{ status: "disabled", quarantineReason: "mcp_tool_removed" }, false, true, false], + [{ status: "removed", quarantineReason: "provider_tool_removed" }, false, true, false], + [{ status: "disabled", quarantineReason: null }, false, false, true], + ] as const; + for (const [entry, offered, removed, blocked] of cases) { + expect([isCatalogEntryOffered(entry), isCatalogEntryRemovedByProvider(entry), isCatalogEntryBlocked(entry)]).toEqual([offered, removed, blocked]); + } + }); +}); diff --git a/packages/shared/src/tool-catalog-availability.ts b/packages/shared/src/tool-catalog-availability.ts new file mode 100644 index 00000000000..eaaadc57176 --- /dev/null +++ b/packages/shared/src/tool-catalog-availability.ts @@ -0,0 +1,15 @@ +import type { ToolCatalogEntryStatus } from "./constants.js"; + +type CatalogEntryState = { status: ToolCatalogEntryStatus; quarantineReason?: string | null }; + +export function isCatalogEntryRemovedByProvider(entry: CatalogEntryState): boolean { + return entry.status === "removed" || (entry.status === "disabled" && entry.quarantineReason === "mcp_tool_removed"); +} + +export function isCatalogEntryOffered(entry: CatalogEntryState): boolean { + return entry.status === "active" || entry.status === "quarantined"; +} + +export function isCatalogEntryBlocked(entry: CatalogEntryState): boolean { + return !isCatalogEntryOffered(entry) && !isCatalogEntryRemovedByProvider(entry); +} diff --git a/server/src/__tests__/google-workspace-mcp-transport.test.ts b/server/src/__tests__/google-workspace-mcp-transport.test.ts index 0ab3bed9171..622b1fd0edb 100644 --- a/server/src/__tests__/google-workspace-mcp-transport.test.ts +++ b/server/src/__tests__/google-workspace-mcp-transport.test.ts @@ -74,6 +74,7 @@ const allowlisted = (keep: (name: string) => boolean = () => true) => LAUNCH_SER .flatMap((service) => googleWorkspaceServiceToolNames(service).map((name) => `${service}__${name}`)).filter(keep).sort(); const SCHEMAS_22634: Record> = JSON.parse( readFileSync(new URL("../google-workspace-rest-tools/fixtures/input-schemas-22634.json", import.meta.url), "utf8")); +const UNLISTED = new Set(); const names = (catalog: Array<{ toolName: string }>) => catalog.map((entry) => entry.toolName).sort(); type Confirmation = "none" | "legacy" | "modern"; @@ -126,7 +127,7 @@ function fakeGoogle(options: { confirm?: Confirmation } = {}) { } if (body.method === "tools/list") { const service = (Object.keys(HOSTS) as Array).find((key) => HOSTS[key] === url.hostname)!; - const tools = [...googleWorkspaceServiceToolNames(service), ...(FORBIDDEN[url.hostname] ?? [])].map((name) => ({ + const tools = [...googleWorkspaceServiceToolNames(service), ...(FORBIDDEN[url.hostname] ?? [])].filter((name) => !UNLISTED.has(name)).map((name) => ({ name, description: `Google's ${name}.`, inputSchema: SCHEMAS[name] ?? { type: "object" }, annotations: { readOnlyHint: /^(get|list|search|read|download|suggest)_/.test(name) }, })); @@ -577,6 +578,43 @@ const support = await getEmbeddedPostgresTestSupport(); } }); + it("saves the review when Google no longer offers actions that were on, and still refuses a disabled action", async () => { + const f = await savedOnRest(); + vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockReturnValue([]); + UNLISTED.add("list_threads").add("search_messages"); + try { + await backOnGoogle(f); + } finally { + vi.mocked(restDispatch.googleWorkspaceRestShippedSchemaHashes).mockRestore(); + UNLISTED.clear(); + } + const [blocked] = await db.update(toolCatalogEntries).set({ status: "disabled", quarantineReason: null, quarantinedAt: null }) + .where(and(eq(toolCatalogEntries.connectionId, f.connectionId), eq(toolCatalogEntries.toolName, "gmail__get_draft"))).returning(); + const rows = await savedRows(f.connectionId); + const removed = rows.filter((row) => row.quarantineReason === "mcp_tool_removed"); + expect(names(removed)).toEqual(["gmail__list_threads", "gmail__search_messages"]); + 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: [], + 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)); + const after = await byName(f.connectionId); + 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); + expect(offered).toContain("gmail__get_message"); + expect(offered).not.toContain("gmail__list_threads"); + expect(offered).not.toContain("gmail__search_messages"); + expect(offered).not.toContain("gmail__get_draft"); + }); + it("keeps an action that a switch before this fix held for review held, because the hold replaced the saved REST definition", async () => { const f = await savedOnRest(); await expect(f.call("gmail__get_message", { messageId: "msg1" })).resolves.toMatchObject({ status: "completed" }); diff --git a/server/src/services/tool-access.ts b/server/src/services/tool-access.ts index ebd7cf72b7a..0470e962756 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 { isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared"; +import { isCatalogEntryBlocked, isCatalogEntryRemovedByProvider, isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared"; import { githubBotRequest } from "./chat-github-client.js"; import { syncConnectionCredentialBindings } from "./connection-credential-bindings.js"; @@ -3406,11 +3406,11 @@ export function toolAccessService( connection.id, input.askFirstCatalogEntryIds, ); - if (enabledRows.some((entry) => entry.status === "disabled" && !(entry.quarantineReason === "mcp_tool_removed" && isRemoteMcpConnectorMethod(connection.config.sourceTemplateKey, connection.config.connectionMethodKey)))) { + if (enabledRows.some(isCatalogEntryBlocked)) { throw badRequest("Disabled actions cannot be enabled"); } if (reviewedIds.length > 0) { - await assertCatalogEntriesForConnection( + const reviewedRows = await assertCatalogEntriesForConnection( companyId, connection.id, reviewedIds, @@ -3425,7 +3425,7 @@ export function toolAccessService( eq(toolCatalogEntries.status, "quarantined"), ), ); - const reviewedIdSet = new Set(reviewedIds); + const reviewedIdSet = new Set(reviewedRows.filter((entry) => !isCatalogEntryRemovedByProvider(entry)).map((entry) => entry.id)); if ( quarantinedRows.length !== reviewedIdSet.size || quarantinedRows.some((entry) => !reviewedIdSet.has(entry.id)) @@ -3720,6 +3720,7 @@ export function toolAccessService( inArray(toolCatalogEntries.id, enabledIds), ne(toolCatalogEntries.status, "quarantined"), ne(toolCatalogEntries.status, "disabled"), + ne(toolCatalogEntries.status, "removed"), ), ); } diff --git a/tests/runner-e2e/source-sdk-public-inputs.json b/tests/runner-e2e/source-sdk-public-inputs.json index 8d493c7e7bf..d51364e61b7 100644 --- a/tests/runner-e2e/source-sdk-public-inputs.json +++ b/tests/runner-e2e/source-sdk-public-inputs.json @@ -278,7 +278,7 @@ "packages/shared/src/home-paths.ts": "fa4f1c440653b76b3dbb7a51026d557287deb45d", "packages/shared/src/humanize-connection.test.ts": "ba9e90a44c63452b0bf9f733adb90241921e8eca", "packages/shared/src/humanize-connection.ts": "fbd414476a0ef7106a7c80d8572208781b091699", - "packages/shared/src/index.ts": "09acd480368d0cc4f0220b8b71296ca97e61dbbf", + "packages/shared/src/index.ts": "87b4a095d07b2eace7cc8dfc44d6db9631012dd6", "packages/shared/src/issue-activity-identity.test.ts": "3dc294640f54c2be67943e3e0149cf4eff2c95ca", "packages/shared/src/issue-activity-identity.ts": "1b0f07ec3fe0506aaaf80a4fa9893f3baf8f0075", "packages/shared/src/issue-attribution.test.ts": "84563685e7f3c48029243cf1cc24a3a09ee3cde9", @@ -395,6 +395,8 @@ "packages/shared/src/telemetry/state.ts": "a060af26a8e9a82a8afb0e2fccbfe6a59e9d0a8a", "packages/shared/src/telemetry/types.ts": "083d4341a72ac47bc1a3bbff163965de8b06f90a", "packages/shared/src/tool-call-progress.ts": "89655767bfd0180e99ac517754d72e25f753eb33", + "packages/shared/src/tool-catalog-availability.test.ts": "74d3374cee09a3bda2923559c5417c265f4109dc", + "packages/shared/src/tool-catalog-availability.ts": "eaaadc571761a523c743044494171c5d76fa52cd", "packages/shared/src/trust-policy.ts": "88001a8d629a2af66dcd8d75b7b4d36298455ef6", "packages/shared/src/types/access.ts": "015d51792da07acaebe59e0e395698d207811d0d", "packages/shared/src/types/activity.ts": "6f5f27417a0bb26ec4daba6ec16a04ce31e484f2", diff --git a/ui/src/features/connections/ConnectionSetupFlow.tsx b/ui/src/features/connections/ConnectionSetupFlow.tsx index 1b6de4cd3c9..5062350a23e 100644 --- a/ui/src/features/connections/ConnectionSetupFlow.tsx +++ b/ui/src/features/connections/ConnectionSetupFlow.tsx @@ -104,6 +104,7 @@ import { vercelConnectSourceHref, } from "@/pages/apps/app-connect-policy"; import { parseGoogleSheetIds } from "@/pages/apps/google-sheets"; +import { withoutUnavailableActions } from "@/pages/apps/app-detail/action-permissions"; import { connectionNameForGrantKind } from "@/pages/apps/connection-identity"; import { defaultGenericMcpName, @@ -2068,12 +2069,13 @@ function StandardConnectionSetupFlow({ const finishMutation = useMutation({ mutationFn: async (input: { result: ConnectToolAppResult; enabled: Record }) => { const { result: connected, enabled: enabledMap } = input; - const enabledIds = Object.entries(enabledMap) - .filter(([, on]) => on) - .map(([id]) => id); + const enabledIds = withoutUnavailableActions( + Object.entries(enabledMap).filter(([, on]) => on).map(([id]) => id), + connected.catalog, + ); const askFirstIds = askFirstCatalogEntryIdsFor( connected, - (catalogEntryId) => Boolean(enabledMap[catalogEntryId]), + (catalogEntryId) => enabledIds.includes(catalogEntryId), ); if (requiredAgentId && installChoice !== "all" && !installAgentIds.has(requiredAgentId)) { throw new Error("Include the requesting agent or choose all agents to continue this task."); diff --git a/ui/src/pages/apps/AppDetail.tsx b/ui/src/pages/apps/AppDetail.tsx index de5fe154e3c..6ffffad9995 100644 --- a/ui/src/pages/apps/AppDetail.tsx +++ b/ui/src/pages/apps/AppDetail.tsx @@ -304,6 +304,7 @@ function StandardAppDetail({ renderActions, renderAgentSettings, renderConnectio companyId: selectedCompanyId, connectionId, connection, + catalog, onError: error => pushToast({ title: "Couldn't save that", body: describeError(error).body, tone: "error" }), }); const profile = useMemo( diff --git a/ui/src/pages/apps/app-detail/action-permissions.test.ts b/ui/src/pages/apps/app-detail/action-permissions.test.ts index 8e24bc9bec6..8965d277b5e 100644 --- a/ui/src/pages/apps/app-detail/action-permissions.test.ts +++ b/ui/src/pages/apps/app-detail/action-permissions.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; import { actionPermissionMutation } from "./action-permissions"; +import { finishAppPermissions } from "./useConnectionPermissionSave"; describe("actionPermissionMutation", () => { const enabled = new Set(["a", "b", "c"]); @@ -27,3 +28,26 @@ describe("actionPermissionMutation", () => { }); }); }); + +describe("finishAppPermissions", () => { + it("never asks to turn on an action Google no longer offers or a disabled action", () => { + const catalog = [ + { id: "get-message", status: "quarantined" as const }, + { id: "create-draft", status: "quarantined" as const }, + { id: "list-threads", status: "disabled" as const }, + { id: "search-messages", status: "disabled" as const }, + { id: "retired", status: "removed" as const }, + ]; + expect(finishAppPermissions({ + enabled: new Set(["get-message", "create-draft", "list-threads", "search-messages", "retired"]), + askFirst: new Set(["create-draft", "list-threads"]), + reviewed: new Set(["get-message", "create-draft"]), + access: { mode: "all", agentIds: new Set() }, + }, catalog)).toEqual({ + enabledCatalogEntryIds: ["get-message", "create-draft"], + askFirstCatalogEntryIds: ["create-draft"], + reviewedCatalogEntryIds: ["get-message", "create-draft"], + access: "all_agents", + }); + }); +}); diff --git a/ui/src/pages/apps/app-detail/action-permissions.ts b/ui/src/pages/apps/app-detail/action-permissions.ts index f18bf4ecd7b..d90f458fb07 100644 --- a/ui/src/pages/apps/app-detail/action-permissions.ts +++ b/ui/src/pages/apps/app-detail/action-permissions.ts @@ -1,4 +1,12 @@ -import { googleWorkspaceToolTitle } from "@paperclipai/shared"; +import { googleWorkspaceToolTitle, isCatalogEntryOffered, type ToolCatalogEntry } from "@paperclipai/shared"; + +export function withoutUnavailableActions( + ids: Iterable, + catalog: ReadonlyArray>, +): string[] { + const unavailable = new Set(catalog.filter((entry) => !isCatalogEntryOffered(entry)).map((entry) => entry.id)); + return [...ids].filter((id) => !unavailable.has(id)); +} /** * Apply one permission to a set of actions in a single change. diff --git a/ui/src/pages/apps/app-detail/useConnectionPermissionSave.ts b/ui/src/pages/apps/app-detail/useConnectionPermissionSave.ts index 3be56af6c78..a54479c9b9c 100644 --- a/ui/src/pages/apps/app-detail/useConnectionPermissionSave.ts +++ b/ui/src/pages/apps/app-detail/useConnectionPermissionSave.ts @@ -1,21 +1,33 @@ import { useState } from "react"; import { useMutation, useQueryClient } from "@tanstack/react-query"; -import type { ToolConnection, ToolPolicy, ToolProfileConfiguration } from "@paperclipai/shared"; +import type { ToolCatalogEntry, ToolConnection, ToolPolicy, ToolProfileConfiguration } from "@paperclipai/shared"; import { toolsApi } from "@/api/tools"; import { queryKeys } from "@/lib/queryKeys"; +import { withoutUnavailableActions } from "./action-permissions"; import type { AccessDraft } from "./types"; -interface PermissionDraft { +export interface PermissionDraft { enabled: Set; askFirst: Set; access: AccessDraft; reviewed?: Set; } -export function useConnectionPermissionSave({ companyId, connectionId, connection, onError }: { +export function finishAppPermissions(next: PermissionDraft, catalog: ReadonlyArray>) { + const enabled = new Set(withoutUnavailableActions(next.enabled, catalog)); + return { + enabledCatalogEntryIds: [...enabled], + askFirstCatalogEntryIds: [...next.askFirst].filter(id => enabled.has(id)), + ...(next.reviewed ? { reviewedCatalogEntryIds: [...next.reviewed] } : {}), + access: next.access.mode === "all" ? "all_agents" as const : { agentIds: [...next.access.agentIds] }, + }; +} + +export function useConnectionPermissionSave({ companyId, connectionId, connection, catalog, onError }: { companyId: string | null; connectionId: string; connection: ToolConnection | undefined; + catalog: ReadonlyArray>; onError: (error: Error) => void; }) { const client = useQueryClient(); @@ -25,12 +37,7 @@ export function useConnectionPermissionSave({ companyId, connectionId, connectio ? toolsApi.putConnectionInstalls(connectionId, next.access.mode === "all" ? [{ targetType: "company", targetId: companyId! }] : [...next.access.agentIds].map(targetId => ({ targetType: "agent" as const, targetId }))) - : toolsApi.finishApp(companyId!, connectionId, { - enabledCatalogEntryIds: [...next.enabled], - askFirstCatalogEntryIds: [...next.askFirst].filter(id => next.enabled.has(id)), - ...(next.reviewed ? { reviewedCatalogEntryIds: [...next.reviewed] } : {}), - access: next.access.mode === "all" ? "all_agents" : { agentIds: [...next.access.agentIds] }, - }), + : toolsApi.finishApp(companyId!, connectionId, finishAppPermissions(next, catalog)), onMutate: async (next) => { setDraft(next); await Promise.all([ diff --git a/ui/src/pages/tools/PasteConfigTab.tsx b/ui/src/pages/tools/PasteConfigTab.tsx index 037dcef9a13..2f71c90daba 100644 --- a/ui/src/pages/tools/PasteConfigTab.tsx +++ b/ui/src/pages/tools/PasteConfigTab.tsx @@ -23,6 +23,7 @@ import { type OAuthConnectPhase, } from "@/features/connections/ConnectionSetupFlow"; import { endpointHost } from "@/pages/apps/generic-mcp-connect"; +import { withoutUnavailableActions } from "@/pages/apps/app-detail/action-permissions"; import { McpConfigHelpDialog } from "./McpConfigHelpDialog"; import { describeError } from "@/api/errors"; @@ -232,9 +233,12 @@ export function PasteConfigTab({ companyId }: { companyId: string }) { const finishMutation = useMutation({ mutationFn: () => { const askFirstLevels = connectResult ? askFirstLevelsFrom(connectResult) : []; - const enabledIds = Object.entries(enabled).filter(([, on]) => on).map(([id]) => id); + const enabledIds = withoutUnavailableActions( + Object.entries(enabled).filter(([, on]) => on).map(([id]) => id), + connectResult?.catalog ?? [], + ); const askFirstIds = (connectResult?.actions.canMakeChanges ?? []) - .filter((action) => enabled[action.catalogEntryId] && askFirstLevels.includes(action.riskLevel)) + .filter((action) => enabledIds.includes(action.catalogEntryId) && askFirstLevels.includes(action.riskLevel)) .map((action) => action.catalogEntryId); return toolsApi.finishApp(companyId, connectResult!.connectionId, { enabledCatalogEntryIds: enabledIds,