@payloadcms/plugin-mcp 4.0.0-internal.fa345ef → 4.0.0-internal.fec2230
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 +2 -7
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +6 -13
- package/dist/endpoint/access.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +1 -1
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +35 -23
- 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 +79 -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 +10 -16
- 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 +10 -15
- 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 +104 -54
- 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 +45 -45
- 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 +32 -39
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/fileInput.d.ts +28 -0
- package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.js +88 -0
- package/dist/mcp/builtin/collections/fileInput.js.map +1 -0
- package/dist/mcp/builtin/collections/fileInput.spec.js +86 -0
- package/dist/mcp/builtin/collections/fileInput.spec.js.map +1 -0
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findDistinctTool.js +12 -28
- 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 +29 -48
- 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 +18 -28
- 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 +17 -32
- 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 +51 -11
- 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 +15 -28
- 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 +67 -87
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js +55 -0
- package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -0
- 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 +5 -7
- 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 +10 -15
- 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 +12 -18
- 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 +14 -26
- 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 +14 -31
- 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 +28 -10
- 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 +16 -24
- 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 +28 -37
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtinTools.d.ts +6 -0
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +9 -3
- package/dist/mcp/builtinTools.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +4 -1
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +2 -1
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +48 -11
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -4
- package/dist/types.js.map +1 -1
- package/dist/utils/camelCase.d.ts.map +1 -1
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
- package/dist/utils/toStandardSchema.d.ts.map +1 -1
- package/package.json +5 -7
- package/src/endpoint/access.ts +5 -13
- package/src/mcp/buildMcpServer.ts +39 -29
- package/src/mcp/builtin/collections/authTools.ts +77 -69
- package/src/mcp/builtin/collections/countTool.ts +10 -23
- package/src/mcp/builtin/collections/countVersionsTool.ts +10 -23
- package/src/mcp/builtin/collections/createTool.ts +110 -77
- package/src/mcp/builtin/collections/deleteTool.ts +36 -65
- package/src/mcp/builtin/collections/duplicateTool.ts +36 -87
- package/src/mcp/builtin/collections/fileInput.spec.ts +93 -0
- package/src/mcp/builtin/collections/fileInput.ts +127 -0
- package/src/mcp/builtin/collections/findDistinctTool.ts +12 -64
- package/src/mcp/builtin/collections/findTool.ts +25 -108
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +16 -60
- package/src/mcp/builtin/collections/findVersionsTool.ts +15 -87
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +48 -14
- package/src/mcp/builtin/collections/restoreVersionTool.ts +15 -58
- package/src/mcp/builtin/collections/updateTool.ts +63 -101
- package/src/mcp/builtin/collections/uploadInstructionsTool.ts +64 -0
- package/src/mcp/builtin/formatEntityError.ts +77 -0
- package/src/mcp/builtin/getConfigInfoTool.ts +4 -5
- package/src/mcp/builtin/globals/countVersionsTool.ts +10 -22
- package/src/mcp/builtin/globals/findTool.ts +14 -53
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +14 -54
- package/src/mcp/builtin/globals/findVersionsTool.ts +15 -94
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +30 -8
- package/src/mcp/builtin/globals/restoreVersionTool.ts +17 -48
- package/src/mcp/builtin/globals/updateTool.ts +39 -62
- package/src/mcp/builtinTools.ts +9 -2
- package/src/mcp/sanitizeMCPConfig.ts +4 -1
- package/src/stdio.ts +2 -1
- package/src/types.ts +47 -11
- 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/localAPIDefaults.d.ts +0 -20
- package/dist/utils/localAPIDefaults.d.ts.map +0 -1
- package/dist/utils/localAPIDefaults.js +0 -19
- package/dist/utils/localAPIDefaults.js.map +0 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +0 -11
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.js +0 -32
- 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/localAPIDefaults.ts +0 -22
- package/src/utils/schemaConversion/getEntityInputSchema.ts +0 -62
- 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,83 +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 {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const DEFAULT_DESCRIPTION = 'Create a document in any collection by passing the collection slug and data.';
|
|
11
|
-
export const createDocumentTool = defineCollectionTool({
|
|
12
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.create),
|
|
5
|
+
import { formatEntityError } from '../formatEntityError.js';
|
|
6
|
+
import { fileInputSchema, resolveFile } from './fileInput.js';
|
|
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('Whether to create the document as a draft').optional().default(false),
|
|
25
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
26
|
-
locale: z.string().describe('Optional: locale code to create the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
27
|
-
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
|
|
28
20
|
})
|
|
29
|
-
}).handler(async ({
|
|
21
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
30
22
|
const payload = req.payload;
|
|
23
|
+
const collectionConfig = payload.collections[slug]?.config;
|
|
31
24
|
const logger = getLogger({
|
|
32
25
|
payload
|
|
33
26
|
});
|
|
34
|
-
const {
|
|
35
|
-
|
|
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}`);
|
|
36
30
|
try {
|
|
37
|
-
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config,
|
|
38
|
-
const
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
}
|
|
46
101
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
...
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
...fallbackLocale ? {
|
|
59
|
-
fallbackLocale
|
|
60
|
-
} : {},
|
|
61
|
-
...select ? {
|
|
62
|
-
select: select
|
|
63
|
-
} : {}
|
|
64
|
-
});
|
|
65
|
-
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}`);
|
|
66
113
|
return {
|
|
67
114
|
content: [
|
|
68
115
|
{
|
|
69
116
|
type: 'text',
|
|
70
|
-
text: `
|
|
117
|
+
text: `Created ${docs.length} of ${documents.length} documents in collection "${slug}".${retryMessage}\nResults:\n\`\`\`json\n${JSON.stringify(batchResult)}\n\`\`\`${schemaMessage}`
|
|
71
118
|
}
|
|
72
119
|
],
|
|
73
|
-
doc:
|
|
120
|
+
doc: structuredContent,
|
|
121
|
+
isError: docs.length === 0 && errors.length > 0,
|
|
122
|
+
structuredContent
|
|
74
123
|
};
|
|
75
124
|
} catch (error) {
|
|
76
|
-
const
|
|
77
|
-
logger.error(`Error creating
|
|
78
|
-
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,
|
|
79
129
|
action: 'creating',
|
|
80
|
-
|
|
130
|
+
entity: 'collection',
|
|
81
131
|
error,
|
|
82
132
|
req
|
|
83
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(\n returning ? { doc: result as Record<string, unknown>, index } : { id: result.id, index },\n )\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,QAAQ,8BAA6B;AACvD,SAASC,iBAAiB,QAAQ,0BAAyB;AAC3D,SAASC,eAAe,EAAEC,WAAW,QAAQ,iBAAgB;AAE7D,MAAMC,sBACJ;AAEF,OAAO,MAAMC,sBAAsBN,qBAAqB;IACtDO,QAAQ,CAACC,OACPT,cAAcS,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,OAAO5B,2BAA2B;QAAE6B,MAAMnB;IAAgB;AAC5D,GAAGoB,OAAO,CAAC,OAAO,EAAEX,IAAI,EAAEY,aAAa,EAAEH,KAAK,EAAEI,GAAG,EAAE;IACnD,MAAMC,UAAUD,IAAIC,OAAO;IAC3B,MAAMC,mBAAmBD,QAAQf,WAAW,CAACC,KAAK,EAAEgB;IACpD,MAAMC,SAAS5B,UAAU;QAAEyB;IAAQ;IACnC,MAAM,EACJI,KAAK,EACLC,SAAS,EACTC,KAAK,EACLC,cAAc,EACdC,MAAM,EACNC,QAAQ,EACRC,iBAAiB,EACjBC,SAAS,EACTC,MAAM,EACP,GAAGjB;IACJ,MAAMkB,yBACJP,UAAU,QAAQL,qBAAqBa,aAAa,CAAC7C,0BAA0BgC;IAEjFE,OAAOY,IAAI,CAAC,CAAC,SAAS,EAAEV,UAAUW,MAAM,CAAC,0B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OAAO,GAAG;QAEzDhC,OAAO+B,KAAK,CAAC,CAAC,4BAA4B,EAAEhD,KAAK,EAAE,EAAEiD,SAAS;QAC9D,OAAO3D,kBAAkB;YAAEU;YAAMoD,QAAQ;YAAYC,QAAQ;YAAcL;YAAOnC;QAAI;IACxF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"deleteTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/deleteTool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"deleteTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/deleteTool.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,mBAAmB,4CAuF9B,CAAA"}
|
|
@@ -1,12 +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 { localAPIDefaults } from '../../../utils/localAPIDefaults.js';
|
|
6
|
-
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
7
5
|
const DEFAULT_DESCRIPTION = 'Delete documents in any collection by passing the collection slug and ID or where clause.';
|
|
8
6
|
export const deleteDocumentsTool = defineCollectionTool({
|
|
9
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.
|
|
7
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.delete),
|
|
10
8
|
annotations: {
|
|
11
9
|
destructiveHint: true,
|
|
12
10
|
idempotentHint: false,
|
|
@@ -15,25 +13,44 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
15
13
|
title: 'Delete Documents'
|
|
16
14
|
},
|
|
17
15
|
description: DEFAULT_DESCRIPTION,
|
|
18
|
-
input:
|
|
19
|
-
|
|
20
|
-
z.string(),
|
|
21
|
-
z.number()
|
|
22
|
-
]).describe('Optional: specific document ID to delete').optional(),
|
|
23
|
-
depth: z.number().int().min(0).max(10).describe('Depth of population for relationships in response').optional().default(0),
|
|
24
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
25
|
-
locale: z.string().describe('Optional: locale code for the operation (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
26
|
-
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()
|
|
27
|
-
})
|
|
28
|
-
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
16
|
+
input: deleteDocumentsInputSchema
|
|
17
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
29
18
|
const payload = req.payload;
|
|
30
19
|
const logger = getLogger({
|
|
31
20
|
payload
|
|
32
21
|
});
|
|
33
22
|
const { id, depth, fallbackLocale, locale, where } = input;
|
|
34
|
-
logger.info(`Deleting document from collection: ${
|
|
23
|
+
logger.info(`Deleting document from collection: ${slug}${id ? ` with ID: ${id}` : ' with where clause'}${locale ? `, locale: ${locale}` : ''}`);
|
|
35
24
|
try {
|
|
36
|
-
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) {
|
|
37
54
|
return {
|
|
38
55
|
content: [
|
|
39
56
|
{
|
|
@@ -43,39 +60,22 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
43
60
|
]
|
|
44
61
|
};
|
|
45
62
|
}
|
|
46
|
-
const
|
|
47
|
-
collection:
|
|
63
|
+
const result = await payload.delete({
|
|
64
|
+
collection: slug,
|
|
48
65
|
depth,
|
|
66
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
49
67
|
req,
|
|
50
|
-
|
|
68
|
+
where,
|
|
51
69
|
...locale && {
|
|
52
70
|
locale
|
|
53
71
|
},
|
|
54
|
-
...fallbackLocale && {
|
|
72
|
+
...fallbackLocale !== undefined && {
|
|
55
73
|
fallbackLocale
|
|
56
74
|
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
deleteOptions.where = where;
|
|
62
|
-
}
|
|
63
|
-
const result = await payload.delete(deleteOptions);
|
|
64
|
-
if (id) {
|
|
65
|
-
return {
|
|
66
|
-
content: [
|
|
67
|
-
{
|
|
68
|
-
type: 'text',
|
|
69
|
-
text: `Document deleted successfully from collection "${collectionSlug}"!\nDeleted document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
70
|
-
}
|
|
71
|
-
],
|
|
72
|
-
doc: result
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
const bulkResult = result;
|
|
76
|
-
const docs = bulkResult.docs || [];
|
|
77
|
-
const errors = bulkResult.errors || [];
|
|
78
|
-
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---`;
|
|
79
79
|
if (docs.length > 0) {
|
|
80
80
|
responseText += `\n\nDeleted documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``;
|
|
81
81
|
}
|
|
@@ -96,12 +96,12 @@ export const deleteDocumentsTool = defineCollectionTool({
|
|
|
96
96
|
};
|
|
97
97
|
} catch (error) {
|
|
98
98
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
99
|
-
logger.error(`Error deleting document from ${
|
|
99
|
+
logger.error(`Error deleting document from ${slug}: ${errorMessage}`);
|
|
100
100
|
return {
|
|
101
101
|
content: [
|
|
102
102
|
{
|
|
103
103
|
type: 'text',
|
|
104
|
-
text: `Error deleting document from collection "${
|
|
104
|
+
text: `Error deleting document from collection "${slug}": ${errorMessage}`
|
|
105
105
|
}
|
|
106
106
|
]
|
|
107
107
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/deleteTool.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/deleteTool.ts"],"sourcesContent":["import { deleteDocumentsInputSchema, parseDocumentID } from 'payload'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Delete documents in any collection by passing the collection slug and ID or where clause.'\n\nexport const deleteDocumentsTool = defineCollectionTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.delete),\n annotations: {\n destructiveHint: true,\n idempotentHint: false,\n openWorldHint: false,\n readOnlyHint: false,\n title: 'Delete Documents',\n },\n description: DEFAULT_DESCRIPTION,\n input: deleteDocumentsInputSchema,\n}).handler(async ({ slug, authorizedMCP, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const { id, depth, fallbackLocale, locale, where } = input\n\n logger.info(\n `Deleting document from collection: ${slug}${id ? ` with ID: ${id}` : ' with where clause'}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n if (id !== undefined) {\n const result = await payload.delete({\n id: parseDocumentID({ id, collectionSlug: slug, payload }),\n collection: slug,\n depth,\n overrideAccess: authorizedMCP.overrideAccess,\n req,\n ...(locale && { locale }),\n ...(fallbackLocale !== undefined && { fallbackLocale }),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Document deleted successfully from collection \"${slug}\"!\\nDeleted document:\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n }\n\n if (where === undefined) {\n return {\n content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],\n }\n }\n\n const result = await payload.delete({\n collection: slug,\n depth,\n overrideAccess: authorizedMCP.overrideAccess,\n req,\n where,\n ...(locale && { locale }),\n ...(fallbackLocale !== undefined && { fallbackLocale }),\n })\n const docs = result.docs || []\n const errors = result.errors || []\n\n let responseText = `Document deleted successfully from collection \"${slug}\"!\\nDeleted: ${docs.length} documents\\nErrors: ${errors.length}\\n---`\n if (docs.length > 0) {\n responseText += `\\n\\nDeleted 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\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 deleting document from ${slug}: ${errorMessage}`)\n return {\n content: [\n {\n type: 'text',\n text: `Error deleting document from collection \"${slug}\": ${errorMessage}`,\n },\n ],\n }\n }\n})\n"],"names":["deleteDocumentsInputSchema","parseDocumentID","defaultAccess","defineCollectionTool","getLogger","DEFAULT_DESCRIPTION","deleteDocumentsTool","access","args","Boolean","permissions","collections","slug","delete","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","handler","authorizedMCP","req","payload","logger","id","depth","fallbackLocale","locale","where","info","undefined","result","collectionSlug","collection","overrideAccess","content","type","text","JSON","stringify","doc","docs","errors","responseText","length","error","errorMessage","Error","message"],"mappings":"AAAA,SAASA,0BAA0B,EAAEC,eAAe,QAAQ,UAAS;AAErE,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AAEvD,MAAMC,sBACJ;AAEF,OAAO,MAAMC,sBAAsBH,qBAAqB;IACtDI,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,OAAOrB;AACT,GAAGsB,OAAO,CAAC,OAAO,EAAEV,IAAI,EAAEW,aAAa,EAAEF,KAAK,EAAEG,GAAG,EAAE;IACnD,MAAMC,UAAUD,IAAIC,OAAO;IAC3B,MAAMC,SAAStB,UAAU;QAAEqB;IAAQ;IAEnC,MAAM,EAAEE,EAAE,EAAEC,KAAK,EAAEC,cAAc,EAAEC,MAAM,EAAEC,KAAK,EAAE,GAAGV;IAErDK,OAAOM,IAAI,CACT,CAAC,mCAAmC,EAAEpB,OAAOe,KAAK,CAAC,UAAU,EAAEA,IAAI,GAAG,uBAAuBG,SAAS,CAAC,UAAU,EAAEA,QAAQ,GAAG,IAAI;IAGpI,IAAI;QACF,IAAIH,OAAOM,WAAW;YACpB,MAAMC,SAAS,MAAMT,QAAQZ,MAAM,CAAC;gBAClCc,IAAI1B,gBAAgB;oBAAE0B;oBAAIQ,gBAAgBvB;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K;gBAAEC;gBAAMC;YAAO;QACtB;IACF,EAAE,OAAOG,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9DzB,OAAOsB,KAAK,CAAC,CAAC,6BAA6B,EAAEpC,KAAK,EAAE,EAAEqC,cAAc;QACpE,OAAO;YACLX,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,yCAAyC,EAAE5B,KAAK,GAAG,EAAEqC,cAAc;gBAC5E;aACD;QACH;IACF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"duplicateTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/duplicateTool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"duplicateTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/collections/duplicateTool.ts"],"names":[],"mappings":"AAiBA,eAAO,MAAM,qBAAqB,4CAkEhC,CAAA"}
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { duplicateDocumentInputSchema, getCollectionVirtualFieldNames, parseDocumentID, 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 { localAPIDefaults } from '../../../utils/localAPIDefaults.js';
|
|
7
|
-
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
8
|
-
import { formatCollectionError } from './formatCollectionError.js';
|
|
5
|
+
import { formatEntityError } from '../formatEntityError.js';
|
|
9
6
|
const DEFAULT_DESCRIPTION = 'Duplicate a document in any collection by passing the collection slug and source document ID.';
|
|
10
7
|
export const duplicateDocumentTool = defineCollectionTool({
|
|
11
|
-
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.
|
|
8
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.create),
|
|
12
9
|
annotations: {
|
|
13
10
|
destructiveHint: false,
|
|
14
11
|
idempotentHint: false,
|
|
@@ -17,77 +14,73 @@ export const duplicateDocumentTool = defineCollectionTool({
|
|
|
17
14
|
title: 'Duplicate Document'
|
|
18
15
|
},
|
|
19
16
|
description: DEFAULT_DESCRIPTION,
|
|
20
|
-
input:
|
|
21
|
-
|
|
22
|
-
z.string(),
|
|
23
|
-
z.number()
|
|
24
|
-
]).describe('The ID of the document to duplicate'),
|
|
25
|
-
data: z.record(z.string(), z.unknown()).describe('Optional: fields to override on the duplicated document').optional(),
|
|
26
|
-
depth: z.number().int().min(0).max(10).describe('How many levels deep to populate relationships in response').optional().default(0),
|
|
27
|
-
draft: z.boolean().describe('Whether to create the duplicate as a draft').optional().default(false),
|
|
28
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
29
|
-
locale: z.string().describe('Optional: locale code to duplicate in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
30
|
-
populate: z.record(z.string(), z.unknown()).describe('Optional: control which fields to include from populated relationship or upload documents.').optional(),
|
|
31
|
-
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(),
|
|
32
|
-
selectedLocales: z.array(z.string()).describe('Optional: localized field locales to include in the duplicated document').optional(),
|
|
33
|
-
showHiddenFields: z.boolean().describe('Optional: include hidden fields in the duplicated document response').optional()
|
|
34
|
-
})
|
|
35
|
-
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
17
|
+
input: duplicateDocumentInputSchema
|
|
18
|
+
}).handler(async ({ slug, authorizedMCP, input, req })=>{
|
|
36
19
|
const payload = req.payload;
|
|
37
20
|
const logger = getLogger({
|
|
38
21
|
payload
|
|
39
22
|
});
|
|
40
|
-
const { id, data, depth, draft, fallbackLocale, locale, populate, select, selectedLocales
|
|
41
|
-
logger.info(`Duplicating document in collection: ${
|
|
23
|
+
const { id, data, depth, draft, fallbackLocale, locale, populate, select, selectedLocales } = input;
|
|
24
|
+
logger.info(`Duplicating document in collection: ${slug} with ID: ${id}`);
|
|
42
25
|
try {
|
|
43
|
-
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config,
|
|
26
|
+
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, slug);
|
|
44
27
|
const inputData = data ? stripVirtualFields(data, virtualFieldNames) : undefined;
|
|
28
|
+
if (inputData) {
|
|
29
|
+
validateCollectionData({
|
|
30
|
+
slug,
|
|
31
|
+
data: inputData,
|
|
32
|
+
partial: true,
|
|
33
|
+
req
|
|
34
|
+
});
|
|
35
|
+
}
|
|
45
36
|
const parsedData = inputData ? transformPointDataToPayload(inputData) : undefined;
|
|
46
37
|
const result = await payload.duplicate({
|
|
47
|
-
id
|
|
48
|
-
|
|
38
|
+
id: parseDocumentID({
|
|
39
|
+
id,
|
|
40
|
+
collectionSlug: slug,
|
|
41
|
+
payload
|
|
42
|
+
}),
|
|
43
|
+
collection: slug,
|
|
49
44
|
depth,
|
|
50
45
|
draft,
|
|
46
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
51
47
|
req,
|
|
52
|
-
...localAPIDefaults(authorizedMCP),
|
|
53
48
|
...parsedData ? {
|
|
54
49
|
data: parsedData
|
|
55
50
|
} : {},
|
|
56
51
|
...locale ? {
|
|
57
52
|
locale
|
|
58
53
|
} : {},
|
|
59
|
-
...fallbackLocale ? {
|
|
54
|
+
...fallbackLocale !== undefined ? {
|
|
60
55
|
fallbackLocale
|
|
61
56
|
} : {},
|
|
62
57
|
...populate ? {
|
|
63
|
-
populate
|
|
58
|
+
populate
|
|
64
59
|
} : {},
|
|
65
60
|
...select ? {
|
|
66
|
-
select
|
|
61
|
+
select
|
|
67
62
|
} : {},
|
|
68
63
|
...selectedLocales ? {
|
|
69
64
|
selectedLocales
|
|
70
|
-
} : {},
|
|
71
|
-
...showHiddenFields !== undefined ? {
|
|
72
|
-
showHiddenFields
|
|
73
65
|
} : {}
|
|
74
66
|
});
|
|
75
|
-
logger.info(`Successfully duplicated document in ${
|
|
67
|
+
logger.info(`Successfully duplicated document in ${slug} from ID: ${id}`);
|
|
76
68
|
return {
|
|
77
69
|
content: [
|
|
78
70
|
{
|
|
79
71
|
type: 'text',
|
|
80
|
-
text: `Document duplicated successfully in collection "${
|
|
72
|
+
text: `Document duplicated successfully in collection "${slug}"!\nDuplicated document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
81
73
|
}
|
|
82
74
|
],
|
|
83
75
|
doc: result
|
|
84
76
|
};
|
|
85
77
|
} catch (error) {
|
|
86
78
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
87
|
-
logger.error(`Error duplicating document in ${
|
|
88
|
-
return
|
|
79
|
+
logger.error(`Error duplicating document in ${slug}: ${errorMessage}`);
|
|
80
|
+
return formatEntityError({
|
|
81
|
+
slug,
|
|
89
82
|
action: 'duplicating',
|
|
90
|
-
|
|
83
|
+
entity: 'collection',
|
|
91
84
|
error,
|
|
92
85
|
req
|
|
93
86
|
});
|