@payloadcms/plugin-mcp 4.0.0-canary.13 → 4.0.0-canary.14

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 (38) hide show
  1. package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
  2. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
  3. package/dist/mcp/builtin/collections/createTool.js +90 -48
  4. package/dist/mcp/builtin/collections/createTool.js.map +1 -1
  5. package/dist/mcp/builtin/collections/fileInput.d.ts +10 -2
  6. package/dist/mcp/builtin/collections/fileInput.d.ts.map +1 -1
  7. package/dist/mcp/builtin/collections/fileInput.js +28 -4
  8. package/dist/mcp/builtin/collections/fileInput.js.map +1 -1
  9. package/dist/mcp/builtin/collections/fileInput.spec.js +18 -4
  10. package/dist/mcp/builtin/collections/fileInput.spec.js.map +1 -1
  11. package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
  12. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +3 -2
  13. package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
  14. package/dist/mcp/builtin/collections/updateTool.js +3 -3
  15. package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
  16. package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts +2 -0
  17. package/dist/mcp/builtin/collections/uploadInstructionsTool.d.ts.map +1 -0
  18. package/dist/mcp/builtin/collections/uploadInstructionsTool.js +55 -0
  19. package/dist/mcp/builtin/collections/uploadInstructionsTool.js.map +1 -0
  20. package/dist/mcp/builtinTools.d.ts +6 -0
  21. package/dist/mcp/builtinTools.d.ts.map +1 -1
  22. package/dist/mcp/builtinTools.js +9 -3
  23. package/dist/mcp/builtinTools.js.map +1 -1
  24. package/dist/mcp/sanitizeMCPConfig.js +4 -1
  25. package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
  26. package/dist/stdio.d.ts.map +1 -1
  27. package/dist/stdio.js +2 -1
  28. package/dist/stdio.js.map +1 -1
  29. package/package.json +3 -3
  30. package/src/mcp/builtin/collections/createTool.ts +87 -40
  31. package/src/mcp/builtin/collections/fileInput.spec.ts +18 -4
  32. package/src/mcp/builtin/collections/fileInput.ts +29 -4
  33. package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +5 -1
  34. package/src/mcp/builtin/collections/updateTool.ts +3 -3
  35. package/src/mcp/builtin/collections/uploadInstructionsTool.ts +64 -0
  36. package/src/mcp/builtinTools.ts +9 -2
  37. package/src/mcp/sanitizeMCPConfig.ts +4 -1
  38. package/src/stdio.ts +2 -1
@@ -1,7 +1,7 @@
1
1
  import type { CollectionSlug, File, FileData, PayloadRequest } from 'payload'
2
2
 
3
3
  import { APIError } from 'payload'
4
- import { getExternalFile, isURLAllowed } from 'payload/internal'
4
+ import { getExternalFile, getFileFromUploadInstructions, isURLAllowed } from 'payload/internal'
5
5
  import { sanitizeFilename } from 'payload/shared'
6
6
  import { z } from 'zod'
7
7
 
@@ -9,6 +9,13 @@ const mimeTypeSchema = z
9
9
  .string()
