@payloadcms/plugin-mcp 4.0.0-internal.c2b57ce → 4.0.0-internal.c6494b5

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 (241) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/defaultAccess.d.ts +3 -0
  3. package/dist/defaultAccess.d.ts.map +1 -0
  4. package/dist/defaultAccess.js +3 -0
  5. package/dist/defaultAccess.js.map +1 -0
  6. package/dist/defineTool.d.ts +8 -8
  7. package/dist/defineTool.d.ts.map +1 -1
  8. package/dist/endpoint/access.d.ts +12 -5
  9. package/dist/endpoint/access.d.ts.map +1 -1
  10. package/dist/endpoint/access.js +75 -88
  11. package/dist/endpoint/access.js.map +1 -1
  12. package/dist/endpoint/index.d.ts +1 -1
  13. package/dist/endpoint/index.d.ts.map +1 -1
  14. package/dist/endpoint/index.js +98 -18
  15. package/dist/endpoint/index.js.map +1 -1
  16. package/dist/exports/client.d.ts +1 -1
  17. package/dist/exports/client.d.ts.map +1 -1
  18. package/dist/exports/client.js +1 -1
  19. package/dist/exports/client.js.map +1 -1
  20. package/dist/exports/internal.d.ts +2 -0
  21. package/dist/exports/internal.d.ts.map +1 -0
  22. package/dist/exports/internal.js +3 -0
  23. package/dist/exports/internal.js.map +1 -0
  24. package/dist/index.d.ts +2 -2
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +15 -20
  27. package/dist/index.js.map +1 -1
  28. package/dist/mcp/buildMcpServer.d.ts +6 -6
  29. package/dist/mcp/buildMcpServer.d.ts.map +1 -1
  30. package/dist/mcp/buildMcpServer.js +146 -74
  31. package/dist/mcp/buildMcpServer.js.map +1 -1
  32. package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -1
  33. package/dist/mcp/builtin/collections/authTools.js +123 -61
  34. package/dist/mcp/builtin/collections/authTools.js.map +1 -1
  35. package/dist/mcp/builtin/collections/countTool.d.ts +2 -0
  36. package/dist/mcp/builtin/collections/countTool.d.ts.map +1 -0
  37. package/dist/mcp/builtin/collections/countTool.js +63 -0
  38. package/dist/mcp/builtin/collections/countTool.js.map +1 -0
  39. package/dist/mcp/builtin/collections/countVersionsTool.d.ts +2 -0
  40. package/dist/mcp/builtin/collections/countVersionsTool.d.ts.map +1 -0
  41. package/dist/mcp/builtin/collections/countVersionsTool.js +60 -0
  42. package/dist/mcp/builtin/collections/countVersionsTool.js.map +1 -0
  43. package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
  44. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  45. package/dist/mcp/builtin/collections/createTool.js +114 -64
  46. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  47. package/dist/mcp/builtin/collections/deleteTool.d.ts +1 -1
  48. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
  49. package/dist/mcp/builtin/collections/deleteTool.js +55 -61
  50. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
  51. package/dist/mcp/builtin/collections/duplicateTool.d.ts +2 -0
  52. package/dist/mcp/builtin/collections/duplicateTool.d.ts.map +1 -0
  53. package/dist/mcp/builtin/collections/duplicateTool.js +90 -0
  54. package/dist/mcp/builtin/collections/duplicateTool.js.map +1 -0
  55. package/dist/mcp/builtin/collections/fileInput.d.ts +28 -0
  56. package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -0
  57. package/dist/mcp/builtin/collections/fileInput.js +88 -0
  58. package/dist/mcp/builtin/collections/fileInput.js.map +1 -0
  59. package/dist/mcp/builtin/collections/findDistinctTool.d.ts +2 -0
  60. package/dist/mcp/builtin/collections/findDistinctTool.d.ts.map +1 -0
  61. package/dist/mcp/builtin/collections/findDistinctTool.js +77 -0
  62. package/dist/mcp/builtin/collections/findDistinctTool.js.map +1 -0
  63. package/dist/mcp/builtin/collections/findTool.d.ts +1 -1
  64. package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
  65. package/dist/mcp/builtin/collections/findTool.js +61 -74
  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 +74 -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 +84 -0
  74. package/dist/mcp/builtin/collections/findVersionsTool.js.map +1 -0
  75. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts +2 -0
  76. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -0
  77. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +103 -0
  78. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -0
  79. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts +2 -0
  80. package/dist/mcp/builtin/collections/restoreVersionTool.d.ts.map +1 -0
  81. package/dist/mcp/builtin/collections/restoreVersionTool.js +69 -0
  82. package/dist/mcp/builtin/collections/restoreVersionTool.js.map +1 -0
  83. package/dist/mcp/builtin/collections/updateTool.d.ts +1 -1
  84. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
  85. package/dist/mcp/builtin/collections/updateTool.js +118 -133
  86. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  87. package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts +2 -0
  88. package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts.map +1 -0
  89. package/dist/mcp/builtin/collections/uploadInstructionsTool.js +55 -0
  90. package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -0
  91. package/dist/mcp/builtin/formatEntityError.d.ts +16 -0
  92. package/dist/mcp/builtin/formatEntityError.d.ts.map +1 -0
  93. package/dist/mcp/builtin/formatEntityError.js +55 -0
  94. package/dist/mcp/builtin/formatEntityError.js.map +1 -0
  95. package/dist/mcp/builtin/getConfigInfoTool.d.ts +2 -0
  96. package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -0
  97. package/dist/mcp/builtin/getConfigInfoTool.js +69 -0
  98. package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -0
  99. package/dist/mcp/builtin/globals/countVersionsTool.d.ts +2 -0
  100. package/dist/mcp/builtin/globals/countVersionsTool.d.ts.map +1 -0
  101. package/dist/mcp/builtin/globals/countVersionsTool.js +60 -0
  102. package/dist/mcp/builtin/globals/countVersionsTool.js.map +1 -0
  103. package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -1
  104. package/dist/mcp/builtin/globals/findTool.js +27 -36
  105. package/dist/mcp/builtin/globals/findTool.js.map +1 -1
  106. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts +2 -0
  107. package/dist/mcp/builtin/globals/findVersionByIDTool.d.ts.map +1 -0
  108. package/dist/mcp/builtin/globals/findVersionByIDTool.js +68 -0
  109. package/dist/mcp/builtin/globals/findVersionByIDTool.js.map +1 -0
  110. package/dist/mcp/builtin/globals/findVersionsTool.d.ts +2 -0
  111. package/dist/mcp/builtin/globals/findVersionsTool.d.ts.map +1 -0
  112. package/dist/mcp/builtin/globals/findVersionsTool.js +78 -0
  113. package/dist/mcp/builtin/globals/findVersionsTool.js.map +1 -0
  114. package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts +2 -0
  115. package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -0
  116. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +81 -0
  117. package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -0
  118. package/dist/mcp/builtin/globals/restoreVersionTool.d.ts +2 -0
  119. package/dist/mcp/builtin/globals/restoreVersionTool.d.ts.map +1 -0
  120. package/dist/mcp/builtin/globals/restoreVersionTool.js +70 -0
  121. package/dist/mcp/builtin/globals/restoreVersionTool.js.map +1 -0
  122. package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
  123. package/dist/mcp/builtin/globals/updateTool.js +44 -58
  124. package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
  125. package/dist/mcp/builtinTools.d.ts +150 -19
  126. package/dist/mcp/builtinTools.d.ts.map +1 -1
  127. package/dist/mcp/builtinTools.js +125 -20
  128. package/dist/mcp/builtinTools.js.map +1 -1
  129. package/dist/mcp/sanitizeMCPConfig.d.ts +0 -1
  130. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
  131. package/dist/mcp/sanitizeMCPConfig.js +124 -58
  132. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  133. package/dist/stdio.d.ts +3 -3
  134. package/dist/stdio.d.ts.map +1 -1
  135. package/dist/stdio.js +86 -47
  136. package/dist/stdio.js.map +1 -1
  137. package/dist/types.d.ts +123 -114
  138. package/dist/types.d.ts.map +1 -1
  139. package/dist/types.js +2 -4
  140. package/dist/types.js.map +1 -1
  141. package/dist/utils/camelCase.d.ts.map +1 -1
  142. package/dist/utils/getPluginConfig.d.ts +1 -1
  143. package/dist/utils/getPluginConfig.js +1 -1
  144. package/dist/utils/getPluginConfig.js.map +1 -1
  145. package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
  146. package/dist/utils/toStandardSchema.d.ts.map +1 -1
  147. package/package.json +14 -21
  148. package/src/defaultAccess.ts +3 -0
  149. package/src/endpoint/access.ts +80 -101
  150. package/src/endpoint/index.ts +88 -16
  151. package/src/exports/client.ts +2 -1
  152. package/src/exports/internal.ts +1 -0
  153. package/src/index.ts +12 -20
  154. package/src/mcp/buildMcpServer.ts +180 -110
  155. package/src/mcp/builtin/collections/authTools.ts +122 -75
  156. package/src/mcp/builtin/collections/countTool.ts +61 -0
  157. package/src/mcp/builtin/collections/countVersionsTool.ts +60 -0
  158. package/src/mcp/builtin/collections/createTool.ts +117 -88
  159. package/src/mcp/builtin/collections/deleteTool.ts +48 -74
  160. package/src/mcp/builtin/collections/duplicateTool.ts +84 -0
  161. package/src/mcp/builtin/collections/fileInput.spec.ts +93 -0
  162. package/src/mcp/builtin/collections/fileInput.ts +127 -0
  163. package/src/mcp/builtin/collections/findDistinctTool.ts +67 -0
  164. package/src/mcp/builtin/collections/findTool.ts +57 -110
  165. package/src/mcp/builtin/collections/findVersionByIDTool.ts +66 -0
  166. package/src/mcp/builtin/collections/findVersionsTool.ts +83 -0
  167. package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +91 -0
  168. package/src/mcp/builtin/collections/restoreVersionTool.ts +65 -0
  169. package/src/mcp/builtin/collections/updateTool.ts +106 -138
  170. package/src/mcp/builtin/collections/uploadInstructionsTool.ts +64 -0
  171. package/src/mcp/builtin/formatEntityError.ts +77 -0
  172. package/src/mcp/builtin/getConfigInfoTool.ts +68 -0
  173. package/src/mcp/builtin/globals/countVersionsTool.ts +60 -0
  174. package/src/mcp/builtin/globals/findTool.ts +27 -57
  175. package/src/mcp/builtin/globals/findVersionByIDTool.ts +64 -0
  176. package/src/mcp/builtin/globals/findVersionsTool.ts +69 -0
  177. package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +73 -0
  178. package/src/mcp/builtin/globals/restoreVersionTool.ts +68 -0
  179. package/src/mcp/builtin/globals/updateTool.ts +51 -77
  180. package/src/mcp/builtinTools.ts +118 -41
  181. package/src/mcp/sanitizeMCPConfig.ts +120 -78
  182. package/src/stdio.ts +80 -47
  183. package/src/types.ts +141 -120
  184. package/src/utils/getPluginConfig.ts +1 -1
  185. package/dist/collection/getAccessField.d.ts +0 -12
  186. package/dist/collection/getAccessField.d.ts.map +0 -1
  187. package/dist/collection/getAccessField.js +0 -57
  188. package/dist/collection/getAccessField.js.map +0 -1
  189. package/dist/collection/index.d.ts +0 -6
  190. package/dist/collection/index.d.ts.map +0 -1
  191. package/dist/collection/index.js +0 -59
  192. package/dist/collection/index.js.map +0 -1
  193. package/dist/components/AccessField/index.client.d.ts +0 -10
  194. package/dist/components/AccessField/index.client.d.ts.map +0 -1
  195. package/dist/components/AccessField/index.client.js +0 -305
  196. package/dist/components/AccessField/index.client.js.map +0 -1
  197. package/dist/components/AccessField/index.css +0 -93
  198. package/dist/utils/getVirtualFieldNames.d.ts +0 -14
  199. package/dist/utils/getVirtualFieldNames.d.ts.map +0 -1
  200. package/dist/utils/getVirtualFieldNames.js +0 -35
  201. package/dist/utils/getVirtualFieldNames.js.map +0 -1
  202. package/dist/utils/localAPIDefaults.d.ts +0 -20
  203. package/dist/utils/localAPIDefaults.d.ts.map +0 -1
  204. package/dist/utils/localAPIDefaults.js +0 -19
  205. package/dist/utils/localAPIDefaults.js.map +0 -1
  206. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +0 -7
  207. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +0 -1
  208. package/dist/utils/schemaConversion/prepareCollectionSchema.js +0 -37
  209. package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +0 -1
  210. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +0 -7
  211. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +0 -1
  212. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js +0 -20
  213. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +0 -1
  214. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +0 -13
  215. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +0 -1
  216. package/dist/utils/schemaConversion/sanitizeJsonSchema.js +0 -56
  217. package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +0 -1
  218. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +0 -20
  219. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +0 -1
  220. package/dist/utils/schemaConversion/simplifyRelationshipFields.js +0 -56
  221. package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +0 -1
  222. package/dist/utils/schemaConversion/transformPointFields.d.ts +0 -3
  223. package/dist/utils/schemaConversion/transformPointFields.d.ts.map +0 -1
  224. package/dist/utils/schemaConversion/transformPointFields.js +0 -57
  225. package/dist/utils/schemaConversion/transformPointFields.js.map +0 -1
  226. package/dist/utils/transformPointDataToPayload.d.ts +0 -7
  227. package/dist/utils/transformPointDataToPayload.d.ts.map +0 -1
  228. package/dist/utils/transformPointDataToPayload.js +0 -28
  229. package/dist/utils/transformPointDataToPayload.js.map +0 -1
  230. package/src/collection/getAccessField.ts +0 -64
  231. package/src/collection/index.ts +0 -63
  232. package/src/components/AccessField/index.client.tsx +0 -344
  233. package/src/components/AccessField/index.css +0 -93
  234. package/src/utils/getVirtualFieldNames.ts +0 -53
  235. package/src/utils/localAPIDefaults.ts +0 -22
  236. package/src/utils/schemaConversion/prepareCollectionSchema.ts +0 -39
  237. package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +0 -27
  238. package/src/utils/schemaConversion/sanitizeJsonSchema.ts +0 -62
  239. package/src/utils/schemaConversion/simplifyRelationshipFields.ts +0 -70
  240. package/src/utils/schemaConversion/transformPointFields.ts +0 -56
  241. package/src/utils/transformPointDataToPayload.ts +0 -40
