@payloadcms/plugin-mcp 4.0.0-canary.1 → 4.0.0-canary.11

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 (222) 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/defineTool.d.ts +8 -8
  8. package/dist/defineTool.d.ts.map +1 -1
  9. package/dist/endpoint/access.d.ts +12 -5
  10. package/dist/endpoint/access.d.ts.map +1 -1
  11. package/dist/endpoint/access.js +75 -88
  12. package/dist/endpoint/access.js.map +1 -1
  13. package/dist/endpoint/index.d.ts +1 -1
  14. package/dist/endpoint/index.d.ts.map +1 -1
  15. package/dist/endpoint/index.js +68 -17
  16. package/dist/endpoint/index.js.map +1 -1
  17. package/dist/exports/client.d.ts +1 -1
  18. package/dist/exports/client.d.ts.map +1 -1
  19. package/dist/exports/client.js +1 -1
  20. package/dist/exports/client.js.map +1 -1
  21. package/dist/exports/internal.d.ts +2 -0
  22. package/dist/exports/internal.d.ts.map +1 -0
  23. package/dist/exports/internal.js +3 -0
  24. package/dist/exports/internal.js.map +1 -0
  25. package/dist/index.d.ts +2 -2
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +15 -20
  28. package/dist/index.js.map +1 -1
  29. package/dist/mcp/buildMcpServer.d.ts +4 -4
  30. package/dist/mcp/buildMcpServer.d.ts.map +1 -1
  31. package/dist/mcp/buildMcpServer.js +34 -10
  32. package/dist/mcp/buildMcpServer.js.map +1 -1
  33. package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
  34. package/dist/mcp/builtin/collections/authTools.js +44 -6
  35. package/dist/mcp/builtin/collections/authTools.js.map +1 -1
  36. package/dist/mcp/builtin/collections/countTool.d.ts +2 -0
  37. package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -0
  38. package/dist/mcp/builtin/collections/countTool.js +68 -0
  39. package/dist/mcp/builtin/collections/countTool.js.map +1 -0
  40. package/dist/mcp/builtin/collections/countVersionsTool.d.ts +2 -0
  41. package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -0
  42. package/dist/mcp/builtin/collections/countVersionsTool.js +64 -0
  43. package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -0
  44. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  45. package/dist/mcp/builtin/collections/createTool.js +26 -24
  46. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  47. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
  48. package/dist/mcp/builtin/collections/deleteTool.js +10 -2
  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/fileInput.d.ts +20 -0
  55. package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -0
  56. package/dist/mcp/builtin/collections/fileInput.js +100 -0
  57. package/dist/mcp/builtin/collections/fileInput.js.map +1 -0
  58. package/dist/mcp/builtin/collections/fileInput.spec.js +72 -0
  59. package/dist/mcp/builtin/collections/fileInput.spec.js.map +1 -0
  60. package/dist/mcp/builtin/collections/findDistinctTool.d.ts +2 -0
  61. package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -0
  62. package/dist/mcp/builtin/collections/findDistinctTool.js +92 -0
  63. package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -0
  64. package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
  65. package/dist/mcp/builtin/collections/findTool.js +46 -26
  66. package/dist/mcp/builtin/collections/findTool.js.map +1 -1
  67. package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts +2 -0
  68. package/dist/mcp/builtin/collections/findVersionByIDTool.d.ts.map +1 -0
  69. package/dist/mcp/builtin/collections/findVersionByIDTool.js +83 -0
  70. package/dist/mcp/builtin/collections/findVersionByIDTool.js.map +1 -0
  71. package/dist/mcp/builtin/collections/findVersionsTool.d.ts +2 -0
  72. package/dist/mcp/builtin/collections/findVersionsTool.d.ts.map +1 -0
  73. package/dist/mcp/builtin/collections/findVersionsTool.js +98 -0
  74. package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
  75. package/dist/mcp/builtin/collections/formatCollectionError.d.ts +1 -1
  76. package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -1
  77. package/dist/mcp/builtin/collections/formatCollectionError.js.map +1 -1
  78. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
  79. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +55 -4
  80. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
  81. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts +2 -0
  82. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -0
  83. package/dist/mcp/builtin/collections/restoreVersionTool.js +81 -0
  84. package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -0
  85. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
  86. package/dist/mcp/builtin/collections/updateTool.js +37 -32
  87. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  88. package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -1
  89. package/dist/mcp/builtin/getConfigInfoTool.js +27 -8
  90. package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -1
  91. package/dist/mcp/builtin/globals/countVersionsTool.d.ts +2 -0
  92. package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -0
  93. package/dist/mcp/builtin/globals/countVersionsTool.js +64 -0
  94. package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -0
  95. package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
  96. package/dist/mcp/builtin/globals/findTool.js +19 -22
  97. package/dist/mcp/builtin/globals/findTool.js.map +1 -1
  98. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts +2 -0
  99. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -0
  100. package/dist/mcp/builtin/globals/findVersionByIDTool.js +79 -0
  101. package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -0
  102. package/dist/mcp/builtin/globals/findVersionsTool.d.ts +2 -0
  103. package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -0
  104. package/dist/mcp/builtin/globals/findVersionsTool.js +94 -0
  105. package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -0
  106. package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -1
  107. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +32 -2
  108. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -1
  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.js +14 -21
  114. package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
  115. package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -1
  116. package/dist/mcp/builtin/validateEntityData.js +1 -1
  117. package/dist/mcp/builtin/validateEntityData.js.map +1 -1
  118. package/dist/mcp/builtinTools.d.ts +69 -12
  119. package/dist/mcp/builtinTools.d.ts.map +1 -1
  120. package/dist/mcp/builtinTools.js +66 -10
  121. package/dist/mcp/builtinTools.js.map +1 -1
  122. package/dist/mcp/sanitizeMCPConfig.d.ts +0 -1
  123. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
  124. package/dist/mcp/sanitizeMCPConfig.js +69 -27
  125. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  126. package/dist/stdio.d.ts +3 -3
  127. package/dist/stdio.d.ts.map +1 -1
  128. package/dist/stdio.js +82 -45
  129. package/dist/stdio.js.map +1 -1
  130. package/dist/types.d.ts +100 -87
  131. package/dist/types.d.ts.map +1 -1
  132. package/dist/types.js +2 -4
  133. package/dist/types.js.map +1 -1
  134. package/dist/utils/camelCase.d.ts.map +1 -1
  135. package/dist/utils/getPluginConfig.d.ts +1 -1
  136. package/dist/utils/getPluginConfig.js +1 -1
  137. package/dist/utils/getPluginConfig.js.map +1 -1
  138. package/dist/utils/resolveProjectRoot.d.ts.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 +5 -3
  144. package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -1
  145. package/dist/utils/schemaConversion/getEntityInputSchema.js +44 -12
  146. package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -1
  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/toStandardSchema.d.ts.map +1 -1
  154. package/package.json +10 -15
  155. package/src/defaultAccess.ts +3 -0
  156. package/src/endpoint/access.ts +81 -101
  157. package/src/endpoint/index.ts +58 -16
  158. package/src/exports/client.ts +2 -1
  159. package/src/exports/internal.ts +1 -0
  160. package/src/index.ts +12 -20
  161. package/src/mcp/buildMcpServer.ts +48 -21
  162. package/src/mcp/builtin/collections/authTools.ts +45 -9
  163. package/src/mcp/builtin/collections/countTool.ts +73 -0
  164. package/src/mcp/builtin/collections/countVersionsTool.ts +69 -0
  165. package/src/mcp/builtin/collections/createTool.ts +27 -18
  166. package/src/mcp/builtin/collections/deleteTool.ts +11 -2
  167. package/src/mcp/builtin/collections/duplicateTool.ts +134 -0
  168. package/src/mcp/builtin/collections/fileInput.spec.ts +79 -0
  169. package/src/mcp/builtin/collections/fileInput.ts +139 -0
  170. package/src/mcp/builtin/collections/findDistinctTool.ts +119 -0
  171. package/src/mcp/builtin/collections/findTool.ts +59 -20
  172. package/src/mcp/builtin/collections/findVersionByIDTool.ts +109 -0
  173. package/src/mcp/builtin/collections/findVersionsTool.ts +154 -0
  174. package/src/mcp/builtin/collections/formatCollectionError.ts +1 -1
  175. package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +51 -3
  176. package/src/mcp/builtin/collections/restoreVersionTool.ts +107 -0
  177. package/src/mcp/builtin/collections/updateTool.ts +39 -23
  178. package/src/mcp/builtin/getConfigInfoTool.ts +27 -4
  179. package/src/mcp/builtin/globals/countVersionsTool.ts +68 -0
  180. package/src/mcp/builtin/globals/findTool.ts +27 -18
  181. package/src/mcp/builtin/globals/findVersionByIDTool.ts +103 -0
  182. package/src/mcp/builtin/globals/findVersionsTool.ts +147 -0
  183. package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +37 -2
  184. package/src/mcp/builtin/globals/restoreVersionTool.ts +99 -0
  185. package/src/mcp/builtin/globals/updateTool.ts +16 -16
  186. package/src/mcp/builtin/validateEntityData.ts +1 -1
  187. package/src/mcp/builtinTools.ts +72 -16
  188. package/src/mcp/sanitizeMCPConfig.ts +48 -30
  189. package/src/stdio.ts +77 -45
  190. package/src/types.ts +108 -90
  191. package/src/utils/getPluginConfig.ts +1 -1
  192. package/src/utils/schemaConversion/filterFieldsByAccess.ts +123 -0
  193. package/src/utils/schemaConversion/getEntityInputSchema.ts +45 -27
  194. package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +12 -19
  195. package/src/utils/schemaConversion/sanitizeEntitySchema.ts +17 -114
  196. package/dist/collection/getAccessField.d.ts +0 -12
  197. package/dist/collection/getAccessField.d.ts.map +0 -1
  198. package/dist/collection/getAccessField.js +0 -57
  199. package/dist/collection/getAccessField.js.map +0 -1
  200. package/dist/collection/index.d.ts +0 -6
  201. package/dist/collection/index.d.ts.map +0 -1
  202. package/dist/collection/index.js +0 -60
  203. package/dist/collection/index.js.map +0 -1
  204. package/dist/components/AccessField/index.client.d.ts +0 -10
  205. package/dist/components/AccessField/index.client.d.ts.map +0 -1
  206. package/dist/components/AccessField/index.client.js +0 -305
  207. package/dist/components/AccessField/index.client.js.map +0 -1
  208. package/dist/components/AccessField/index.css +0 -93
  209. package/dist/utils/localAPIDefaults.d.ts +0 -20
  210. package/dist/utils/localAPIDefaults.d.ts.map +0 -1
  211. package/dist/utils/localAPIDefaults.js +0 -19
  212. package/dist/utils/localAPIDefaults.js.map +0 -1
  213. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +0 -7
  214. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +0 -1
  215. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js +0 -20
  216. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +0 -1
  217. package/src/collection/getAccessField.ts +0 -64
  218. package/src/collection/index.ts +0 -64
  219. package/src/components/AccessField/index.client.tsx +0 -347
  220. package/src/components/AccessField/index.css +0 -93
  221. package/src/utils/localAPIDefaults.ts +0 -22
  222. package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
