@payloadcms/plugin-mcp 4.0.0-internal.40de3ec → 4.0.0-internal.43ccea7

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.
Files changed (232) hide show
  1. package/LICENSE.md +1 -1
  2. package/bin.js +7 -0
  3. package/dist/defaultAccess.d.ts +3 -0
  4. package/dist/defaultAccess.d.ts.map +1 -0
  5. package/dist/defaultAccess.js +3 -0
  6. package/dist/defaultAccess.js.map +1 -0
  7. package/dist/endpoint/access.d.ts +12 -5
  8. package/dist/endpoint/access.d.ts.map +1 -1
  9. package/dist/endpoint/access.js +75 -88
  10. package/dist/endpoint/access.js.map +1 -1
  11. package/dist/endpoint/index.d.ts +1 -1
  12. package/dist/endpoint/index.d.ts.map +1 -1
  13. package/dist/endpoint/index.js +68 -17
  14. package/dist/endpoint/index.js.map +1 -1
  15. package/dist/exports/client.d.ts +1 -1
  16. package/dist/exports/client.d.ts.map +1 -1
  17. package/dist/exports/client.js +1 -1
  18. package/dist/exports/client.js.map +1 -1
  19. package/dist/exports/internal.d.ts +2 -0
  20. package/dist/exports/internal.d.ts.map +1 -0
  21. package/dist/exports/internal.js +3 -0
  22. package/dist/exports/internal.js.map +1 -0
  23. package/dist/index.d.ts +2 -2
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js +15 -20
  26. package/dist/index.js.map +1 -1
  27. package/dist/mcp/buildMcpServer.d.ts +4 -4
  28. package/dist/mcp/buildMcpServer.d.ts.map +1 -1
  29. package/dist/mcp/buildMcpServer.js +133 -80
  30. package/dist/mcp/buildMcpServer.js.map +1 -1
  31. package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
  32. package/dist/mcp/builtin/collections/authTools.js +44 -6
  33. package/dist/mcp/builtin/collections/authTools.js.map +1 -1
  34. package/dist/mcp/builtin/collections/countTool.d.ts +2 -0
  35. package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -0
  36. package/dist/mcp/builtin/collections/countTool.js +68 -0
  37. package/dist/mcp/builtin/collections/countTool.js.map +1 -0
  38. package/dist/mcp/builtin/collections/countVersionsTool.d.ts +2 -0
  39. package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -0
  40. package/dist/mcp/builtin/collections/countVersionsTool.js +64 -0
  41. package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -0
  42. package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
  43. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  44. package/dist/mcp/builtin/collections/createTool.js +39 -44
  45. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  46. package/dist/mcp/builtin/collections/deleteTool.d.ts +1 -1
  47. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
  48. package/dist/mcp/builtin/collections/deleteTool.js +15 -22
  49. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
  50. package/dist/mcp/builtin/collections/duplicateTool.d.ts +2 -0
  51. package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -0
  52. package/dist/mcp/builtin/collections/duplicateTool.js +96 -0
  53. package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -0
  54. package/dist/mcp/builtin/collections/findDistinctTool.d.ts +2 -0
  55. package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -0
  56. package/dist/mcp/builtin/collections/findDistinctTool.js +92 -0
  57. package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -0
  58. package/dist/mcp/builtin/collections/findTool.d.ts +1 -1
  59. package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
  60. package/dist/mcp/builtin/collections/findTool.js +52 -47
  61. package/dist/mcp/builtin/collections/findTool.js.map +1 -1
  62. package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts +2 -0
  63. package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -0
  64. package/dist/mcp/builtin/collections/findVersionByIDTool.js +83 -0
  65. package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -0
  66. package/dist/mcp/builtin/collections/findVersionsTool.d.ts +2 -0
  67. package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -0
  68. package/dist/mcp/builtin/collections/findVersionsTool.js +98 -0
  69. package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
  70. package/dist/mcp/builtin/collections/formatCollectionError.d.ts +9 -0
  71. package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -0
  72. package/dist/mcp/builtin/collections/formatCollectionError.js +60 -0
  73. package/dist/mcp/builtin/collections/formatCollectionError.js.map +1 -0
  74. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts +2 -0
  75. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -0
  76. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +65 -0
  77. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -0
  78. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts +2 -0
  79. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -0
  80. package/dist/mcp/builtin/collections/restoreVersionTool.js +81 -0
  81. package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -0
  82. package/dist/mcp/builtin/collections/updateTool.d.ts +1 -1
  83. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
  84. package/dist/mcp/builtin/collections/updateTool.js +96 -89
  85. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  86. package/dist/mcp/builtin/getConfigInfoTool.d.ts +2 -0
  87. package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -0
  88. package/dist/mcp/builtin/getConfigInfoTool.js +68 -0
  89. package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -0
  90. package/dist/mcp/builtin/globals/countVersionsTool.d.ts +2 -0
  91. package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -0
  92. package/dist/mcp/builtin/globals/countVersionsTool.js +64 -0
  93. package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -0
  94. package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
  95. package/dist/mcp/builtin/globals/findTool.js +20 -23
  96. package/dist/mcp/builtin/globals/findTool.js.map +1 -1
  97. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts +2 -0
  98. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -0
  99. package/dist/mcp/builtin/globals/findVersionByIDTool.js +79 -0
  100. package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -0
  101. package/dist/mcp/builtin/globals/findVersionsTool.d.ts +2 -0
  102. package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -0
  103. package/dist/mcp/builtin/globals/findVersionsTool.js +94 -0
  104. package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -0
  105. package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts +2 -0
  106. package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -0
  107. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +65 -0
  108. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -0
  109. package/dist/mcp/builtin/globals/restoreVersionTool.d.ts +2 -0
  110. package/dist/mcp/builtin/globals/restoreVersionTool.d.ts.map +1 -0
  111. package/dist/mcp/builtin/globals/restoreVersionTool.js +77 -0
  112. package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -0
  113. package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
  114. package/dist/mcp/builtin/globals/updateTool.js +33 -42
  115. package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
  116. package/dist/mcp/builtin/validateEntityData.d.ts +14 -0
  117. package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -0
  118. package/dist/mcp/builtin/validateEntityData.js +82 -0
  119. package/dist/mcp/builtin/validateEntityData.js.map +1 -0
  120. package/dist/mcp/builtinTools.d.ts +144 -19
  121. package/dist/mcp/builtinTools.d.ts.map +1 -1
  122. package/dist/mcp/builtinTools.js +119 -20
  123. package/dist/mcp/builtinTools.js.map +1 -1
  124. package/dist/mcp/sanitizeMCPConfig.d.ts +0 -1
  125. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
  126. package/dist/mcp/sanitizeMCPConfig.js +121 -58
  127. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  128. package/dist/stdio.d.ts +3 -3
  129. package/dist/stdio.d.ts.map +1 -1
  130. package/dist/stdio.js +82 -45
  131. package/dist/stdio.js.map +1 -1
  132. package/dist/types.d.ts +112 -110
  133. package/dist/types.d.ts.map +1 -1
  134. package/dist/types.js +2 -4
  135. package/dist/types.js.map +1 -1
  136. package/dist/utils/getPluginConfig.d.ts +1 -1
  137. package/dist/utils/getPluginConfig.js +1 -1
  138. package/dist/utils/getPluginConfig.js.map +1 -1
  139. package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts +28 -0
  140. package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +1 -0
  141. package/dist/utils/schemaConversion/filterFieldsByAccess.js +71 -0
  142. package/dist/utils/schemaConversion/filterFieldsByAccess.js.map +1 -0
  143. package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +13 -0
  144. package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -0
  145. package/dist/utils/schemaConversion/getEntityInputSchema.js +66 -0
  146. package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -0
  147. package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +5 -10
  148. package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -1
  149. package/dist/utils/schemaConversion/sanitizeEntitySchema.js +15 -95
  150. package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -1
  151. package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +15 -40
  152. package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -1
  153. package/dist/utils/whereSchema.d.ts +9 -0
  154. package/dist/utils/whereSchema.d.ts.map +1 -0
  155. package/dist/utils/whereSchema.js +13 -0
  156. package/dist/utils/whereSchema.js.map +1 -0
  157. package/package.json +10 -15
  158. package/src/defaultAccess.ts +3 -0
  159. package/src/endpoint/access.ts +81 -101
  160. package/src/endpoint/index.ts +58 -16
  161. package/src/exports/client.ts +2 -1
  162. package/src/exports/internal.ts +1 -0
  163. package/src/index.ts +12 -20
  164. package/src/mcp/buildMcpServer.ts +170 -115
  165. package/src/mcp/builtin/collections/authTools.ts +45 -9
  166. package/src/mcp/builtin/collections/countTool.ts +73 -0
  167. package/src/mcp/builtin/collections/countVersionsTool.ts +69 -0
  168. package/src/mcp/builtin/collections/createTool.ts +62 -61
  169. package/src/mcp/builtin/collections/deleteTool.ts +20 -18
  170. package/src/mcp/builtin/collections/duplicateTool.ts +134 -0
  171. package/src/mcp/builtin/collections/findDistinctTool.ts +119 -0
  172. package/src/mcp/builtin/collections/findTool.ts +66 -37
  173. package/src/mcp/builtin/collections/findVersionByIDTool.ts +109 -0
  174. package/src/mcp/builtin/collections/findVersionsTool.ts +154 -0
  175. package/src/mcp/builtin/collections/formatCollectionError.ts +84 -0
  176. package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +63 -0
  177. package/src/mcp/builtin/collections/restoreVersionTool.ts +107 -0
  178. package/src/mcp/builtin/collections/updateTool.ts +127 -112
  179. package/src/mcp/builtin/getConfigInfoTool.ts +67 -0
  180. package/src/mcp/builtin/globals/countVersionsTool.ts +68 -0
  181. package/src/mcp/builtin/globals/findTool.ts +28 -19
  182. package/src/mcp/builtin/globals/findVersionByIDTool.ts +103 -0
  183. package/src/mcp/builtin/globals/findVersionsTool.ts +147 -0
  184. package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +63 -0
  185. package/src/mcp/builtin/globals/restoreVersionTool.ts +99 -0
  186. package/src/mcp/builtin/globals/updateTool.ts +52 -57
  187. package/src/mcp/builtin/validateEntityData.ts +132 -0
  188. package/src/mcp/builtinTools.ts +111 -41
  189. package/src/mcp/sanitizeMCPConfig.ts +117 -78
  190. package/src/stdio.ts +77 -45
  191. package/src/types.ts +128 -115
  192. package/src/utils/getPluginConfig.ts +1 -1
  193. package/src/utils/schemaConversion/filterFieldsByAccess.ts +123 -0
  194. package/src/utils/schemaConversion/getEntityInputSchema.ts +96 -0
  195. package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +12 -19
  196. package/src/utils/schemaConversion/sanitizeEntitySchema.ts +17 -114
  197. package/src/utils/whereSchema.ts +24 -0
  198. package/dist/@types/assets.d.js +0 -2
  199. package/dist/@types/assets.d.js.map +0 -1
  200. package/dist/collection/getAccessField.d.ts +0 -12
  201. package/dist/collection/getAccessField.d.ts.map +0 -1
  202. package/dist/collection/getAccessField.js +0 -57
  203. package/dist/collection/getAccessField.js.map +0 -1
  204. package/dist/collection/index.d.ts +0 -6
  205. package/dist/collection/index.d.ts.map +0 -1
  206. package/dist/collection/index.js +0 -60
  207. package/dist/collection/index.js.map +0 -1
  208. package/dist/components/AccessField/index.client.d.ts +0 -10
  209. package/dist/components/AccessField/index.client.d.ts.map +0 -1
  210. package/dist/components/AccessField/index.client.js +0 -305
  211. package/dist/components/AccessField/index.client.js.map +0 -1
  212. package/dist/components/AccessField/index.css +0 -93
  213. package/dist/utils/localAPIDefaults.d.ts +0 -20
  214. package/dist/utils/localAPIDefaults.d.ts.map +0 -1
  215. package/dist/utils/localAPIDefaults.js +0 -19
  216. package/dist/utils/localAPIDefaults.js.map +0 -1
  217. package/dist/utils/schemaConversion/buildToolInput.d.ts +0 -29
  218. package/dist/utils/schemaConversion/buildToolInput.d.ts.map +0 -1
  219. package/dist/utils/schemaConversion/buildToolInput.js +0 -51
  220. package/dist/utils/schemaConversion/buildToolInput.js.map +0 -1
  221. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +0 -7
  222. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +0 -1
  223. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js +0 -20
  224. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +0 -1
  225. package/src/@types/assets.d.ts +0 -3
  226. package/src/collection/getAccessField.ts +0 -64
  227. package/src/collection/index.ts +0 -64
  228. package/src/components/AccessField/index.client.tsx +0 -344
  229. package/src/components/AccessField/index.css +0 -93
  230. package/src/utils/localAPIDefaults.ts +0 -22
  231. package/src/utils/schemaConversion/buildToolInput.ts +0 -68
  232. package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
