@payloadcms/plugin-mcp 3.86.0-internal.ac46214 → 4.0.0-internal.183b315
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/README.md +2 -2
- package/bin.js +39 -0
- package/dist/@types/assets.d.js +2 -0
- package/dist/@types/assets.d.js.map +1 -0
- package/dist/collection/getAccessField.d.ts +12 -0
- package/dist/collection/getAccessField.d.ts.map +1 -0
- package/dist/collection/getAccessField.js +57 -0
- package/dist/collection/getAccessField.js.map +1 -0
- package/dist/collection/index.d.ts +6 -0
- package/dist/collection/index.d.ts.map +1 -0
- package/dist/collection/index.js +60 -0
- package/dist/collection/index.js.map +1 -0
- package/dist/components/AccessField/index.client.d.ts +10 -0
- package/dist/components/AccessField/index.client.d.ts.map +1 -0
- package/dist/components/AccessField/index.client.js +305 -0
- package/dist/components/AccessField/index.client.js.map +1 -0
- package/dist/components/AccessField/index.css +93 -0
- package/dist/defineTool.d.ts +26 -0
- package/dist/defineTool.d.ts.map +1 -0
- package/dist/defineTool.js +37 -0
- package/dist/defineTool.js.map +1 -0
- package/dist/endpoint/access.d.ts +10 -0
- package/dist/endpoint/access.d.ts.map +1 -0
- package/dist/endpoint/access.js +106 -0
- package/dist/endpoint/access.js.map +1 -0
- package/dist/endpoint/index.d.ts +3 -0
- package/dist/endpoint/index.d.ts.map +1 -0
- package/dist/endpoint/index.js +36 -0
- package/dist/endpoint/index.js.map +1 -0
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +4 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +44 -67
- package/dist/index.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts +19 -0
- package/dist/mcp/buildMcpServer.d.ts.map +1 -0
- package/dist/mcp/buildMcpServer.js +170 -0
- package/dist/mcp/buildMcpServer.js.map +1 -0
- package/dist/mcp/builtin/collections/authTools.d.ts +7 -0
- package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/authTools.js +250 -0
- package/dist/mcp/builtin/collections/authTools.js.map +1 -0
- package/dist/mcp/builtin/collections/createTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/createTool.js +91 -0
- package/dist/mcp/builtin/collections/createTool.js.map +1 -0
- package/dist/mcp/builtin/collections/deleteTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/deleteTool.js +117 -0
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -0
- package/dist/mcp/builtin/collections/findTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/findTool.js +159 -0
- package/dist/mcp/builtin/collections/findTool.js.map +1 -0
- package/dist/mcp/builtin/collections/updateTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/updateTool.js +191 -0
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/findTool.js +76 -0
- package/dist/mcp/builtin/globals/findTool.js.map +1 -0
- package/dist/mcp/builtin/globals/updateTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/updateTool.js +96 -0
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -0
- package/dist/mcp/builtinTools.d.ts +37 -0
- package/dist/mcp/builtinTools.d.ts.map +1 -0
- package/dist/mcp/builtinTools.js +64 -0
- package/dist/mcp/builtinTools.js.map +1 -0
- package/dist/mcp/sanitizeMCPConfig.d.ts +17 -0
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -0
- package/dist/mcp/sanitizeMCPConfig.js +167 -0
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -0
- package/dist/stdio.d.ts +8 -0
- package/dist/stdio.d.ts.map +1 -0
- package/dist/stdio.js +89 -0
- package/dist/stdio.js.map +1 -0
- package/dist/types.d.ts +262 -455
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +6 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/camelCase.d.ts.map +1 -1
- package/dist/utils/getLogger.d.ts +10 -0
- package/dist/utils/getLogger.d.ts.map +1 -0
- package/dist/utils/getLogger.js +22 -0
- package/dist/utils/getLogger.js.map +1 -0
- package/dist/utils/getPluginConfig.d.ts +12 -0
- package/dist/utils/getPluginConfig.d.ts.map +1 -0
- package/dist/utils/getPluginConfig.js +15 -0
- package/dist/utils/getPluginConfig.js.map +1 -0
- package/dist/utils/localAPIDefaults.d.ts +20 -0
- package/dist/utils/localAPIDefaults.d.ts.map +1 -0
- package/dist/utils/localAPIDefaults.js +19 -0
- package/dist/utils/localAPIDefaults.js.map +1 -0
- package/dist/utils/resolveProjectRoot.d.ts +7 -0
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -0
- package/dist/utils/resolveProjectRoot.js +15 -0
- package/dist/utils/resolveProjectRoot.js.map +1 -0
- package/dist/utils/schemaConversion/buildToolInput.d.ts +29 -0
- package/dist/utils/schemaConversion/buildToolInput.d.ts.map +1 -0
- package/dist/utils/schemaConversion/buildToolInput.js +51 -0
- package/dist/utils/schemaConversion/buildToolInput.js.map +1 -0
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +2 -2
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +15 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +464 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +158 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -0
- package/dist/utils/toStandardSchema.d.ts +5 -0
- package/dist/utils/toStandardSchema.d.ts.map +1 -0
- package/dist/utils/toStandardSchema.js +4 -0
- package/dist/utils/toStandardSchema.js.map +1 -0
- package/package.json +35 -10
- package/src/@types/assets.d.ts +3 -0
- package/src/collection/getAccessField.ts +64 -0
- package/src/collection/index.ts +64 -0
- package/src/components/AccessField/index.client.tsx +344 -0
- package/src/components/AccessField/index.css +93 -0
- package/src/defineTool.ts +44 -0
- package/src/endpoint/access.ts +132 -0
- package/src/endpoint/index.ts +35 -0
- package/src/exports/client.ts +2 -0
- package/src/index.ts +35 -85
- package/src/mcp/buildMcpServer.ts +229 -0
- package/src/mcp/builtin/collections/authTools.ts +233 -0
- package/src/mcp/builtin/collections/createTool.ts +116 -0
- package/src/mcp/builtin/collections/deleteTool.ts +123 -0
- package/src/mcp/builtin/collections/findTool.ts +187 -0
- package/src/mcp/builtin/collections/updateTool.ts +210 -0
- package/src/mcp/builtin/globals/findTool.ts +96 -0
- package/src/mcp/builtin/globals/updateTool.ts +120 -0
- package/src/mcp/builtinTools.ts +84 -0
- package/src/mcp/sanitizeMCPConfig.ts +239 -0
- package/src/stdio.ts +98 -0
- package/src/types.ts +295 -490
- package/src/utils/getLogger.ts +22 -0
- package/src/utils/getPluginConfig.ts +24 -0
- package/src/utils/localAPIDefaults.ts +22 -0
- package/src/utils/resolveProjectRoot.ts +17 -0
- package/src/utils/schemaConversion/buildToolInput.ts +68 -0
- package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +3 -3
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +103 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +529 -0
- package/src/utils/toStandardSchema.ts +9 -0
- package/dist/collections/createApiKeysCollection.d.ts +0 -7
- package/dist/collections/createApiKeysCollection.d.ts.map +0 -1
- package/dist/collections/createApiKeysCollection.js +0 -317
- package/dist/collections/createApiKeysCollection.js.map +0 -1
- package/dist/defaults.d.ts +0 -4
- package/dist/defaults.d.ts.map +0 -1
- package/dist/defaults.js +0 -5
- package/dist/defaults.js.map +0 -1
- package/dist/endpoints/mcp.d.ts +0 -4
- package/dist/endpoints/mcp.d.ts.map +0 -1
- package/dist/endpoints/mcp.js +0 -71
- package/dist/endpoints/mcp.js.map +0 -1
- package/dist/mcp/createRequest.d.ts +0 -3
- package/dist/mcp/createRequest.d.ts.map +0 -1
- package/dist/mcp/createRequest.js +0 -14
- package/dist/mcp/createRequest.js.map +0 -1
- package/dist/mcp/getMcpHandler.d.ts +0 -4
- package/dist/mcp/getMcpHandler.d.ts.map +0 -1
- package/dist/mcp/getMcpHandler.js +0 -231
- package/dist/mcp/getMcpHandler.js.map +0 -1
- package/dist/mcp/helpers/config.d.ts +0 -22
- package/dist/mcp/helpers/config.d.ts.map +0 -1
- package/dist/mcp/helpers/config.js +0 -153
- package/dist/mcp/helpers/config.js.map +0 -1
- package/dist/mcp/helpers/fields.d.ts +0 -19
- package/dist/mcp/helpers/fields.d.ts.map +0 -1
- package/dist/mcp/helpers/fields.js +0 -102
- package/dist/mcp/helpers/fields.js.map +0 -1
- package/dist/mcp/helpers/fileValidation.d.ts +0 -67
- package/dist/mcp/helpers/fileValidation.d.ts.map +0 -1
- package/dist/mcp/helpers/fileValidation.js +0 -267
- package/dist/mcp/helpers/fileValidation.js.map +0 -1
- package/dist/mcp/registerTool.d.ts +0 -6
- package/dist/mcp/registerTool.d.ts.map +0 -1
- package/dist/mcp/registerTool.js +0 -18
- package/dist/mcp/registerTool.js.map +0 -1
- package/dist/mcp/tools/auth/auth.d.ts +0 -4
- package/dist/mcp/tools/auth/auth.d.ts.map +0 -1
- package/dist/mcp/tools/auth/auth.js +0 -57
- package/dist/mcp/tools/auth/auth.js.map +0 -1
- package/dist/mcp/tools/auth/forgotPassword.d.ts +0 -4
- package/dist/mcp/tools/auth/forgotPassword.d.ts.map +0 -1
- package/dist/mcp/tools/auth/forgotPassword.js +0 -48
- package/dist/mcp/tools/auth/forgotPassword.js.map +0 -1
- package/dist/mcp/tools/auth/login.d.ts +0 -4
- package/dist/mcp/tools/auth/login.d.ts.map +0 -1
- package/dist/mcp/tools/auth/login.js +0 -51
- package/dist/mcp/tools/auth/login.js.map +0 -1
- package/dist/mcp/tools/auth/resetPassword.d.ts +0 -4
- package/dist/mcp/tools/auth/resetPassword.d.ts.map +0 -1
- package/dist/mcp/tools/auth/resetPassword.js +0 -49
- package/dist/mcp/tools/auth/resetPassword.js.map +0 -1
- package/dist/mcp/tools/auth/unlock.d.ts +0 -4
- package/dist/mcp/tools/auth/unlock.d.ts.map +0 -1
- package/dist/mcp/tools/auth/unlock.js +0 -48
- package/dist/mcp/tools/auth/unlock.js.map +0 -1
- package/dist/mcp/tools/auth/verify.d.ts +0 -4
- package/dist/mcp/tools/auth/verify.d.ts.map +0 -1
- package/dist/mcp/tools/auth/verify.js +0 -45
- package/dist/mcp/tools/auth/verify.js.map +0 -1
- package/dist/mcp/tools/collection/create.d.ts +0 -10
- package/dist/mcp/tools/collection/create.d.ts.map +0 -1
- package/dist/mcp/tools/collection/create.js +0 -139
- package/dist/mcp/tools/collection/create.js.map +0 -1
- package/dist/mcp/tools/collection/delete.d.ts +0 -10
- package/dist/mcp/tools/collection/delete.d.ts.map +0 -1
- package/dist/mcp/tools/collection/delete.js +0 -154
- package/dist/mcp/tools/collection/delete.js.map +0 -1
- package/dist/mcp/tools/collection/find.d.ts +0 -10
- package/dist/mcp/tools/collection/find.d.ts.map +0 -1
- package/dist/mcp/tools/collection/find.js +0 -165
- package/dist/mcp/tools/collection/find.js.map +0 -1
- package/dist/mcp/tools/collection/update.d.ts +0 -10
- package/dist/mcp/tools/collection/update.d.ts.map +0 -1
- package/dist/mcp/tools/collection/update.js +0 -209
- package/dist/mcp/tools/collection/update.js.map +0 -1
- package/dist/mcp/tools/config/find.d.ts +0 -10
- package/dist/mcp/tools/config/find.d.ts.map +0 -1
- package/dist/mcp/tools/config/find.js +0 -97
- package/dist/mcp/tools/config/find.js.map +0 -1
- package/dist/mcp/tools/config/update.d.ts +0 -10
- package/dist/mcp/tools/config/update.d.ts.map +0 -1
- package/dist/mcp/tools/config/update.js +0 -215
- package/dist/mcp/tools/config/update.js.map +0 -1
- package/dist/mcp/tools/global/find.d.ts +0 -5
- package/dist/mcp/tools/global/find.d.ts.map +0 -1
- package/dist/mcp/tools/global/find.js +0 -82
- package/dist/mcp/tools/global/find.js.map +0 -1
- package/dist/mcp/tools/global/update.d.ts +0 -6
- package/dist/mcp/tools/global/update.d.ts.map +0 -1
- package/dist/mcp/tools/global/update.js +0 -124
- package/dist/mcp/tools/global/update.js.map +0 -1
- package/dist/mcp/tools/job/create.d.ts +0 -10
- package/dist/mcp/tools/job/create.d.ts.map +0 -1
- package/dist/mcp/tools/job/create.js +0 -293
- package/dist/mcp/tools/job/create.js.map +0 -1
- package/dist/mcp/tools/job/run.d.ts +0 -10
- package/dist/mcp/tools/job/run.d.ts.map +0 -1
- package/dist/mcp/tools/job/run.js +0 -129
- package/dist/mcp/tools/job/run.js.map +0 -1
- package/dist/mcp/tools/job/update.d.ts +0 -11
- package/dist/mcp/tools/job/update.d.ts.map +0 -1
- package/dist/mcp/tools/job/update.js +0 -186
- package/dist/mcp/tools/job/update.js.map +0 -1
- package/dist/mcp/tools/resource/create.d.ts +0 -6
- package/dist/mcp/tools/resource/create.d.ts.map +0 -1
- package/dist/mcp/tools/resource/create.js +0 -124
- package/dist/mcp/tools/resource/create.js.map +0 -1
- package/dist/mcp/tools/resource/delete.d.ts +0 -5
- package/dist/mcp/tools/resource/delete.d.ts.map +0 -1
- package/dist/mcp/tools/resource/delete.js +0 -151
- package/dist/mcp/tools/resource/delete.js.map +0 -1
- package/dist/mcp/tools/resource/find.d.ts +0 -5
- package/dist/mcp/tools/resource/find.d.ts.map +0 -1
- package/dist/mcp/tools/resource/find.js +0 -170
- package/dist/mcp/tools/resource/find.js.map +0 -1
- package/dist/mcp/tools/resource/update.d.ts +0 -6
- package/dist/mcp/tools/resource/update.d.ts.map +0 -1
- package/dist/mcp/tools/resource/update.js +0 -256
- package/dist/mcp/tools/resource/update.js.map +0 -1
- package/dist/mcp/tools/schemas.d.ts +0 -457
- package/dist/mcp/tools/schemas.d.ts.map +0 -1
- package/dist/mcp/tools/schemas.js +0 -243
- package/dist/mcp/tools/schemas.js.map +0 -1
- package/dist/utils/adminEntitySettings.d.ts +0 -17
- package/dist/utils/adminEntitySettings.d.ts.map +0 -1
- package/dist/utils/adminEntitySettings.js +0 -41
- package/dist/utils/adminEntitySettings.js.map +0 -1
- package/dist/utils/createApiKeyFields.d.ts +0 -15
- package/dist/utils/createApiKeyFields.d.ts.map +0 -1
- package/dist/utils/createApiKeyFields.js +0 -57
- package/dist/utils/createApiKeyFields.js.map +0 -1
- package/dist/utils/getEnabledSlugs.d.ts +0 -13
- package/dist/utils/getEnabledSlugs.d.ts.map +0 -1
- package/dist/utils/getEnabledSlugs.js +0 -32
- package/dist/utils/getEnabledSlugs.js.map +0 -1
- package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts +0 -3
- package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts.map +0 -1
- package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js +0 -43
- package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +0 -13
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js +0 -56
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +0 -20
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js +0 -53
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.d.ts +0 -3
- package/dist/utils/schemaConversion/transformPointFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.js +0 -51
- package/dist/utils/schemaConversion/transformPointFields.js.map +0 -1
- package/src/collections/createApiKeysCollection.ts +0 -373
- package/src/defaults.ts +0 -3
- package/src/endpoints/mcp.ts +0 -91
- package/src/mcp/createRequest.ts +0 -13
- package/src/mcp/getMcpHandler.ts +0 -545
- package/src/mcp/helpers/config.ts +0 -213
- package/src/mcp/helpers/fields.ts +0 -154
- package/src/mcp/helpers/fileValidation.ts +0 -362
- package/src/mcp/registerTool.ts +0 -22
- package/src/mcp/tools/auth/auth.ts +0 -71
- package/src/mcp/tools/auth/forgotPassword.ts +0 -70
- package/src/mcp/tools/auth/login.ts +0 -72
- package/src/mcp/tools/auth/resetPassword.ts +0 -61
- package/src/mcp/tools/auth/unlock.ts +0 -64
- package/src/mcp/tools/auth/verify.ts +0 -57
- package/src/mcp/tools/collection/create.ts +0 -210
- package/src/mcp/tools/collection/delete.ts +0 -211
- package/src/mcp/tools/collection/find.ts +0 -224
- package/src/mcp/tools/collection/update.ts +0 -290
- package/src/mcp/tools/config/find.ts +0 -128
- package/src/mcp/tools/config/update.ts +0 -280
- package/src/mcp/tools/global/find.ts +0 -128
- package/src/mcp/tools/global/update.ts +0 -207
- package/src/mcp/tools/job/create.ts +0 -416
- package/src/mcp/tools/job/run.ts +0 -167
- package/src/mcp/tools/job/update.ts +0 -274
- package/src/mcp/tools/resource/create.ts +0 -211
- package/src/mcp/tools/resource/delete.ts +0 -218
- package/src/mcp/tools/resource/find.ts +0 -246
- package/src/mcp/tools/resource/update.ts +0 -383
- package/src/mcp/tools/schemas.ts +0 -520
- package/src/utils/adminEntitySettings.ts +0 -40
- package/src/utils/createApiKeyFields.ts +0 -72
- package/src/utils/getEnabledSlugs.ts +0 -42
- package/src/utils/schemaConversion/convertCollectionSchemaToZod.ts +0 -52
- package/src/utils/schemaConversion/sanitizeJsonSchema.ts +0 -62
- package/src/utils/schemaConversion/simplifyRelationshipFields.ts +0 -65
- package/src/utils/schemaConversion/transformPointFields.ts +0 -55
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { SelectType } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
6
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
7
|
+
import {
|
|
8
|
+
getCollectionVirtualFieldNames,
|
|
9
|
+
stripVirtualFields,
|
|
10
|
+
} from '../../../utils/getVirtualFieldNames.js'
|
|
11
|
+
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
12
|
+
import { buildToolInput } from '../../../utils/schemaConversion/buildToolInput.js'
|
|
13
|
+
import { sanitizeEntitySchema } from '../../../utils/schemaConversion/sanitizeEntitySchema.js'
|
|
14
|
+
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
15
|
+
|
|
16
|
+
const DEFAULT_DESCRIPTION = 'Create a document in a collection.'
|
|
17
|
+
|
|
18
|
+
export const createCollectionTool = defineCollectionTool({
|
|
19
|
+
description: DEFAULT_DESCRIPTION,
|
|
20
|
+
input: ({ collectionSchema }) =>
|
|
21
|
+
buildToolInput({
|
|
22
|
+
controls: {
|
|
23
|
+
depth: z
|
|
24
|
+
.number()
|
|
25
|
+
.int()
|
|
26
|
+
.min(0)
|
|
27
|
+
.max(10)
|
|
28
|
+
.describe('How many levels deep to populate relationships in response')
|
|
29
|
+
.optional()
|
|
30
|
+
.default(0),
|
|
31
|
+
draft: z
|
|
32
|
+
.boolean()
|
|
33
|
+
.describe('Whether to create the document as a draft')
|
|
34
|
+
.optional()
|
|
35
|
+
.default(false),
|
|
36
|
+
fallbackLocale: z
|
|
37
|
+
.string()
|
|
38
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
39
|
+
.optional(),
|
|
40
|
+
locale: z
|
|
41
|
+
.string()
|
|
42
|
+
.describe(
|
|
43
|
+
'Optional: locale code to create the document in (e.g., "en", "es"). Defaults to the default locale',
|
|
44
|
+
)
|
|
45
|
+
.optional(),
|
|
46
|
+
select: z
|
|
47
|
+
.string()
|
|
48
|
+
.describe(
|
|
49
|
+
'Optional: define exactly which fields you\'d like to create (JSON), e.g., \'{"title": "My Post"}\'',
|
|
50
|
+
)
|
|
51
|
+
.optional(),
|
|
52
|
+
},
|
|
53
|
+
dataDescription: 'The document fields to create',
|
|
54
|
+
dataSchema: sanitizeEntitySchema(collectionSchema),
|
|
55
|
+
}),
|
|
56
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
57
|
+
const payload = req.payload
|
|
58
|
+
const logger = getLogger({ payload })
|
|
59
|
+
|
|
60
|
+
const { data, depth, draft, fallbackLocale, locale, select } = input
|
|
61
|
+
|
|
62
|
+
logger.info(
|
|
63
|
+
`Creating document in collection: ${collectionSlug}${locale ? ` with locale: ${locale}` : ''}`,
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
let parsedData = transformPointDataToPayload(data)
|
|
68
|
+
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)
|
|
69
|
+
parsedData = stripVirtualFields(parsedData, virtualFieldNames)
|
|
70
|
+
|
|
71
|
+
let selectClause: SelectType | undefined
|
|
72
|
+
if (select) {
|
|
73
|
+
try {
|
|
74
|
+
selectClause = JSON.parse(select) as SelectType
|
|
75
|
+
} catch {
|
|
76
|
+
logger.warn(`Invalid select clause JSON: ${select}`)
|
|
77
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const result = await payload.create({
|
|
82
|
+
collection: collectionSlug,
|
|
83
|
+
data: parsedData,
|
|
84
|
+
depth,
|
|
85
|
+
draft,
|
|
86
|
+
req,
|
|
87
|
+
...localAPIDefaults(authorizedMCP),
|
|
88
|
+
...(locale ? { locale } : {}),
|
|
89
|
+
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
90
|
+
...(selectClause ? { select: selectClause } : {}),
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
logger.info(`Successfully created document in ${collectionSlug} with ID: ${result.id}`)
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
content: [
|
|
97
|
+
{
|
|
98
|
+
type: 'text',
|
|
99
|
+
text: `Document created successfully in collection "${collectionSlug}"!\nCreated document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
doc: result as Record<string, unknown>,
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
106
|
+
logger.error(`Error creating document in ${collectionSlug}: ${errorMessage}`)
|
|
107
|
+
return {
|
|
108
|
+
content: [
|
|
109
|
+
{
|
|
110
|
+
type: 'text',
|
|
111
|
+
text: `Error creating document in collection "${collectionSlug}": ${errorMessage}`,
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
4
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
5
|
+
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
6
|
+
|
|
7
|
+
const DEFAULT_DESCRIPTION = 'Delete documents in a collection by ID or where clause.'
|
|
8
|
+
|
|
9
|
+
export const deleteCollectionTool = defineCollectionTool({
|
|
10
|
+
description: DEFAULT_DESCRIPTION,
|
|
11
|
+
input: z.object({
|
|
12
|
+
id: z
|
|
13
|
+
.union([z.string(), z.number()])
|
|
14
|
+
.describe('Optional: specific document ID to delete')
|
|
15
|
+
.optional(),
|
|
16
|
+
depth: z
|
|
17
|
+
.number()
|
|
18
|
+
.int()
|
|
19
|
+
.min(0)
|
|
20
|
+
.max(10)
|
|
21
|
+
.describe('Depth of population for relationships in response')
|
|
22
|
+
.optional()
|
|
23
|
+
.default(0),
|
|
24
|
+
fallbackLocale: z
|
|
25
|
+
.string()
|
|
26
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
27
|
+
.optional(),
|
|
28
|
+
locale: z
|
|
29
|
+
.string()
|
|
30
|
+
.describe(
|
|
31
|
+
'Optional: locale code for the operation (e.g., "en", "es"). Defaults to the default locale',
|
|
32
|
+
)
|
|
33
|
+
.optional(),
|
|
34
|
+
where: z
|
|
35
|
+
.string()
|
|
36
|
+
.describe('Optional: JSON string for where clause to delete multiple documents')
|
|
37
|
+
.optional(),
|
|
38
|
+
}),
|
|
39
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
40
|
+
const payload = req.payload
|
|
41
|
+
const logger = getLogger({ payload })
|
|
42
|
+
|
|
43
|
+
const { id, depth, fallbackLocale, locale, where } = input
|
|
44
|
+
|
|
45
|
+
logger.info(
|
|
46
|
+
`Deleting document from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}${locale ? `, locale: ${locale}` : ''}`,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
if (!id && !where) {
|
|
51
|
+
return {
|
|
52
|
+
content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let whereClause: Record<string, unknown> = {}
|
|
57
|
+
if (where) {
|
|
58
|
+
try {
|
|
59
|
+
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
60
|
+
} catch {
|
|
61
|
+
logger.warn(`Invalid where clause JSON: ${where}`)
|
|
62
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const deleteOptions: Record<string, unknown> = {
|
|
67
|
+
collection: collectionSlug,
|
|
68
|
+
depth,
|
|
69
|
+
req,
|
|
70
|
+
...localAPIDefaults(authorizedMCP),
|
|
71
|
+
...(locale && { locale }),
|
|
72
|
+
...(fallbackLocale && { fallbackLocale }),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (id) {
|
|
76
|
+
deleteOptions.id = id
|
|
77
|
+
} else {
|
|
78
|
+
deleteOptions.where = whereClause
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const result = await payload.delete(deleteOptions as Parameters<typeof payload.delete>[0])
|
|
82
|
+
|
|
83
|
+
if (id) {
|
|
84
|
+
return {
|
|
85
|
+
content: [
|
|
86
|
+
{
|
|
87
|
+
type: 'text',
|
|
88
|
+
text: `Document deleted successfully from collection "${collectionSlug}"!\nDeleted document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
|
|
89
|
+
},
|
|
90
|
+
],
|
|
91
|
+
doc: result as Record<string, unknown>,
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
|
|
96
|
+
const docs = bulkResult.docs || []
|
|
97
|
+
const errors = bulkResult.errors || []
|
|
98
|
+
|
|
99
|
+
let responseText = `Document deleted successfully from collection "${collectionSlug}"!\nDeleted: ${docs.length} documents\nErrors: ${errors.length}\n---`
|
|
100
|
+
if (docs.length > 0) {
|
|
101
|
+
responseText += `\n\nDeleted documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``
|
|
102
|
+
}
|
|
103
|
+
if (errors.length > 0) {
|
|
104
|
+
responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
content: [{ type: 'text', text: responseText }],
|
|
109
|
+
doc: { docs, errors } as unknown as Record<string, unknown>,
|
|
110
|
+
}
|
|
111
|
+
} catch (error) {
|
|
112
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
113
|
+
logger.error(`Error deleting document from ${collectionSlug}: ${errorMessage}`)
|
|
114
|
+
return {
|
|
115
|
+
content: [
|
|
116
|
+
{
|
|
117
|
+
type: 'text',
|
|
118
|
+
text: `Error deleting document from collection "${collectionSlug}": ${errorMessage}`,
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
})
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import type { SelectType } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
6
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
7
|
+
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
8
|
+
|
|
9
|
+
const DEFAULT_DESCRIPTION =
|
|
10
|
+
'Find documents in a collection by ID or where clause using Find or FindByID.'
|
|
11
|
+
|
|
12
|
+
export const findCollectionTool = defineCollectionTool({
|
|
13
|
+
description: DEFAULT_DESCRIPTION,
|
|
14
|
+
input: z.object({
|
|
15
|
+
id: z
|
|
16
|
+
.union([z.string(), z.number()])
|
|
17
|
+
.describe(
|
|
18
|
+
'Optional: specific document ID to retrieve. If not provided, returns all documents',
|
|
19
|
+
)
|
|
20
|
+
.optional(),
|
|
21
|
+
depth: z
|
|
22
|
+
.number()
|
|
23
|
+
.int()
|
|
24
|
+
.min(0)
|
|
25
|
+
.max(10)
|
|
26
|
+
.describe('How many levels deep to populate relationships (default: 0)')
|
|
27
|
+
.optional()
|
|
28
|
+
.default(0),
|
|
29
|
+
draft: z
|
|
30
|
+
.boolean()
|
|
31
|
+
.describe(
|
|
32
|
+
'Optional: whether the document should be queried from the versions table/collection or not.',
|
|
33
|
+
)
|
|
34
|
+
.optional(),
|
|
35
|
+
fallbackLocale: z
|
|
36
|
+
.string()
|
|
37
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
38
|
+
.optional(),
|
|
39
|
+
limit: z
|
|
40
|
+
.number()
|
|
41
|
+
.int()
|
|
42
|
+
.min(1)
|
|
43
|
+
.max(100)
|
|
44
|
+
.describe('Maximum number of documents to return (default: 10, max: 100)')
|
|
45
|
+
.optional()
|
|
46
|
+
.default(10),
|
|
47
|
+
locale: z
|
|
48
|
+
.string()
|
|
49
|
+
.describe(
|
|
50
|
+
'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
|
|
51
|
+
)
|
|
52
|
+
.optional(),
|
|
53
|
+
page: z
|
|
54
|
+
.number()
|
|
55
|
+
.int()
|
|
56
|
+
.min(1)
|
|
57
|
+
.describe('Page number for pagination (default: 1)')
|
|
58
|
+
.optional()
|
|
59
|
+
.default(1),
|
|
60
|
+
select: z
|
|
61
|
+
.string()
|
|
62
|
+
.describe(
|
|
63
|
+
"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'",
|
|
64
|
+
)
|
|
65
|
+
.optional(),
|
|
66
|
+
sort: z
|
|
67
|
+
.string()
|
|
68
|
+
.describe('Field to sort by (e.g., "createdAt", "-updatedAt" for descending)')
|
|
69
|
+
.optional(),
|
|
70
|
+
where: z
|
|
71
|
+
.string()
|
|
72
|
+
.describe(
|
|
73
|
+
'Optional JSON string for where clause filtering (e.g., \'{"title": {"contains": "test"}}\')',
|
|
74
|
+
)
|
|
75
|
+
.optional(),
|
|
76
|
+
}),
|
|
77
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
78
|
+
const payload = req.payload
|
|
79
|
+
const logger = getLogger({ payload })
|
|
80
|
+
|
|
81
|
+
const { id, depth, draft, fallbackLocale, limit, locale, page, select, sort, where } = input
|
|
82
|
+
|
|
83
|
+
logger.info(
|
|
84
|
+
`Reading document from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ''}, limit: ${limit}, page: ${page}${locale ? `, locale: ${locale}` : ''}`,
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
try {
|
|
88
|
+
let whereClause: Record<string, unknown> = {}
|
|
89
|
+
if (where) {
|
|
90
|
+
try {
|
|
91
|
+
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
92
|
+
} catch {
|
|
93
|
+
logger.warn(`Invalid where clause JSON: ${where}`)
|
|
94
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let selectClause: SelectType | undefined
|
|
99
|
+
if (select) {
|
|
100
|
+
try {
|
|
101
|
+
selectClause = JSON.parse(select) as SelectType
|
|
102
|
+
} catch {
|
|
103
|
+
logger.warn(`Invalid select clause JSON: ${select}`)
|
|
104
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (id) {
|
|
109
|
+
try {
|
|
110
|
+
const doc = await payload.findByID({
|
|
111
|
+
id,
|
|
112
|
+
collection: collectionSlug,
|
|
113
|
+
depth,
|
|
114
|
+
req,
|
|
115
|
+
...localAPIDefaults(authorizedMCP),
|
|
116
|
+
...(selectClause && { select: selectClause }),
|
|
117
|
+
...(locale && { locale }),
|
|
118
|
+
...(fallbackLocale && { fallbackLocale }),
|
|
119
|
+
...(draft !== undefined && { draft }),
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
content: [
|
|
124
|
+
{
|
|
125
|
+
type: 'text',
|
|
126
|
+
text: `Document from collection "${collectionSlug}":\n${JSON.stringify(doc)}`,
|
|
127
|
+
},
|
|
128
|
+
],
|
|
129
|
+
doc: doc as Record<string, unknown>,
|
|
130
|
+
}
|
|
131
|
+
} catch {
|
|
132
|
+
logger.warn(`Document not found with ID: ${id} in collection: ${collectionSlug}`)
|
|
133
|
+
return {
|
|
134
|
+
content: [
|
|
135
|
+
{
|
|
136
|
+
type: 'text',
|
|
137
|
+
text: `Error: Document with ID "${id}" not found in collection "${collectionSlug}"`,
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const findOptions: Parameters<typeof payload.find>[0] = {
|
|
145
|
+
collection: collectionSlug,
|
|
146
|
+
depth,
|
|
147
|
+
limit,
|
|
148
|
+
page,
|
|
149
|
+
req,
|
|
150
|
+
...localAPIDefaults(authorizedMCP),
|
|
151
|
+
...(selectClause && { select: selectClause }),
|
|
152
|
+
...(locale && { locale }),
|
|
153
|
+
...(fallbackLocale && { fallbackLocale }),
|
|
154
|
+
...(draft !== undefined && { draft }),
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (sort) {
|
|
158
|
+
findOptions.sort = sort
|
|
159
|
+
}
|
|
160
|
+
if (Object.keys(whereClause).length > 0) {
|
|
161
|
+
findOptions.where = whereClause as Parameters<typeof payload.find>[0]['where']
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const result = await payload.find(findOptions)
|
|
165
|
+
|
|
166
|
+
let responseText = `Collection: "${collectionSlug}"\nTotal: ${result.totalDocs} documents\nPage: ${result.page} of ${result.totalPages}\n`
|
|
167
|
+
for (const doc of result.docs) {
|
|
168
|
+
responseText += `\n\`\`\`json\n${JSON.stringify(doc)}\n\`\`\``
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
content: [{ type: 'text', text: responseText }],
|
|
173
|
+
doc: result as unknown as Record<string, unknown>,
|
|
174
|
+
}
|
|
175
|
+
} catch (error) {
|
|
176
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
177
|
+
logger.error(`Error reading documents from collection ${collectionSlug}: ${errorMessage}`)
|
|
178
|
+
return {
|
|
179
|
+
content: [
|
|
180
|
+
{
|
|
181
|
+
type: 'text',
|
|
182
|
+
text: `❌ **Error reading documents from collection "${collectionSlug}":** ${errorMessage}`,
|
|
183
|
+
},
|
|
184
|
+
],
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
})
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import type { SelectType } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod'
|
|
4
|
+
|
|
5
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
6
|
+
import { getLogger } from '../../../utils/getLogger.js'
|
|
7
|
+
import {
|
|
8
|
+
getCollectionVirtualFieldNames,
|
|
9
|
+
stripVirtualFields,
|
|
10
|
+
} from '../../../utils/getVirtualFieldNames.js'
|
|
11
|
+
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
12
|
+
import { buildToolInput } from '../../../utils/schemaConversion/buildToolInput.js'
|
|
13
|
+
import { sanitizeEntitySchema } from '../../../utils/schemaConversion/sanitizeEntitySchema.js'
|
|
14
|
+
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
15
|
+
|
|
16
|
+
const DEFAULT_DESCRIPTION = 'Update documents in a collection by ID or where clause.'
|
|
17
|
+
|
|
18
|
+
export const updateCollectionTool = defineCollectionTool({
|
|
19
|
+
description: DEFAULT_DESCRIPTION,
|
|
20
|
+
input: ({ collectionSchema }) => {
|
|
21
|
+
const partialSchema = sanitizeEntitySchema(collectionSchema)
|
|
22
|
+
|
|
23
|
+
// Collection updates do not require all required fields to be passed => delete .required.
|
|
24
|
+
//
|
|
25
|
+
// Local API equivalent: packages/payload/src/collections/operations/local/update.ts#BaseOptions#data:
|
|
26
|
+
// data: DeepPartial<RequiredDataFromCollectionSlug<TSlug>>
|
|
27
|
+
delete partialSchema.required
|
|
28
|
+
|
|
29
|
+
return buildToolInput({
|
|
30
|
+
controls: {
|
|
31
|
+
id: z
|
|
32
|
+
.union([z.string(), z.number()])
|
|
33
|
+
.describe('The ID of the document to update')
|
|
34
|
+
.optional(),
|
|
35
|
+
depth: z
|
|
36
|
+
.number()
|
|
37
|
+
.describe('How many levels deep to populate relationships')
|
|
38
|
+
.optional()
|
|
39
|
+
.default(0),
|
|
40
|
+
draft: z
|
|
41
|
+
.boolean()
|
|
42
|
+
.describe('Whether to update the document as a draft')
|
|
43
|
+
.optional()
|
|
44
|
+
.default(false),
|
|
45
|
+
fallbackLocale: z
|
|
46
|
+
.string()
|
|
47
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
48
|
+
.optional(),
|
|
49
|
+
filePath: z.string().describe('File path for file uploads').optional(),
|
|
50
|
+
locale: z
|
|
51
|
+
.string()
|
|
52
|
+
.describe(
|
|
53
|
+
'Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale',
|
|
54
|
+
)
|
|
55
|
+
.optional(),
|
|
56
|
+
overrideLock: z
|
|
57
|
+
.boolean()
|
|
58
|
+
.describe('Whether to override document locks')
|
|
59
|
+
.optional()
|
|
60
|
+
.default(true),
|
|
61
|
+
overwriteExistingFiles: z
|
|
62
|
+
.boolean()
|
|
63
|
+
.describe('Whether to overwrite existing files')
|
|
64
|
+
.optional()
|
|
65
|
+
.default(false),
|
|
66
|
+
select: z
|
|
67
|
+
.string()
|
|
68
|
+
.describe(
|
|
69
|
+
'Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"title": "My Post"}\'',
|
|
70
|
+
)
|
|
71
|
+
.optional(),
|
|
72
|
+
where: z
|
|
73
|
+
.string()
|
|
74
|
+
.describe('JSON string for where clause to update multiple documents')
|
|
75
|
+
.optional(),
|
|
76
|
+
},
|
|
77
|
+
dataDescription: 'The fields to update',
|
|
78
|
+
dataSchema: partialSchema,
|
|
79
|
+
})
|
|
80
|
+
},
|
|
81
|
+
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
82
|
+
const payload = req.payload
|
|
83
|
+
const logger = getLogger({ payload })
|
|
84
|
+
|
|
85
|
+
const {
|
|
86
|
+
id,
|
|
87
|
+
data,
|
|
88
|
+
depth,
|
|
89
|
+
draft,
|
|
90
|
+
fallbackLocale,
|
|
91
|
+
filePath,
|
|
92
|
+
locale,
|
|
93
|
+
overrideLock,
|
|
94
|
+
overwriteExistingFiles,
|
|
95
|
+
select,
|
|
96
|
+
where,
|
|
97
|
+
} = input
|
|
98
|
+
|
|
99
|
+
logger.info(
|
|
100
|
+
`Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
if (!id && !where) {
|
|
105
|
+
return {
|
|
106
|
+
content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
let parsedData = transformPointDataToPayload(data)
|
|
111
|
+
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)
|
|
112
|
+
parsedData = stripVirtualFields(parsedData, virtualFieldNames)
|
|
113
|
+
|
|
114
|
+
let whereClause: Record<string, unknown> = {}
|
|
115
|
+
if (where) {
|
|
116
|
+
try {
|
|
117
|
+
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
118
|
+
} catch {
|
|
119
|
+
logger.error(`Invalid where clause JSON: ${where}`)
|
|
120
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let selectClause: SelectType | undefined
|
|
125
|
+
if (select) {
|
|
126
|
+
try {
|
|
127
|
+
selectClause = JSON.parse(select) as SelectType
|
|
128
|
+
} catch {
|
|
129
|
+
logger.warn(`Invalid select clause JSON: ${select}`)
|
|
130
|
+
return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (id) {
|
|
135
|
+
const updateOptions = {
|
|
136
|
+
id,
|
|
137
|
+
collection: collectionSlug,
|
|
138
|
+
data: parsedData,
|
|
139
|
+
depth,
|
|
140
|
+
draft,
|
|
141
|
+
overrideLock,
|
|
142
|
+
req,
|
|
143
|
+
...localAPIDefaults(authorizedMCP),
|
|
144
|
+
...(filePath ? { filePath } : {}),
|
|
145
|
+
...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
|
|
146
|
+
...(locale ? { locale } : {}),
|
|
147
|
+
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
148
|
+
...(selectClause ? { select: selectClause } : {}),
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const result = await payload.update(updateOptions as any)
|
|
152
|
+
|
|
153
|
+
return {
|
|
154
|
+
content: [
|
|
155
|
+
{
|
|
156
|
+
type: 'text',
|
|
157
|
+
text: `Document updated successfully in collection "${collectionSlug}"!\nUpdated document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
|
|
158
|
+
},
|
|
159
|
+
],
|
|
160
|
+
doc: result as Record<string, unknown>,
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const updateOptions = {
|
|
165
|
+
collection: collectionSlug,
|
|
166
|
+
data: parsedData,
|
|
167
|
+
depth,
|
|
168
|
+
draft,
|
|
169
|
+
overrideLock,
|
|
170
|
+
req,
|
|
171
|
+
...localAPIDefaults(authorizedMCP),
|
|
172
|
+
where: whereClause,
|
|
173
|
+
...(filePath ? { filePath } : {}),
|
|
174
|
+
...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
|
|
175
|
+
...(locale ? { locale } : {}),
|
|
176
|
+
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
177
|
+
...(selectClause ? { select: selectClause } : {}),
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const result = await payload.update(updateOptions as any)
|
|
181
|
+
|
|
182
|
+
const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
|
|
183
|
+
const docs = bulkResult.docs || []
|
|
184
|
+
const errors = bulkResult.errors || []
|
|
185
|
+
|
|
186
|
+
let responseText = `Multiple documents updated in collection "${collectionSlug}"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`
|
|
187
|
+
if (docs.length > 0) {
|
|
188
|
+
responseText += `\n\nUpdated documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``
|
|
189
|
+
}
|
|
190
|
+
if (errors.length > 0) {
|
|
191
|
+
responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
content: [{ type: 'text', text: responseText }],
|
|
196
|
+
doc: { docs, errors } as unknown as Record<string, unknown>,
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
200
|
+
logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)
|
|
201
|
+
return {
|
|
202
|
+
content: [
|
|
203
|
+
{
|
|
204
|
+
type: 'text',
|
|
205
|
+
text: `Error updating document in collection "${collectionSlug}": ${errorMessage}`,
|
|
206
|
+
},
|
|
207
|
+
],
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
})
|