From d1b0f0f44afce84eeda45e600b62725636e2ba0f Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 21 Nov 2026 17:39:46 +0000 Subject: [PATCH] feat: Expose document verification through MCP, drop cloud-hosting gate - verify_document MCP tool to verify or unverify a document, available to tokens whose scopes cover documents.verify - verificationInterval parameter on the update_document and update_collection MCP tools, gated on the same admin access as the REST API - the verify/unverify abilities are now gated on the plan entitlement alone, no longer additionally on cloud hosting, so entitled self-hosted licenses can use the feature - verification state changes no longer advance revisionCount, which is used for edit conflict detection, so verifying cannot spuriously reject a concurrent editor's update Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01Mb5McXaNbEDyyCZXPAdGAY --- server/mcp/tools/collections.test.ts | 50 ++++++ server/mcp/tools/collections.ts | 20 +++ server/mcp/tools/documents.test.ts | 159 ++++++++++++++++++ server/mcp/tools/documents.ts | 99 ++++++++++- server/models/Document.ts | 19 ++- server/policies/document.ts | 2 - server/routes/api/documents/documents.test.ts | 20 --- 7 files changed, 338 insertions(+), 31 deletions(-) diff --git a/server/mcp/tools/collections.test.ts b/server/mcp/tools/collections.test.ts index 9d2cfbd37a..7992c300aa 100644 --- a/server/mcp/tools/collections.test.ts +++ b/server/mcp/tools/collections.test.ts @@ -293,3 +293,53 @@ describe("delete_collection", () => { } ); }); + +describe("update_collection verificationInterval", () => { + it("sets and clears the cadence as an admin", async () => { + const { user, accessToken } = await buildOAuthUser({ role: "admin" }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + + const res = await callMcpTool(server, accessToken, "update_collection", { + id: collection.id, + verificationInterval: 90, + }); + expect(res?.result?.isError).toBeUndefined(); + + await collection.reload(); + expect(collection.verificationInterval).toEqual(90); + + const cleared = await callMcpTool( + server, + accessToken, + "update_collection", + { + id: collection.id, + verificationInterval: null, + } + ); + expect(cleared?.result?.isError).toBeUndefined(); + + await collection.reload(); + expect(collection.verificationInterval).toEqual(null); + }); + + it("does not allow a member to set the cadence", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + + const res = await callMcpTool(server, accessToken, "update_collection", { + id: collection.id, + verificationInterval: 90, + }); + expect(res?.result?.isError).toBe(true); + + await collection.reload(); + expect(collection.verificationInterval).toEqual(null); + }); +}); diff --git a/server/mcp/tools/collections.ts b/server/mcp/tools/collections.ts index e2a2766e82..c8cead0ee4 100644 --- a/server/mcp/tools/collections.ts +++ b/server/mcp/tools/collections.ts @@ -2,6 +2,7 @@ import { z } from "zod"; import { Sequelize, Op, type WhereOptions } from "sequelize"; import type { McpServer } from "@modelcontextprotocol/server"; import { Collection, Team } from "@server/models"; +import { AuthorizationError } from "@server/errors"; import { buildWhere } from "@server/models/helpers/Filters"; import { sequelize } from "@server/storage/database"; import { authorize } from "@server/policies"; @@ -269,6 +270,16 @@ export function collectionTools(server: McpServer, scopes: string[]) { .describe( "The hex color for the collection icon. Set to null to remove." ), + verificationInterval: z + .number() + .int() + .min(1) + .max(3650) + .nullable() + .optional() + .describe( + "The number of days after which document verifications in this collection expire and documents must be re-verified. Requires workspace admin access. Set to null to clear and inherit the workspace default." + ), }), }, withTracing("update_collection", async (input, context) => { @@ -282,6 +293,12 @@ export function collectionTools(server: McpServer, scopes: string[]) { }); authorize(user, "update", collection); + // the verification cadence is policy for every document in the + // collection, so changing it additionally requires team admin access. + if (input.verificationInterval !== undefined && !user.isAdmin) { + throw AuthorizationError(); + } + if (input.name !== undefined) { collection.name = input.name.trim(); } @@ -294,6 +311,9 @@ export function collectionTools(server: McpServer, scopes: string[]) { if (input.color !== undefined) { collection.color = input.color; } + if (input.verificationInterval !== undefined) { + collection.verificationInterval = input.verificationInterval; + } // A write that changes nothing must fail loud rather than return a // success the caller would read as a completed write — the request diff --git a/server/mcp/tools/documents.test.ts b/server/mcp/tools/documents.test.ts index bcbf2284ee..5f43382f23 100644 --- a/server/mcp/tools/documents.test.ts +++ b/server/mcp/tools/documents.test.ts @@ -3,6 +3,7 @@ import { DeprecationValidation } from "@shared/validations"; import { CollectionPermission, DocumentPermission, + Plan, Scope, TeamPreference, } from "@shared/types"; @@ -22,6 +23,7 @@ import { Document, GroupMembership, SearchQuery, + Team, UserMembership, } from "@server/models"; import { SearchQuerySource } from "@server/models/SearchQuery"; @@ -1599,3 +1601,160 @@ describe("delete_document", () => { } ); }); + +describe("verify_document", () => { + beforeEach(() => { + vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Business); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("verifies a document as an admin", async () => { + const { user, accessToken } = await buildOAuthUser({ role: "admin" }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "verify_document", { + id: document.id, + }); + expect(res?.result?.isError).toBeUndefined(); + const data = JSON.parse(res?.result?.content?.[0]?.text ?? "{}"); + expect(data.verifiedAt).toBeTruthy(); + expect(data.verifiedById).toEqual(user.id); + + await document.reload(); + expect(document.verifiedAt).toBeTruthy(); + }); + + it("removes the verification with unverify", async () => { + const { user, accessToken } = await buildOAuthUser({ role: "admin" }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + verifiedAt: new Date(), + verifiedById: user.id, + }); + + const res = await callMcpTool(server, accessToken, "verify_document", { + id: document.id, + unverify: true, + }); + expect(res?.result?.isError).toBeUndefined(); + + await document.reload(); + expect(document.verifiedAt).toEqual(null); + expect(document.verifiedById).toEqual(null); + }); + + it("does not allow a member to verify", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "verify_document", { + id: document.id, + }); + expect(res?.result?.isError).toBe(true); + }); + + it("does not allow verifying without the plan entitlement", async () => { + vi.restoreAllMocks(); + + const { user, accessToken } = await buildOAuthUser({ role: "admin" }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "verify_document", { + id: document.id, + }); + expect(res?.result?.isError).toBe(true); + }); +}); + +describe("update_document verificationInterval", () => { + beforeEach(() => { + vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Business); + }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("sets the interval as an admin and recomputes the deadline", async () => { + const { user, accessToken } = await buildOAuthUser({ role: "admin" }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const verifiedAt = new Date(); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + verifiedAt, + verifiedById: user.id, + }); + + const res = await callMcpTool(server, accessToken, "update_document", { + id: document.id, + verificationInterval: 30, + }); + expect(res?.result?.isError).toBeUndefined(); + + await document.reload(); + expect(document.verificationInterval).toEqual(30); + expect(document.verificationExpiresAt).toEqual( + new Date(verifiedAt.getTime() + 30 * 24 * 60 * 60 * 1000) + ); + }); + + it("does not allow a member to set the interval", async () => { + const { user, accessToken } = await buildOAuthUser(); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "update_document", { + id: document.id, + verificationInterval: 30, + }); + expect(res?.result?.isError).toBe(true); + + await document.reload(); + expect(document.verificationInterval).toEqual(null); + }); +}); diff --git a/server/mcp/tools/documents.ts b/server/mcp/tools/documents.ts index bd142da4e2..ed9ca45c17 100644 --- a/server/mcp/tools/documents.ts +++ b/server/mcp/tools/documents.ts @@ -690,6 +690,16 @@ export function documentTools(server: McpServer, scopes: string[]) { .describe( "Whether the document should occupy full width of the screen." ), + verificationInterval: z + .number() + .int() + .min(1) + .max(3650) + .nullable() + .optional() + .describe( + "The number of days after which a verification of this document expires and it must be re-verified. Requires admin access to the document. Set to null to clear the override and inherit the collection or workspace default." + ), }), }, withTracing("update_document", async (input, context) => { @@ -753,6 +763,10 @@ export function documentTools(server: McpServer, scopes: string[]) { authorize(user, "update", document); + if (input.verificationInterval !== undefined) { + authorize(user, "verify", document); + } + // The collection is only used when publishing a draft that has // none, so the personal collection is only created in that case. let collectionId = @@ -772,7 +786,7 @@ export function documentTools(server: McpServer, scopes: string[]) { collectionId = collection?.id; } - const { revisionCount } = document; + const { revisionCount, verificationInterval } = document; const result = await documentUpdater(ctx, { document, @@ -780,12 +794,16 @@ export function documentTools(server: McpServer, scopes: string[]) { collectionId, }); - // Every save increments revisionCount, so an unchanged count means - // nothing was persisted. Fail loud rather than return a success - // the caller would read as a completed write — the request either - // carried no recognized fields or values identical to the current - // document. - return result.revisionCount === revisionCount ? null : result; + // Every content save increments revisionCount, so an unchanged + // count means nothing was persisted. Fail loud rather than return + // a success the caller would read as a completed write — the + // request either carried no recognized fields or values identical + // to the current document. Verification cadence changes are not + // content edits and are detected separately. + return result.revisionCount === revisionCount && + result.verificationInterval === verificationInterval + ? null + : result; }); if (!updated) { @@ -843,6 +861,73 @@ export function documentTools(server: McpServer, scopes: string[]) { ); } + if (AuthenticationHelper.canAccess("documents.verify", scopes)) { + server.registerTool( + "verify_document", + { + title: "Verify document", + description: + "Marks a document as verified, vouching that its content is accurate and up to date. When the workspace, collection, or document has a re-verification interval configured, the verification expires after that many days and the document is due to be reviewed again. Requires admin access to the document. Set unverify to true to remove an existing verification instead.", + annotations: { + destructiveHint: false, + idempotentHint: true, + openWorldHint: false, + readOnlyHint: false, + }, + inputSchema: z.object({ + id: z + .string() + .describe("The unique identifier of the document to verify."), + unverify: z + .boolean() + .optional() + .describe( + "Set to true to remove the current verification from the document instead of verifying it." + ), + }), + }, + withTracing("verify_document", async (input, context) => { + try { + const ctx = buildAPIContext(context); + const { user } = ctx.state.auth; + + const document = await sequelize.transaction(async (transaction) => { + ctx.state.transaction = transaction; + ctx.context.transaction = transaction; + + const doc = await Document.findByPk(input.id, { + userId: user.id, + rejectOnEmpty: true, + transaction, + }); + + if (input.unverify) { + authorize(user, "unverify", doc); + return doc.unverify(ctx); + } + + authorize(user, "verify", doc); + return doc.verify(ctx); + }); + + return success({ + success: true, + ...pathToUrl(user.team, { + id: document.id, + title: document.title, + url: document.url, + }), + verifiedAt: document.verifiedAt, + verifiedById: document.verifiedById, + verificationExpiresAt: document.verificationExpiresAt, + }); + } catch (message) { + return error(message); + } + }) + ); + } + if (AuthenticationHelper.canAccess("documents.delete", scopes)) { server.registerTool( "delete_document", diff --git a/server/models/Document.ts b/server/models/Document.ts index 755b28eea5..fd7dbf2274 100644 --- a/server/models/Document.ts +++ b/server/models/Document.ts @@ -636,8 +636,23 @@ class Document extends ArchivableModel< model.collaboratorIds.concat(model.lastModifiedById) ); - // increment revision - model.revisionCount += 1; + // increment revision, except for verification state changes, which are + // not content edits and must not advance the revision counter used for + // edit conflict detection. + const changed = model.changed() || []; + const verificationOnly = + changed.length > 0 && + changed.every((attr) => + [ + "verifiedAt", + "verifiedById", + "verificationExpiresAt", + "verificationInterval", + ].includes(attr) + ); + if (!verificationOnly) { + model.revisionCount += 1; + } } @BeforeUpdate diff --git a/server/policies/document.ts b/server/policies/document.ts index fc4830749d..12022e17a8 100644 --- a/server/policies/document.ts +++ b/server/policies/document.ts @@ -9,7 +9,6 @@ import { Document, Revision, User, Team } from "@server/models"; import { allow, cannot, can } from "./cancan"; import { and, - isCloudHosted, isCollectionOwner, isCollectionWritable, isCommentingEnabled, @@ -123,7 +122,6 @@ allow(User, "publish", Document, (actor, document) => allow(User, ["verify", "unverify"], Document, (actor, document) => and( - isCloudHosted(), teamHasEntitlement(actor.team, PlanFeature.DocumentVerification), isTeamMutable(actor), can(actor, "read", document), diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index c83c0b43c5..9754f0428d 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -55,7 +55,6 @@ import { import { getTestServer, readZipResponse, - setSelfHosted, withAPIContext, } from "@server/test/support"; @@ -9922,25 +9921,6 @@ describe("#documents.verify", () => { expect(res.status).toEqual(403); }); - it("should not allow verifying on self-hosted installations", async () => { - setSelfHosted(); - - const admin = await buildAdmin(); - const collection = await buildCollection({ - teamId: admin.teamId, - }); - const document = await buildDocument({ - teamId: admin.teamId, - collectionId: collection.id, - }); - const res = await server.post("/api/documents.verify", admin, { - body: { - id: document.id, - }, - }); - expect(res.status).toEqual(403); - }); - it("should not allow a member with write access to verify", async () => { const user = await buildUser(); const collection = await buildCollection({