@@ -1,83 +1,92 @@
1
- import type { SelectType } from 'payload'
1
+ import type { SelectType, Where } from 'payload'
2
2
 
3
3
  import { z } from 'zod'
4
4
 
5
+ import { defaultAccess } from '../../../defaultAccess.js'
5
6
  import { defineCollectionTool } from '../../../defineTool.js'
6
7
  import { getLogger } from '../../../utils/getLogger.js'
7
8
  import {
8
9
  getCollectionVirtualFieldNames,
9
10
  stripVirtualFields,
10
11
  } 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'
12
+ import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
14
13
  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
- })
14
+ import { whereSchema } from '../../../utils/whereSchema.js'
15
+ import { validateCollectionData } from '../validateEntityData.js'
16
+ import { formatCollectionError } from './formatCollectionError.js'
17
+
18
+ const DEFAULT_DESCRIPTION =
19
+ 'Update documents in any collection by passing the collection slug and data.'
20
+
21
+ export const updateDocumentTool = defineCollectionTool({
22
+ access: (args) =>
23
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.update),
24
+ annotations: {
25
+ destructiveHint: true,
26
+ idempotentHint: false,
27
+ openWorldHint: false,
28
+ readOnlyHint: false,
29
+ title: 'Update Document',
80
30
  },
31
+ description: DEFAULT_DESCRIPTION,
32
+ input: z.object({
33
+ id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),
34
+ data: z
35
+ .record(z.string(), z.unknown())
36
+ .describe(
37
+ 'The fields to update. Only include fields permitted by the schema returned by getCollectionSchema.',
38
+ ),
39
+ depth: z
40
+ .number()
41
+ .describe('How many levels deep to populate relationships')
42
+ .optional()
43
+ .default(0),
44
+ draft: z
45
+ .boolean()
46
+ .describe(
47
+ '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.',
48
+ )
49
+ .optional()
50
+ .default(false),
51
+ fallbackLocale: z
52
+ .string()
53
+ .describe('Optional: fallback locale code to use when requested locale is not available')
54
+ .optional(),
55
+ filePath: z.string().describe('File path for file uploads').optional(),
56
+ locale: z
57
+ .string()
58
+ .describe(
59
+ 'Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale',
60
+ )
61
+ .optional(),
62
+ overrideLock: z
63
+ .boolean()
64
+ .describe('Whether to override document locks')
65
+ .optional()
66
+ .default(true),
67
+ overwriteExistingFiles: z
68
+ .boolean()
69
+ .describe('Whether to overwrite existing files')
70
+ .optional()
71
+ .default(false),
72
+ publishAllLocales: z
73
+ .boolean()
74
+ .describe(
75
+ 'For collections with localized publishing status, whether publishing should affect every locale. Set false with locale to publish only that locale.',
76
+ )
77
+ .optional(),
78
+ select: z
79
+ .record(z.string(), z.unknown())
80
+ .describe(
81
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}',
82
+ )
83
+ .optional(),
84
+ where: whereSchema
85
+ .describe(
86
+ 'Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}',
87
+ )
88
+ .optional(),
89
+ }),
81
90
  }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
