@payloadcms/plugin-mcp 4.0.0-internal.e7d4ebc → 4.0.0-internal.ee5d8bd
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +0 -7
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +17 -5
- package/dist/endpoint/access.js.map +1 -1
- package/dist/endpoint/index.d.ts.map +1 -1
- package/dist/endpoint/index.js +46 -4
- package/dist/endpoint/index.js.map +1 -1
- package/dist/endpoint/oauth.d.ts +39 -0
- package/dist/endpoint/oauth.d.ts.map +1 -0
- package/dist/endpoint/oauth.js +94 -0
- package/dist/endpoint/oauth.js.map +1 -0
- package/dist/endpoint/oauthLoginToken.d.ts +15 -0
- package/dist/endpoint/oauthLoginToken.d.ts.map +1 -0
- package/dist/endpoint/oauthLoginToken.js +45 -0
- package/dist/endpoint/oauthLoginToken.js.map +1 -0
- package/dist/endpoint/oauthServer.d.ts +12 -0
- package/dist/endpoint/oauthServer.d.ts.map +1 -0
- package/dist/endpoint/oauthServer.js +386 -0
- package/dist/endpoint/oauthServer.js.map +1 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +2 -2
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +36 -24
- package/dist/mcp/buildMcpServer.js.map +1 -1
- package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/authTools.js +80 -56
- package/dist/mcp/builtin/collections/authTools.js.map +1 -1
- package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/countTool.js +9 -14
- package/dist/mcp/builtin/collections/countTool.js.map +1 -1
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/countVersionsTool.js +9 -13
- package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +103 -62
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js +44 -43
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.js +31 -37
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/fileInput.d.ts +21 -21
- package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/fileInput.js +43 -72
- package/dist/mcp/builtin/collections/fileInput.js.map +1 -1
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findDistinctTool.js +11 -26
- package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +28 -46
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.js +17 -26
- package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findVersionsTool.js +16 -30
- package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +30 -14
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/restoreVersionTool.js +14 -26
- package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +56 -88
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js +13 -13
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -1
- package/dist/mcp/builtin/formatEntityError.d.ts +16 -0
- package/dist/mcp/builtin/formatEntityError.d.ts.map +1 -0
- package/dist/mcp/builtin/formatEntityError.js +55 -0
- package/dist/mcp/builtin/formatEntityError.js.map +1 -0
- package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -1
- package/dist/mcp/builtin/getConfigInfoTool.js +3 -2
- package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -1
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/countVersionsTool.js +9 -13
- package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findTool.js +10 -16
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.js +13 -24
- package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findVersionsTool.js +13 -29
- package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +29 -13
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/globals/restoreVersionTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/restoreVersionTool.js +15 -22
- package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js +27 -36
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +3 -3
- package/dist/mcp/builtinTools.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +3 -0
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/stdio.js +4 -3
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +95 -7
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/toStandardSchema.js.map +1 -1
- package/package.json +23 -28
- package/src/endpoint/access.ts +14 -6
- package/src/endpoint/index.ts +41 -3
- package/src/endpoint/oauth.ts +92 -0
- package/src/endpoint/oauthLoginToken.ts +59 -0
- package/src/endpoint/oauthServer.ts +440 -0
- package/src/index.ts +40 -5
- package/src/mcp/buildMcpServer.ts +40 -30
- package/src/mcp/builtin/collections/authTools.ts +82 -71
- package/src/mcp/builtin/collections/countTool.ts +9 -21
- package/src/mcp/builtin/collections/countVersionsTool.ts +9 -18
- package/src/mcp/builtin/collections/createTool.ts +107 -85
- package/src/mcp/builtin/collections/deleteTool.ts +37 -65
- package/src/mcp/builtin/collections/duplicateTool.ts +36 -86
- package/src/mcp/builtin/collections/fileInput.spec.ts +2 -2
- package/src/mcp/builtin/collections/fileInput.ts +54 -81
- package/src/mcp/builtin/collections/findDistinctTool.ts +11 -63
- package/src/mcp/builtin/collections/findTool.ts +24 -106
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +15 -58
- package/src/mcp/builtin/collections/findVersionsTool.ts +14 -85
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +23 -12
- package/src/mcp/builtin/collections/restoreVersionTool.ts +15 -57
- package/src/mcp/builtin/collections/updateTool.ts +56 -110
- package/src/mcp/builtin/collections/uploadInstructionsTool.ts +15 -15
- package/src/mcp/builtin/formatEntityError.ts +77 -0
- package/src/mcp/builtin/getConfigInfoTool.ts +2 -1
- package/src/mcp/builtin/globals/countVersionsTool.ts +9 -17
- package/src/mcp/builtin/globals/findTool.ts +13 -52
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +13 -52
- package/src/mcp/builtin/globals/findVersionsTool.ts +14 -92
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +21 -11
- package/src/mcp/builtin/globals/restoreVersionTool.ts +14 -45
- package/src/mcp/builtin/globals/updateTool.ts +38 -61
- package/src/mcp/builtinTools.ts +2 -2
- package/src/mcp/sanitizeMCPConfig.ts +1 -0
- package/src/stdio.ts +2 -2
- package/src/types.ts +97 -7
- package/src/utils/toStandardSchema.ts +1 -1
- package/dist/mcp/builtin/collections/fileInput.spec.js +0 -86
- package/dist/mcp/builtin/collections/fileInput.spec.js.map +0 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +0 -9
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +0 -1
- package/dist/mcp/builtin/collections/formatCollectionError.js +0 -60
- package/dist/mcp/builtin/collections/formatCollectionError.js.map +0 -1
- package/dist/mcp/builtin/validateEntityData.d.ts +0 -14
- package/dist/mcp/builtin/validateEntityData.d.ts.map +0 -1
- package/dist/mcp/builtin/validateEntityData.js +0 -82
- package/dist/mcp/builtin/validateEntityData.js.map +0 -1
- package/dist/utils/getVirtualFieldNames.d.ts +0 -14
- package/dist/utils/getVirtualFieldNames.d.ts.map +0 -1
- package/dist/utils/getVirtualFieldNames.js +0 -35
- package/dist/utils/getVirtualFieldNames.js.map +0 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts +0 -28
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +0 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.js +0 -71
- package/dist/utils/schemaConversion/filterFieldsByAccess.js.map +0 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +0 -13
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.js +0 -66
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +0 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +0 -10
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +0 -384
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +0 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +0 -133
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +0 -1
- package/dist/utils/transformPointDataToPayload.d.ts +0 -7
- package/dist/utils/transformPointDataToPayload.d.ts.map +0 -1
- package/dist/utils/transformPointDataToPayload.js +0 -28
- package/dist/utils/transformPointDataToPayload.js.map +0 -1
- package/dist/utils/whereSchema.d.ts +0 -9
- package/dist/utils/whereSchema.d.ts.map +0 -1
- package/dist/utils/whereSchema.js +0 -13
- package/dist/utils/whereSchema.js.map +0 -1
- package/src/mcp/builtin/collections/formatCollectionError.ts +0 -84
- package/src/mcp/builtin/validateEntityData.ts +0 -132
- package/src/utils/getVirtualFieldNames.ts +0 -53
- package/src/utils/schemaConversion/filterFieldsByAccess.ts +0 -123
- package/src/utils/schemaConversion/getEntityInputSchema.ts +0 -96
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +0 -96
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +0 -432
- package/src/utils/transformPointDataToPayload.ts +0 -40
- package/src/utils/whereSchema.ts +0 -24
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { countDocumentsInputSchema } from 'payload';
|
|
2
2
|
import { defaultAccess } from '../../../defaultAccess.js';
|
|
3
3
|
import { defineCollectionTool } from '../../../defineTool.js';
|
|
4
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
5
|
-
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
6
5
|
const DEFAULT_DESCRIPTION = 'Count documents in any collection by passing the collection slug and optional where clause.';
|
|
7
6
|
export const countDocumentsTool = defineCollectionTool({
|
|
8
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.
|
|
7
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.read),
|
|
9
8
|
annotations: {
|
|
10
9
|
destructiveHint: false,
|
|
11
10
|
idempotentHint: true,
|
|
@@ -14,21 +13,17 @@ export const countDocumentsTool = defineCollectionTool({
|
|
|
14
13
|
title: 'Count Documents'
|
|
15
14
|
},
|
|
16
15
|
description: DEFAULT_DESCRIPTION,
|
|
17
|
-
input:
|
|
18
|
-
|
|
19
|
-
trash: z.boolean().describe('Optional: include soft-deleted documents when trash is enabled on the collection').optional(),
|
|
20
|
-
where: whereSchema.describe('Optional: where clause for filtering. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}').optional()
|
|
21
|
-
})
|
|
22
|
-
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
16
|
+
input: countDocumentsInputSchema
|
|
17
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
23
18
|
const payload = req.payload;
|
|
24
19
|
const logger = getLogger({
|
|
25
20
|
payload
|
|
26
21
|
});
|
|
27
22
|
const { locale, trash, where } = input;
|
|
28
|
-
logger.info(`Counting documents in collection: ${
|
|
23
|
+
logger.info(`Counting documents in collection: ${slug}`);
|
|
29
24
|
try {
|
|
30
25
|
const result = await payload.count({
|
|
31
|
-
collection:
|
|
26
|
+
collection: slug,
|
|
32
27
|
overrideAccess: authorizedMCP.overrideAccess,
|
|
33
28
|
req,
|
|
34
29
|
...locale ? {
|
|
@@ -45,19 +40,19 @@ export const countDocumentsTool = defineCollectionTool({
|
|
|
45
40
|
content: [
|
|
46
41
|
{
|
|
47
42
|
type: 'text',
|
|
48
|
-
text: `Collection "${
|
|
43
|
+
text: `Collection "${slug}" contains ${result.totalDocs} matching documents.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
49
44
|
}
|
|
50
45
|
],
|
|
51
46
|
doc: result
|
|
52
47
|
};
|
|
53
48
|
} catch (error) {
|
|
54
49
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
55
|
-
logger.error(`Error counting documents in ${
|
|
50
|
+
logger.error(`Error counting documents in ${slug}: ${errorMessage}`);
|
|
56
51
|
return {
|
|
57
52
|
content: [
|
|
58
53
|
{
|
|
59
54
|
type: 'text',
|
|
60
|
-
text: `❌ **Error counting documents in collection "${
|
|
55
|
+
text: `❌ **Error counting documents in collection "${slug}":** ${errorMessage}`
|
|
61
56
|
}
|
|
62
57
|
],
|
|
63
58
|
isError: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/countTool.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/countTool.ts"],"sourcesContent":["import { countDocumentsInputSchema } from 'payload'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Count documents in any collection by passing the collection slug and optional where clause.'\n\nexport const countDocumentsTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.read),\n annotations: {\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n readOnlyHint: true,\n title: 'Count Documents',\n },\n description: DEFAULT_DESCRIPTION,\n input: countDocumentsInputSchema,\n}).handler(async ({ slug, authorizedMCP, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n const { locale, trash, where } = input\n\n logger.info(`Counting documents in collection: ${slug}`)\n\n try {\n const result = await payload.count({\n collection: slug,\n overrideAccess: authorizedMCP.overrideAccess,\n req,\n ...(locale ? { locale } : {}),\n ...(trash !== undefined ? { trash } : {}),\n ...(where ? { where } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Collection \"${slug}\" contains ${result.totalDocs} matching documents.\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error counting documents in ${slug}: ${errorMessage}`)\n return {\n content: [\n {\n type: 'text',\n text: `❌ **Error counting documents in collection \"${slug}\":** ${errorMessage}`,\n },\n ],\n isError: true,\n }\n }\n})\n"],"names":["countDocumentsInputSchema","defaultAccess","defineCollectionTool","getLogger","DEFAULT_DESCRIPTION","countDocumentsTool","access","args","Boolean","permissions","collections","slug","read","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","handler","authorizedMCP","req","payload","logger","locale","trash","where","info","result","count","collection","overrideAccess","undefined","content","type","text","totalDocs","JSON","stringify","doc","error","errorMessage","Error","message","isError"],"mappings":"AAAA,SAASA,yBAAyB,QAAQ,UAAS;AAEnD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AAEvD,MAAMC,sBACJ;AAEF,OAAO,MAAMC,qBAAqBH,qBAAqB;IACrDI,QAAQ,CAACC,OACPN,cAAcM,SAASC,QAAQD,KAAKE,WAAW,EAAEC,aAAa,CAACH,KAAKI,IAAI,CAAC,EAAEC;IAC7EC,aAAa;QACXC,iBAAiB;QACjBC,gBAAgB;QAChBC,eAAe;QACfC,cAAc;QACdC,OAAO;IACT;IACAC,aAAaf;IACbgB,OAAOpB;AACT,GAAGqB,OAAO,CAAC,OAAO,EAAEV,IAAI,EAAEW,aAAa,EAAEF,KAAK,EAAEG,GAAG,EAAE;IACnD,MAAMC,UAAUD,IAAIC,OAAO;IAC3B,MAAMC,SAAStB,UAAU;QAAEqB;IAAQ;IACnC,MAAM,EAAEE,MAAM,EAAEC,KAAK,EAAEC,KAAK,EAAE,GAAGR;IAEjCK,OAAOI,IAAI,CAAC,CAAC,kCAAkC,EAAElB,MAAM;IAEvD,IAAI;QACF,MAAMmB,SAAS,MAAMN,QAAQO,KAAK,CAAC;YACjCC,YAAYrB;YACZsB,gBAAgBX,cAAcW,cAAc;YAC5CV;YACA,GAAIG,SAAS;gBAAEA;YAAO,IAAI,CAAC,CAAC;YAC5B,GAAIC,UAAUO,YAAY;gBAAEP;YAAM,IAAI,CAAC,CAAC;YACxC,GAAIC,QAAQ;gBAAEA;YAAM,IAAI,CAAC,CAAC;QAC5B;QAEA,OAAO;YACLO,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,YAAY,EAAE1B,KAAK,WAAW,EAAEmB,OAAOQ,SAAS,CAAC,kCAAkC,EAAEC,KAAKC,SAAS,CAACV,QAAQ,QAAQ,CAAC;gBAC9H;aACD;YACDW,KAAKX;QACP;IACF,EAAE,OAAOY,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9DpB,OAAOiB,KAAK,CAAC,CAAC,4BAA4B,EAAE/B,KAAK,EAAE,EAAEgC,cAAc;QACnE,OAAO;YACLR,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,4CAA4C,EAAE1B,KAAK,KAAK,EAAEgC,cAAc;gBACjF;aACD;YACDG,SAAS;QACX;IACF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"countVersionsTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/countVersionsTool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"countVersionsTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/countVersionsTool.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,iBAAiB,4CAkD5B,CAAA"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { countVersionsInputSchema } from 'payload';
|
|
2
2
|
import { defaultAccess } from '../../../defaultAccess.js';
|
|
3
3
|
import { defineCollectionTool } from '../../../defineTool.js';
|
|
4
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
5
|
-
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
6
5
|
const DEFAULT_DESCRIPTION = 'Count document versions in any version-enabled collection by passing the collection slug and optional where clause.';
|
|
7
6
|
export const countVersionsTool = defineCollectionTool({
|
|
8
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.
|
|
7
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.readVersions),
|
|
9
8
|
annotations: {
|
|
10
9
|
destructiveHint: false,
|
|
11
10
|
idempotentHint: true,
|
|
@@ -14,20 +13,17 @@ export const countVersionsTool = defineCollectionTool({
|
|
|
14
13
|
title: 'Count Versions'
|
|
15
14
|
},
|
|
16
15
|
description: DEFAULT_DESCRIPTION,
|
|
17
|
-
input:
|
|
18
|
-
|
|
19
|
-
where: whereSchema.describe('Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.title":{"contains":"test"}}').optional()
|
|
20
|
-
})
|
|
21
|
-
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
16
|
+
input: countVersionsInputSchema
|
|
17
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
22
18
|
const payload = req.payload;
|
|
23
19
|
const logger = getLogger({
|
|
24
20
|
payload
|
|
25
21
|
});
|
|
26
22
|
const { locale, where } = input;
|
|
27
|
-
logger.info(`Counting versions in collection: ${
|
|
23
|
+
logger.info(`Counting versions in collection: ${slug}`);
|
|
28
24
|
try {
|
|
29
25
|
const result = await payload.countVersions({
|
|
30
|
-
collection:
|
|
26
|
+
collection: slug,
|
|
31
27
|
overrideAccess: authorizedMCP.overrideAccess,
|
|
32
28
|
req,
|
|
33
29
|
...locale ? {
|
|
@@ -41,19 +37,19 @@ export const countVersionsTool = defineCollectionTool({
|
|
|
41
37
|
content: [
|
|
42
38
|
{
|
|
43
39
|
type: 'text',
|
|
44
|
-
text: `Collection "${
|
|
40
|
+
text: `Collection "${slug}" contains ${result.totalDocs} matching versions.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
45
41
|
}
|
|
46
42
|
],
|
|
47
43
|
doc: result
|
|
48
44
|
};
|
|
49
45
|
} catch (error) {
|
|
50
46
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
51
|
-
logger.error(`Error counting versions in ${
|
|
47
|
+
logger.error(`Error counting versions in ${slug}: ${errorMessage}`);
|
|
52
48
|
return {
|
|
53
49
|
content: [
|
|
54
50
|
{
|
|
55
51
|
type: 'text',
|
|
56
|
-
text: `❌ **Error counting versions in collection "${
|
|
52
|
+
text: `❌ **Error counting versions in collection "${slug}":** ${errorMessage}`
|
|
57
53
|
}
|
|
58
54
|
],
|
|
59
55
|
isError: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/countVersionsTool.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/countVersionsTool.ts"],"sourcesContent":["import { countVersionsInputSchema } from 'payload'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Count document versions in any version-enabled collection by passing the collection slug and optional where clause.'\n\nexport const countVersionsTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.readVersions),\n annotations: {\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n readOnlyHint: true,\n title: 'Count Versions',\n },\n description: DEFAULT_DESCRIPTION,\n input: countVersionsInputSchema,\n}).handler(async ({ slug, authorizedMCP, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n const { locale, where } = input\n\n logger.info(`Counting versions in collection: ${slug}`)\n\n try {\n const result = await payload.countVersions({\n collection: slug,\n overrideAccess: authorizedMCP.overrideAccess,\n req,\n ...(locale ? { locale } : {}),\n ...(where ? { where } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Collection \"${slug}\" contains ${result.totalDocs} matching versions.\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error counting versions in ${slug}: ${errorMessage}`)\n return {\n content: [\n {\n type: 'text',\n text: `❌ **Error counting versions in collection \"${slug}\":** ${errorMessage}`,\n },\n ],\n isError: true,\n }\n }\n})\n"],"names":["countVersionsInputSchema","defaultAccess","defineCollectionTool","getLogger","DEFAULT_DESCRIPTION","countVersionsTool","access","args","Boolean","permissions","collections","slug","readVersions","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","handler","authorizedMCP","req","payload","logger","locale","where","info","result","countVersions","collection","overrideAccess","content","type","text","totalDocs","JSON","stringify","doc","error","errorMessage","Error","message","isError"],"mappings":"AAAA,SAASA,wBAAwB,QAAQ,UAAS;AAElD,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AAEvD,MAAMC,sBACJ;AAEF,OAAO,MAAMC,oBAAoBH,qBAAqB;IACpDI,QAAQ,CAACC,OACPN,cAAcM,SAASC,QAAQD,KAAKE,WAAW,EAAEC,aAAa,CAACH,KAAKI,IAAI,CAAC,EAAEC;IAC7EC,aAAa;QACXC,iBAAiB;QACjBC,gBAAgB;QAChBC,eAAe;QACfC,cAAc;QACdC,OAAO;IACT;IACAC,aAAaf;IACbgB,OAAOpB;AACT,GAAGqB,OAAO,CAAC,OAAO,EAAEV,IAAI,EAAEW,aAAa,EAAEF,KAAK,EAAEG,GAAG,EAAE;IACnD,MAAMC,UAAUD,IAAIC,OAAO;IAC3B,MAAMC,SAAStB,UAAU;QAAEqB;IAAQ;IACnC,MAAM,EAAEE,MAAM,EAAEC,KAAK,EAAE,GAAGP;IAE1BK,OAAOG,IAAI,CAAC,CAAC,iCAAiC,EAAEjB,MAAM;IAEtD,IAAI;QACF,MAAMkB,SAAS,MAAML,QAAQM,aAAa,CAAC;YACzCC,YAAYpB;YACZqB,gBAAgBV,cAAcU,cAAc;YAC5CT;YACA,GAAIG,SAAS;gBAAEA;YAAO,IAAI,CAAC,CAAC;YAC5B,GAAIC,QAAQ;gBAAEA;YAAM,IAAI,CAAC,CAAC;QAC5B;QAEA,OAAO;YACLM,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,YAAY,EAAExB,KAAK,WAAW,EAAEkB,OAAOO,SAAS,CAAC,iCAAiC,EAAEC,KAAKC,SAAS,CAACT,QAAQ,QAAQ,CAAC;gBAC7H;aACD;YACDU,KAAKV;QACP;IACF,EAAE,OAAOW,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9DlB,OAAOe,KAAK,CAAC,CAAC,2BAA2B,EAAE7B,KAAK,EAAE,EAAE8B,cAAc;QAClE,OAAO;YACLR,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,2CAA2C,EAAExB,KAAK,KAAK,EAAE8B,cAAc;gBAChF;aACD;YACDG,SAAS;QACX;IACF;AACF,GAAE"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const
|
|
1
|
+
export declare const createDocumentsTool: import("../../../types.js").CollectionTool;
|
|
2
2
|
//# sourceMappingURL=createTool.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/createTool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/createTool.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,mBAAmB,4CA4H9B,CAAA"}
|
|
@@ -1,92 +1,133 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createDocumentsInputSchema, getCollectionVirtualFieldNames, hasDraftValidationEnabled, stripVirtualFields, transformPointDataToPayload, validateCollectionData } from 'payload';
|
|
2
2
|
import { defaultAccess } from '../../../defaultAccess.js';
|
|
3
3
|
import { defineCollectionTool } from '../../../defineTool.js';
|
|
4
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
5
|
-
import {
|
|
6
|
-
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
7
|
-
import { validateCollectionData } from '../validateEntityData.js';
|
|
5
|
+
import { formatEntityError } from '../formatEntityError.js';
|
|
8
6
|
import { fileInputSchema, resolveFile } from './fileInput.js';
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.create),
|
|
7
|
+
const DEFAULT_DESCRIPTION = 'Create one or more documents. Each can have different data or a file. Prefer uploadReference after upload, externalURL for URLs, or base64 for small local files.';
|
|
8
|
+
export const createDocumentsTool = defineCollectionTool({
|
|
9
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.create),
|
|
13
10
|
annotations: {
|
|
14
11
|
destructiveHint: false,
|
|
15
12
|
idempotentHint: false,
|
|
16
13
|
openWorldHint: false,
|
|
17
14
|
readOnlyHint: false,
|
|
18
|
-
title: 'Create
|
|
15
|
+
title: 'Create Documents'
|
|
19
16
|
},
|
|
20
17
|
description: DEFAULT_DESCRIPTION,
|
|
21
|
-
input:
|
|
22
|
-
|
|
23
|
-
depth: z.number().int().min(0).max(10).describe('How many levels deep to populate relationships in response').optional().default(0),
|
|
24
|
-
draft: z.boolean().describe('Only if getCollectionSchema includes _status; otherwise _status does not exist. true forces data._status to "draft"; with false, data._status controls draft or published.').optional().default(false),
|
|
25
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
26
|
-
file: fileInputSchema.optional(),
|
|
27
|
-
locale: z.string().describe('Optional: locale code to create the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
28
|
-
select: z.record(z.string(), z.unknown()).describe('Optional: define exactly which fields you\'d like to return, e.g., {"title": true}').optional()
|
|
18
|
+
input: createDocumentsInputSchema({
|
|
19
|
+
file: fileInputSchema
|
|
29
20
|
})
|
|
30
|
-
}).handler(async ({
|
|
21
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
31
22
|
const payload = req.payload;
|
|
23
|
+
const collectionConfig = payload.collections[slug]?.config;
|
|
32
24
|
const logger = getLogger({
|
|
33
25
|
payload
|
|
34
26
|
});
|
|
35
|
-
const {
|
|
36
|
-
|
|
27
|
+
const { depth, documents, draft, fallbackLocale, locale, populate, publishAllLocales, returning, select } = input;
|
|
28
|
+
const shouldUsePartialSchema = draft === true && collectionConfig !== undefined && !hasDraftValidationEnabled(collectionConfig);
|
|
29
|
+
logger.info(`Creating ${documents.length} documents in collection: ${slug}`);
|
|
37
30
|
try {
|
|
38
|
-
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config,
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, slug);
|
|
32
|
+
const docs = [];
|
|
33
|
+
const errors = [];
|
|
34
|
+
let validationSchema;
|
|
35
|
+
for (const [index, document] of documents.entries()){
|
|
36
|
+
try {
|
|
37
|
+
const inputData = stripVirtualFields(document.data, virtualFieldNames);
|
|
38
|
+
validateCollectionData({
|
|
39
|
+
slug,
|
|
40
|
+
data: inputData,
|
|
41
|
+
partial: shouldUsePartialSchema,
|
|
42
|
+
req
|
|
43
|
+
});
|
|
44
|
+
const parsedData = transformPointDataToPayload(inputData);
|
|
45
|
+
const file = await resolveFile({
|
|
46
|
+
slug,
|
|
47
|
+
input: document.file,
|
|
48
|
+
req
|
|
49
|
+
});
|
|
50
|
+
const result = await payload.create({
|
|
51
|
+
collection: slug,
|
|
52
|
+
data: parsedData,
|
|
53
|
+
depth,
|
|
54
|
+
draft,
|
|
55
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
56
|
+
populate,
|
|
57
|
+
publishAllLocales,
|
|
58
|
+
req,
|
|
59
|
+
...file ? {
|
|
60
|
+
file
|
|
61
|
+
} : {},
|
|
62
|
+
...locale ? {
|
|
63
|
+
locale
|
|
64
|
+
} : {},
|
|
65
|
+
...fallbackLocale !== undefined ? {
|
|
66
|
+
fallbackLocale
|
|
67
|
+
} : {},
|
|
68
|
+
select: returning ? select : {
|
|
69
|
+
id: true
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
docs.push(returning ? {
|
|
73
|
+
doc: result,
|
|
74
|
+
index
|
|
75
|
+
} : {
|
|
76
|
+
id: result.id,
|
|
77
|
+
index
|
|
78
|
+
});
|
|
79
|
+
} catch (error) {
|
|
80
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
81
|
+
const formattedError = formatEntityError({
|
|
82
|
+
slug,
|
|
83
|
+
action: 'creating',
|
|
84
|
+
entity: 'collection',
|
|
85
|
+
error,
|
|
86
|
+
req
|
|
87
|
+
});
|
|
88
|
+
const structuredContent = formattedError.structuredContent;
|
|
89
|
+
const schema = structuredContent?.schema;
|
|
90
|
+
const errorContent = formattedError.content[0];
|
|
91
|
+
const formattedMessage = errorContent?.type === 'text' ? errorContent.text.split('\n\nUse this schema')[0] ?? message : message;
|
|
92
|
+
if (!validationSchema && schema && typeof schema === 'object') {
|
|
93
|
+
validationSchema = schema;
|
|
94
|
+
}
|
|
95
|
+
logger.error(`Error creating document at index ${index} in ${slug}: ${message}`);
|
|
96
|
+
errors.push({
|
|
97
|
+
index,
|
|
98
|
+
message: formattedMessage
|
|
99
|
+
});
|
|
100
|
+
}
|
|
47
101
|
}
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
overrideAccess: authorizedMCP.overrideAccess,
|
|
60
|
-
req,
|
|
61
|
-
...file ? {
|
|
62
|
-
file
|
|
63
|
-
} : {},
|
|
64
|
-
...locale ? {
|
|
65
|
-
locale
|
|
66
|
-
} : {},
|
|
67
|
-
...fallbackLocale ? {
|
|
68
|
-
fallbackLocale
|
|
69
|
-
} : {},
|
|
70
|
-
...select ? {
|
|
71
|
-
select: select
|
|
72
|
-
} : {}
|
|
73
|
-
});
|
|
74
|
-
logger.info(`Successfully created document in ${collectionSlug} with ID: ${result.id}`);
|
|
102
|
+
const batchResult = {
|
|
103
|
+
docs,
|
|
104
|
+
errors
|
|
105
|
+
};
|
|
106
|
+
const retryMessage = errors.length > 0 ? '\nRetry failed indexes only.' : '';
|
|
107
|
+
const schemaMessage = validationSchema ? `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(validationSchema)}\n\`\`\`` : '';
|
|
108
|
+
const structuredContent = validationSchema ? {
|
|
109
|
+
...batchResult,
|
|
110
|
+
schema: validationSchema
|
|
111
|
+
} : batchResult;
|
|
112
|
+
logger.info(`Created ${docs.length} of ${documents.length} documents in ${slug}`);
|
|
75
113
|
return {
|
|
76
114
|
content: [
|
|
77
115
|
{
|
|
78
116
|
type: 'text',
|
|
79
|
-
text: `
|
|
117
|
+
text: `Created ${docs.length} of ${documents.length} documents in collection "${slug}".${retryMessage}\nResults:\n\`\`\`json\n${JSON.stringify(batchResult)}\n\`\`\`${schemaMessage}`
|
|
80
118
|
}
|
|
81
119
|
],
|
|
82
|
-
doc:
|
|
120
|
+
doc: structuredContent,
|
|
121
|
+
isError: docs.length === 0 && errors.length > 0,
|
|
122
|
+
structuredContent
|
|
83
123
|
};
|
|
84
124
|
} catch (error) {
|
|
85
|
-
const
|
|
86
|
-
logger.error(`Error creating
|
|
87
|
-
return
|
|
125
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
126
|
+
logger.error(`Error creating documents in ${slug}: ${message}`);
|
|
127
|
+
return formatEntityError({
|
|
128
|
+
slug,
|
|
88
129
|
action: 'creating',
|
|
89
|
-
|
|
130
|
+
entity: 'collection',
|
|
90
131
|
error,
|
|
91
132
|
req
|
|
92
133
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/createTool.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/createTool.ts"],"sourcesContent":["import {\n createDocumentsInputSchema,\n getCollectionVirtualFieldNames,\n hasDraftValidationEnabled,\n stripVirtualFields,\n transformPointDataToPayload,\n validateCollectionData,\n} from 'payload'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport { formatEntityError } from '../formatEntityError.js'\nimport { fileInputSchema, resolveFile } from './fileInput.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Create one or more documents. Each can have different data or a file. Prefer uploadReference after upload, externalURL for URLs, or base64 for small local files.'\n\nexport const createDocumentsTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.create),\n annotations: {\n destructiveHint: false,\n idempotentHint: false,\n openWorldHint: false,\n readOnlyHint: false,\n title: 'Create Documents',\n },\n description: DEFAULT_DESCRIPTION,\n input: createDocumentsInputSchema({ file: fileInputSchema }),\n}).handler(async ({ slug, authorizedMCP, input, req }) => {\n const payload = req.payload\n const collectionConfig = payload.collections[slug]?.config\n const logger = getLogger({ payload })\n const {\n depth,\n documents,\n draft,\n fallbackLocale,\n locale,\n populate,\n publishAllLocales,\n returning,\n select,\n } = input\n const shouldUsePartialSchema =\n draft === true && collectionConfig !== undefined && !hasDraftValidationEnabled(collectionConfig)\n\n logger.info(`Creating ${documents.length} documents in collection: ${slug}`)\n\n try {\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, slug)\n const docs: Array<\n { doc: Record<string, unknown>; index: number } | { id: number | string; index: number }\n > = []\n const errors: Array<{ index: number; message: string }> = []\n let validationSchema: Record<string, unknown> | undefined\n\n for (const [index, document] of documents.entries()) {\n try {\n const inputData = stripVirtualFields(document.data, virtualFieldNames)\n validateCollectionData({\n slug,\n data: inputData,\n partial: shouldUsePartialSchema,\n req,\n })\n\n const parsedData = transformPointDataToPayload(inputData)\n const file = await resolveFile({ slug, input: document.file, req })\n const result = await payload.create({\n collection: slug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: authorizedMCP.overrideAccess,\n populate,\n publishAllLocales,\n req,\n ...(file ? { file } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale !== undefined ? { fallbackLocale } : {}),\n select: returning ? select : { id: true },\n })\n\n docs.push(returning ? { doc: result, index } : { id: result.id, index })\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error'\n const formattedError = formatEntityError({\n slug,\n action: 'creating',\n entity: 'collection',\n error,\n req,\n })\n const structuredContent = formattedError.structuredContent as\n | { schema?: unknown }\n | undefined\n const schema = structuredContent?.schema\n const errorContent = formattedError.content[0]\n const formattedMessage =\n errorContent?.type === 'text'\n ? (errorContent.text.split('\\n\\nUse this schema')[0] ?? message)\n : message\n\n if (!validationSchema && schema && typeof schema === 'object') {\n validationSchema = schema as Record<string, unknown>\n }\n\n logger.error(`Error creating document at index ${index} in ${slug}: ${message}`)\n errors.push({ index, message: formattedMessage })\n }\n }\n\n const batchResult = { docs, errors }\n const retryMessage = errors.length > 0 ? '\\nRetry failed indexes only.' : ''\n const schemaMessage = validationSchema\n ? `\\n\\nUse this schema for data:\\n\\`\\`\\`json\\n${JSON.stringify(validationSchema)}\\n\\`\\`\\``\n : ''\n const structuredContent = validationSchema\n ? { ...batchResult, schema: validationSchema }\n : batchResult\n\n logger.info(`Created ${docs.length} of ${documents.length} documents in ${slug}`)\n\n return {\n content: [\n {\n type: 'text',\n text: `Created ${docs.length} of ${documents.length} documents in collection \"${slug}\".${retryMessage}\\nResults:\\n\\`\\`\\`json\\n${JSON.stringify(batchResult)}\\n\\`\\`\\`${schemaMessage}`,\n },\n ],\n doc: structuredContent as unknown as Record<string, unknown>,\n isError: docs.length === 0 && errors.length > 0,\n structuredContent,\n }\n } catch (error) {\n const message = error instanceof Error ? error.message : 'Unknown error'\n\n logger.error(`Error creating documents in ${slug}: ${message}`)\n return formatEntityError({ slug, action: 'creating', entity: 'collection', error, req })\n }\n})\n"],"names":["createDocumentsInputSchema","getCollectionVirtualFieldNames","hasDraftValidationEnabled","stripVirtualFields","transformPointDataToPayload","validateCollectionData","defaultAccess","defineCollectionTool","getLogger","formatEntityError","fileInputSchema","resolveFile","DEFAULT_DESCRIPTION","createDocumentsTool","access","args","Boolean","permissions","collections","slug","create","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","file","handler","authorizedMCP","req","payload","collectionConfig","config","logger","depth","documents","draft","fallbackLocale","locale","populate","publishAllLocales","returning","select","shouldUsePartialSchema","undefined","info","length","virtualFieldNames","docs","errors","validationSchema","index","document","entries","inputData","data","partial","parsedData","result","collection","overrideAccess","id","push","doc","error","message","Error","formattedError","action","entity","structuredContent","schema","errorContent","content","formattedMessage","type","text","split","batchResult","retryMessage","schemaMessage","JSON","stringify","isError"],"mappings":"AAAA,SACEA,0BAA0B,EAC1BC,8BAA8B,EAC9BC,yBAAyB,EACzBC,kBAAkB,EAClBC,2BAA2B,EAC3BC,sBAAsB,QACjB,UAAS;AAEhB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAA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
|
+
{"version":3,"file":"deleteTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/deleteTool.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,mBAAmB,4CAuF9B,CAAA"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { deleteDocumentsInputSchema, parseDocumentID } from 'payload';
|
|
2
2
|
import { defaultAccess } from '../../../defaultAccess.js';
|
|
3
3
|
import { defineCollectionTool } from '../../../defineTool.js';
|
|
4
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
5
|
-
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
6
5
|
const DEFAULT_DESCRIPTION = 'Delete documents in any collection by passing the collection slug and ID or where clause.';
|
|
7
6
|
export const deleteDocumentsTool = defineCollectionTool({
|
|
8
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.
|
|
7
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.delete),
|
|
9
8
|
annotations: {
|
|
10
9
|
destructiveHint: true,
|
|
11
10
|
idempotentHint: false,
|
|
@@ -14,25 +13,44 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
14
13
|
title: 'Delete Documents'
|
|
15
14
|
},
|
|
16
15
|
description: DEFAULT_DESCRIPTION,
|
|
17
|
-
input:
|
|
18
|
-
|
|
19
|
-
z.string(),
|
|
20
|
-
z.number()
|
|
21
|
-
]).describe('Optional: specific document ID to delete').optional(),
|
|
22
|
-
depth: z.number().int().min(0).max(10).describe('Depth of population for relationships in response').optional().default(0),
|
|
23
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
24
|
-
locale: z.string().describe('Optional: locale code for the operation (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
25
|
-
where: whereSchema.describe('Optional: where clause to delete multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}').optional()
|
|
26
|
-
})
|
|
27
|
-
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
16
|
+
input: deleteDocumentsInputSchema
|
|
17
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
28
18
|
const payload = req.payload;
|
|
29
19
|
const logger = getLogger({
|
|
30
20
|
payload
|
|
31
21
|
});
|
|
32
22
|
const { id, depth, fallbackLocale, locale, where } = input;
|
|
33
|
-
logger.info(`Deleting document from collection: ${
|
|
23
|
+
logger.info(`Deleting document from collection: ${slug}${id ? ` with ID: ${id}` : ' with where clause'}${locale ? `, locale: ${locale}` : ''}`);
|
|
34
24
|
try {
|
|
35
|
-
if (
|
|
25
|
+
if (id !== undefined) {
|
|
26
|
+
const result = await payload.delete({
|
|
27
|
+
id: parseDocumentID({
|
|
28
|
+
id,
|
|
29
|
+
collectionSlug: slug,
|
|
30
|
+
payload
|
|
31
|
+
}),
|
|
32
|
+
collection: slug,
|
|
33
|
+
depth,
|
|
34
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
35
|
+
req,
|
|
36
|
+
...locale && {
|
|
37
|
+
locale
|
|
38
|
+
},
|
|
39
|
+
...fallbackLocale !== undefined && {
|
|
40
|
+
fallbackLocale
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: 'text',
|
|
47
|
+
text: `Document deleted successfully from collection "${slug}"!\nDeleted document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
doc: result
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (where === undefined) {
|
|
36
54
|
return {
|
|
37
55
|
content: [
|
|
38
56
|
{
|
|
@@ -42,39 +60,22 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
42
60
|
]
|
|
43
61
|
};
|
|
44
62
|
}
|
|
45
|
-
const
|
|
46
|
-
collection:
|
|
63
|
+
const result = await payload.delete({
|
|
64
|
+
collection: slug,
|
|
47
65
|
depth,
|
|
48
66
|
overrideAccess: authorizedMCP.overrideAccess,
|
|
49
67
|
req,
|
|
68
|
+
where,
|
|
50
69
|
...locale && {
|
|
51
70
|
locale
|
|
52
71
|
},
|
|
53
|
-
...fallbackLocale && {
|
|
72
|
+
...fallbackLocale !== undefined && {
|
|
54
73
|
fallbackLocale
|
|
55
74
|
}
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
deleteOptions.where = where;
|
|
61
|
-
}
|
|
62
|
-
const result = await payload.delete(deleteOptions);
|
|
63
|
-
if (id) {
|
|
64
|
-
return {
|
|
65
|
-
content: [
|
|
66
|
-
{
|
|
67
|
-
type: 'text',
|
|
68
|
-
text: `Document deleted successfully from collection "${collectionSlug}"!\nDeleted document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
69
|
-
}
|
|
70
|
-
],
|
|
71
|
-
doc: result
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
const bulkResult = result;
|
|
75
|
-
const docs = bulkResult.docs || [];
|
|
76
|
-
const errors = bulkResult.errors || [];
|
|
77
|
-
let responseText = `Document deleted successfully from collection "${collectionSlug}"!\nDeleted: ${docs.length} documents\nErrors: ${errors.length}\n---`;
|
|
75
|
+
});
|
|
76
|
+
const docs = result.docs || [];
|
|
77
|
+
const errors = result.errors || [];
|
|
78
|
+
let responseText = `Document deleted successfully from collection "${slug}"!\nDeleted: ${docs.length} documents\nErrors: ${errors.length}\n---`;
|
|
78
79
|
if (docs.length > 0) {
|
|
79
80
|
responseText += `\n\nDeleted documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``;
|
|
80
81
|
}
|
|
@@ -95,12 +96,12 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
95
96
|
};
|
|
96
97
|
} catch (error) {
|
|
97
98
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
98
|
-
logger.error(`Error deleting document from ${
|
|
99
|
+
logger.error(`Error deleting document from ${slug}: ${errorMessage}`);
|
|
99
100
|
return {
|
|
100
101
|
content: [
|
|
101
102
|
{
|
|
102
103
|
type: 'text',
|
|
103
|
-
text: `Error deleting document from collection "${
|
|
104
|
+
text: `Error deleting document from collection "${slug}": ${errorMessage}`
|
|
104
105
|
}
|
|
105
106
|
]
|
|
106
107
|
};
|