@payloadcms/plugin-mcp 4.0.0-internal.40de3ec → 4.0.0-internal.435b2d6
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/LICENSE.md +1 -1
- package/bin.js +7 -0
- package/dist/defaultAccess.d.ts +3 -0
- package/dist/defaultAccess.d.ts.map +1 -0
- package/dist/defaultAccess.js +3 -0
- package/dist/defaultAccess.js.map +1 -0
- package/dist/defineTool.d.ts +8 -8
- package/dist/defineTool.d.ts.map +1 -1
- package/dist/endpoint/access.d.ts +12 -5
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/endpoint/access.js +75 -88
- package/dist/endpoint/access.js.map +1 -1
- package/dist/endpoint/index.d.ts +1 -1
- package/dist/endpoint/index.d.ts.map +1 -1
- package/dist/endpoint/index.js +68 -17
- package/dist/endpoint/index.js.map +1 -1
- package/dist/exports/client.d.ts +1 -1
- package/dist/exports/client.d.ts.map +1 -1
- package/dist/exports/client.js +1 -1
- package/dist/exports/client.js.map +1 -1
- package/dist/exports/internal.d.ts +2 -0
- package/dist/exports/internal.d.ts.map +1 -0
- package/dist/exports/internal.js +3 -0
- package/dist/exports/internal.js.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -20
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +4 -4
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +133 -80
- 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 +44 -6
- package/dist/mcp/builtin/collections/authTools.js.map +1 -1
- package/dist/mcp/builtin/collections/countTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/countTool.js +68 -0
- package/dist/mcp/builtin/collections/countTool.js.map +1 -0
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/countVersionsTool.js +64 -0
- package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -0
- 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 +50 -45
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js +15 -22
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/duplicateTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/duplicateTool.js +96 -0
- package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -0
- 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/findDistinctTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findDistinctTool.js +92 -0
- package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -0
- package/dist/mcp/builtin/collections/findTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +52 -47
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.js +83 -0
- package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -0
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findVersionsTool.js +98 -0
- package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +9 -0
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/formatCollectionError.js +60 -0
- package/dist/mcp/builtin/collections/formatCollectionError.js.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +86 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.js +81 -0
- package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -0
- package/dist/mcp/builtin/collections/updateTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +106 -93
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/getConfigInfoTool.d.ts +2 -0
- package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -0
- package/dist/mcp/builtin/getConfigInfoTool.js +68 -0
- package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -0
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/countVersionsTool.js +64 -0
- package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/findTool.js +20 -23
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.js +79 -0
- package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/findVersionsTool.js +94 -0
- package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +65 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.js +77 -0
- package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -0
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js +33 -42
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtin/validateEntityData.d.ts +14 -0
- package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -0
- package/dist/mcp/builtin/validateEntityData.js +82 -0
- package/dist/mcp/builtin/validateEntityData.js.map +1 -0
- package/dist/mcp/builtinTools.d.ts +144 -19
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +119 -20
- package/dist/mcp/builtinTools.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts +0 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +121 -58
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/stdio.d.ts +3 -3
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +82 -45
- package/dist/stdio.js.map +1 -1
- package/dist/types.d.ts +112 -110
- 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/getPluginConfig.d.ts +1 -1
- package/dist/utils/getPluginConfig.js +1 -1
- package/dist/utils/getPluginConfig.js.map +1 -1
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts +28 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +1 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.js +71 -0
- package/dist/utils/schemaConversion/filterFieldsByAccess.js.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +13 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js +66 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +5 -10
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +15 -95
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +15 -40
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -1
- package/dist/utils/toStandardSchema.d.ts.map +1 -1
- package/dist/utils/whereSchema.d.ts +9 -0
- package/dist/utils/whereSchema.d.ts.map +1 -0
- package/dist/utils/whereSchema.js +13 -0
- package/dist/utils/whereSchema.js.map +1 -0
- package/package.json +10 -15
- package/src/defaultAccess.ts +3 -0
- package/src/endpoint/access.ts +81 -101
- package/src/endpoint/index.ts +58 -16
- package/src/exports/client.ts +2 -1
- package/src/exports/internal.ts +1 -0
- package/src/index.ts +12 -20
- package/src/mcp/buildMcpServer.ts +170 -115
- package/src/mcp/builtin/collections/authTools.ts +45 -9
- package/src/mcp/builtin/collections/countTool.ts +73 -0
- package/src/mcp/builtin/collections/countVersionsTool.ts +69 -0
- package/src/mcp/builtin/collections/createTool.ts +67 -62
- package/src/mcp/builtin/collections/deleteTool.ts +20 -18
- package/src/mcp/builtin/collections/duplicateTool.ts +134 -0
- package/src/mcp/builtin/collections/fileInput.spec.ts +79 -0
- package/src/mcp/builtin/collections/fileInput.ts +139 -0
- package/src/mcp/builtin/collections/findDistinctTool.ts +119 -0
- package/src/mcp/builtin/collections/findTool.ts +66 -37
- package/src/mcp/builtin/collections/findVersionByIDTool.ts +109 -0
- package/src/mcp/builtin/collections/findVersionsTool.ts +154 -0
- package/src/mcp/builtin/collections/formatCollectionError.ts +84 -0
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +76 -0
- package/src/mcp/builtin/collections/restoreVersionTool.ts +107 -0
- package/src/mcp/builtin/collections/updateTool.ts +132 -115
- package/src/mcp/builtin/getConfigInfoTool.ts +67 -0
- package/src/mcp/builtin/globals/countVersionsTool.ts +68 -0
- package/src/mcp/builtin/globals/findTool.ts +28 -19
- package/src/mcp/builtin/globals/findVersionByIDTool.ts +103 -0
- package/src/mcp/builtin/globals/findVersionsTool.ts +147 -0
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +63 -0
- package/src/mcp/builtin/globals/restoreVersionTool.ts +99 -0
- package/src/mcp/builtin/globals/updateTool.ts +52 -57
- package/src/mcp/builtin/validateEntityData.ts +132 -0
- package/src/mcp/builtinTools.ts +111 -41
- package/src/mcp/sanitizeMCPConfig.ts +117 -78
- package/src/stdio.ts +77 -45
- package/src/types.ts +128 -115
- package/src/utils/getPluginConfig.ts +1 -1
- package/src/utils/schemaConversion/filterFieldsByAccess.ts +123 -0
- package/src/utils/schemaConversion/getEntityInputSchema.ts +96 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +12 -19
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +17 -114
- package/src/utils/whereSchema.ts +24 -0
- package/dist/@types/assets.d.js +0 -2
- package/dist/@types/assets.d.js.map +0 -1
- package/dist/collection/getAccessField.d.ts +0 -12
- package/dist/collection/getAccessField.d.ts.map +0 -1
- package/dist/collection/getAccessField.js +0 -57
- package/dist/collection/getAccessField.js.map +0 -1
- package/dist/collection/index.d.ts +0 -6
- package/dist/collection/index.d.ts.map +0 -1
- package/dist/collection/index.js +0 -60
- package/dist/collection/index.js.map +0 -1
- package/dist/components/AccessField/index.client.d.ts +0 -10
- package/dist/components/AccessField/index.client.d.ts.map +0 -1
- package/dist/components/AccessField/index.client.js +0 -305
- package/dist/components/AccessField/index.client.js.map +0 -1
- package/dist/components/AccessField/index.css +0 -93
- 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/buildToolInput.d.ts +0 -29
- package/dist/utils/schemaConversion/buildToolInput.d.ts.map +0 -1
- package/dist/utils/schemaConversion/buildToolInput.js +0 -51
- package/dist/utils/schemaConversion/buildToolInput.js.map +0 -1
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +0 -7
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js +0 -20
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +0 -1
- package/src/@types/assets.d.ts +0 -3
- package/src/collection/getAccessField.ts +0 -64
- package/src/collection/index.ts +0 -64
- package/src/components/AccessField/index.client.tsx +0 -344
- package/src/components/AccessField/index.css +0 -93
- package/src/utils/localAPIDefaults.ts +0 -22
- package/src/utils/schemaConversion/buildToolInput.ts +0 -68
- package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
|
@@ -1,47 +1,48 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { defaultAccess } from '../../../defaultAccess.js';
|
|
2
3
|
import { defineCollectionTool } from '../../../defineTool.js';
|
|
3
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
4
5
|
import { getCollectionVirtualFieldNames, stripVirtualFields } from '../../../utils/getVirtualFieldNames.js';
|
|
5
|
-
import {
|
|
6
|
-
import { buildToolInput } from '../../../utils/schemaConversion/buildToolInput.js';
|
|
7
|
-
import { sanitizeEntitySchema } from '../../../utils/schemaConversion/sanitizeEntitySchema.js';
|
|
6
|
+
import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js';
|
|
8
7
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
9
|
+
import { validateCollectionData } from '../validateEntityData.js';
|
|
10
|
+
import { fileInputSchema, resolveFileInput } from './fileInput.js';
|
|
11
|
+
import { formatCollectionError } from './formatCollectionError.js';
|
|
12
|
+
const DEFAULT_DESCRIPTION = 'Update documents in any collection by passing the collection slug and data.';
|
|
13
|
+
export const updateDocumentTool = defineCollectionTool({
|
|
14
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.update),
|
|
15
|
+
annotations: {
|
|
16
|
+
destructiveHint: true,
|
|
17
|
+
idempotentHint: false,
|
|
18
|
+
openWorldHint: false,
|
|
19
|
+
readOnlyHint: false,
|
|
20
|
+
title: 'Update Document'
|
|
21
|
+
},
|
|
11
22
|
description: DEFAULT_DESCRIPTION,
|
|
12
|
-
input: ({
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
locale: z.string().describe('Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
30
|
-
overrideLock: z.boolean().describe('Whether to override document locks').optional().default(true),
|
|
31
|
-
overwriteExistingFiles: z.boolean().describe('Whether to overwrite existing files').optional().default(false),
|
|
32
|
-
select: z.string().describe('Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"title": "My Post"}\'').optional(),
|
|
33
|
-
where: z.string().describe('JSON string for where clause to update multiple documents').optional()
|
|
34
|
-
},
|
|
35
|
-
dataDescription: 'The fields to update',
|
|
36
|
-
dataSchema: partialSchema
|
|
37
|
-
});
|
|
38
|
-
}
|
|
23
|
+
input: z.object({
|
|
24
|
+
id: z.union([
|
|
25
|
+
z.string(),
|
|
26
|
+
z.number()
|
|
27
|
+
]).describe('The ID of the document to update').optional(),
|
|
28
|
+
data: z.record(z.string(), z.unknown()).describe('The fields to update. Only include fields permitted by the schema returned by getCollectionSchema.'),
|
|
29
|
+
depth: z.number().describe('How many levels deep to populate relationships').optional().default(0),
|
|
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),
|
|
31
|
+
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
32
|
+
file: fileInputSchema.optional(),
|
|
33
|
+
locale: z.string().describe('Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
34
|
+
overrideLock: z.boolean().describe('Whether to override document locks').optional().default(true),
|
|
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(),
|
|
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(),
|
|
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()
|
|
39
|
+
})
|
|
39
40
|
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
40
41
|
const payload = req.payload;
|
|
41
42
|
const logger = getLogger({
|
|
42
43
|
payload
|
|
43
44
|
});
|
|
44
|
-
const { id, data, depth, draft, fallbackLocale,
|
|
45
|
+
const { id, data, depth, draft, fallbackLocale, file: fileInput, locale, overrideLock, overwriteExistingFiles, publishAllLocales, select, where } = input;
|
|
45
46
|
logger.info(`Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`);
|
|
46
47
|
try {
|
|
47
48
|
if (!id && !where) {
|
|
@@ -54,68 +55,53 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
54
55
|
]
|
|
55
56
|
};
|
|
56
57
|
}
|
|
57
|
-
let parsedData = transformPointDataToPayload(data);
|
|
58
58
|
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
{
|
|
69
|
-
type: 'text',
|
|
70
|
-
text: 'Error: Invalid JSON in where clause'
|
|
71
|
-
}
|
|
72
|
-
]
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
let selectClause;
|
|
77
|
-
if (select) {
|
|
78
|
-
try {
|
|
79
|
-
selectClause = JSON.parse(select);
|
|
80
|
-
} catch {
|
|
81
|
-
logger.warn(`Invalid select clause JSON: ${select}`);
|
|
82
|
-
return {
|
|
83
|
-
content: [
|
|
84
|
-
{
|
|
85
|
-
type: 'text',
|
|
86
|
-
text: 'Error: Invalid JSON in select clause'
|
|
87
|
-
}
|
|
88
|
-
]
|
|
89
|
-
};
|
|
90
|
-
}
|
|
59
|
+
const inputData = stripVirtualFields(data, virtualFieldNames);
|
|
60
|
+
const validationError = validateCollectionData({
|
|
61
|
+
collectionSlug,
|
|
62
|
+
data: inputData,
|
|
63
|
+
partial: true,
|
|
64
|
+
req
|
|
65
|
+
});
|
|
66
|
+
if (validationError) {
|
|
67
|
+
return validationError;
|
|
91
68
|
}
|
|
69
|
+
const parsedData = transformPointDataToPayload(inputData);
|
|
70
|
+
const file = await resolveFileInput({
|
|
71
|
+
collectionSlug,
|
|
72
|
+
input: fileInput,
|
|
73
|
+
req
|
|
74
|
+
});
|
|
75
|
+
const whereClause = where ?? {};
|
|
92
76
|
if (id) {
|
|
93
|
-
const
|
|
77
|
+
const result = await payload.update({
|
|
94
78
|
id,
|
|
95
79
|
collection: collectionSlug,
|
|
96
80
|
data: parsedData,
|
|
97
81
|
depth,
|
|
98
82
|
draft,
|
|
83
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
99
84
|
overrideLock,
|
|
100
85
|
req,
|
|
101
|
-
...
|
|
102
|
-
|
|
103
|
-
filePath
|
|
86
|
+
...file ? {
|
|
87
|
+
file
|
|
104
88
|
} : {},
|
|
105
89
|
...overwriteExistingFiles ? {
|
|
106
90
|
overwriteExistingFiles
|
|
107
91
|
} : {},
|
|
92
|
+
...publishAllLocales !== undefined ? {
|
|
93
|
+
publishAllLocales
|
|
94
|
+
} : {},
|
|
108
95
|
...locale ? {
|
|
109
96
|
locale
|
|
110
97
|
} : {},
|
|
111
98
|
...fallbackLocale ? {
|
|
112
99
|
fallbackLocale
|
|
113
100
|
} : {},
|
|
114
|
-
...
|
|
115
|
-
select:
|
|
101
|
+
...select ? {
|
|
102
|
+
select: select
|
|
116
103
|
} : {}
|
|
117
|
-
};
|
|
118
|
-
const result = await payload.update(updateOptions);
|
|
104
|
+
});
|
|
119
105
|
return {
|
|
120
106
|
content: [
|
|
121
107
|
{
|
|
@@ -126,41 +112,70 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
126
112
|
doc: result
|
|
127
113
|
};
|
|
128
114
|
}
|
|
129
|
-
const
|
|
115
|
+
const result = await payload.update({
|
|
130
116
|
collection: collectionSlug,
|
|
131
117
|
data: parsedData,
|
|
132
118
|
depth,
|
|
133
119
|
draft,
|
|
120
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
134
121
|
overrideLock,
|
|
135
122
|
req,
|
|
136
|
-
...localAPIDefaults(authorizedMCP),
|
|
137
123
|
where: whereClause,
|
|
138
|
-
...
|
|
139
|
-
|
|
124
|
+
...file ? {
|
|
125
|
+
file
|
|
140
126
|
} : {},
|
|
141
127
|
...overwriteExistingFiles ? {
|
|
142
128
|
overwriteExistingFiles
|
|
143
129
|
} : {},
|
|
130
|
+
...publishAllLocales !== undefined ? {
|
|
131
|
+
publishAllLocales
|
|
132
|
+
} : {},
|
|
144
133
|
...locale ? {
|
|
145
134
|
locale
|
|
146
135
|
} : {},
|
|
147
136
|
...fallbackLocale ? {
|
|
148
137
|
fallbackLocale
|
|
149
138
|
} : {},
|
|
150
|
-
...
|
|
151
|
-
select:
|
|
139
|
+
...select ? {
|
|
140
|
+
select: select
|
|
152
141
|
} : {}
|
|
153
|
-
};
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
const docs = bulkResult.docs || [];
|
|
157
|
-
const errors = bulkResult.errors || [];
|
|
142
|
+
});
|
|
143
|
+
const docs = result.docs || [];
|
|
144
|
+
const errors = result.errors || [];
|
|
158
145
|
let responseText = `Multiple documents updated in collection "${collectionSlug}"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`;
|
|
159
146
|
if (docs.length > 0) {
|
|
160
147
|
responseText += `\n\nUpdated documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``;
|
|
161
148
|
}
|
|
162
149
|
if (errors.length > 0) {
|
|
163
150
|
responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``;
|
|
151
|
+
const errorSchema = getCollectionInputSchema({
|
|
152
|
+
collectionSlug,
|
|
153
|
+
req
|
|
154
|
+
});
|
|
155
|
+
if (errorSchema) {
|
|
156
|
+
responseText += `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(errorSchema)}\n\`\`\``;
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
content: [
|
|
160
|
+
{
|
|
161
|
+
type: 'text',
|
|
162
|
+
text: responseText
|
|
163
|
+
}
|
|
164
|
+
],
|
|
165
|
+
doc: {
|
|
166
|
+
docs,
|
|
167
|
+
errors
|
|
168
|
+
},
|
|
169
|
+
isError: true,
|
|
170
|
+
...errorSchema ? {
|
|
171
|
+
structuredContent: {
|
|
172
|
+
collectionSlug,
|
|
173
|
+
docs,
|
|
174
|
+
errors,
|
|
175
|
+
schema: errorSchema
|
|
176
|
+
}
|
|
177
|
+
} : {}
|
|
178
|
+
};
|
|
164
179
|
}
|
|
165
180
|
return {
|
|
166
181
|
content: [
|
|
@@ -177,14 +192,12 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
177
192
|
} catch (error) {
|
|
178
193
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
179
194
|
logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`);
|
|
180
|
-
return {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
]
|
|
187
|
-
};
|
|
195
|
+
return formatCollectionError({
|
|
196
|
+
action: 'updating',
|
|
197
|
+
collectionSlug,
|
|
198
|
+
error,
|
|
199
|
+
req
|
|
200
|
+
});
|
|
188
201
|
}
|
|
189
202
|
});
|
|
190
203
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/updateTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getCollectionVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\nimport { buildToolInput } from '../../../utils/schemaConversion/buildToolInput.js'\nimport { sanitizeEntitySchema } from '../../../utils/schemaConversion/sanitizeEntitySchema.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\n\nconst DEFAULT_DESCRIPTION = 'Update documents in a collection by ID or where clause.'\n\nexport const updateCollectionTool = defineCollectionTool({\n description: DEFAULT_DESCRIPTION,\n input: ({ collectionSchema }) => {\n const partialSchema = sanitizeEntitySchema(collectionSchema)\n\n // Collection updates do not require all required fields to be passed => delete .required.\n //\n // Local API equivalent: packages/payload/src/collections/operations/local/update.ts#BaseOptions#data:\n // data: DeepPartial<RequiredDataFromCollectionSlug<TSlug>>\n delete partialSchema.required\n\n return buildToolInput({\n controls: {\n id: z\n .union([z.string(), z.number()])\n .describe('The ID of the document to update')\n .optional(),\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 .string()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n )\n .optional(),\n where: z\n .string()\n .describe('JSON string for where clause to update multiple documents')\n .optional(),\n },\n dataDescription: 'The fields to update',\n dataSchema: partialSchema,\n })\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 let parsedData = transformPointDataToPayload(data)\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)\n parsedData = stripVirtualFields(parsedData, virtualFieldNames)\n\n let whereClause: Record<string, unknown> = {}\n if (where) {\n try {\n whereClause = JSON.parse(where) as Record<string, unknown>\n } catch {\n logger.error(`Invalid where clause JSON: ${where}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch {\n logger.warn(`Invalid select clause JSON: ${select}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }\n }\n }\n\n if (id) {\n const updateOptions = {\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n ...localAPIDefaults(authorizedMCP),\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n }\n\n const result = await payload.update(updateOptions as any)\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 updateOptions = {\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n ...localAPIDefaults(authorizedMCP),\n where: whereClause,\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n }\n\n const result = await payload.update(updateOptions as any)\n\n const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }\n const docs = bulkResult.docs || []\n const errors = bulkResult.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\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 {\n content: [\n {\n type: 'text',\n text: `Error updating document in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n }\n})\n"],"names":["z","defineCollectionTool","getLogger","getCollectionVirtualFieldNames","stripVirtualFields","localAPIDefaults","buildToolInput","sanitizeEntitySchema","transformPointDataToPayload","DEFAULT_DESCRIPTION","updateCollectionTool","description","input","collectionSchema","partialSchema","required","controls","id","union","string","number","describe","optional","depth","default","draft","boolean","fallbackLocale","filePath","locale","overrideLock","overwriteExistingFiles","select","where","dataDescription","dataSchema","handler","authorizedMCP","collectionSlug","req","payload","logger","data","info","content","type","text","parsedData","virtualFieldNames","config","whereClause","JSON","parse","error","selectClause","warn","updateOptions","collection","result","update","stringify","doc","bulkResult","docs","errors","responseText","length","errorMessage","Error","message"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AACvD,SACEC,8BAA8B,EAC9BC,kBAAkB,QACb,yCAAwC;AAC/C,SAASC,gBAAgB,QAAQ,qCAAoC;AACrE,SAASC,cAAc,QAAQ,oDAAmD;AAClF,SAASC,oBAAoB,QAAQ,0DAAyD;AAC9F,SAASC,2BAA2B,QAAQ,gDAA+C;AAE3F,MAAMC,sBAAsB;AAE5B,OAAO,MAAMC,uBAAuBT,qBAAqB;IACvDU,aAAaF;IACbG,OAAO,CAAC,EAAEC,gBAAgB,EAAE;QAC1B,MAAMC,gBAAgBP,qBAAqBM;QAE3C,0FAA0F;QAC1F,EAAE;QACF,sGAAsG;QACtG,2DAA2D;QAC3D,OAAOC,cAAcC,QAAQ;QAE7B,OAAOT,eAAe;YACpBU,UAAU;gBACRC,IAAIjB,EACDkB,KAAK,CAAC;oBAAClB,EAAEmB,MAAM;oBAAInB,EAAEoB,MAAM;iBAAG,EAC9BC,QAAQ,CAAC,oCACTC,QAAQ;gBACXC,OAAOvB,EACJoB,MAAM,GACNC,QAAQ,CAAC,kDACTC,QAAQ,GACRE,OAAO,CAAC;gBACXC,OAAOzB,EACJ0B,OAAO,GACPL,QAAQ,CAAC,6CACTC,QAAQ,GACRE,OAAO,CAAC;gBACXG,gBAAgB3B,EACbmB,MAAM,GACNE,QAAQ,CAAC,gFACTC,QAAQ;gBACXM,UAAU5B,EAAEmB,MAAM,GAAGE,QAAQ,CAAC,8BAA8BC,QAAQ;gBACpEO,QAAQ7B,EACLmB,MAAM,GACNE,QAAQ,CACP,sGAEDC,QAAQ;gBACXQ,cAAc9B,EACX0B,OAAO,GACPL,QAAQ,CAAC,sCACTC,QAAQ,GACRE,OAAO,CAAC;gB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;gBAAEA;YAAO,IAAI,CAAC,CAAC;YAC5B,GAAIF,iBAAiB;gBAAEA;YAAe,IAAI,CAAC,CAAC;YAC5C,GAAI2B,eAAe;gBAAEtB,QAAQsB;YAAa,IAAI,CAAC,CAAC;QAClD;QAEA,MAAMI,SAAS,MAAMlB,QAAQmB,MAAM,CAACH;QAEpC,MAAMM,aAAaJ;QACnB,MAAMK,OAAOD,WAAWC,IAAI,IAAI,EAAE;QAClC,MAAMC,SAASF,WAAWE,MAAM,IAAI,EAAE;QAEtC,IAAIC,eAAe,CAAC,0CAA0C,EAAE3B,eAAe,aAAa,EAAEyB,KAAKG,MAAM,CAAC,oBAAoB,EAAEF,OAAOE,MAAM,CAAC,KAAK,CAAC;QACpJ,IAAIH,KAAKG,MAAM,GAAG,GAAG;YACnBD,gBAAgB,CAAC,oCAAoC,EAAEd,KAAKS,SAAS,CAACG,MAAM,QAAQ,CAAC;QACvF;QACA,IAAIC,OAAOE,MAAM,GAAG,GAAG;YACrBD,gBAAgB,CAAC,yBAAyB,EAAEd,KAAKS,SAAS,CAACI,QAAQ,QAAQ,CAAC;QAC9E;QAEA,OAAO;YACLpB,SAAS;gBAAC;oBAAEC,MAAM;oBAAQC,MAAMmB;gBAAa;aAAE;YAC/CJ,KAAK;gBAAEE;gBAAMC;YAAO;QACtB;IACF,EAAE,OAAOX,OAAO;QACd,MAAMc,eAAed,iBAAiBe,QAAQf,MAAMgB,OAAO,GAAG;QAC9D5B,OAAOY,KAAK,CAAC,CAAC,2BAA2B,EAAEf,eAAe,EAAE,EAAE6B,cAAc;QAC5E,OAAO;YACLvB,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,uCAAuC,EAAER,eAAe,GAAG,EAAE6B,cAAc;gBACpF;aACD;QACH;IACF;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,M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CAACM,aAAa,QAAQ,CAAC;YACrG;YAEA,OAAO;gBACLrB,SAAS;oBAAC;wBAAEC,MAAM;wBAAQC,MAAMiB;oBAAa;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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getConfigInfoTool.d.ts","sourceRoot":"","sources":["../../../src/mcp/builtin/getConfigInfoTool.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,+BA8D5B,CAAA"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { getAccessResults, isEntityHidden } from 'payload';
|
|
2
|
+
import { defineTool } from '../../defineTool.js';
|
|
3
|
+
export const getConfigInfoTool = defineTool({
|
|
4
|
+
annotations: {
|
|
5
|
+
destructiveHint: false,
|
|
6
|
+
idempotentHint: true,
|
|
7
|
+
openWorldHint: false,
|
|
8
|
+
readOnlyHint: true,
|
|
9
|
+
title: 'Config Info'
|
|
10
|
+
},
|
|
11
|
+
description: 'List the Payload collection and global slugs visible to this MCP client.'
|
|
12
|
+
}).handler(async ({ authorizedMCP, req })=>{
|
|
13
|
+
const user = req.user;
|
|
14
|
+
const permissions = authorizedMCP.overrideAccess ? null : await getAccessResults({
|
|
15
|
+
req
|
|
16
|
+
});
|
|
17
|
+
const authorizedCollectionSlugs = new Set();
|
|
18
|
+
const authorizedGlobalSlugs = new Set();
|
|
19
|
+
for (const item of authorizedMCP.items){
|
|
20
|
+
if (item.type === 'collectionTool') {
|
|
21
|
+
authorizedCollectionSlugs.add(item.collectionSlug);
|
|
22
|
+
} else if (item.type === 'globalTool') {
|
|
23
|
+
authorizedGlobalSlugs.add(item.globalSlug);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const collections = [];
|
|
27
|
+
const globals = [];
|
|
28
|
+
for (const collection of req.payload.config.collections){
|
|
29
|
+
if (!authorizedCollectionSlugs.has(collection.slug)) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (user && isEntityHidden({
|
|
33
|
+
hidden: collection.admin.hidden,
|
|
34
|
+
user
|
|
35
|
+
})) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
if (!authorizedMCP.overrideAccess && !permissions?.collections?.[collection.slug]?.read) {
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
collections.push(collection.slug);
|
|
42
|
+
}
|
|
43
|
+
for (const global of req.payload.config.globals){
|
|
44
|
+
if (!authorizedGlobalSlugs.has(global.slug)) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (user && isEntityHidden({
|
|
48
|
+
hidden: global.admin.hidden,
|
|
49
|
+
user
|
|
50
|
+
})) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (!authorizedMCP.overrideAccess && !permissions?.globals?.[global.slug]?.read) {
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
globals.push(global.slug);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
content: [
|
|
60
|
+
{
|
|
61
|
+
type: 'text',
|
|
62
|
+
text: `Collections: ${collections.join(', ') || 'none'}\nGlobals: ${globals.join(', ') || 'none'}`
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
//# sourceMappingURL=getConfigInfoTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/mcp/builtin/getConfigInfoTool.ts"],"sourcesContent":["import { getAccessResults, isEntityHidden } from 'payload'\n\nimport { defineTool } from '../../defineTool.js'\n\nexport const getConfigInfoTool = defineTool({\n annotations: {\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n readOnlyHint: true,\n title: 'Config Info',\n },\n description: 'List the Payload collection and global slugs visible to this MCP client.',\n}).handler(async ({ authorizedMCP, req }) => {\n const user = req.user\n const permissions = authorizedMCP.overrideAccess ? null : await getAccessResults({ req })\n const authorizedCollectionSlugs = new Set<string>()\n const authorizedGlobalSlugs = new Set<string>()\n\n for (const item of authorizedMCP.items) {\n if (item.type === 'collectionTool') {\n authorizedCollectionSlugs.add(item.collectionSlug)\n } else if (item.type === 'globalTool') {\n authorizedGlobalSlugs.add(item.globalSlug)\n }\n }\n\n const collections: string[] = []\n const globals: string[] = []\n\n for (const collection of req.payload.config.collections) {\n if (!authorizedCollectionSlugs.has(collection.slug)) {\n continue\n }\n if (user && isEntityHidden({ hidden: collection.admin.hidden, user })) {\n continue\n }\n if (!authorizedMCP.overrideAccess && !permissions?.collections?.[collection.slug]?.read) {\n continue\n }\n\n collections.push(collection.slug)\n }\n\n for (const global of req.payload.config.globals) {\n if (!authorizedGlobalSlugs.has(global.slug)) {\n continue\n }\n if (user && isEntityHidden({ hidden: global.admin.hidden, user })) {\n continue\n }\n if (!authorizedMCP.overrideAccess && !permissions?.globals?.[global.slug]?.read) {\n continue\n }\n\n globals.push(global.slug)\n }\n\n return {\n content: [\n {\n type: 'text',\n text: `Collections: ${collections.join(', ') || 'none'}\\nGlobals: ${globals.join(', ') || 'none'}`,\n },\n ],\n }\n})\n"],"names":["getAccessResults","isEntityHidden","defineTool","getConfigInfoTool","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","handler","authorizedMCP","req","user","permissions","overrideAccess","authorizedCollectionSlugs","Set","authorizedGlobalSlugs","item","items","type","add","collectionSlug","globalSlug","collections","globals","collection","payload","config","has","slug","hidden","admin","read","push","global","content","text","join"],"mappings":"AAAA,SAASA,gBAAgB,EAAEC,cAAc,QAAQ,UAAS;AAE1D,SAASC,UAAU,QAAQ,sBAAqB;AAEhD,OAAO,MAAMC,oBAAoBD,WAAW;IAC1CE,aAAa;QACXC,iBAAiB;QACjBC,gBAAgB;QAChBC,eAAe;QACfC,cAAc;QACdC,OAAO;IACT;IACAC,aAAa;AACf,GAAGC,OAAO,CAAC,OAAO,EAAEC,aAAa,EAAEC,GAAG,EAAE;IACtC,MAAMC,OAAOD,IAAIC,IAAI;IACrB,MAAMC,cAAcH,cAAcI,cAAc,GAAG,OAAO,MAAMhB,iBAAiB;QAAEa;IAAI;IACvF,MAAMI,4BAA4B,IAAIC;IACtC,MAAMC,wBAAwB,IAAID;IAElC,KAAK,MAAME,QAAQR,cAAcS,KAAK,CAAE;QACtC,IAAID,KAAKE,IAAI,KAAK,kBAAkB;YAClCL,0BAA0BM,GAAG,CAACH,KAAKI,cAAc;QACnD,OAAO,IAAIJ,KAAKE,IAAI,KAAK,cAAc;YACrCH,sBAAsBI,GAAG,CAACH,KAAKK,UAAU;QAC3C;IACF;IAEA,MAAMC,cAAwB,EAAE;IAChC,MAAMC,UAAoB,EAAE;IAE5B,KAAK,MAAMC,cAAcf,IAAIgB,OAAO,CAACC,MAAM,CAACJ,WAAW,CAAE;QACvD,IAAI,CAACT,0BAA0Bc,GAAG,CAACH,WAAWI,IAAI,GAAG;YACnD;QACF;QACA,IAAIlB,QAAQb,eAAe;YAAEgC,QAAQL,WAAWM,KAAK,CAACD,MAAM;YAAEnB;QAAK,IAAI;YACrE;QACF;QACA,IAAI,CAACF,cAAcI,cAAc,IAAI,CAACD,aAAaW,aAAa,CAACE,WAAWI,IAAI,CAAC,EAAEG,MAAM;YACvF;QACF;QAEAT,YAAYU,IAAI,CAACR,WAAWI,IAAI;IAClC;IAEA,KAAK,MAAMK,UAAUxB,IAAIgB,OAAO,CAACC,MAAM,CAACH,OAAO,CAAE;QAC/C,IAAI,CAACR,sBAAsBY,GAAG,CAACM,OAAOL,IAAI,GAAG;YAC3C;QACF;QACA,IAAIlB,QAAQb,eAAe;YAAEgC,QAAQI,OAAOH,KAAK,CAACD,MAAM;YAAEnB;QAAK,IAAI;YACjE;QACF;QACA,IAAI,CAACF,cAAcI,cAAc,IAAI,CAACD,aAAaY,SAAS,CAACU,OAAOL,IAAI,CAAC,EAAEG,MAAM;YAC/E;QACF;QAEAR,QAAQS,IAAI,CAACC,OAAOL,IAAI;IAC1B;IAEA,OAAO;QACLM,SAAS;YACP;gBACEhB,MAAM;gBACNiB,MAAM,CAAC,aAAa,EAAEb,YAAYc,IAAI,CAAC,SAAS,OAAO,WAAW,EAAEb,QAAQa,IAAI,CAAC,SAAS,QAAQ;YACpG;SACD;IACH;AACF,GAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"countVersionsTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/countVersionsTool.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,uBAAuB,wCAyDlC,CAAA"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { defaultAccess } from '../../../defaultAccess.js';
|
|
3
|
+
import { defineGlobalTool } from '../../../defineTool.js';
|
|
4
|
+
import { getLogger } from '../../../utils/getLogger.js';
|
|
5
|
+
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
6
|
+
const DEFAULT_DESCRIPTION = 'Count global versions in any version-enabled global by passing the global slug and optional where clause.';
|
|
7
|
+
export const countGlobalVersionsTool = defineGlobalTool({
|
|
8
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.readVersions),
|
|
9
|
+
annotations: {
|
|
10
|
+
destructiveHint: false,
|
|
11
|
+
idempotentHint: true,
|
|
12
|
+
openWorldHint: false,
|
|
13
|
+
readOnlyHint: true,
|
|
14
|
+
title: 'Count Global Versions'
|
|
15
|
+
},
|
|
16
|
+
description: DEFAULT_DESCRIPTION,
|
|
17
|
+
input: z.object({
|
|
18
|
+
locale: z.string().describe('Optional: locale code to count versions in').optional(),
|
|
19
|
+
where: whereSchema.describe('Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.siteName":{"contains":"test"}}').optional()
|
|
20
|
+
})
|
|
21
|
+
}).handler(async ({ authorizedMCP, globalSlug, input, req })=>{
|
|
22
|
+
const payload = req.payload;
|
|
23
|
+
const logger = getLogger({
|
|
24
|
+
payload
|
|
25
|
+
});
|
|
26
|
+
const { locale, where } = input;
|
|
27
|
+
logger.info(`Counting versions for global: ${globalSlug}`);
|
|
28
|
+
try {
|
|
29
|
+
const result = await payload.countGlobalVersions({
|
|
30
|
+
global: globalSlug,
|
|
31
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
32
|
+
req,
|
|
33
|
+
...locale ? {
|
|
34
|
+
locale
|
|
35
|
+
} : {},
|
|
36
|
+
...where ? {
|
|
37
|
+
where
|
|
38
|
+
} : {}
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
content: [
|
|
42
|
+
{
|
|
43
|
+
type: 'text',
|
|
44
|
+
text: `Global "${globalSlug}" contains ${result.totalDocs} matching versions.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
doc: result
|
|
48
|
+
};
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
51
|
+
logger.error(`Error counting versions for global ${globalSlug}: ${errorMessage}`);
|
|
52
|
+
return {
|
|
53
|
+
content: [
|
|
54
|
+
{
|
|
55
|
+
type: 'text',
|
|
56
|
+
text: `❌ **Error counting versions for global "${globalSlug}":** ${errorMessage}`
|
|
57
|
+
}
|
|
58
|
+
],
|
|
59
|
+
isError: true
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
//# sourceMappingURL=countVersionsTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/globals/countVersionsTool.ts"],"sourcesContent":["import { z } from 'zod'\n\nimport { defaultAccess } from '../../../defaultAccess.js'\nimport { defineGlobalTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport { whereSchema } from '../../../utils/whereSchema.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Count global versions in any version-enabled global by passing the global slug and optional where clause.'\n\nexport const countGlobalVersionsTool = defineGlobalTool({\n access: (args) =>\n defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.readVersions),\n annotations: {\n destructiveHint: false,\n idempotentHint: true,\n openWorldHint: false,\n readOnlyHint: true,\n title: 'Count Global Versions',\n },\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n locale: z.string().describe('Optional: locale code to count versions in').optional(),\n where: whereSchema\n .describe(\n 'Optional: where clause for filtering versions. Version document fields are usually under \"version\". Example: {\"version.siteName\":{\"contains\":\"test\"}}',\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, globalSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n const { locale, where } = input\n\n logger.info(`Counting versions for global: ${globalSlug}`)\n\n try {\n const result = await payload.countGlobalVersions({\n global: globalSlug,\n overrideAccess: authorizedMCP.overrideAccess,\n req,\n ...(locale ? { locale } : {}),\n ...(where ? { where } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Global \"${globalSlug}\" contains ${result.totalDocs} matching versions.\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error counting versions for global ${globalSlug}: ${errorMessage}`)\n return {\n content: [\n {\n type: 'text',\n text: `❌ **Error counting versions for global \"${globalSlug}\":** ${errorMessage}`,\n },\n ],\n isError: true,\n }\n }\n})\n"],"names":["z","defaultAccess","defineGlobalTool","getLogger","whereSchema","DEFAULT_DESCRIPTION","countGlobalVersionsTool","access","args","Boolean","permissions","globals","globalSlug","readVersions","annotations","destructiveHint","idempotentHint","openWorldHint","readOnlyHint","title","description","input","object","locale","string","describe","optional","where","handler","authorizedMCP","req","payload","logger","info","result","countGlobalVersions","global","overrideAccess","content","type","text","totalDocs","JSON","stringify","doc","error","errorMessage","Error","message","isError"],"mappings":"AAAA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,SAAS,QAAQ,8BAA6B;AACvD,SAASC,WAAW,QAAQ,gCAA+B;AAE3D,MAAMC,sBACJ;AAEF,OAAO,MAAMC,0BAA0BJ,iBAAiB;IACtDK,QAAQ,CAACC,OACPP,cAAcO,SAASC,QAAQD,KAAKE,WAAW,EAAEC,SAAS,CAACH,KAAKI,UAAU,CAAC,EAAEC;IAC/EC,aAAa;QACXC,iBAAiB;QACjBC,gBAAgB;QAChBC,eAAe;QACfC,cAAc;QACdC,OAAO;IACT;IACAC,aAAaf;IACbgB,OAAOrB,EAAEsB,MAAM,CAAC;QACdC,QAAQvB,EAAEwB,MAAM,GAAGC,QAAQ,CAAC,8CAA8CC,QAAQ;QAClFC,OAAOvB,YACJqB,QAAQ,CACP,yJAEDC,QAAQ;IACb;AACF,GAAGE,OAAO,CAAC,OAAO,EAAEC,aAAa,EAAEjB,UAAU,EAAES,KAAK,EAAES,GAAG,EAAE;IACzD,MAAMC,UAAUD,IAAIC,OAAO;IAC3B,MAAMC,SAAS7B,UAAU;QAAE4B;IAAQ;IACnC,MAAM,EAAER,MAAM,EAAEI,KAAK,EAAE,GAAGN;IAE1BW,OAAOC,IAAI,CAAC,CAAC,8BAA8B,EAAErB,YAAY;IAEzD,IAAI;QACF,MAAMsB,SAAS,MAAMH,QAAQI,mBAAmB,CAAC;YAC/CC,QAAQxB;YACRyB,gBAAgBR,cAAcQ,cAAc;YAC5CP;YACA,GAAIP,SAAS;gBAAEA;YAAO,IAAI,CAAC,CAAC;YAC5B,GAAII,QAAQ;gBAAEA;YAAM,IAAI,CAAC,CAAC;QAC5B;QAEA,OAAO;YACLW,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,QAAQ,EAAE5B,WAAW,WAAW,EAAEsB,OAAOO,SAAS,CAAC,iCAAiC,EAAEC,KAAKC,SAAS,CAACT,QAAQ,QAAQ,CAAC;gBAC/H;aACD;YACDU,KAAKV;QACP;IACF,EAAE,OAAOW,OAAO;QACd,MAAMC,eAAeD,iBAAiBE,QAAQF,MAAMG,OAAO,GAAG;QAC9DhB,OAAOa,KAAK,CAAC,CAAC,mCAAmC,EAAEjC,WAAW,EAAE,EAAEkC,cAAc;QAChF,OAAO;YACLR,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,wCAAwC,EAAE5B,WAAW,KAAK,EAAEkC,cAAc;gBACnF;aACD;YACDG,SAAS;QACX;IACF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/findTool.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,cAAc,
|
|
1
|
+
{"version":3,"file":"findTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/findTool.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,cAAc,wCA8FzB,CAAA"}
|
|
@@ -1,53 +1,50 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
+
import { defaultAccess } from '../../../defaultAccess.js';
|
|
2
3
|
import { defineGlobalTool } from '../../../defineTool.js';
|
|
3
4
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
4
|
-
|
|
5
|
-
const DEFAULT_DESCRIPTION = 'Find a Payload global singleton configuration.';
|
|
5
|
+
const DEFAULT_DESCRIPTION = 'Find any Payload global by passing the global slug.';
|
|
6
6
|
export const findGlobalTool = defineGlobalTool({
|
|
7
|
+
access: (args)=>defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.read),
|
|
8
|
+
annotations: {
|
|
9
|
+
destructiveHint: false,
|
|
10
|
+
idempotentHint: true,
|
|
11
|
+
openWorldHint: false,
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
title: 'Find Global'
|
|
14
|
+
},
|
|
7
15
|
description: DEFAULT_DESCRIPTION,
|
|
8
16
|
input: z.object({
|
|
9
17
|
depth: z.number().int().min(0).max(10).describe('Depth of population for relationships').optional().default(0),
|
|
10
18
|
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
11
19
|
locale: z.string().describe('Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields').optional(),
|
|
12
|
-
|
|
20
|
+
populate: z.record(z.string(), z.unknown()).describe('Optional: control which fields to include from populated relationship or upload documents.').optional(),
|
|
21
|
+
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()
|
|
13
22
|
})
|
|
14
23
|
}).handler(async ({ authorizedMCP, globalSlug, input, req })=>{
|
|
15
24
|
const payload = req.payload;
|
|
16
25
|
const logger = getLogger({
|
|
17
26
|
payload
|
|
18
27
|
});
|
|
19
|
-
const { depth, fallbackLocale, locale, select } = input;
|
|
28
|
+
const { depth, fallbackLocale, locale, populate, select } = input;
|
|
20
29
|
logger.info(`Reading global: ${globalSlug}, depth: ${depth}${locale ? `, locale: ${locale}` : ''}`);
|
|
21
30
|
try {
|
|
22
31
|
const findOptions = {
|
|
23
32
|
slug: globalSlug,
|
|
24
33
|
depth,
|
|
25
|
-
|
|
34
|
+
overrideAccess: authorizedMCP.overrideAccess,
|
|
35
|
+
req
|
|
26
36
|
};
|
|
27
|
-
let selectClause;
|
|
28
|
-
if (select) {
|
|
29
|
-
try {
|
|
30
|
-
selectClause = JSON.parse(select);
|
|
31
|
-
} catch {
|
|
32
|
-
logger.warn(`Invalid select clause JSON for global: ${select}`);
|
|
33
|
-
return {
|
|
34
|
-
content: [
|
|
35
|
-
{
|
|
36
|
-
type: 'text',
|
|
37
|
-
text: 'Error: Invalid JSON in select clause'
|
|
38
|
-
}
|
|
39
|
-
]
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
37
|
if (locale) {
|
|
44
38
|
findOptions.locale = locale;
|
|
45
39
|
}
|
|
46
40
|
if (fallbackLocale) {
|
|
47
41
|
findOptions.fallbackLocale = fallbackLocale;
|
|
48
42
|
}
|
|
49
|
-
if (
|
|
50
|
-
findOptions.select =
|
|
43
|
+
if (select) {
|
|
44
|
+
findOptions.select = select;
|
|
45
|
+
}
|
|
46
|
+
if (populate) {
|
|
47
|
+
findOptions.populate = populate;
|
|
51
48
|
}
|
|
52
49
|
const result = await payload.findGlobal(findOptions);
|
|
53
50
|
return {
|