82
91
  const payload = req.payload
83
92
  const logger = getLogger({ payload })
@@ -92,6 +101,7 @@ export const updateCollectionTool = defineCollectionTool({
92
101
  locale,
93
102
  overrideLock,
94
103
  overwriteExistingFiles,
104
+ publishAllLocales,
95
105
  select,
96
106
  where,
97
107
  } = input
@@ -107,48 +117,40 @@ export const updateCollectionTool = defineCollectionTool({
107
117
  }
108
118
  }
109
119
 
110
- let parsedData = transformPointDataToPayload(data)
111
120
  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
- }
121
+ const inputData = stripVirtualFields(data, virtualFieldNames)
122
+ const validationError = validateCollectionData({
123
+ collectionSlug,
124
+ data: inputData,
125
+ partial: true,
126
+ req,
127
+ })
123
128
 
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
- }
129
+ if (validationError) {
130
+ return validationError
132
131
  }
133
132
 
133
+ const parsedData = transformPointDataToPayload(inputData)
134
+
135
+ const whereClause: Where = where ?? {}
136
+
134
137
  if (id) {
135
- const updateOptions = {
138
+ const result = await payload.update({
136
139
  id,
137
140
  collection: collectionSlug,
138
141
  data: parsedData,
139
142
  depth,
140
143
  draft,
144
+ overrideAccess: authorizedMCP.overrideAccess,
141
145
  overrideLock,
142
146
  req,
143
- ...localAPIDefaults(authorizedMCP),
144
147
  ...(filePath ? { filePath } : {}),
145
148
  ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
149
+ ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),
146
150
  ...(locale ? { locale } : {}),
147
151
  ...(fallbackLocale ? { fallbackLocale } : {}),
148
- ...(selectClause ? { select: selectClause } : {}),
149
- }
150
-
151
- const result = await payload.update(updateOptions as any)
152
+ ...(select ? { select: select as SelectType } : {}),
153
+ })
152
154
 