10
10
  .regex(/^[!#$%&'*+.^`|~\w-]+\/[!#$%&'*+.^`|~\w-]+$/, 'MIME type must use the type/subtype format')
11
11
 
12
+ const uploadFileSchema = z.object({
13
+ filename: z.string(),
14
+ mimeType: z.string(),
15
+ size: z.number().int().nonnegative(),
16
+ uploadReference: z.record(z.string(), z.unknown()),
17
+ })
18
+
12
19
  export const fileInputSchema = z
13
20
  .discriminatedUnion('source', [
14
21
  z.object({
@@ -19,17 +26,21 @@ export const fileInputSchema = z
19
26
  }),
20
27
  z.object({
21
28
  name: z.string().min(1).describe('Optional file name override').optional(),
22
- source: z.literal('url'),
29
+ source: z.literal('externalURL'),
23
30
  url: z.url().describe('The http or https URL to download'),
24
31
  }),
32
+ z.object({
33
+ file: uploadFileSchema.describe('getUploadInstructions file field post-upload'),
34
+ source: z.literal('uploadReference'),
35
+ }),
25
36
  ])
26
37
  .describe(
27
- 'A file for an upload collection. Use only a source listed by getCollectionSchema: url for an online file or base64 for a local file.',
38
+ '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.',
28
39
  )
29
40
 
30
41
  type FileInput = z.infer<typeof fileInputSchema>
31
42
 
32
- export async function resolveFileInput({
43
+ export async function resolveFile({
33
44
  collectionSlug,
34
45
  input,
35
46
  req,
@@ -42,6 +53,20 @@ export async function resolveFileInput({
42
53
  return undefined
43
54
  }
44
55
 
56
+ if (input.source === 'uploadReference') {
57
+ try {
58
+ return await getFileFromUploadInstructions({ collectionSlug, file: input.file, req })
59
+ } catch (error) {
60
+ if (error instanceof Error && error.message === 'Staged upload was not found.') {
61
+ throw new APIError(
62
+ 'Staged upload not found. Complete the upload action first, or use base64 for small local files.',
63
+ 400,
64
+ )
65
+ }
66
+ throw error
67
+ }
68
+ }
69
+
45
70
  const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
46
71
 
47
72
  if (!uploadConfig) {
@@ -55,7 +55,11 @@ export const getCollectionSchemaTool = defineCollectionTool({
55
55
  enabled: true,
56
56
  filesRequiredOnCreate: uploadConfig.filesRequiredOnCreate !== false,
57
57
  mimeTypes: uploadConfig.mimeTypes ?? ['*/*'],
58
- sources: [...(uploadConfig.pasteURL !== false ? ['url'] : []), 'base64'],
58
+ sources: [
59
+ ...(uploadConfig.pasteURL !== false ? ['externalURL'] : []),
60
+ 'base64',
61
+ 'uploadReference',
62
+ ],
59
63
  ...(typeof maxFileSize === 'number' && Number.isFinite(maxFileSize) ? { maxFileSize } : {}),
60
64
  }
61
65
  : { enabled: false }
@@ -13,11 +13,11 @@ import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEnt
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
+ import { fileInputSchema, resolveFile } from './fileInput.js'
17
17
  import { formatCollectionError } from './formatCollectionError.js'
18
18
 
19
19
  const DEFAULT_DESCRIPTION =
20
- 'Update documents in any collection by passing the collection slug and data.'
20
+ 'Update documents. Prefer uploadReference after upload, externalURL for URLs, or base64 for small local files.'
21
21
 
22
22
  export const updateDocumentTool = defineCollectionTool({
23
23
  access: (args) =>
@@ -132,7 +132,7 @@ export const updateDocumentTool = defineCollectionTool({
132
132
  }
133
133
 
134
134
  const parsedData = transformPointDataToPayload(inputData)
135
- const file = await resolveFileInput({ collectionSlug, input: fileInput, req })
135
+ const file = await resolveFile({ collectionSlug, input: fileInput, req })
136
136
 
137
137
  const whereClause: Where = where ?? {}
138
138
 
@@ -0,0 +1,64 @@
1
+ import { getUploadInstructions as getPayloadUploadInstructions } from 'payload/internal'
2
+ import { z } from 'zod'
3
+
4
+ import { defaultAccess } from '../../../defaultAccess.js'
5
+ import { defineCollectionTool } from '../../../defineTool.js'
6
+
7
+ export const getUploadInstructionsTool = defineCollectionTool({
8
+ access: (args) =>
9
+ defaultAccess(args) &&
10
+ Boolean(
11
+ args.permissions?.collections?.[args.collectionSlug]?.create ||
12
+ args.permissions?.collections?.[args.collectionSlug]?.update,
13
+ ),
14
+ annotations: {
15
+ destructiveHint: false,
16
+ idempotentHint: false,
17
+ openWorldHint: true,
18
+ readOnlyHint: false,
19
+ title: 'Get Upload Instructions',
20
+ },
21
+ description:
22
+ 'Prepare uploads for createDocuments or updateDocument. This does not upload bytes; finish the returned action before use.',
23
+ input: z.object({
24
+ docPrefix: z.string().describe('Optional document folder or prefix').optional(),
25
+ filename: z.string().describe('The original file name'),
26
+ filesize: z.number().int().nonnegative().describe('The file size in bytes'),
27
+ mimeType: z.string().describe('The file MIME type'),
28
+ }),
29
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
30
+ try {
31
+ const instructions = await getPayloadUploadInstructions({
32
+ ...input,
33
+ collectionSlug,
34
+ overrideAccess: authorizedMCP.overrideAccess,
35
+ req,
36
+ })
37
+
38
+ const nextStep =
39
+ instructions.type === 'http'
40
+ ? 'Upload bytes with instructions.request. After success, pass { source: "uploadReference", file: instructions.file }.'
41
+ : `Call "${instructions.name}" with file and data. After success, pass { source: "uploadReference", file: instructions.file }.`
42
+
43
+ return {
44
+ content: [
45
+ {
46
+ type: 'text',
47
+ text: `Upload instructions for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(instructions)}\n\`\`\`\n${nextStep}`,
48
+ },
49
+ ],
50
+ structuredContent: { instructions },
51
+ }
52
+ } catch (error) {
53
+ const message = error instanceof Error ? error.message : 'Unknown error'
54
+ return {
55
+ content: [
56
+ {
57
+ type: 'text',
58
+ text: `Error getting upload instructions for collection "${collectionSlug}": ${message}`,
59
+ },
60
+ ],
61
+ isError: true,
62
+ }
63
+ }
64
+ })
@@ -10,7 +10,7 @@ import {
10
10
  } from './builtin/collections/authTools.js'
11
11
  import { countDocumentsTool } from './builtin/collections/countTool.js'
12
12
  import { countVersionsTool } from './builtin/collections/countVersionsTool.js'
13
- import { createDocumentTool } from './builtin/collections/createTool.js'
13
+ import { createDocumentsTool } from './builtin/collections/createTool.js'
14
14
  import { deleteDocumentsTool } from './builtin/collections/deleteTool.js'
15
15
  import { duplicateDocumentTool } from './builtin/collections/duplicateTool.js'
16
16
  import { findDistinctTool } from './builtin/collections/findDistinctTool.js'
@@ -20,6 +20,7 @@ import { findVersionsTool } from './builtin/collections/findVersionsTool.js'
20
20
  import { getCollectionSchemaTool } from './builtin/collections/getCollectionSchemaTool.js'
21
21
  import { restoreVersionTool } from './builtin/collections/restoreVersionTool.js'
22
22
  import { updateDocumentTool } from './builtin/collections/updateTool.js'
23
+ import { getUploadInstructionsTool } from './builtin/collections/uploadInstructionsTool.js'
23
24
  import { getConfigInfoTool } from './builtin/getConfigInfoTool.js'
24
25
  import { countGlobalVersionsTool } from './builtin/globals/countVersionsTool.js'
25
26
  import { findGlobalTool } from './builtin/globals/findTool.js'
@@ -32,6 +33,7 @@ import { updateGlobalTool } from './builtin/globals/updateTool.js'
32
33
  type CollectionBuiltin = {
33
34
  mcpName: string
34
35
  requiresDuplicateEnabled?: boolean
36
+ requiresUpload?: boolean
35
37
  requiresVersions?: boolean
36
38
  tool: CollectionTool
37
39
  }
@@ -54,7 +56,7 @@ export const TOOL_BUILTINS = {
54
56
  export const COLLECTION_BUILTINS = {
55
57
  count: { mcpName: 'countDocuments', tool: countDocumentsTool },
56
58
  countVersions: { mcpName: 'countVersions', requiresVersions: true, tool: countVersionsTool },
57
- create: { mcpName: 'createDocument', tool: createDocumentTool },
59
+ create: { mcpName: 'createDocuments', tool: createDocumentsTool },
58
60
  delete: { mcpName: 'deleteDocuments', tool: deleteDocumentsTool },
59
61
  duplicate: {
60
62
  mcpName: 'duplicateDocument',
@@ -70,6 +72,11 @@ export const COLLECTION_BUILTINS = {
70
72
  },
71
73
  findVersions: { mcpName: 'findVersions', requiresVersions: true, tool: findVersionsTool },
72
74
  getCollectionSchema: { mcpName: 'getCollectionSchema', tool: getCollectionSchemaTool },
75
+ getUploadInstructions: {
76
+ mcpName: 'getUploadInstructions',
77
+ requiresUpload: true,
78
+ tool: getUploadInstructionsTool,
79
+ },
73
80
  restoreVersion: { mcpName: 'restoreVersion', requiresVersions: true, tool: restoreVersionTool },
74
81
  update: { mcpName: 'updateDocument', tool: updateDocumentTool },
75
82
  } satisfies Record<string, CollectionBuiltin>
@@ -127,7 +127,7 @@ const sanitizeCollectionConfig = ({
127
127
 
128
128
  for (const [
129
129
  toolKey,
130
- { mcpName, requiresDuplicateEnabled, requiresVersions, tool },
130
+ { mcpName, requiresDuplicateEnabled, requiresUpload, requiresVersions, tool },
131
131
  ] of COLLECTION_BUILTIN_ENTRIES) {
132
132
  if (requiresVersions && !collection.versions) {
133
133
  continue
@@ -135,6 +135,9 @@ const sanitizeCollectionConfig = ({
135
135
  if (requiresDuplicateEnabled && isDuplicateDisabled) {
136
136
  continue
137
137
  }
138
+ if (requiresUpload && !collection.upload) {
139
+ continue
140
+ }
138
141
  const matchedConfigEntry = collectionPluginConfig?.tools?.[toolKey]
139
142
  if (matchedConfigEntry === false) {
140
143
  continue
package/src/stdio.ts CHANGED
@@ -4,7 +4,7 @@ import type { Config, Plugin, SanitizedConfig } from 'payload'
4
4
  import { serveStdio } from '@modelcontextprotocol/server/stdio'
5
5
  import { fileURLToPath, pathToFileURL } from 'node:url'
6
6
  import { createLocalReq, getPayload } from 'payload'
7
- import { findConfig } from 'payload/node'
7
+ import { findConfig, loadEnv } from 'payload/node'
8
8
 
9
9
  import type { SanitizedMCPPluginConfig } from './types.js'
10
10
 
@@ -28,6 +28,7 @@ export const runMcpStdio = async (): Promise<void> => {
28
28
  process.chdir(projectRoot)
29
29
  }
30
30
 
31
+ loadEnv()
31
32
  const configPath = findConfig()
32
33
  const configModule = await import(pathToFileURL(configPath).toString())
33
34
  const config = (await (configModule.default ?? configModule)) as SanitizedConfig