From 0f2a1fae600153443b8ef3cc68cc91b2fa2f72e2 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 3 Nov 2026 10:23:17 -0500 Subject: [PATCH] fix: Allow adding yourself as a collection owner, include collection owners in MCP exact match --- server/commands/collectionOwners.ts | 7 +---- server/mcp/tools/documents.test.ts | 26 +++++++++++++++++++ server/mcp/tools/documents.ts | 5 +++- .../api/collections/collections.test.ts | 15 +++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/server/commands/collectionOwners.ts b/server/commands/collectionOwners.ts index a626a10959..37b8acfda2 100644 --- a/server/commands/collectionOwners.ts +++ b/server/commands/collectionOwners.ts @@ -105,8 +105,7 @@ export async function updateCollectionOwners( /** * Gives users the manage permission on a collection when they are made - * owners. The acting user is skipped, as they can already manage the - * collection. + * owners. * * @param ctx the API context containing the acting user. * @param collection the collection that the users own. @@ -122,10 +121,6 @@ export async function grantCollectionOwnerAccess( const { transaction } = ctx.state; for (const userId of userIds) { - if (userId === user.id) { - continue; - } - const membership = await UserMembership.findOneForUpdate( { userId, collectionId: collection.id }, { transaction } diff --git a/server/mcp/tools/documents.test.ts b/server/mcp/tools/documents.test.ts index e98dd999c2..54a856b491 100644 --- a/server/mcp/tools/documents.test.ts +++ b/server/mcp/tools/documents.test.ts @@ -178,6 +178,32 @@ describe("list_documents", () => { expect(data[0].document.id).toEqual(document.id); }); + it("returns an exact match owned through its collection", async () => { + const { user, accessToken } = await buildOAuthUser(); + const owner = await buildUser({ teamId: user.teamId }); + const collection = await buildCollection({ + teamId: user.teamId, + userId: user.id, + ownerIds: [owner.id], + }); + const document = await buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection.id, + }); + + const res = await callMcpTool(server, accessToken, "list_documents", { + query: document.id, + ownerId: owner.id, + }); + + expect(res?.result?.isError).toBeUndefined(); + const data = (res?.result?.content ?? []).map((c: { text: string }) => + JSON.parse(c.text) + ); + expect(data[0].document.id).toEqual(document.id); + }); + it("returns documents shared directly with the user", async () => { const { user, accessToken } = await buildOAuthUser(); const author = await buildUser({ teamId: user.teamId }); diff --git a/server/mcp/tools/documents.ts b/server/mcp/tools/documents.ts index a045a7e36f..151191748a 100644 --- a/server/mcp/tools/documents.ts +++ b/server/mcp/tools/documents.ts @@ -189,7 +189,10 @@ export function documentTools(server: McpServer, scopes: string[]) { (!collectionId || exactMatchCandidate.collectionId === collectionId) && (!input.ownerId || - exactMatchCandidate.ownerIds?.includes(input.ownerId)) + exactMatchCandidate.ownerIds?.includes(input.ownerId) || + exactMatchCandidate.collection?.ownerIds?.includes( + input.ownerId + )) ? exactMatchCandidate : null; diff --git a/server/routes/api/collections/collections.test.ts b/server/routes/api/collections/collections.test.ts index 4072bdb051..cca28c0ea2 100644 --- a/server/routes/api/collections/collections.test.ts +++ b/server/routes/api/collections/collections.test.ts @@ -3156,6 +3156,21 @@ describe("collection owners", () => { expect(body.data.collections[0].ownerIds).toEqual([owner.id]); }); + it("should allow a user to add themselves as an owner", async () => { + const admin = await buildAdmin(); + const collection = await buildCollection({ teamId: admin.teamId }); + + const res = await server.post("/api/collections.add_user", admin, { + body: { id: collection.id, userId: admin.id, owner: true }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.memberships[0].permission).toEqual( + CollectionPermission.Admin + ); + expect(body.data.collections[0].ownerIds).toEqual([admin.id]); + }); + it("should add owners from concurrent invitations", async () => { const admin = await buildAdmin(); const first = await buildUser({ teamId: admin.teamId });