@@ -0,0 +1,127 @@
1
+ import type { CollectionSlug, File, PayloadRequest } from 'payload'
2
+
3
+ import { APIError, z } from 'payload'
4
+ import {
5
+ externalURLInputSchema,
6
+ getFileFromUploadInstructions,
7
+ resolveURLUploadInput,
8
+ } from 'payload/internal'
9
+ import { sanitizeFilename } from 'payload/shared'
10
+
11
+ const mimeTypeSchema = z
12
+ .string()
13
+ .check(
14
+ z.regex(
15
+ /^[!#$%&'*+.^`|~\w-]+\/[!#$%&'*+.^`|~\w-]+$/,
16
+ 'MIME type must use the type/subtype format',
17
+ ),
18
+ )
19
+
20
+ const uploadFileSchema = z.strictObject({
21
+ filename: z.string(),
22
+ mimeType: z.string(),
23
+ size: z.int().check(z.nonnegative()),
24
+ uploadReference: z.record(z.string(), z.unknown()),
25
+ })
26
+
27
+ export const fileInputSchema = z
28
+ .discriminatedUnion('source', [
29
+ z.strictObject({
30
+ name: z.string().check(z.minLength(1), z.describe('The file name, including its extension.')),
31
+ data: z
32
+ .string()
33
+ .check(z.describe('The base64-encoded file bytes, without a data URL prefix.')),
34
+ mimeType: mimeTypeSchema.check(z.describe('The file MIME type, for example image/png.')),
35
+ source: z.literal('base64'),
36
+ }),
37
+ externalURLInputSchema,
38
+ z.strictObject({
39
+ file: uploadFileSchema.check(z.describe('getUploadInstructions file field post-upload.')),
40
+ source: z.literal('uploadReference'),
41
+ }),
42
+ ])
43
+ .check(
44
+ z.describe(
45
+ 'A file for an upload collection. Prefer uploadReference after its upload succeeds; use base64 only for small local files or externalURL for an online file.',
46
+ ),
47
+ )
48
+
49
+ type FileInput = z.infer<typeof fileInputSchema>
50
+
51
+ export async function resolveFile({
52
+ slug,
53
+ input,
54
+ req,
55
+ }: {
56
+ input?: FileInput
57
+ req: PayloadRequest
58
+ slug: CollectionSlug
59
+ }): Promise<File | undefined> {
60
+ if (!input) {
61
+ return undefined
62
+ }
63
+
64
+ if (input.source === 'uploadReference') {
65
+ try {
66
+ return await getFileFromUploadInstructions({ collectionSlug: slug, file: input.file, req })
67
+ } catch (error) {
68
+ if (error instanceof Error && error.message === 'Staged upload was not found.') {
69
+ throw new APIError(
70
+ 'Staged upload not found. Complete the upload action first, or use base64 for small local files.',
71
+ 400,
72
+ )
73
+ }
74
+ throw error
75
+ }
76
+ }
77
+
78
+ if (input.source === 'externalURL') {
79
+ return resolveURLUploadInput({ slug, input, req })
80
+ }
81
+
82
+ const uploadConfig = req.payload.collections[slug]?.config.upload
83
+
84
+ if (!uploadConfig) {
85
+ throw new APIError(`Collection "${slug}" does not support file uploads.`, 400)
86
+ }
87
+
88
+ const maxFileSize = req.payload.config.upload.limits?.fileSize
89
+ const data = decodeBase64({ maxFileSize, value: input.data })
90
+ const file: File = {
91
+ name: sanitizeFilename(input.name),
92
+ data,
93
+ mimetype: input.mimeType,
94
+ size: data.length,
95
+ }
96
+
97
+ if (maxFileSize !== undefined && Number.isFinite(maxFileSize) && file.size > maxFileSize) {
98
+ throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
99
+ }
100
+
101
+ return file
102
+ }
103
+
104
+ function decodeBase64({ maxFileSize, value }: { maxFileSize?: number; value: string }): Buffer {
105
+ const normalized = value.replace(/\s/g, '')
106
+
107
+ if (!/^[a-z0-9+/]*={0,2}$/i.test(normalized) || normalized.length % 4 === 1) {
108
+ throw new APIError('File data must be valid base64.', 400)
109
+ }
110
+
111
+ if (maxFileSize !== undefined && Number.isFinite(maxFileSize)) {
112
+ const paddingLength = normalized.endsWith('==') ? 2 : normalized.endsWith('=') ? 1 : 0
113
+ const decodedSize = Math.floor((normalized.length * 3) / 4) - paddingLength
114
+
115
+ if (decodedSize > maxFileSize) {
116
+ throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
117
+ }
118
+ }
119
+
120
+ const data = Buffer.from(normalized, 'base64')
121
+
122
+ if (data.toString('base64').replace(/=+$/, '') !== normalized.replace(/=+$/, '')) {
123
+ throw new APIError('File data must be valid base64.', 400)
124
+ }
125
+
126
+ return data
127
+ }
@@ -0,0 +1,67 @@
1
+ import { findDistinctInputSchema } from 'payload'
2
+
3
+ import { defaultAccess } from '../../../defaultAccess.js'
4
+ import { defineCollectionTool } from '../../../defineTool.js'
5
+ import { getLogger } from '../../../utils/getLogger.js'
6
+
7
+ const DEFAULT_DESCRIPTION =
8
+ 'Find distinct values for a field in any collection by passing the collection slug and field path.'
9
+
10
+ export const findDistinctTool = defineCollectionTool({
11
+ access: (args) =>
12
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.read),
13
+ annotations: {
14
+ destructiveHint: false,
15
+ idempotentHint: true,
16
+ openWorldHint: false,
17
+ readOnlyHint: true,
18
+ title: 'Find Distinct',
19
+ },
20
+ description: DEFAULT_DESCRIPTION,
21
+ input: findDistinctInputSchema,
22
+ }).handler(async ({ slug, authorizedMCP, input, req }) => {
23
+ const payload = req.payload
24
+ const logger = getLogger({ payload })
25
+ const { depth, field, limit, locale, page, populate, sort, trash, where } = input
26
+
27
+ logger.info(`Finding distinct values in collection: ${slug}, field: ${field}`)
28
+
29
+ try {
30
+ const result = await payload.findDistinct({
31
+ collection: slug,
32
+ depth,
33
+ field,
34
+ overrideAccess: authorizedMCP.overrideAccess,
35
+ req,
36
+ ...(limit ? { limit } : {}),
37
+ ...(locale ? { locale } : {}),
38
+ ...(page ? { page } : {}),
39
+ ...(populate ? { populate } : {}),
40
+ ...(sort ? { sort } : {}),
41
+ ...(trash !== undefined ? { trash } : {}),
42
+ ...(where ? { where } : {}),
43
+ })
44
+
45
+ return {
46
+ content: [
47
+ {
48
+ type: 'text',
49
+ text: `Distinct values for "${field}" in collection "${slug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
50
+ },
51
+ ],
52
+ doc: result as unknown as Record<string, unknown>,
53
+ }
54
+ } catch (error) {
55
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
56
+ logger.error(`Error finding distinct values in ${slug}: ${errorMessage}`)
57
+ return {
58
+ content: [
59
+ {
60
+ type: 'text',
61
+ text: `❌ **Error finding distinct values in collection "${slug}":** ${errorMessage}`,
62
+ },
63
+ ],
64
+ isError: true,
65
+ }
66
+ }
67
+ })
@@ -1,140 +1,83 @@
1
- import type { SelectType } from 'payload'
2
-
3
- import { z } from 'zod'
1
+ import { findDocumentsInputSchema, parseDocumentID } from 'payload'
4
2
 
3
+ import { defaultAccess } from '../../../defaultAccess.js'
5
4
  import { defineCollectionTool } from '../../../defineTool.js'
6
5
  import { getLogger } from '../../../utils/getLogger.js'
7
- import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
8
6
 
9
7
  const DEFAULT_DESCRIPTION =
10
- 'Find documents in a collection by ID or where clause using Find or FindByID.'
8
+ 'Find documents in any collection by passing the collection slug and optional ID or where clause.'
11
9
 
12
- export const findCollectionTool = defineCollectionTool({
10
+ export const findDocumentsTool = defineCollectionTool({
11
+ access: (args) =>
12
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.read),
13
+ annotations: {
14
+ destructiveHint: false,
15
+ idempotentHint: true,
16
+ openWorldHint: false,
17
+ readOnlyHint: true,
18
+ title: 'Find Documents',
19
+ },
13
20
  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 }) => {
21
+ input: findDocumentsInputSchema,
22
+ }).handler(async ({ slug, authorizedMCP, input, req }) => {
78
23
  const payload = req.payload
79
24
  const logger = getLogger({ payload })
80
25
 
81
- const { id, depth, draft, fallbackLocale, limit, locale, page, select, sort, where } = input
26
+ const {
27
+ id,
28
+ depth,
29
+ draft,
30
+ fallbackLocale,
31
+ joins,
32
+ limit,
33
+ locale,
34
+ page,
35
+ pagination,
36
+ populate,
37
+ select,
38
+ sort,
39
+ trash,
40
+ where,
41
+ } = input
82
42
 
83
43
  logger.info(
84
- `Reading document from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ''}, limit: ${limit}, page: ${page}${locale ? `, locale: ${locale}` : ''}`,
44
+ `Reading document from collection: ${slug}${id ? ` with ID: ${id}` : ''}, limit: ${limit}, page: ${page}${locale ? `, locale: ${locale}` : ''}`,
85
45
  )
86
46
 
87
47
  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) {
48
+ if (id !== undefined) {
109
49
  try {
110
50
  const doc = await payload.findByID({
111
- id,
112
- collection: collectionSlug,
51
+ id: parseDocumentID({ id, collectionSlug: slug, payload }),
52
+ collection: slug,
113
53
  depth,
54
+ overrideAccess: authorizedMCP.overrideAccess,
114
55
  req,
115
- ...localAPIDefaults(authorizedMCP),
116
- ...(selectClause && { select: selectClause }),
56
+ ...(select && { select }),
57
+ ...(populate && { populate }),
58
+ ...(joins !== undefined && { joins }),
117
59
  ...(locale && { locale }),
118
- ...(fallbackLocale && { fallbackLocale }),
60
+ ...(fallbackLocale !== undefined && { fallbackLocale }),
119
61
  ...(draft !== undefined && { draft }),
62
+ ...(trash !== undefined && { trash }),
120
63
  })
121
64
 
122
65
  return {
123
66
  content: [
124
67
  {
125
68
  type: 'text',
126
- text: `Document from collection "${collectionSlug}":\n${JSON.stringify(doc)}`,
69
+ text: `Document from collection "${slug}":\n${JSON.stringify(doc)}`,
127
70
  },
128
71
  ],
129
72
  doc: doc as Record<string, unknown>,
130
73
  }
131
74
  } catch {
132
- logger.warn(`Document not found with ID: ${id} in collection: ${collectionSlug}`)
75
+ logger.warn(`Document not found with ID: ${id} in collection: ${slug}`)
133
76
  return {
134
77
  content: [
135
78
  {
136
79
  type: 'text',
137
- text: `Error: Document with ID "${id}" not found in collection "${collectionSlug}"`,
80
+ text: `Error: Document with ID "${id}" not found in collection "${slug}"`,
138
81
  },
139
82
  ],
140
83
  }
@@ -142,28 +85,32 @@ export const findCollectionTool = defineCollectionTool({
142
85
  }
143
86
 
144
87
  const findOptions: Parameters<typeof payload.find>[0] = {
145
- collection: collectionSlug,
88
+ collection: slug,
146
89
  depth,
147
90
  limit,
91
+ overrideAccess: authorizedMCP.overrideAccess,
148
92
  page,
149
93
  req,
150
- ...localAPIDefaults(authorizedMCP),
151
- ...(selectClause && { select: selectClause }),
94
+ ...(select && { select }),
95
+ ...(populate && { populate }),
96
+ ...(joins !== undefined && { joins }),
152
97
  ...(locale && { locale }),
153
- ...(fallbackLocale && { fallbackLocale }),
98
+ ...(fallbackLocale !== undefined && { fallbackLocale }),
154
99
  ...(draft !== undefined && { draft }),
100
+ ...(pagination !== undefined && { pagination }),
101
+ ...(trash !== undefined && { trash }),
155
102
  }
156
103
 
157
104
  if (sort) {
158
105
  findOptions.sort = sort
159
106
  }
160
- if (Object.keys(whereClause).length > 0) {
161
- findOptions.where = whereClause as Parameters<typeof payload.find>[0]['where']
107
+ if (where) {
108
+ findOptions.where = where
162
109
  }
163
110
 
164
111
  const result = await payload.find(findOptions)
165
112
 
166
- let responseText = `Collection: "${collectionSlug}"\nTotal: ${result.totalDocs} documents\nPage: ${result.page} of ${result.totalPages}\n`
113
+ let responseText = `Collection: "${slug}"\nTotal: ${result.totalDocs} documents\nPage: ${result.page} of ${result.totalPages}\n`
167
114
  for (const doc of result.docs) {
168
115
  responseText += `\n\`\`\`json\n${JSON.stringify(doc)}\n\`\`\``
169
116
  }
@@ -174,12 +121,12 @@ export const findCollectionTool = defineCollectionTool({
174
121
  }
175
122
  } catch (error) {
176
123
  const errorMessage = error instanceof Error ? error.message : 'Unknown error'
177
- logger.error(`Error reading documents from collection ${collectionSlug}: ${errorMessage}`)
124
+ logger.error(`Error reading documents from collection ${slug}: ${errorMessage}`)
178
125
  return {
179
126
  content: [
180
127
  {
181
128
  type: 'text',
182
- text: `❌ **Error reading documents from collection "${collectionSlug}":** ${errorMessage}`,
129
+ text: `❌ **Error reading documents from collection "${slug}":** ${errorMessage}`,
183
130
  },
184
131
  ],
185
132
  }
@@ -0,0 +1,66 @@
1
+ import { findVersionByIDInputSchema } from 'payload'
2
+
3
+ import { defaultAccess } from '../../../defaultAccess.js'
4
+ import { defineCollectionTool } from '../../../defineTool.js'
5
+ import { getLogger } from '../../../utils/getLogger.js'
6
+
7
+ const DEFAULT_DESCRIPTION =
8
+ 'Find a specific document version in any version-enabled collection by passing the collection slug and version ID.'
9
+
10
+ export const findVersionByIDTool = defineCollectionTool({
11
+ access: (args) =>
12
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.readVersions),
13
+ annotations: {
14
+ destructiveHint: false,
15
+ idempotentHint: true,
16
+ openWorldHint: false,
17
+ readOnlyHint: true,
18
+ title: 'Find Version By ID',
19
+ },
20
+ description: DEFAULT_DESCRIPTION,
21
+ input: findVersionByIDInputSchema,
22
+ }).handler(async ({ slug, authorizedMCP, input, req }) => {
23
+ const payload = req.payload
24
+ const logger = getLogger({ payload })
25
+ const { id, depth, draft, fallbackLocale, locale, populate, select, trash } = input
26
+
27
+ logger.info(`Finding version in collection: ${slug} with ID: ${id}`)
28
+
29
+ try {
30
+ const result = await payload.findVersionByID({
31
+ id: String(id),
32
+ collection: slug,
33
+ depth,
34
+ overrideAccess: authorizedMCP.overrideAccess,
35
+ req,
36
+ ...(draft !== undefined ? { draft } : {}),
37
+ ...(fallbackLocale !== undefined ? { fallbackLocale } : {}),
38
+ ...(locale ? { locale } : {}),
39
+ ...(populate ? { populate } : {}),
40
+ ...(select ? { select } : {}),
41
+ ...(trash !== undefined ? { trash } : {}),
42
+ })
43
+
44
+ return {
45
+ content: [
46
+ {
47
+ type: 'text',
48
+ text: `Version "${id}" from collection "${slug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
49
+ },
50
+ ],
51
+ doc: result as unknown as Record<string, unknown>,
52
+ }
53
+ } catch (error) {
54
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
55
+ logger.error(`Error finding version ${id} in ${slug}: ${errorMessage}`)
56
+ return {
57
+ content: [
58
+ {
59
+ type: 'text',
60
+ text: `❌ **Error finding version "${id}" in collection "${slug}":** ${errorMessage}`,
61
+ },
62
+ ],
63
+ isError: true,
64
+ }
65
+ }
66
+ })
@@ -0,0 +1,83 @@
1
+ import { findVersionsInputSchema } from 'payload'
2
+
3
+ import { defaultAccess } from '../../../defaultAccess.js'
4
+ import { defineCollectionTool } from '../../../defineTool.js'
5
+ import { getLogger } from '../../../utils/getLogger.js'
6
+
7
+ const DEFAULT_DESCRIPTION =
8
+ 'Find document versions in any version-enabled collection by passing the collection slug and optional where clause.'
9
+
10
+ export const findVersionsTool = defineCollectionTool({
11
+ access: (args) =>
12
+ defaultAccess(args) && Boolean(args.permissions?.collections?.[args.slug]?.readVersions),
13
+ annotations: {
14
+ destructiveHint: false,
15
+ idempotentHint: true,
16
+ openWorldHint: false,
17
+ readOnlyHint: true,
18
+ title: 'Find Versions',
19
+ },
20
+ description: DEFAULT_DESCRIPTION,
21
+ input: findVersionsInputSchema,
22
+ }).handler(async ({ slug, authorizedMCP, input, req }) => {
23
+ const payload = req.payload
24
+ const logger = getLogger({ payload })
25
+ const {
26
+ depth,
27
+ draft,
28
+ fallbackLocale,
29
+ limit,
30
+ locale,
31
+ page,
32
+ pagination,
33
+ populate,
34
+ select,
35
+ sort,
36
+ trash,
37
+ where,
38
+ } = input
39
+
40
+ logger.info(`Finding versions in collection: ${slug}, limit: ${limit}, page: ${page}`)
41
+
42
+ try {
43
+ const result = await payload.findVersions({
44
+ collection: slug,
45
+ depth,
46
+ limit,
47
+ overrideAccess: authorizedMCP.overrideAccess,
48
+ page,
49
+ req,
50
+ ...(draft !== undefined ? { draft } : {}),
51
+ ...(fallbackLocale !== undefined ? { fallbackLocale } : {}),
52
+ ...(locale ? { locale } : {}),
53
+ ...(pagination !== undefined ? { pagination } : {}),
54
+ ...(populate ? { populate } : {}),
55
+ ...(select ? { select } : {}),
56
+ ...(sort ? { sort } : {}),
57
+ ...(trash !== undefined ? { trash } : {}),
58
+ ...(where ? { where } : {}),
59
+ })
60
+
61
+ return {
62
+ content: [
63
+ {
64
+ type: 'text',
65
+ text: `Versions for collection "${slug}":\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
66
+ },
67
+ ],
68
+ doc: result as unknown as Record<string, unknown>,
69
+ }
70
+ } catch (error) {
71
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
72
+ logger.error(`Error finding versions in ${slug}: ${errorMessage}`)
73
+ return {
74
+ content: [
75
+ {
76
+ type: 'text',
77
+ text: `❌ **Error finding versions in collection "${slug}":** ${errorMessage}`,
78
+ },
79
+ ],
80
+ isError: true,
81
+ }
82
+ }
83
+ })
@@ -0,0 +1,91 @@
1
+ import { getAccessResults, getCollectionInputSchema, getCollectionSchemaInputSchema } from 'payload'
2
+ import { getLLMInstructions } from 'payload/internal'
3
+
4
+ import { defaultAccess } from '../../../defaultAccess.js'
5
+ import { defineCollectionTool } from '../../../defineTool.js'
6
+
7
+ export const getCollectionSchemaTool = defineCollectionTool({
8
+ access: (args) => {
9
+ const permissions = args.permissions?.collections?.[args.slug]
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
+ },
20
+ description:
21
+ 'Get the input schema and LLM instructions before creating or updating documents in a collection.',
22
+ input: getCollectionSchemaInputSchema,
23
+ }).handler(async ({ slug, authorizedMCP, req }) => {
24
+ const permissions = authorizedMCP.overrideAccess
25
+ ? null
26
+ : (await getAccessResults({ req })).collections?.[slug]
27
+
28
+ if (!authorizedMCP.overrideAccess && !permissions?.create && !permissions?.update) {
29
+ return {
30
+ content: [
31
+ {
32
+ type: 'text',
33
+ text: `Error: MCP access to "getCollectionSchema" is not enabled for collection "${slug}"`,
34
+ },
35
+ ],
36
+ isError: true,
37
+ }
38
+ }
39
+
40
+ const inputSchema = getCollectionInputSchema({
41
+ collectionSlug: slug,
42
+ req,
43
+ ...(permissions ? { permissions } : {}),
44
+ })
45
+
46
+ if (!inputSchema) {
47
+ return {
48
+ content: [{ type: 'text', text: `Error: Collection "${slug}" not found` }],
49
+ isError: true,
50
+ }
51
+ }
52
+
53
+ const uploadConfig = req.payload.collections[slug]?.config.upload
54
+ const maxFileSize = req.payload.config.upload.limits?.fileSize
55
+ const upload = uploadConfig
56
+ ? {
57
+ enabled: true,
58
+ filesRequiredOnCreate: uploadConfig.filesRequiredOnCreate !== false,
59
+ mimeTypes: uploadConfig.mimeTypes ?? ['*/*'],
60
+ sources: [
61
+ ...(uploadConfig.pasteURL !== false ? ['externalURL'] : []),
62
+ 'base64',
63
+ 'uploadReference',
64
+ ],
65
+ ...(typeof maxFileSize === 'number' && Number.isFinite(maxFileSize) ? { maxFileSize } : {}),
66
+ }
67
+ : { enabled: false }
68
+
69
+ const instructions = await getLLMInstructions({
70
+ slug,
71
+ type: 'collection',
72
+ overrideAccess: authorizedMCP.overrideAccess,
73
+ req,
74
+ })
75
+
76
+ return {
77
+ content: [
78
+ {
79
+ type: 'text',
80
+ text: `Schema for collection "${slug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\`\nUpload configuration:\n\`\`\`json\n${JSON.stringify(upload)}\n\`\`\``,
81
+ },
82
+ ...(instructions ? [{ type: 'text' as const, text: instructions }] : []),
83
+ ],
84
+ structuredContent: {
85
+ slug,
86
+ schema: inputSchema,
87
+ upload,
88
+ ...(instructions ? { instructions } : {}),
89
+ },
90
+ }
91
+ })