@payloadcms/plugin-mcp 4.0.0-internal.5214550 → 4.0.0-internal.5852d1a
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 +7 -0
- package/dist/defaultAccess.d.ts.map +1 -1
- package/dist/defineTool.d.ts +8 -8
- package/dist/defineTool.d.ts.map +1 -1
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +13 -3
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/fileInput.d.ts +20 -0
- package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.js +100 -0
- package/dist/mcp/builtin/collections/fileInput.js.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.spec.js +72 -0
- package/dist/mcp/builtin/collections/fileInput.spec.js.map +1 -0
- package/dist/mcp/builtin/collections/findTool.js +1 -1
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +1 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +23 -2
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +21 -8
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -1
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/utils/camelCase.d.ts.map +1 -1
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +1 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +2 -2
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -1
- package/dist/utils/toStandardSchema.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/mcp/builtin/collections/createTool.ts +13 -3
- package/src/mcp/builtin/collections/fileInput.spec.ts +79 -0
- package/src/mcp/builtin/collections/fileInput.ts +139 -0
- package/src/mcp/builtin/collections/findTool.ts +1 -1
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +14 -1
- package/src/mcp/builtin/collections/updateTool.ts +23 -6
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +2 -2
|
@@ -7,6 +7,7 @@ import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEnt
|
|
|
7
7
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
8
8
|
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
9
9
|
import { validateCollectionData } from '../validateEntityData.js';
|
|
10
|
+
import { fileInputSchema, resolveFileInput } from './fileInput.js';
|
|
10
11
|
import { formatCollectionError } from './formatCollectionError.js';
|
|
11
12
|
const DEFAULT_DESCRIPTION = 'Update documents in any collection by passing the collection slug and data.';
|
|
12
13
|
export const updateDocumentTool = defineCollectionTool({
|
|
@@ -24,14 +25,15 @@ export const updateDocumentTool = defineCollectionTool({
|
|
|
24
25
|
z.string(),
|
|
25
26
|
z.number()
|
|
26
27
|
]).describe('The ID of the document to update').optional(),
|
|
27
|
-
data: z.record(z.string(), z.unknown()).describe('The fields to update'),
|
|
28
|
+
data: z.record(z.string(), z.unknown()).describe('The fields to update. Only include fields permitted by the schema returned by getCollectionSchema.'),
|
|
28
29
|
depth: z.number().describe('How many levels deep to populate relationships').optional().default(0),
|
|
29
|
-
draft: z.boolean().describe('
|
|
30
|
+
draft: z.boolean().describe('Only if getCollectionSchema includes _status; otherwise _status does not exist. true saves only a draft version; false updates main and versions. data._status: "published" overrides true.').optional().default(false),
|
|
30
31
|
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
31
|
-
|
|
32
|
+
file: fileInputSchema.optional(),
|
|
32
33
|
locale: z.string().describe('Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
33
34
|
overrideLock: z.boolean().describe('Whether to override document locks').optional().default(true),
|
|
34
35
|
overwriteExistingFiles: z.boolean().describe('Whether to overwrite existing files').optional().default(false),
|
|
36
|
+
publishAllLocales: z.boolean().describe('For collections with localized publishing status, whether publishing should affect every locale. Set false with locale to publish only that locale.').optional(),
|
|
35
37
|
select: z.record(z.string(), z.unknown()).describe('Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}').optional(),
|
|
36
38
|
where: whereSchema.describe('Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}').optional()
|
|
37
39
|
})
|
|
@@ -40,7 +42,7 @@ export const updateDocumentTool = defineCollectionTool({
|
|
|
40
42
|
const logger = getLogger({
|
|
41
43
|
payload
|
|
42
44
|
});
|
|
43
|
-
const { id, data, depth, draft, fallbackLocale,
|
|
45
|
+
const { id, data, depth, draft, fallbackLocale, file: fileInput, locale, overrideLock, overwriteExistingFiles, publishAllLocales, select, where } = input;
|
|
44
46
|
logger.info(`Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`);
|
|
45
47
|
try {
|
|
46
48
|
if (!id && !where) {
|
|
@@ -65,6 +67,11 @@ export const updateDocumentTool = defineCollectionTool({
|
|
|
65
67
|
return validationError;
|
|
66
68
|
}
|
|
67
69
|
const parsedData = transformPointDataToPayload(inputData);
|
|
70
|
+
const file = await resolveFileInput({
|
|
71
|
+
collectionSlug,
|
|
72
|
+
input: fileInput,
|
|
73
|
+
req
|
|
74
|
+
});
|
|
68
75
|
const whereClause = where ?? {};
|
|
69
76
|
if (id) {
|
|
70
77
|
const result = await payload.update({
|
|
@@ -76,12 +83,15 @@ export const updateDocumentTool = defineCollectionTool({
|
|
|
76
83
|
overrideAccess: authorizedMCP.overrideAccess,
|
|
77
84
|
overrideLock,
|
|
78
85
|
req,
|
|
79
|
-
...
|
|
80
|
-
|
|
86
|
+
...file ? {
|
|
87
|
+
file
|
|
81
88
|
} : {},
|
|
82
89
|
...overwriteExistingFiles ? {
|
|
83
90
|
overwriteExistingFiles
|
|
84
91
|
} : {},
|
|
92
|
+
...publishAllLocales !== undefined ? {
|
|
93
|
+
publishAllLocales
|
|
94
|
+
} : {},
|
|
85
95
|
...locale ? {
|
|
86
96
|
locale
|
|
87
97
|
} : {},
|
|
@@ -111,12 +121,15 @@ export const updateDocumentTool = defineCollectionTool({
|
|
|
111
121
|
overrideLock,
|
|
112
122
|
req,
|
|
113
123
|
where: whereClause,
|
|
114
|
-
...
|
|
115
|
-
|
|
124
|
+
...file ? {
|
|
125
|
+
file
|
|
116
126
|
} : {},
|
|
117
127
|
...overwriteExistingFiles ? {
|
|
118
128
|
overwriteExistingFiles
|
|
119
129
|
} : {},
|
|
130
|
+
...publishAllLocales !== undefined ? {
|
|
131
|
+
publishAllLocales
|
|
132
|
+
} : {},
|
|
120
133
|
...locale ? {
|
|
121
134
|
locale
|
|
122
135
|
} : {},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/updateTool.ts"],"sourcesContent":["import type { SelectType, Where } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getCollectionVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { whereSchema } from '../../../utils/whereSchema.js'\nimport { validateCollectionData } from '../validateEntityData.js'\nimport { formatCollectionError } from './formatCollectionError.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Update documents in any collection by passing the collection slug and data.'\n\nexport const updateDocumentTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.update),\n annotations: {\n destructiveHint: true,\n idempotentHint: false,\n openWorldHint: false,\n readOnlyHint: false,\n title: 'Update Document',\n },\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),\n data: z.record(z.string(), z.unknown()).describe('The fields to update'),\n depth: z\n .number()\n .describe('How many levels deep to populate relationships')\n .optional()\n .default(0),\n draft: z\n .boolean()\n .describe('Whether to update the document as a draft')\n .optional()\n .default(false),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n filePath: z.string().describe('File path for file uploads').optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n )\n .optional(),\n overrideLock: z\n .boolean()\n .describe('Whether to override document locks')\n .optional()\n .default(true),\n overwriteExistingFiles: z\n .boolean()\n .describe('Whether to overwrite existing files')\n .optional()\n .default(false),\n select: z\n .record(z.string(), z.unknown())\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response, e.g., {\"title\": true}',\n )\n .optional(),\n where: whereSchema\n .describe(\n 'Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {\"title\":{\"contains\":\"test\"}}',\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const {\n id,\n data,\n depth,\n draft,\n fallbackLocale,\n filePath,\n locale,\n overrideLock,\n overwriteExistingFiles,\n select,\n where,\n } = input\n\n logger.info(\n `Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n if (!id && !where) {\n return {\n content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],\n }\n }\n\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)\n const inputData = stripVirtualFields(data, virtualFieldNames)\n const validationError = validateCollectionData({\n collectionSlug,\n data: inputData,\n partial: true,\n req,\n })\n\n if (validationError) {\n return validationError\n }\n\n const parsedData = transformPointDataToPayload(inputData)\n\n const whereClause: Where = where ?? {}\n\n if (id) {\n const result = await payload.update({\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: authorizedMCP.overrideAccess,\n overrideLock,\n req,\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(select ? { select: select as SelectType } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Document updated successfully in collection \"${collectionSlug}\"!\\nUpdated document:\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n }\n\n const result = await payload.update({\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: authorizedMCP.overrideAccess,\n overrideLock,\n req,\n where: whereClause,\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(select ? { select: select as SelectType } : {}),\n })\n\n const docs = result.docs || []\n const errors = result.errors || []\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\\nUpdated: ${docs.length} documents\\nErrors: ${errors.length}\\n---`\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\\n\\`\\`\\`json\\n${JSON.stringify(docs)}\\n\\`\\`\\``\n }\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\\n\\`\\`\\`json\\n${JSON.stringify(errors)}\\n\\`\\`\\``\n\n const errorSchema = getCollectionInputSchema({ collectionSlug, req })\n\n if (errorSchema) {\n responseText += `\\n\\nUse this schema for data:\\n\\`\\`\\`json\\n${JSON.stringify(errorSchema)}\\n\\`\\`\\``\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n isError: true,\n ...(errorSchema\n ? {\n structuredContent: {\n collectionSlug,\n docs,\n errors,\n schema: errorSchema,\n },\n }\n : {}),\n }\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)\n return formatCollectionError({ action: 'updating', collectionSlug, error, req })\n }\n})\n"],"names":["z","defaultAccess","defineCollectionTool","getLogger","getCollectionVirtualFieldNames","stripVirtualFields","getCollectionInputSchema","transformPointDataToPayload","whereSchema","validateCollectionData","formatCollectionError","DEFAULT_DESCRIPTION","updateDocumentTool","access","args","Boolean","permissions","collections","collectionSlug","update","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","object","id","union","string","number","describe","optional","data","record","unknown","depth","default","draft","boolean","fallbackLocale","filePath","locale","overrideLock","overwriteExistingFiles","select","where","handler","authorizedMCP","req","payload","logger","info","content","type","text","virtualFieldNames","config","inputData","validationError","partial","parsedData","whereClause","result","collection","overrideAccess","JSON","stringify","doc","docs","errors","responseText","length","errorSchema","isError","structuredContent","schema","error","errorMessage","Error","message","action"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,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QACpJ,IAAIH,KAAKG,MAAM,GAAG,GAAG;YACnBD,gBAAgB,CAAC,oCAAoC,EAAEL,KAAKC,SAAS,CAACE,MAAM,QAAQ,CAAC;QACvF;QACA,IAAIC,OAAOE,MAAM,GAAG,GAAG;YACrBD,gBAAgB,CAAC,yBAAyB,EAAEL,KAAKC,SAAS,CAACG,QAAQ,QAAQ,CAAC;YAE5E,MAAMG,cAAcrE,yBAAyB;gBAAEY;gBAAgBiC;YAAI;YAEnE,IAAIwB,aAAa;gBACfF,gBAAgB,CAAC,2CAA2C,EAAEL,KAAKC,SAAS,CAACM,aAAa,QAAQ,CAAC;YACrG;YAEA,OAAO;gBACLpB,SAAS;oBAAC;wBAAEC,MAAM;wBAAQC,MAAMgB;oBAAa;iBAAE;gBAC/CH,KAAK;oBAAEC;oBAAMC;gBAAO;gBACpBI,SAAS;gBACT,GAAID,cACA;oBACEE,mBAAmB;wBACjB3D;wBACAqD;wBACAC;wBACAM,QAAQH;oBACV;gBACF,IACA,CAAC,CAAC;YACR;QACF;QAEA,OAAO;YACLpB,SAAS;gBAAC;oBAAEC,MAAM;oBAAQC,MAAMgB;gBAAa;aAAE;YAC/CH,KAAK;gBAAEC;gBAAMC;YAAO;QACtB;IACF,EAAE,OAAOO,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9D7B,OAAO0B,KAAK,CAAC,CAAC,2BAA2B,EAAE7D,eAAe,EAAE,EAAE8D,cAAc;QAC5E,OAAOtE,sBAAsB;YAAEyE,QAAQ;YAAYjE;YAAgB6D;YAAO5B;QAAI;IAChF;AACF,GAAE"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/updateTool.ts"],"sourcesContent":["import type { SelectType, Where } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getCollectionVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { whereSchema } from '../../../utils/whereSchema.js'\nimport { validateCollectionData } from '../validateEntityData.js'\nimport { fileInputSchema, resolveFileInput } from './fileInput.js'\nimport { formatCollectionError } from './formatCollectionError.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Update documents in any collection by passing the collection slug and data.'\n\nexport const updateDocumentTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.update),\n annotations: {\n destructiveHint: true,\n idempotentHint: false,\n openWorldHint: false,\n readOnlyHint: false,\n title: 'Update Document',\n },\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),\n data: z\n .record(z.string(), z.unknown())\n .describe(\n 'The fields to update. Only include fields permitted by the schema returned by getCollectionSchema.',\n ),\n depth: z\n .number()\n .describe('How many levels deep to populate relationships')\n .optional()\n .default(0),\n draft: z\n .boolean()\n .describe(\n 'Only if getCollectionSchema includes _status; otherwise _status does not exist. true saves only a draft version; false updates main and versions. data._status: \"published\" overrides true.',\n )\n .optional()\n .default(false),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n file: fileInputSchema.optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n )\n .optional(),\n overrideLock: z\n .boolean()\n .describe('Whether to override document locks')\n .optional()\n .default(true),\n overwriteExistingFiles: z\n .boolean()\n .describe('Whether to overwrite existing files')\n .optional()\n .default(false),\n publishAllLocales: z\n .boolean()\n .describe(\n 'For collections with localized publishing status, whether publishing should affect every locale. Set false with locale to publish only that locale.',\n )\n .optional(),\n select: z\n .record(z.string(), z.unknown())\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response, e.g., {\"title\": true}',\n )\n .optional(),\n where: whereSchema\n .describe(\n 'Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {\"title\":{\"contains\":\"test\"}}',\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const {\n id,\n data,\n depth,\n draft,\n fallbackLocale,\n file: fileInput,\n locale,\n overrideLock,\n overwriteExistingFiles,\n publishAllLocales,\n select,\n where,\n } = input\n\n logger.info(\n `Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n if (!id && !where) {\n return {\n content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],\n }\n }\n\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)\n const inputData = stripVirtualFields(data, virtualFieldNames)\n const validationError = validateCollectionData({\n collectionSlug,\n data: inputData,\n partial: true,\n req,\n })\n\n if (validationError) {\n return validationError\n }\n\n const parsedData = transformPointDataToPayload(inputData)\n const file = await resolveFileInput({ collectionSlug, input: fileInput, req })\n\n const whereClause: Where = where ?? {}\n\n if (id) {\n const result = await payload.update({\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: authorizedMCP.overrideAccess,\n overrideLock,\n req,\n ...(file ? { file } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(select ? { select: select as SelectType } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Document updated successfully in collection \"${collectionSlug}\"!\\nUpdated document:\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n }\n\n const result = await payload.update({\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: authorizedMCP.overrideAccess,\n overrideLock,\n req,\n where: whereClause,\n ...(file ? { file } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(select ? { select: select as SelectType } : {}),\n })\n\n const docs = result.docs || []\n const errors = result.errors || []\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\\nUpdated: ${docs.length} documents\\nErrors: ${errors.length}\\n---`\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\\n\\`\\`\\`json\\n${JSON.stringify(docs)}\\n\\`\\`\\``\n }\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\\n\\`\\`\\`json\\n${JSON.stringify(errors)}\\n\\`\\`\\``\n\n const errorSchema = getCollectionInputSchema({ collectionSlug, req })\n\n if (errorSchema) {\n responseText += `\\n\\nUse this schema for data:\\n\\`\\`\\`json\\n${JSON.stringify(errorSchema)}\\n\\`\\`\\``\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n isError: true,\n ...(errorSchema\n ? {\n structuredContent: {\n collectionSlug,\n docs,\n errors,\n schema: errorSchema,\n },\n }\n : {}),\n }\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)\n return formatCollectionError({ action: 'updating', collectionSlug, error, req })\n }\n})\n"],"names":["z","defaultAccess","defineCollectionTool","getLogger","getCollectionVirtualFieldNames","stripVirtualFields","getCollectionInputSchema","transformPointDataToPayload","whereSchema","validateCollectionData","fileInputSchema","resolveFileInput","formatCollectionError","DEFAULT_DESCRIPTION","updateDocumentTool","access","args","Boolean","permissions","collections","collectionSlug","update","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","object","id","union","string","number","describe","optional","data","record","unknown","depth","default","draft","boolean","fallbackLocale","file","locale","overrideLock","overwriteExistingFiles","publishAllLocales","select","where","handler","authorizedMCP","req","payload","logger","fileInput","info","content","type","text","virtualFieldNames","config","inputData","validationError","partial","parsedData","whereClause","result","collection","overrideAccess","undefined","JSON","stringify","doc","docs","errors","responseText","length","errorSchema","isError","structuredContent","schema","error","errorMessage","Error","message","action"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AACvD,SACEC,8BAA8B,EAC9BC,kBAAkB,QACb,yCAAwC;AAC/C,SAASC,wBAAwB,QAAQ,0DAAyD;AAClG,SAASC,2BAA2B,QAAQ,gDAA+C;AAC3F,SAASC,WAAW,QAAQ,gCAA+B;AAC3D,SAASC,sBAAsB,QAAQ,2BAA0B;AACjE,SAASC,eAAe,EAAEC,gBAAgB,QAAQ,iBAAgB;AAClE,SAASC,qBAAqB,QAAQ,6BAA4B;AAElE,MAAMC,sBACJ;AAEF,OAAO,MAAMC,qBAAqBZ,qBAAqB;IACrDa,QAAQ,CAACC,OACPf,cAAce,SAASC,QAAQD,KAAKE,WAAW,EAAEC,aAAa,CAACH,KAAKI,cAAc,CAAC,EAAEC;IACvFC,aAAa;QACXC,iBAAiB;QACjBC,gBAAgB;QAChBC,eAAe;QACfC,cAAc;QACdC,OAAO;IACT;IACAC,aAAaf;IACbgB,OAAO7B,EAAE8B,MAAM,CAAC;QACdC,IAAI/B,EAAEgC,KAAK,CAAC;YAAChC,EAAEiC,MAAM;YAAIjC,EAAEkC,MAAM;SAAG,EAAEC,QAAQ,CAAC,oCAAoCC,QAAQ;QAC3FC,MAAMrC,EACHsC,MAAM,CAACtC,EAAEiC,MAAM,I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a;iBAAE;gBAC/CH,KAAK;oBAAEC;oBAAMC;gBAAO;gBACpBI,SAAS;gBACT,GAAID,cACA;oBACEE,mBAAmB;wBACjB9D;wBACAwD;wBACAC;wBACAM,QAAQH;oBACV;gBACF,IACA,CAAC,CAAC;YACR;QACF;QAEA,OAAO;YACLrB,SAAS;gBAAC;oBAAEC,MAAM;oBAAQC,MAAMiB;gBAAa;aAAE;YAC/CH,KAAK;gBAAEC;gBAAMC;YAAO;QACtB;IACF,EAAE,OAAOO,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9D/B,OAAO4B,KAAK,CAAC,CAAC,2BAA2B,EAAEhE,eAAe,EAAE,EAAEiE,cAAc;QAC5E,OAAOzE,sBAAsB;YAAE4E,QAAQ;YAAYpE;YAAgBgE;YAAO9B;QAAI;IAChF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateEntityData.d.ts","sourceRoot":"","sources":["../../../src/mcp/builtin/validateEntityData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAmB,MAAM,SAAS,CAAA;AAI1F,OAAO,KAAK,EAAkB,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAuBrE,eAAO,MAAM,sBAAsB,
|
|
1
|
+
{"version":3,"file":"validateEntityData.d.ts","sourceRoot":"","sources":["../../../src/mcp/builtin/validateEntityData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAmB,MAAM,SAAS,CAAA;AAI1F,OAAO,KAAK,EAAkB,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAuBrE,eAAO,MAAM,sBAAsB,4CAKhC;IACD,cAAc,EAAE,cAAc,CAAA;IAC9B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,eAAe,GAAG,IAQlB,CAAA;AAEJ,eAAO,MAAM,kBAAkB,+BAI5B;IACD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,UAAU,EAAE,UAAU,CAAA;IACtB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,eAAe,GAAG,IAQlB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"builtinTools.d.ts","sourceRoot":"","sources":["../../src/mcp/builtinTools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AA+BnE,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,wBAAwB,CAAC,EAAE,OAAO,CAAA;IAClC,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,IAAI,EAAE,cAAc,CAAA;CACrB,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,IAAI,EAAE,UAAU,CAAA;CACjB,CAAA;AAED,eAAO,MAAM,aAAa
|
|
1
|
+
{"version":3,"file":"builtinTools.d.ts","sourceRoot":"","sources":["../../src/mcp/builtinTools.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AA+BnE,KAAK,iBAAiB,GAAG;IACvB,OAAO,EAAE,MAAM,CAAA;IACf,wBAAwB,CAAC,EAAE,OAAO,CAAA;IAClC,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,IAAI,EAAE,cAAc,CAAA;CACrB,CAAA;AAED,KAAK,aAAa,GAAG;IACnB,OAAO,EAAE,MAAM,CAAA;IACf,gBAAgB,CAAC,EAAE,OAAO,CAAA;IAC1B,IAAI,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AM,+BAA+B,EAA+C,KAAK,CAC9F,CAAC,yBAAyB,EAAE,CAAC,OAAO,wBAAwB,CAAC,CAAC,yBAAyB,CAAC,CAAC,CAC1F,CAAA;AAED,eAAO,MAAM,sBAAsB,EAAsC,KAAK,CAC5E,CAAC,oBAAoB,EAAE,aAAa,CAAC,CACtC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitizeMCPConfig.d.ts","sourceRoot":"","sources":["../../src/mcp/sanitizeMCPConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,MAAM,EAGN,eAAe,EAEhB,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAMV,eAAe,EACf,wBAAwB,EACzB,MAAM,aAAa,CAAA;AAcpB;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"sanitizeMCPConfig.d.ts","sourceRoot":"","sources":["../../src/mcp/sanitizeMCPConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,MAAM,EAGN,eAAe,EAEhB,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAMV,eAAe,EACf,wBAAwB,EACzB,MAAM,aAAa,CAAA;AAcpB;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,8BAG3B;IACD,MAAM,EAAE,MAAM,GAAG,eAAe,CAAA;IAChC,YAAY,EAAE,eAAe,CAAA;CAC9B,KAAG,wBA6DH,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"camelCase.d.ts","sourceRoot":"","sources":["../../src/utils/camelCase.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"camelCase.d.ts","sourceRoot":"","sources":["../../src/utils/camelCase.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,QAAS,MAAM,KAAG,MAIzC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolveProjectRoot.d.ts","sourceRoot":"","sources":["../../src/utils/resolveProjectRoot.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,
|
|
1
|
+
{"version":3,"file":"resolveProjectRoot.d.ts","sourceRoot":"","sources":["../../src/utils/resolveProjectRoot.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,aAAc,MAAM,KAAG,IAAI,GAAG,MAS5D,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterFieldsByAccess.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/filterFieldsByAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,SAAS,EACT,4BAA4B,EAE7B,MAAM,SAAS,CAAA;AAEhB,KAAK,cAAc,GAAG,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAClD,KAAK,sBAAsB,GAAG,MAAM,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oBAAoB,
|
|
1
|
+
{"version":3,"file":"filterFieldsByAccess.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/filterFieldsByAccess.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,SAAS,EACT,4BAA4B,EAE7B,MAAM,SAAS,CAAA;AAEhB,KAAK,cAAc,GAAG,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAClD,KAAK,sBAAsB,GAAG,MAAM,CAAC,cAAc,EAAE,OAAO,CAAC,CAAA;AAE7D;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oBAAoB,yDAK9B;IACD,MAAM,CAAC,EAAE,cAAc,EAAE,CAAA;IACzB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,WAAW,EAAE,4BAA4B,CAAA;IACzC,kBAAkB,EAAE,CAAC,iBAAiB,EAAE,sBAAsB,KAAK,OAAO,CAAA;CAC3E,KAAG,cAAc,EAoFjB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getEntityInputSchema.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/getEntityInputSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAGnB,KAAK,UAAU,EACf,KAAK,cAAc,EAEnB,KAAK,6BAA6B,EAElC,KAAK,yBAAyB,EAC/B,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAKpD,eAAO,MAAM,wBAAwB,
|
|
1
|
+
{"version":3,"file":"getEntityInputSchema.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/getEntityInputSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAGnB,KAAK,UAAU,EACf,KAAK,cAAc,EAEnB,KAAK,6BAA6B,EAElC,KAAK,yBAAyB,EAC/B,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAKpD,eAAO,MAAM,wBAAwB,0CAIlC;IACD,cAAc,EAAE,cAAc,CAAA;IAC9B,WAAW,CAAC,EAAE,6BAA6B,CAAA;IAC3C,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,cAAc,GAAG,IAmBpB,CAAA;AAED,eAAO,MAAM,oBAAoB,sCAI9B;IACD,UAAU,EAAE,UAAU,CAAA;IACtB,WAAW,CAAC,EAAE,yBAAyB,CAAA;IACvC,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,cAAc,GAAG,IAmBpB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitizeEntitySchema.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/sanitizeEntitySchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,
|
|
1
|
+
{"version":3,"file":"sanitizeEntitySchema.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/sanitizeEntitySchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAEpD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,WAAY,cAAc,KAAG,cA0B7D,CAAA"}
|
|
@@ -104,8 +104,8 @@ describe('sanitizeEntitySchema', ()=>{
|
|
|
104
104
|
}
|
|
105
105
|
},
|
|
106
106
|
properties: {
|
|
107
|
-
// Managed
|
|
108
|
-
//
|
|
107
|
+
// Managed timestamps are excluded upstream by the `input` variant. `id` stays because it's
|
|
108
|
+
// a valid optional input (a client may supply a custom ID).
|
|
109
109
|
id: {
|
|
110
110
|
type: 'string'
|
|
111
111
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/schemaConversion/sanitizeEntitySchema.spec.ts"],"sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport type { JsonSchemaType } from '../../types.js'\n\nimport { sanitizeEntitySchema } from './sanitizeEntitySchema.js'\n\ndescribe('sanitizeEntitySchema', () => {\n it('renames the Lexical node-union definition to a short name and keeps it a strict oneOf', () => {\n // Shaped like the schema the MCP server prepares for a collection with a Lexical richText field\n // (Payload's `input` variant): a `$defs` node union (a `oneOf` of node shapes). The input variant\n // already types the relationship node's `value` as a bare ID - there is no populated-doc `$ref` to\n // reduce here (the variant did that upstream), so sanitize leaves the value untouched.\n const standalone: JsonSchemaType = {\n type: 'object',\n $defs: {\n LexicalNodes_ABCDEF12: {\n oneOf: [\n {\n type: 'object',\n additionalProperties: false,\n properties: { type: { const: 'paragraph' } },\n required: ['type'],\n },\n {\n type: 'object',\n additionalProperties: false,\n properties: {\n type: { const: 'relationship' },\n value: { type: 'string', description: 'The related document ID.' },\n },\n required: ['type'],\n },\n ],\n },\n },\n properties: {\n id: { type: 'string' },\n content: {\n type: 'object',\n properties: {\n root: {\n type: 'object',\n properties: {\n children: { type: 'array', items: { $ref: '#/$defs/LexicalNodes_ABCDEF12' } },\n },\n },\n },\n },\n },\n }\n\n expect(sanitizeEntitySchema(standalone)).toStrictEqual({\n type: 'object',\n $defs: {\n // The node union is renamed to a short, readable `node`, and stays a strict discriminated\n // `oneOf` - not loosened to `anyOf`.\n node: {\n oneOf: [\n {\n type: 'object',\n additionalProperties: false,\n properties: { type: { const: 'paragraph' } },\n required: ['type'],\n },\n {\n type: 'object',\n additionalProperties: false,\n properties: {\n type: { const: 'relationship' },\n // The value is already ID-only from the input variant - left as-is.\n value: { type: 'string', description: 'The related document ID.' },\n },\n required: ['type'],\n },\n ],\n },\n },\n properties: {\n // Managed
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/schemaConversion/sanitizeEntitySchema.spec.ts"],"sourcesContent":["import { describe, expect, it } from 'vitest'\n\nimport type { JsonSchemaType } from '../../types.js'\n\nimport { sanitizeEntitySchema } from './sanitizeEntitySchema.js'\n\ndescribe('sanitizeEntitySchema', () => {\n it('renames the Lexical node-union definition to a short name and keeps it a strict oneOf', () => {\n // Shaped like the schema the MCP server prepares for a collection with a Lexical richText field\n // (Payload's `input` variant): a `$defs` node union (a `oneOf` of node shapes). The input variant\n // already types the relationship node's `value` as a bare ID - there is no populated-doc `$ref` to\n // reduce here (the variant did that upstream), so sanitize leaves the value untouched.\n const standalone: JsonSchemaType = {\n type: 'object',\n $defs: {\n LexicalNodes_ABCDEF12: {\n oneOf: [\n {\n type: 'object',\n additionalProperties: false,\n properties: { type: { const: 'paragraph' } },\n required: ['type'],\n },\n {\n type: 'object',\n additionalProperties: false,\n properties: {\n type: { const: 'relationship' },\n value: { type: 'string', description: 'The related document ID.' },\n },\n required: ['type'],\n },\n ],\n },\n },\n properties: {\n id: { type: 'string' },\n content: {\n type: 'object',\n properties: {\n root: {\n type: 'object',\n properties: {\n children: { type: 'array', items: { $ref: '#/$defs/LexicalNodes_ABCDEF12' } },\n },\n },\n },\n },\n },\n }\n\n expect(sanitizeEntitySchema(standalone)).toStrictEqual({\n type: 'object',\n $defs: {\n // The node union is renamed to a short, readable `node`, and stays a strict discriminated\n // `oneOf` - not loosened to `anyOf`.\n node: {\n oneOf: [\n {\n type: 'object',\n additionalProperties: false,\n properties: { type: { const: 'paragraph' } },\n required: ['type'],\n },\n {\n type: 'object',\n additionalProperties: false,\n properties: {\n type: { const: 'relationship' },\n // The value is already ID-only from the input variant - left as-is.\n value: { type: 'string', description: 'The related document ID.' },\n },\n required: ['type'],\n },\n ],\n },\n },\n properties: {\n // Managed timestamps are excluded upstream by the `input` variant. `id` stays because it's\n // a valid optional input (a client may supply a custom ID).\n id: { type: 'string' },\n content: {\n type: 'object',\n properties: {\n root: {\n type: 'object',\n properties: {\n children: { type: 'array', items: { $ref: '#/$defs/node' } },\n },\n },\n },\n },\n },\n })\n })\n})\n"],"names":["describe","expect","it","sanitizeEntitySchema","standalone","type","$defs","LexicalNodes_ABCDEF12","oneOf","additionalProperties","properties","const","required","value","description","id","content","root","children","items","$ref","toStrictEqual","node"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,EAAE,QAAQ,SAAQ;AAI7C,SAASC,oBAAoB,QAAQ,4BAA2B;AAEhEH,SAAS,wBAAwB;IAC/BE,GAAG,yFAAyF;QAC1F,gGAAgG;QAChG,kGAAkG;QAClG,mGAAmG;QACnG,uFAAuF;QACvF,MAAME,aAA6B;YACjCC,MAAM;YACNC,OAAO;gBACLC,uBAAuB;oBACrBC,OAAO;wBACL;4BACEH,MAAM;4BACNI,sBAAsB;4BACtBC,YAAY;gCAAEL,MAAM;oCAAEM,OAAO;gCAAY;4BAAE;4BAC3CC,UAAU;gCAAC;6BAAO;wBACpB;wBACA;4BACEP,MAAM;4BACNI,sBAAsB;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/utils/toStandardSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,sBAAsB,EAAE,MAAM,8BAA8B,CAAA;AAE1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElD,gIAAgI;AAChI,eAAO,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"toStandardSchema.d.ts","sourceRoot":"","sources":["../../src/utils/toStandardSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,sBAAsB,EAAE,MAAM,8BAA8B,CAAA;AAE1F,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAElD,gIAAgI;AAChI,eAAO,MAAM,gBAAgB,WAAY,eAAe,KAAG,sBAG/B,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/plugin-mcp",
|
|
3
|
-
"version": "4.0.0-internal.
|
|
3
|
+
"version": "4.0.0-internal.5852d1a",
|
|
4
4
|
"description": "MCP (Model Context Protocol) capabilities with Payload",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"plugin",
|
|
@@ -55,16 +55,16 @@
|
|
|
55
55
|
"bin.js"
|
|
56
56
|
],
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"@modelcontextprotocol/server": "2.0.0-beta.
|
|
58
|
+
"@modelcontextprotocol/server": "2.0.0-beta.3",
|
|
59
59
|
"tsx": "4.22.4",
|
|
60
60
|
"zod": "^4.2.0"
|
|
61
61
|
},
|
|
62
62
|
"devDependencies": {
|
|
63
63
|
"@payloadcms/eslint-config": "3.28.0",
|
|
64
|
-
"payload": "4.0.0-internal.
|
|
64
|
+
"payload": "4.0.0-internal.5852d1a"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
|
-
"payload": "4.0.0-internal.
|
|
67
|
+
"payload": "4.0.0-internal.5852d1a"
|
|
68
68
|
},
|
|
69
69
|
"//deps_notes": {
|
|
70
70
|
"zod": "zod is a hard dependency of @modelcontextprotocol/server, thus we can safely use it without it impacting bundle size. Make extra sure the zod version here matches exactly what's defined in the dependencies of @modelcontextprotocol/server to avoid duplicate versions being installed.",
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
} from '../../../utils/getVirtualFieldNames.js'
|
|
12
12
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
13
13
|
import { validateCollectionData } from '../validateEntityData.js'
|
|
14
|
+
import { fileInputSchema, resolveFileInput } from './fileInput.js'
|
|
14
15
|
import { formatCollectionError } from './formatCollectionError.js'
|
|
15
16
|
|
|
16
17
|
const DEFAULT_DESCRIPTION =
|
|
@@ -28,7 +29,11 @@ export const createDocumentTool = defineCollectionTool({
|
|
|
28
29
|
},
|
|
29
30
|
description: DEFAULT_DESCRIPTION,
|
|
30
31
|
input: z.object({
|
|
31
|
-
data: z
|
|
32
|
+
data: z
|
|
33
|
+
.record(z.string(), z.unknown())
|
|
34
|
+
.describe(
|
|
35
|
+
'The document fields to create. Only include fields permitted by the schema returned by getCollectionSchema.',
|
|
36
|
+
),
|
|
32
37
|
depth: z
|
|
33
38
|
.number()
|
|
34
39
|
.int()
|
|
@@ -39,13 +44,16 @@ export const createDocumentTool = defineCollectionTool({
|
|
|
39
44
|
.default(0),
|
|
40
45
|
draft: z
|
|
41
46
|
.boolean()
|
|
42
|
-
.describe(
|
|
47
|
+
.describe(
|
|
48
|
+
'Only if getCollectionSchema includes _status; otherwise _status does not exist. true forces data._status to "draft"; with false, data._status controls draft or published.',
|
|
49
|
+
)
|
|
43
50
|
.optional()
|
|
44
51
|
.default(false),
|
|
45
52
|
fallbackLocale: z
|
|
46
53
|
.string()
|
|
47
54
|
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
48
55
|
.optional(),
|
|
56
|
+
file: fileInputSchema.optional(),
|
|
49
57
|
locale: z
|
|
50
58
|
.string()
|
|
51
59
|
.describe(
|
|
@@ -63,7 +71,7 @@ export const createDocumentTool = defineCollectionTool({
|
|
|
63
71
|
const payload = req.payload
|
|
64
72
|
const logger = getLogger({ payload })
|
|
65
73
|
|
|
66
|
-
const { data, depth, draft, fallbackLocale, locale, select } = input
|
|
74
|
+
const { data, depth, draft, fallbackLocale, file: fileInput, locale, select } = input
|
|
67
75
|
|
|
68
76
|
logger.info(
|
|
69
77
|
`Creating document in collection: ${collectionSlug}${locale ? ` with locale: ${locale}` : ''}`,
|
|
@@ -79,6 +87,7 @@ export const createDocumentTool = defineCollectionTool({
|
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
const parsedData = transformPointDataToPayload(inputData)
|
|
90
|
+
const file = await resolveFileInput({ collectionSlug, input: fileInput, req })
|
|
82
91
|
|
|
83
92
|
const result = await payload.create({
|
|
84
93
|
collection: collectionSlug,
|
|
@@ -87,6 +96,7 @@ export const createDocumentTool = defineCollectionTool({
|
|
|
87
96
|
draft,
|
|
88
97
|
overrideAccess: authorizedMCP.overrideAccess,
|
|
89
98
|
req,
|
|
99
|
+
...(file ? { file } : {}),
|
|
90
100
|
...(locale ? { locale } : {}),
|
|
91
101
|
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
92
102
|
...(select ? { select: select as SelectType } : {}),
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { describe, expect, it } from 'vitest'
|
|
4
|
+
|
|
5
|
+
import { fileInputSchema, resolveFileInput } from './fileInput.js'
|
|
6
|
+
|
|
7
|
+
describe('MCP file input', () => {
|
|
8
|
+
it('should reject malformed MIME types', () => {
|
|
9
|
+
const result = fileInputSchema.safeParse({
|
|
10
|
+
data: 'aGVsbG8=',
|
|
11
|
+
mimeType: 'text/plain\r\nx-test: value',
|
|
12
|
+
name: 'hello.txt',
|
|
13
|
+
source: 'base64',
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
expect(result.success).toBe(false)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('should reject oversized base64 files', async () => {
|
|
20
|
+
const req = createRequest({ maxFileSize: 4 })
|
|
21
|
+
|
|
22
|
+
await expect(
|
|
23
|
+
resolveFileInput({
|
|
24
|
+
collectionSlug: 'media',
|
|
25
|
+
input: {
|
|
26
|
+
data: Buffer.from('hello').toString('base64'),
|
|
27
|
+
mimeType: 'text/plain',
|
|
28
|
+
name: 'hello.txt',
|
|
29
|
+
source: 'base64',
|
|
30
|
+
},
|
|
31
|
+
req,
|
|
32
|
+
}),
|
|
33
|
+
).rejects.toThrow('File exceeds the 4 byte upload limit.')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it('should reject URLs outside the collection allowlist', async () => {
|
|
37
|
+
const req = createRequest({
|
|
38
|
+
allowList: [{ hostname: 'assets.example.com', protocol: 'https' }],
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
await expect(
|
|
42
|
+
resolveFileInput({
|
|
43
|
+
collectionSlug: 'media',
|
|
44
|
+
input: {
|
|
45
|
+
source: 'url',
|
|
46
|
+
url: 'https://example.com/image.png',
|
|
47
|
+
},
|
|
48
|
+
req,
|
|
49
|
+
}),
|
|
50
|
+
).rejects.toThrow('The provided file URL is not allowed.')
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
function createRequest({
|
|
55
|
+
allowList,
|
|
56
|
+
maxFileSize,
|
|
57
|
+
}: {
|
|
58
|
+
allowList?: Array<{ hostname: string; protocol: 'http' | 'https' }>
|
|
59
|
+
maxFileSize?: number
|
|
60
|
+
}): PayloadRequest {
|
|
61
|
+
return {
|
|
62
|
+
payload: {
|
|
63
|
+
collections: {
|
|
64
|
+
media: {
|
|
65
|
+
config: {
|
|
66
|
+
upload: allowList ? { pasteURL: { allowList } } : {},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
config: {
|
|
71
|
+
upload: {
|
|
72
|
+
limits: {
|
|
73
|
+
fileSize: maxFileSize,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
} as unknown as PayloadRequest
|
|
79
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { CollectionSlug, File, FileData, PayloadRequest } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { APIError } from 'payload'
|
|
4
|
+
import { getExternalFile, isURLAllowed } from 'payload/internal'
|
|
5
|
+
import { sanitizeFilename } from 'payload/shared'
|
|
6
|
+
import { z } from 'zod'
|
|
7
|
+
|
|
8
|
+
const mimeTypeSchema = z
|
|
9
|
+
.string()
|
|
10
|
+
.regex(/^[!#$%&'*+.^`|~\w-]+\/[!#$%&'*+.^`|~\w-]+$/, 'MIME type must use the type/subtype format')
|
|
11
|
+
|
|
12
|
+
export const fileInputSchema = z
|
|
13
|
+
.discriminatedUnion('source', [
|
|
14
|
+
z.object({
|
|
15
|
+
name: z.string().min(1).describe('The file name, including its extension'),
|
|
16
|
+
data: z.string().describe('The base64-encoded file bytes, without a data URL prefix'),
|
|
17
|
+
mimeType: mimeTypeSchema.describe('The file MIME type, for example image/png'),
|
|
18
|
+
source: z.literal('base64'),
|
|
19
|
+
}),
|
|
20
|
+
z.object({
|
|
21
|
+
name: z.string().min(1).describe('Optional file name override').optional(),
|
|
22
|
+
source: z.literal('url'),
|
|
23
|
+
url: z.url().describe('The http or https URL to download'),
|
|
24
|
+
}),
|
|
25
|
+
])
|
|
26
|
+
.describe(
|
|
27
|
+
'A file for an upload collection. Use only a source listed by getCollectionSchema: url for an online file or base64 for a local file.',
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
type FileInput = z.infer<typeof fileInputSchema>
|
|
31
|
+
|
|
32
|
+
export async function resolveFileInput({
|
|
33
|
+
collectionSlug,
|
|
34
|
+
input,
|
|
35
|
+
req,
|
|
36
|
+
}: {
|
|
37
|
+
collectionSlug: CollectionSlug
|
|
38
|
+
input?: FileInput
|
|
39
|
+
req: PayloadRequest
|
|
40
|
+
}): Promise<File | undefined> {
|
|
41
|
+
if (!input) {
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
|
|
46
|
+
|
|
47
|
+
if (!uploadConfig) {
|
|
48
|
+
throw new APIError(`Collection "${collectionSlug}" does not support file uploads.`, 400)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const maxFileSize = req.payload.config.upload.limits?.fileSize
|
|
52
|
+
let file: File
|
|
53
|
+
|
|
54
|
+
if (input.source === 'base64') {
|
|
55
|
+
const data = decodeBase64({ maxFileSize, value: input.data })
|
|
56
|
+
|
|
57
|
+
file = {
|
|
58
|
+
name: sanitizeFilename(input.name),
|
|
59
|
+
data,
|
|
60
|
+
mimetype: input.mimeType,
|
|
61
|
+
size: data.length,
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
if (uploadConfig.pasteURL === false) {
|
|
65
|
+
throw new APIError(
|
|
66
|
+
`Uploading files from URLs is disabled for collection "${collectionSlug}".`,
|
|
67
|
+
400,
|
|
68
|
+
)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const url = new URL(input.url)
|
|
72
|
+
|
|
73
|
+
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
74
|
+
throw new APIError('File URLs must use http or https.', 400)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (
|
|
78
|
+
typeof uploadConfig.pasteURL === 'object' &&
|
|
79
|
+
!isURLAllowed(input.url, uploadConfig.pasteURL.allowList)
|
|
80
|
+
) {
|
|
81
|
+
throw new APIError('The provided file URL is not allowed.', 400)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
file = await getExternalFile({
|
|
85
|
+
data: {
|
|
86
|
+
filename: sanitizeFilename(input.name || getURLFilename(url)),
|
|
87
|
+
url: input.url,
|
|
88
|
+
} as FileData,
|
|
89
|
+
req,
|
|
90
|
+
uploadConfig: {
|
|
91
|
+
...uploadConfig,
|
|
92
|
+
externalFileHeaderFilter: uploadConfig.externalFileHeaderFilter ?? (() => ({})),
|
|
93
|
+
},
|
|
94
|
+
})
|
|
95
|
+
file.mimetype = file.mimetype?.split(';')[0] || 'application/octet-stream'
|
|
96
|
+
file.size = file.data.length
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (maxFileSize !== undefined && Number.isFinite(maxFileSize) && file.size > maxFileSize) {
|
|
100
|
+
throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return file
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function decodeBase64({ maxFileSize, value }: { maxFileSize?: number; value: string }): Buffer {
|
|
107
|
+
const normalized = value.replace(/\s/g, '')
|
|
108
|
+
|
|
109
|
+
if (!/^[a-z0-9+/]*={0,2}$/i.test(normalized) || normalized.length % 4 === 1) {
|
|
110
|
+
throw new APIError('File data must be valid base64.', 400)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (maxFileSize !== undefined && Number.isFinite(maxFileSize)) {
|
|
114
|
+
const paddingLength = normalized.endsWith('==') ? 2 : normalized.endsWith('=') ? 1 : 0
|
|
115
|
+
const decodedSize = Math.floor((normalized.length * 3) / 4) - paddingLength
|
|
116
|
+
|
|
117
|
+
if (decodedSize > maxFileSize) {
|
|
118
|
+
throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const data = Buffer.from(normalized, 'base64')
|
|
123
|
+
|
|
124
|
+
if (data.toString('base64').replace(/=+$/, '') !== normalized.replace(/=+$/, '')) {
|
|
125
|
+
throw new APIError('File data must be valid base64.', 400)
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return data
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getURLFilename(url: URL): string {
|
|
132
|
+
const pathSegment = url.pathname.split('/').pop() || 'upload'
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
return decodeURIComponent(pathSegment)
|
|
136
|
+
} catch {
|
|
137
|
+
return pathSegment
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -39,7 +39,7 @@ export const findDocumentsTool = defineCollectionTool({
|
|
|
39
39
|
draft: z
|
|
40
40
|
.boolean()
|
|
41
41
|
.describe(
|
|
42
|
-
'
|
|
42
|
+
'For versioned collections, true returns the latest draft version when available. False reads the main document.',
|
|
43
43
|
)
|
|
44
44
|
.optional(),
|
|
45
45
|
fallbackLocale: z
|
|
@@ -48,16 +48,29 @@ export const getCollectionSchemaTool = defineCollectionTool({
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
|
|
52
|
+
const maxFileSize = req.payload.config.upload.limits?.fileSize
|
|
53
|
+
const upload = uploadConfig
|
|
54
|
+
? {
|
|
55
|
+
enabled: true,
|
|
56
|
+
filesRequiredOnCreate: uploadConfig.filesRequiredOnCreate !== false,
|
|
57
|
+
mimeTypes: uploadConfig.mimeTypes ?? ['*/*'],
|
|
58
|
+
sources: [...(uploadConfig.pasteURL !== false ? ['url'] : []), 'base64'],
|
|
59
|
+
...(typeof maxFileSize === 'number' && Number.isFinite(maxFileSize) ? { maxFileSize } : {}),
|
|
60
|
+
}
|
|
61
|
+
: { enabled: false }
|
|
62
|
+
|
|
51
63
|
return {
|
|
52
64
|
content: [
|
|
53
65
|
{
|
|
54
66
|
type: 'text',
|
|
55
|
-
text: `Schema for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``,
|
|
67
|
+
text: `Schema for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\`\nUpload configuration:\n\`\`\`json\n${JSON.stringify(upload)}\n\`\`\``,
|
|
56
68
|
},
|
|
57
69
|
],
|
|
58
70
|
structuredContent: {
|
|
59
71
|
collectionSlug,
|
|
60
72
|
schema: inputSchema,
|
|
73
|
+
upload,
|
|
61
74
|
},
|
|
62
75
|
}
|
|
63
76
|
})
|