153
155
  return {
154
156
  content: [
@@ -161,27 +163,25 @@ export const updateCollectionTool = defineCollectionTool({
161
163
  }
162
164
  }
163
165
 
164
- const updateOptions = {
166
+ const result = await payload.update({
165
167
  collection: collectionSlug,
166
168
  data: parsedData,
167
169
  depth,
168
170
  draft,
171
+ overrideAccess: authorizedMCP.overrideAccess,
169
172
  overrideLock,
170
173
  req,
171
- ...localAPIDefaults(authorizedMCP),
172
174
  where: whereClause,
173
175
  ...(filePath ? { filePath } : {}),
174
176
  ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
177
+ ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),
175
178
  ...(locale ? { locale } : {}),
176
179
  ...(fallbackLocale ? { fallbackLocale } : {}),
177
- ...(selectClause ? { select: selectClause } : {}),
178
- }
179
-
180
- const result = await payload.update(updateOptions as any)
180
+ ...(select ? { select: select as SelectType } : {}),
181
+ })
181
182
 
182
- const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
183
- const docs = bulkResult.docs || []
184
- const errors = bulkResult.errors || []
183
+ const docs = result.docs || []
184
+ const errors = result.errors || []
185
185
 
186
186
  let responseText = `Multiple documents updated in collection "${collectionSlug}"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`
187
187
  if (docs.length > 0) {
@@ -189,6 +189,28 @@ export const updateCollectionTool = defineCollectionTool({
189
189
  }
190
190
  if (errors.length > 0) {
191
191
  responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``
192
+
193
+ const errorSchema = getCollectionInputSchema({ collectionSlug, req })
194
+
195
+ if (errorSchema) {
196
+ responseText += `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(errorSchema)}\n\`\`\``
197
+ }
198
+
199
+ return {
200
+ content: [{ type: 'text', text: responseText }],
201
+ doc: { docs, errors } as unknown as Record<string, unknown>,
202
+ isError: true,
203
+ ...(errorSchema
204
+ ? {
205
+ structuredContent: {
206
+ collectionSlug,
207
+ docs,
208
+ errors,
209
+ schema: errorSchema,
210
+ },
211
+ }
212
+ : {}),
213
+ }
192
214
  }
193
215
 
194
216
  return {
@@ -198,13 +220,6 @@ export const updateCollectionTool = defineCollectionTool({
198
220
  } catch (error) {
199
221
  const errorMessage = error instanceof Error ? error.message : 'Unknown error'
200
222
  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
- }
223
+ return formatCollectionError({ action: 'updating', collectionSlug, error, req })
209
224
  }
210
225
  })
@@ -0,0 +1,67 @@
1
+ import { getAccessResults, isEntityHidden } from 'payload'
2
+
3
+ import { defineTool } from '../../defineTool.js'
4
+
5
+ export const getConfigInfoTool = defineTool({
6
+ annotations: {
7
+ destructiveHint: false,
8
+ idempotentHint: true,
9
+ openWorldHint: false,
10
+ readOnlyHint: true,
11
+ title: 'Config Info',
12
+ },
13
+ description: 'List the Payload collection and global slugs visible to this MCP client.',
14
+ }).handler(async ({ authorizedMCP, req }) => {
15
+ const user = req.user
16
+ const permissions = authorizedMCP.overrideAccess ? null : await getAccessResults({ req })
17
+ const authorizedCollectionSlugs = new Set<string>()
18
+ const authorizedGlobalSlugs = new Set<string>()
19
+
20
+ for (const item of authorizedMCP.items) {
21
+ if (item.type === 'collectionTool') {
22
+ authorizedCollectionSlugs.add(item.collectionSlug)
23
+ } else if (item.type === 'globalTool') {
24
+ authorizedGlobalSlugs.add(item.globalSlug)
25
+ }
26
+ }
27
+
28
+ const collections: string[] = []
29
+ const globals: string[] = []
30
+
31
+ for (const collection of req.payload.config.collections) {
32
+ if (!authorizedCollectionSlugs.has(collection.slug)) {
33
+ continue
34
+ }
35
+ if (user && isEntityHidden({ hidden: collection.admin.hidden, user })) {
36
+ continue
37
+ }
38
+ if (!authorizedMCP.overrideAccess && !permissions?.collections?.[collection.slug]?.read) {
39
+ continue
40
+ }
41
+
42
+ collections.push(collection.slug)
43
+ }
44
+
45
+ for (const global of req.payload.config.globals) {
46
+ if (!authorizedGlobalSlugs.has(global.slug)) {
47
+ continue
48
+ }
49
+ if (user && isEntityHidden({ hidden: global.admin.hidden, user })) {
50
+ continue
51
+ }
52
+ if (!authorizedMCP.overrideAccess && !permissions?.globals?.[global.slug]?.read) {
53
+ continue
54
+ }
55
+
56
+ globals.push(global.slug)
57
+ }
58
+
59
+ return {
60
+ content: [
61
+ {
62
+ type: 'text',
63
+ text: `Collections: ${collections.join(', ') || 'none'}\nGlobals: ${globals.join(', ') || 'none'}`,
64
+ },
65
+ ],
66
+ }
67
+ })
@@ -0,0 +1,68 @@
1
+ import { z } from 'zod'
2
+
3
+ import { defaultAccess } from '../../../defaultAccess.js'
4
+ import { defineGlobalTool } from '../../../defineTool.js'
5
+ import { getLogger } from '../../../utils/getLogger.js'
6
+ import { whereSchema } from '../../../utils/whereSchema.js'
7
+
8
+ const DEFAULT_DESCRIPTION =
9
+ 'Count global versions in any version-enabled global by passing the global slug and optional where clause.'
10
+
11
+ export const countGlobalVersionsTool = defineGlobalTool({
12
+ access: (args) =>
13
+ defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.readVersions),
14
+ annotations: {
15
+ destructiveHint: false,
16
+ idempotentHint: true,
17
+ openWorldHint: false,
18
+ readOnlyHint: true,
19
+ title: 'Count Global Versions',
20
+ },
21
+ description: DEFAULT_DESCRIPTION,
22
+ input: z.object({
23
+ locale: z.string().describe('Optional: locale code to count versions in').optional(),
24
+ where: whereSchema
25
+ .describe(
26
+ 'Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.siteName":{"contains":"test"}}',
27
+ )
28
+ .optional(),
29
+ }),
30
+ }).handler(async ({ authorizedMCP, globalSlug, input, req }) => {
31
+ const payload = req.payload
32
+ const logger = getLogger({ payload })
33
+ const { locale, where } = input
34
+
35
+ logger.info(`Counting versions for global: ${globalSlug}`)
36
+
37
+ try {
38
+ const result = await payload.countGlobalVersions({
39
+ global: globalSlug,
40
+ overrideAccess: authorizedMCP.overrideAccess,
41
+ req,
42
+ ...(locale ? { locale } : {}),
43
+ ...(where ? { where } : {}),
44
+ })
45
+
46
+ return {
47
+ content: [
48
+ {
49
+ type: 'text',
50
+ text: `Global "${globalSlug}" contains ${result.totalDocs} matching versions.\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
51
+ },
52
+ ],
53
+ doc: result,
54
+ }
55
+ } catch (error) {
56
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
57
+ logger.error(`Error counting versions for global ${globalSlug}: ${errorMessage}`)
58
+ return {
59
+ content: [
60
+ {
61
+ type: 'text',
62
+ text: `❌ **Error counting versions for global "${globalSlug}":** ${errorMessage}`,
63
+ },
64
+ ],
65
+ isError: true,
66
+ }
67
+ }
68
+ })
@@ -1,14 +1,23 @@
1
- import type { SelectType } from 'payload'
1
+ import type { PopulateType, SelectType } from 'payload'
2
2
 
3
3
  import { z } from 'zod'
4
4
 
5
+ import { defaultAccess } from '../../../defaultAccess.js'
5
6
  import { defineGlobalTool } from '../../../defineTool.js'
6
7
  import { getLogger } from '../../../utils/getLogger.js'
7
- import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
8
8
 
9
- const DEFAULT_DESCRIPTION = 'Find a Payload global singleton configuration.'
9
+ const DEFAULT_DESCRIPTION = 'Find any Payload global by passing the global slug.'
10
10
 
11
11
  export const findGlobalTool = defineGlobalTool({
12
+ access: (args) =>
13
+ defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.read),
14
+ annotations: {
15
+ destructiveHint: false,
16
+ idempotentHint: true,
17
+ openWorldHint: false,
18
+ readOnlyHint: true,
19
+ title: 'Find Global',
20
+ },
12
21
  description: DEFAULT_DESCRIPTION,
13
22
  input: z.object({
14
23
  depth: z
@@ -29,10 +38,16 @@ export const findGlobalTool = defineGlobalTool({
29
38
  'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
30
39
  )
31
40
  .optional(),
41
+ populate: z
42
+ .record(z.string(), z.unknown())
43
+ .describe(
44
+ 'Optional: control which fields to include from populated relationship or upload documents.',
45
+ )
46
+ .optional(),
32
47
  select: z
33
- .string()
48
+ .record(z.string(), z.unknown())
34
49
  .describe(
35
- "Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'",
50
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}',
36
51
  )
37
52
  .optional(),
38
53
  }),
@@ -40,7 +55,7 @@ export const findGlobalTool = defineGlobalTool({
40
55
  const payload = req.payload
41
56
  const logger = getLogger({ payload })
42
57
 
43
- const { depth, fallbackLocale, locale, select } = input
58
+ const { depth, fallbackLocale, locale, populate, select } = input
44
59
 
45
60
  logger.info(
46
61
  `Reading global: ${globalSlug}, depth: ${depth}${locale ? `, locale: ${locale}` : ''}`,
@@ -50,17 +65,8 @@ export const findGlobalTool = defineGlobalTool({
50
65
  const findOptions: Parameters<typeof payload.findGlobal>[0] = {
51
66
  slug: globalSlug,
52
67
  depth,
53
- ...localAPIDefaults(authorizedMCP),
54
- }
55
-
56
- let selectClause: SelectType | undefined
57
- if (select) {
58
- try {
59
- selectClause = JSON.parse(select) as SelectType
60
- } catch {
61
- logger.warn(`Invalid select clause JSON for global: ${select}`)
62
- return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
63
- }
68
+ overrideAccess: authorizedMCP.overrideAccess,
69
+ req,
64
70
  }
65
71
 
66
72
  if (locale) {
@@ -69,8 +75,11 @@ export const findGlobalTool = defineGlobalTool({
69
75
  if (fallbackLocale) {
70
76
  findOptions.fallbackLocale = fallbackLocale
71
77
  }
72
- if (selectClause) {
73
- findOptions.select = selectClause
78
+ if (select) {
79
+ findOptions.select = select as SelectType
80
+ }
81
+ if (populate) {
82
+ findOptions.populate = populate as PopulateType
74
83
  }
75
84
 
76
85
  const result = await payload.findGlobal(findOptions)
@@ -0,0 +1,103 @@
1
+ import type { PopulateType, SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defaultAccess } from '../../../defaultAccess.js'
6
+ import { defineGlobalTool } from '../../../defineTool.js'
7
+ import { getLogger } from '../../../utils/getLogger.js'
8
+
9
+ const DEFAULT_DESCRIPTION =
10
+ 'Find a specific global version in any version-enabled global by passing the global slug and version ID.'
11
+
12
+ export const findGlobalVersionByIDTool = defineGlobalTool({
13
+ access: (args) =>
14
+ defaultAccess(args) && Boolean(args.permissions?.globals?.[args.globalSlug]?.readVersions),
15
+ annotations: {
16
+ destructiveHint: false,
17
+ idempotentHint: true,
18
+ openWorldHint: false,
19
+ readOnlyHint: true,
20
+ title: 'Find Global Version By ID',
21
+ },
22
+ description: DEFAULT_DESCRIPTION,
23
+ input: z.object({
24
+ id: z.string().describe('The ID of the global version to retrieve'),
25
+ depth: z
26
+ .number()
27
+ .int()
28
+ .min(0)
29
+ .max(10)
30
+ .describe('How many levels deep to populate relationships in the global version document')
31
+ .optional()
32
+ .default(0),
33
+ fallbackLocale: z
34
+ .string()
35
+ .describe('Optional: fallback locale code to use when requested locale is not available')
36
+ .optional(),
37
+ locale: z
38
+ .string()
39
+ .describe(
40
+ 'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
41
+ )
42
+ .optional(),
43
+ populate: z
44
+ .record(z.string(), z.unknown())
45
+ .describe(
46
+ 'Optional: control which fields to include from populated relationship or upload documents.',
47
+ )
48
+ .optional(),
49
+ select: z
50
+ .record(z.string(), z.unknown())
51
+ .describe(
52
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"version.siteName": true}',
53
+ )
54
+ .optional(),
55
+ showHiddenFields: z
56
+ .boolean()
57
+ .describe('Optional: include hidden fields in the returned global version document')
58
+ .optional(),
59
+ }),
60
+ }).handler(async ({ authorizedMCP, globalSlug, input, req }) => {
61
+ const payload = req.payload
62
+ const logger = getLogger({ payload })
63
+ const { id, depth, fallbackLocale, locale, populate, select, showHiddenFields } = input
64
+
65
+ logger.info(`Finding version for global: ${globalSlug} with ID: ${id}`)
66
+
67
+ try {
68
+ const result = await payload.findGlobalVersionByID({
69
+ id,
70
+ slug: globalSlug,
71
+ depth,
72
+ overrideAccess: authorizedMCP.overrideAccess,
73
+ req,
74
+ ...(fallbackLocale ? { fallbackLocale } : {}),
75
+ ...(locale ? { locale } : {}),
76
+ ...(populate ? { populate: populate as PopulateType } : {}),
77
+ ...(select ? { select: select as SelectType } : {}),
78
+ ...(showHiddenFields !== undefined ? { showHiddenFields } : {}),
79
+ })
80
+
81
+ return {
82
+ content: [
83
+ {
84
+ type: 'text',
85
+ text: `Version "${id}" from global "${globalSlug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
86
+ },
87
+ ],
88
+ doc: result as unknown as Record<string, unknown>,
89
+ }
90
+ } catch (error) {
91
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
92
+ logger.error(`Error finding version ${id} for global ${globalSlug}: ${errorMessage}`)
93
+ return {
94
+ content: [
95
+ {
96
+ type: 'text',
97
+ text: `❌ **Error finding version "${id}" for global "${globalSlug}":** ${errorMessage}`,
98
+ },
99
+ ],
100
+ isError: true,
101
+ }
102
+ }
103
+ })