@@ -0,0 +1,109 @@
1
+ import type { PopulateType, SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defaultAccess } from '../../../defaultAccess.js'
6
+ import { defineCollectionTool } from '../../../defineTool.js'
7
+ import { getLogger } from '../../../utils/getLogger.js'
8
+
9
+ const DEFAULT_DESCRIPTION =
10
+ 'Find a specific document version in any version-enabled collection by passing the collection slug and version ID.'
11
+
12
+ export const findVersionByIDTool = defineCollectionTool({
13
+ access: (args) =>
14
+ defaultAccess(args) &&
15
+ Boolean(args.permissions?.collections?.[args.collectionSlug]?.readVersions),
16
+ annotations: {
17
+ destructiveHint: false,
18
+ idempotentHint: true,
19
+ openWorldHint: false,
20
+ readOnlyHint: true,
21
+ title: 'Find Version By ID',
22
+ },
23
+ description: DEFAULT_DESCRIPTION,
24
+ input: z.object({
25
+ id: z.string().describe('The ID of the version to retrieve'),
26
+ depth: z
27
+ .number()
28
+ .int()
29
+ .min(0)
30
+ .max(10)
31
+ .describe('How many levels deep to populate relationships in the version document')
32
+ .optional()
33
+ .default(0),
34
+ fallbackLocale: z
35
+ .string()
36
+ .describe('Optional: fallback locale code to use when requested locale is not available')
37
+ .optional(),
38
+ locale: z
39
+ .string()
40
+ .describe(
41
+ 'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
42
+ )
43
+ .optional(),
44
+ populate: z
45
+ .record(z.string(), z.unknown())
46
+ .describe(
47
+ 'Optional: control which fields to include from populated relationship or upload documents.',
48
+ )
49
+ .optional(),
50
+ select: z
51
+ .record(z.string(), z.unknown())
52
+ .describe(
53
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"version.title": true}',
54
+ )
55
+ .optional(),
56
+ showHiddenFields: z
57
+ .boolean()
58
+ .describe('Optional: include hidden fields in the returned version document')
59
+ .optional(),
60
+ trash: z
61
+ .boolean()
62
+ .describe('Optional: include soft-deleted version documents when trash is enabled')
63
+ .optional(),
64
+ }),
65
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
66
+ const payload = req.payload
67
+ const logger = getLogger({ payload })
68
+ const { id, depth, fallbackLocale, locale, populate, select, showHiddenFields, trash } = input
69
+
70
+ logger.info(`Finding version in collection: ${collectionSlug} with ID: ${id}`)
71
+
72
+ try {
73
+ const result = await payload.findVersionByID({
74
+ id,
75
+ collection: collectionSlug,
76
+ depth,
77
+ overrideAccess: authorizedMCP.overrideAccess,
78
+ req,
79
+ ...(fallbackLocale ? { fallbackLocale } : {}),
80
+ ...(locale ? { locale } : {}),
81
+ ...(populate ? { populate: populate as PopulateType } : {}),
82
+ ...(select ? { select: select as SelectType } : {}),
83
+ ...(showHiddenFields !== undefined ? { showHiddenFields } : {}),
84
+ ...(trash !== undefined ? { trash } : {}),
85
+ })
86
+
87
+ return {
88
+ content: [
89
+ {
90
+ type: 'text',
91
+ text: `Version "${id}" from collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
92
+ },
93
+ ],
94
+ doc: result as unknown as Record<string, unknown>,
95
+ }
96
+ } catch (error) {
97
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
98
+ logger.error(`Error finding version ${id} in ${collectionSlug}: ${errorMessage}`)
99
+ return {
100
+ content: [
101
+ {
102
+ type: 'text',
103
+ text: `❌ **Error finding version "${id}" in collection "${collectionSlug}":** ${errorMessage}`,
104
+ },
105
+ ],
106
+ isError: true,
107
+ }
108
+ }
109
+ })
@@ -0,0 +1,154 @@
1
+ import type { PopulateType, SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defaultAccess } from '../../../defaultAccess.js'
6
+ import { defineCollectionTool } from '../../../defineTool.js'
7
+ import { getLogger } from '../../../utils/getLogger.js'
8
+ import { whereSchema } from '../../../utils/whereSchema.js'
9
+
10
+ const DEFAULT_DESCRIPTION =
11
+ 'Find document versions in any version-enabled collection by passing the collection slug and optional where clause.'
12
+
13
+ export const findVersionsTool = defineCollectionTool({
14
+ access: (args) =>
15
+ defaultAccess(args) &&
16
+ Boolean(args.permissions?.collections?.[args.collectionSlug]?.readVersions),
17
+ annotations: {
18
+ destructiveHint: false,
19
+ idempotentHint: true,
20
+ openWorldHint: false,
21
+ readOnlyHint: true,
22
+ title: 'Find Versions',
23
+ },
24
+ description: DEFAULT_DESCRIPTION,
25
+ input: z.object({
26
+ depth: z
27
+ .number()
28
+ .int()
29
+ .min(0)
30
+ .max(10)
31
+ .describe('How many levels deep to populate relationships in version documents')
32
+ .optional()
33
+ .default(0),
34
+ fallbackLocale: z
35
+ .string()
36
+ .describe('Optional: fallback locale code to use when requested locale is not available')
37
+ .optional(),
38
+ limit: z
39
+ .number()
40
+ .int()
41
+ .min(1)
42
+ .max(100)
43
+ .describe('Maximum number of versions to return (default: 10, max: 100)')
44
+ .optional()
45
+ .default(10),
46
+ locale: z
47
+ .string()
48
+ .describe(
49
+ 'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
50
+ )
51
+ .optional(),
52
+ page: z
53
+ .number()
54
+ .int()
55
+ .min(1)
56
+ .describe('Page number for pagination (default: 1)')
57
+ .optional()
58
+ .default(1),
59
+ pagination: z
60
+ .boolean()
61
+ .describe('Optional: set to false to skip the count query overhead')
62
+ .optional(),
63
+ populate: z
64
+ .record(z.string(), z.unknown())
65
+ .describe(
66
+ 'Optional: control which fields to include from populated relationship or upload documents.',
67
+ )
68
+ .optional(),
69
+ select: z
70
+ .record(z.string(), z.unknown())
71
+ .describe(
72
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"version.title": true}',
73
+ )
74
+ .optional(),
75
+ showHiddenFields: z
76
+ .boolean()
77
+ .describe('Optional: include hidden fields in returned version documents')
78
+ .optional(),
79
+ sort: z
80
+ .string()
81
+ .describe('Field to sort by (e.g., "-updatedAt", "-version.updatedAt" for descending)')
82
+ .optional(),
83
+ trash: z
84
+ .boolean()
85
+ .describe('Optional: include versions for soft-deleted documents when trash is enabled')
86
+ .optional(),
87
+ where: whereSchema
88
+ .describe(
89
+ 'Optional: where clause for filtering versions. Version document fields are usually under "version". Example: {"version.title":{"contains":"test"}}',
90
+ )
91
+ .optional(),
92
+ }),
93
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
94
+ const payload = req.payload
95
+ const logger = getLogger({ payload })
96
+ const {
97
+ depth,
98
+ fallbackLocale,
99
+ limit,
100
+ locale,
101
+ page,
102
+ pagination,
103
+ populate,
104
+ select,
105
+ showHiddenFields,
106
+ sort,
107
+ trash,
108
+ where,
109
+ } = input
110
+
111
+ logger.info(`Finding versions in collection: ${collectionSlug}, limit: ${limit}, page: ${page}`)
112
+
113
+ try {
114
+ const result = await payload.findVersions({
115
+ collection: collectionSlug,
116
+ depth,
117
+ limit,
118
+ overrideAccess: authorizedMCP.overrideAccess,
119
+ page,
120
+ req,
121
+ ...(fallbackLocale ? { fallbackLocale } : {}),
122
+ ...(locale ? { locale } : {}),
123
+ ...(pagination !== undefined ? { pagination } : {}),
124
+ ...(populate ? { populate: populate as PopulateType } : {}),
125
+ ...(select ? { select: select as SelectType } : {}),
126
+ ...(showHiddenFields !== undefined ? { showHiddenFields } : {}),
127
+ ...(sort ? { sort } : {}),
128
+ ...(trash !== undefined ? { trash } : {}),
129
+ ...(where ? { where } : {}),
130
+ })
131
+
132
+ return {
133
+ content: [
134
+ {
135
+ type: 'text',
136
+ text: `Versions for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
137
+ },
138
+ ],
139
+ doc: result as unknown as Record<string, unknown>,
140
+ }
141
+ } catch (error) {
142
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
143
+ logger.error(`Error finding versions in ${collectionSlug}: ${errorMessage}`)
144
+ return {
145
+ content: [
146
+ {
147
+ type: 'text',
148
+ text: `❌ **Error finding versions in collection "${collectionSlug}":** ${errorMessage}`,
149
+ },
150
+ ],
151
+ isError: true,
152
+ }
153
+ }
154
+ })
@@ -38,7 +38,7 @@ export const formatCollectionError = ({
38
38
  error,
39
39
  req,
40
40
  }: {
41
- action: 'creating' | 'updating'
41
+ action: 'creating' | 'duplicating' | 'updating'
42
42
  collectionSlug: CollectionSlug
43
43
  error: unknown
44
44
  req: PayloadRequest
@@ -1,10 +1,45 @@
1
+ import { getAccessResults } from 'payload'
2
+
3
+ import { defaultAccess } from '../../../defaultAccess.js'
1
4
  import { defineCollectionTool } from '../../../defineTool.js'
2
5
  import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
3
6
 
4
7
  export const getCollectionSchemaTool = defineCollectionTool({
8
+ access: (args) => {
9
+ const permissions = args.permissions?.collections?.[args.collectionSlug]
10
+
11
+ return defaultAccess(args) && Boolean(permissions?.create || permissions?.update)
12
+ },
13
+ annotations: {
14
+ destructiveHint: false,
15
+ idempotentHint: true,
16
+ openWorldHint: false,
17
+ readOnlyHint: true,
18
+ title: 'Get Collection Schema',
19
+ },
5
20
  description: 'Get the input schema for creating or updating documents in a collection.',
6
- }).handler(({ collectionSlug, req }) => {
7
- const inputSchema = getCollectionInputSchema({ collectionSlug, req })
21
+ }).handler(async ({ authorizedMCP, collectionSlug, req }) => {
22
+ const permissions = authorizedMCP.overrideAccess
23
+ ? null
24
+ : (await getAccessResults({ req })).collections?.[collectionSlug]
25
+
26
+ if (!authorizedMCP.overrideAccess && !permissions?.create && !permissions?.update) {
27
+ return {
28
+ content: [
29
+ {
30
+ type: 'text',
31
+ text: `Error: MCP access to "getCollectionSchema" is not enabled for collection "${collectionSlug}"`,
32
+ },
33
+ ],
34
+ isError: true,
35
+ }
36
+ }
37
+
38
+ const inputSchema = getCollectionInputSchema({
39
+ collectionSlug,
40
+ req,
41
+ ...(permissions ? { permissions } : {}),
42
+ })
8
43
 
9
44
  if (!inputSchema) {
10
45
  return {
@@ -13,16 +48,29 @@ export const getCollectionSchemaTool = defineCollectionTool({
13
48
  }
14
49
  }
15
50
 
51
+ const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
52
+ const maxFileSize = req.payload.config.upload.limits?.fileSize
53
+ const upload = uploadConfig
54
+ ? {
55
+ enabled: true,
56
+ filesRequiredOnCreate: uploadConfig.filesRequiredOnCreate !== false,
57
+ mimeTypes: uploadConfig.mimeTypes ?? ['*/*'],
58
+ sources: [...(uploadConfig.pasteURL !== false ? ['url'] : []), 'base64'],
59
+ ...(typeof maxFileSize === 'number' && Number.isFinite(maxFileSize) ? { maxFileSize } : {}),
60
+ }
61
+ : { enabled: false }
62
+
16
63
  return {
17
64
  content: [
18
65
  {
19
66
  type: 'text',
20
- text: `Schema for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``,
67
+ text: `Schema for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\`\nUpload configuration:\n\`\`\`json\n${JSON.stringify(upload)}\n\`\`\``,
21
68
  },
22
69
  ],
23
70
  structuredContent: {
24
71
  collectionSlug,
25
72
  schema: inputSchema,
73
+ upload,
26
74
  },
27
75
  }
28
76
  })
@@ -0,0 +1,107 @@
1
+ import type { PopulateType, SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defaultAccess } from '../../../defaultAccess.js'
6
+ import { defineCollectionTool } from '../../../defineTool.js'
7
+ import { getLogger } from '../../../utils/getLogger.js'
8
+
9
+ const DEFAULT_DESCRIPTION =
10
+ 'Restore a document from a previous version in any version-enabled collection.'
11
+
12
+ export const restoreVersionTool = defineCollectionTool({
13
+ access: (args) =>
14
+ 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: 'Restore Version',
21
+ },
22
+ description: DEFAULT_DESCRIPTION,
23
+ input: z.object({
24
+ id: z.string().describe('The ID of the version to restore'),
25
+ depth: z
26
+ .number()
27
+ .int()
28
+ .min(0)
29
+ .max(10)
30
+ .describe('How many levels deep to populate relationships in response')
31
+ .optional()
32
+ .default(0),
33
+ draft: z
34
+ .boolean()
35
+ .describe('Whether to restore the version as a draft')
36
+ .optional()
37
+ .default(false),
38
+ fallbackLocale: z
39
+ .string()
40
+ .describe('Optional: fallback locale code to use when requested locale is not available')
41
+ .optional(),
42
+ locale: z
43
+ .string()
44
+ .describe('Optional: locale code to restore in (e.g., "en", "es")')
45
+ .optional(),
46
+ populate: z
47
+ .record(z.string(), z.unknown())
48
+ .describe(
49
+ 'Optional: control which fields to include from populated relationship or upload documents.',
50
+ )
51
+ .optional(),
52
+ select: z
53
+ .record(z.string(), z.unknown())
54
+ .describe(
55
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}',
56
+ )
57
+ .optional(),
58
+ showHiddenFields: z
59
+ .boolean()
60
+ .describe('Optional: include hidden fields in the restored document response')
61
+ .optional(),
62
+ }),
63
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
64
+ const payload = req.payload
65
+ const logger = getLogger({ payload })
66
+ const { id, depth, draft, fallbackLocale, locale, populate, select, showHiddenFields } = input
67
+
68
+ logger.info(`Restoring version in collection: ${collectionSlug} with ID: ${id}`)
69
+
70
+ try {
71
+ const result = await payload.restoreVersion({
72
+ id,
73
+ collection: collectionSlug,
74
+ depth,
75
+ draft,
76
+ overrideAccess: authorizedMCP.overrideAccess,
77
+ req,
78
+ ...(fallbackLocale ? { fallbackLocale } : {}),
79
+ ...(locale ? { locale } : {}),
80
+ ...(populate ? { populate: populate as PopulateType } : {}),
81
+ ...(select ? { select: select as SelectType } : {}),
82
+ ...(showHiddenFields !== undefined ? { showHiddenFields } : {}),
83
+ })
84
+
85
+ return {
86
+ content: [
87
+ {
88
+ type: 'text',
89
+ text: `Version "${id}" restored successfully in collection "${collectionSlug}"!\nRestored document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
90
+ },
91
+ ],
92
+ doc: result as Record<string, unknown>,
93
+ }
94
+ } catch (error) {
95
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
96
+ logger.error(`Error restoring version ${id} in ${collectionSlug}: ${errorMessage}`)
97
+ return {
98
+ content: [
99
+ {
100
+ type: 'text',
101
+ text: `❌ **Error restoring version "${id}" in collection "${collectionSlug}":** ${errorMessage}`,
102
+ },
103
+ ],
104
+ isError: true,
105
+ }
106
+ }
107
+ })
@@ -2,27 +2,41 @@ 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
12
  import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
13
13
  import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
14
14
  import { whereSchema } from '../../../utils/whereSchema.js'
15
15
  import { validateCollectionData } from '../validateEntityData.js'
16
+ import { fileInputSchema, resolveFileInput } from './fileInput.js'
16
17
  import { formatCollectionError } from './formatCollectionError.js'
17
18
 
18
19
  const DEFAULT_DESCRIPTION =
19
20
  'Update documents in any collection by passing the collection slug and data.'
20
21
 
21
22
  export const updateDocumentTool = defineCollectionTool({
23
+ access: (args) =>
24
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.collectionSlug]?.update),
25
+ annotations: {
26
+ destructiveHint: true,
27
+ idempotentHint: false,
28
+ openWorldHint: false,
29
+ readOnlyHint: false,
30
+ title: 'Update Document',
31
+ },
22
32
  description: DEFAULT_DESCRIPTION,
23
33
  input: z.object({
24
34
  id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),
25
- data: z.record(z.string(), z.unknown()).describe('The fields to update'),
35
+ data: z
36
+ .record(z.string(), z.unknown())
37
+ .describe(
38
+ 'The fields to update. Only include fields permitted by the schema returned by getCollectionSchema.',
39
+ ),
26
40
  depth: z
27
41
  .number()
28
42
  .describe('How many levels deep to populate relationships')
@@ -30,14 +44,16 @@ export const updateDocumentTool = defineCollectionTool({
30
44
  .default(0),
31
45
  draft: z
32
46
  .boolean()
33
- .describe('Whether to update the document as a draft')
47
+ .describe(
48
+ '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.',
49
+ )
34
50
  .optional()
35
51
  .default(false),
36
52
  fallbackLocale: z
37
53
  .string()
38
54
  .describe('Optional: fallback locale code to use when requested locale is not available')
39
55
  .optional(),
40
- filePath: z.string().describe('File path for file uploads').optional(),
56
+ file: fileInputSchema.optional(),
41
57
  locale: z
42
58
  .string()
43
59
  .describe(
@@ -54,10 +70,16 @@ export const updateDocumentTool = defineCollectionTool({
54
70
  .describe('Whether to overwrite existing files')
55
71
  .optional()
56
72
  .default(false),
73
+ publishAllLocales: z
74
+ .boolean()
75
+ .describe(
76
+ 'For collections with localized publishing status, whether publishing should affect every locale. Set false with locale to publish only that locale.',
77
+ )
78
+ .optional(),
57
79
  select: z
58
- .string()
80
+ .record(z.string(), z.unknown())
59
81
  .describe(
60
- "Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'",
82
+ 'Optional: define exactly which fields you\'d like to return in the response, e.g., {"title": true}',
61
83
  )
62
84
  .optional(),
63
85
  where: whereSchema
@@ -76,10 +98,11 @@ export const updateDocumentTool = defineCollectionTool({
76
98
  depth,
77
99
  draft,
78
100
  fallbackLocale,
79
- filePath,
101
+ file: fileInput,
80
102
  locale,
81
103
  overrideLock,
82
104
  overwriteExistingFiles,
105
+ publishAllLocales,
83
106
  select,
84
107
  where,
85
108
  } = input
@@ -109,19 +132,10 @@ export const updateDocumentTool = defineCollectionTool({
109
132
  }
110
133
 
111
134
  const parsedData = transformPointDataToPayload(inputData)
135
+ const file = await resolveFileInput({ collectionSlug, input: fileInput, req })
112
136
 
113
137
  const whereClause: Where = where ?? {}
114
138
 
115
- let selectClause: SelectType | undefined
116
- if (select) {
117
- try {
118
- selectClause = JSON.parse(select) as SelectType
119
- } catch {
120
- logger.warn(`Invalid select clause JSON: ${select}`)
121
- return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
122
- }
123
- }
124
-
125
139
  if (id) {
126
140
  const result = await payload.update({
127
141
  id,
@@ -129,14 +143,15 @@ export const updateDocumentTool = defineCollectionTool({
129
143
  data: parsedData,
130
144
  depth,
131
145
  draft,
146
+ overrideAccess: authorizedMCP.overrideAccess,
132
147
  overrideLock,
133
148
  req,
134
- ...localAPIDefaults(authorizedMCP),
135
- ...(filePath ? { filePath } : {}),
149
+ ...(file ? { file } : {}),
136
150
  ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
151
+ ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),
137
152
  ...(locale ? { locale } : {}),
138
153
  ...(fallbackLocale ? { fallbackLocale } : {}),
139
- ...(selectClause ? { select: selectClause } : {}),
154
+ ...(select ? { select: select as SelectType } : {}),
140
155
  })
141
156
 
142
157
  return {
@@ -155,15 +170,16 @@ export const updateDocumentTool = defineCollectionTool({
155
170
  data: parsedData,
156
171
  depth,
157
172
  draft,
173
+ overrideAccess: authorizedMCP.overrideAccess,
158
174
  overrideLock,
159
175
  req,
160
176
  where: whereClause,
161
- ...localAPIDefaults(authorizedMCP),
162
- ...(filePath ? { filePath } : {}),
177
+ ...(file ? { file } : {}),
163
178
  ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
179
+ ...(publishAllLocales !== undefined ? { publishAllLocales } : {}),
164
180
  ...(locale ? { locale } : {}),
165
181
  ...(fallbackLocale ? { fallbackLocale } : {}),
166
- ...(selectClause ? { select: selectClause } : {}),
182
+ ...(select ? { select: select as SelectType } : {}),
167
183
  })
168
184
 
169
185
  const docs = result.docs || []
@@ -3,19 +3,39 @@ import { getAccessResults, isEntityHidden } from 'payload'
3
3
  import { defineTool } from '../../defineTool.js'
4
4
 
5
5
  export const getConfigInfoTool = defineTool({
6
+ annotations: {
7
+ destructiveHint: false,
8
+ idempotentHint: true,
9
+ openWorldHint: false,
10
+ readOnlyHint: true,
11
+ title: 'Config Info',
12
+ },
6
13
  description: 'List the Payload collection and global slugs visible to this MCP client.',
7
14
  }).handler(async ({ authorizedMCP, req }) => {
8
- const user = authorizedMCP.user ?? req.user ?? null
9
- const permissions = user ? await getAccessResults({ req: { ...req, user } }) : null
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
+ }
10
27
 
11
28
  const collections: string[] = []
12
29
  const globals: string[] = []
13
30
 
14
31
  for (const collection of req.payload.config.collections) {
32
+ if (!authorizedCollectionSlugs.has(collection.slug)) {
33
+ continue
34
+ }
15
35
  if (user && isEntityHidden({ hidden: collection.admin.hidden, user })) {
16
36
  continue
17
37
  }
18
- if (user && !permissions?.collections?.[collection.slug]?.read) {
38
+ if (!authorizedMCP.overrideAccess && !permissions?.collections?.[collection.slug]?.read) {
19
39
  continue
20
40
  }
21
41
 
@@ -23,10 +43,13 @@ export const getConfigInfoTool = defineTool({
23
43
  }
24
44
 
25
45
  for (const global of req.payload.config.globals) {
46
+ if (!authorizedGlobalSlugs.has(global.slug)) {
47
+ continue
48
+ }
26
49
  if (user && isEntityHidden({ hidden: global.admin.hidden, user })) {
27
50
  continue
28
51
  }
29
- if (user && !permissions?.globals?.[global.slug]?.read) {
52
+ if (!authorizedMCP.overrideAccess && !permissions?.globals?.[global.slug]?.read) {
30
53
  continue
31
54
  }
32
55