@payloadcms/plugin-mcp 4.0.0-internal.cea8867 → 4.0.0-internal.d927017
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +8 -6
- package/dist/bin/index.d.ts +2 -0
- package/dist/bin/index.d.ts.map +1 -0
- package/dist/bin/index.js +12 -0
- package/dist/bin/index.js.map +1 -0
- package/dist/bin/initializeViteHMR.d.ts +8 -0
- package/dist/bin/initializeViteHMR.d.ts.map +1 -0
- package/dist/bin/initializeViteHMR.js +63 -0
- package/dist/bin/initializeViteHMR.js.map +1 -0
- package/dist/bin/viteConfigBoundary.d.ts +6 -0
- package/dist/bin/viteConfigBoundary.d.ts.map +1 -0
- package/dist/bin/viteConfigBoundary.js +25 -0
- package/dist/bin/viteConfigBoundary.js.map +1 -0
- package/dist/defaultAccess.d.ts.map +1 -1
- package/dist/defineTool.d.ts +8 -8
- package/dist/defineTool.d.ts.map +1 -1
- package/dist/endpoint/access.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +1 -11
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +1 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +2 -23
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +6 -12
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -1
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/stdio.d.ts +2 -1
- package/dist/stdio.d.ts.map +1 -1
- package/dist/stdio.js +4 -3
- package/dist/stdio.js.map +1 -1
- package/dist/utils/camelCase.d.ts.map +1 -1
- package/dist/utils/resolveProjectRoot.d.ts.map +1 -1
- package/dist/utils/schemaConversion/filterFieldsByAccess.d.ts.map +1 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -1
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -1
- package/dist/utils/toStandardSchema.d.ts.map +1 -1
- package/package.json +17 -4
- package/src/bin/index.ts +16 -0
- package/src/bin/initializeViteHMR.ts +60 -0
- package/src/bin/viteConfigBoundary.ts +39 -0
- package/src/mcp/builtin/collections/createTool.ts +1 -5
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +1 -14
- package/src/mcp/builtin/collections/updateTool.ts +4 -6
- package/src/stdio.ts +4 -3
- package/dist/mcp/builtin/collections/fileInput.d.ts +0 -20
- package/dist/mcp/builtin/collections/fileInput.d.ts.map +0 -1
- package/dist/mcp/builtin/collections/fileInput.js +0 -100
- package/dist/mcp/builtin/collections/fileInput.js.map +0 -1
- package/dist/mcp/builtin/collections/fileInput.spec.js +0 -72
- package/dist/mcp/builtin/collections/fileInput.spec.js.map +0 -1
- package/src/mcp/builtin/collections/fileInput.spec.ts +0 -79
- package/src/mcp/builtin/collections/fileInput.ts +0 -139
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/fileInput.spec.ts"],"sourcesContent":["import type { PayloadRequest } from 'payload'\n\nimport { describe, expect, it } from 'vitest'\n\nimport { fileInputSchema, resolveFileInput } from './fileInput.js'\n\ndescribe('MCP file input', () => {\n it('should reject malformed MIME types', () => {\n const result = fileInputSchema.safeParse({\n data: 'aGVsbG8=',\n mimeType: 'text/plain\\r\\nx-test: value',\n name: 'hello.txt',\n source: 'base64',\n })\n\n expect(result.success).toBe(false)\n })\n\n it('should reject oversized base64 files', async () => {\n const req = createRequest({ maxFileSize: 4 })\n\n await expect(\n resolveFileInput({\n collectionSlug: 'media',\n input: {\n data: Buffer.from('hello').toString('base64'),\n mimeType: 'text/plain',\n name: 'hello.txt',\n source: 'base64',\n },\n req,\n }),\n ).rejects.toThrow('File exceeds the 4 byte upload limit.')\n })\n\n it('should reject URLs outside the collection allowlist', async () => {\n const req = createRequest({\n allowList: [{ hostname: 'assets.example.com', protocol: 'https' }],\n })\n\n await expect(\n resolveFileInput({\n collectionSlug: 'media',\n input: {\n source: 'url',\n url: 'https://example.com/image.png',\n },\n req,\n }),\n ).rejects.toThrow('The provided file URL is not allowed.')\n })\n})\n\nfunction createRequest({\n allowList,\n maxFileSize,\n}: {\n allowList?: Array<{ hostname: string; protocol: 'http' | 'https' }>\n maxFileSize?: number\n}): PayloadRequest {\n return {\n payload: {\n collections: {\n media: {\n config: {\n upload: allowList ? { pasteURL: { allowList } } : {},\n },\n },\n },\n config: {\n upload: {\n limits: {\n fileSize: maxFileSize,\n },\n },\n },\n },\n } as unknown as PayloadRequest\n}\n"],"names":["describe","expect","it","fileInputSchema","resolveFileInput","result","safeParse","data","mimeType","name","source","success","toBe","req","createRequest","maxFileSize","collectionSlug","input","Buffer","from","toString","rejects","toThrow","allowList","hostname","protocol","url","payload","collections","media","config","upload","pasteURL","limits","fileSize"],"mappings":"AAEA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,EAAE,QAAQ,SAAQ;AAE7C,SAASC,eAAe,EAAEC,gBAAgB,QAAQ,iBAAgB;AAElEJ,SAAS,kBAAkB;IACzBE,GAAG,sCAAsC;QACvC,MAAMG,SAASF,gBAAgBG,SAAS,CAAC;YACvCC,MAAM;YACNC,UAAU;YACVC,MAAM;YACNC,QAAQ;QACV;QAEAT,OAAOI,OAAOM,OAAO,EAAEC,IAAI,CAAC;IAC9B;IAEAV,GAAG,wCAAwC;QACzC,MAAMW,MAAMC,cAAc;YAAEC,aAAa;QAAE;QAE3C,MAAMd,OACJG,iBAAiB;YACfY,gBAAgB;YAChBC,OAAO;gBACLV,MAAMW,OAAOC,IAAI,CAAC,SAASC,QAAQ,CAAC;gBACpCZ,UAAU;gBACVC,MAAM;gBACNC,QAAQ;YACV;YACAG;QACF,IACAQ,OAAO,CAACC,OAAO,CAAC;IACpB;IAEApB,GAAG,uDAAuD;QACxD,MAAMW,MAAMC,cAAc;YACxBS,WAAW;gBAAC;oBAAEC,UAAU;oBAAsBC,UAAU;gBAAQ;aAAE;QACpE;QAEA,MAAMxB,OACJG,iBAAiB;YACfY,gBAAgB;YAChBC,OAAO;gBACLP,QAAQ;gBACRgB,KAAK;YACP;YACAb;QACF,IACAQ,OAAO,CAACC,OAAO,CAAC;IACpB;AACF;AAEA,SAASR,cAAc,EACrBS,SAAS,EACTR,WAAW,EAIZ;IACC,OAAO;QACLY,SAAS;YACPC,aAAa;gBACXC,OAAO;oBACLC,QAAQ;wBACNC,QAAQR,YAAY;4BAAES,UAAU;gCAAET;4BAAU;wBAAE,IAAI,CAAC;oBACrD;gBACF;YACF;YACAO,QAAQ;gBACNC,QAAQ;oBACNE,QAAQ;wBACNC,UAAUnB;oBACZ;gBACF;YACF;QACF;IACF;AACF"}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import type { PayloadRequest } from 'payload'
|
|
2
|
-
|
|
3
|
-
import { describe, expect, it } from 'vitest'
|
|
4
|
-
|
|
5
|
-
import { fileInputSchema, resolveFileInput } from './fileInput.js'
|
|
6
|
-
|
|
7
|
-
describe('MCP file input', () => {
|
|
8
|
-
it('should reject malformed MIME types', () => {
|
|
9
|
-
const result = fileInputSchema.safeParse({
|
|
10
|
-
data: 'aGVsbG8=',
|
|
11
|
-
mimeType: 'text/plain\r\nx-test: value',
|
|
12
|
-
name: 'hello.txt',
|
|
13
|
-
source: 'base64',
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
expect(result.success).toBe(false)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('should reject oversized base64 files', async () => {
|
|
20
|
-
const req = createRequest({ maxFileSize: 4 })
|
|
21
|
-
|
|
22
|
-
await expect(
|
|
23
|
-
resolveFileInput({
|
|
24
|
-
collectionSlug: 'media',
|
|
25
|
-
input: {
|
|
26
|
-
data: Buffer.from('hello').toString('base64'),
|
|
27
|
-
mimeType: 'text/plain',
|
|
28
|
-
name: 'hello.txt',
|
|
29
|
-
source: 'base64',
|
|
30
|
-
},
|
|
31
|
-
req,
|
|
32
|
-
}),
|
|
33
|
-
).rejects.toThrow('File exceeds the 4 byte upload limit.')
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should reject URLs outside the collection allowlist', async () => {
|
|
37
|
-
const req = createRequest({
|
|
38
|
-
allowList: [{ hostname: 'assets.example.com', protocol: 'https' }],
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
await expect(
|
|
42
|
-
resolveFileInput({
|
|
43
|
-
collectionSlug: 'media',
|
|
44
|
-
input: {
|
|
45
|
-
source: 'url',
|
|
46
|
-
url: 'https://example.com/image.png',
|
|
47
|
-
},
|
|
48
|
-
req,
|
|
49
|
-
}),
|
|
50
|
-
).rejects.toThrow('The provided file URL is not allowed.')
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
function createRequest({
|
|
55
|
-
allowList,
|
|
56
|
-
maxFileSize,
|
|
57
|
-
}: {
|
|
58
|
-
allowList?: Array<{ hostname: string; protocol: 'http' | 'https' }>
|
|
59
|
-
maxFileSize?: number
|
|
60
|
-
}): PayloadRequest {
|
|
61
|
-
return {
|
|
62
|
-
payload: {
|
|
63
|
-
collections: {
|
|
64
|
-
media: {
|
|
65
|
-
config: {
|
|
66
|
-
upload: allowList ? { pasteURL: { allowList } } : {},
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
config: {
|
|
71
|
-
upload: {
|
|
72
|
-
limits: {
|
|
73
|
-
fileSize: maxFileSize,
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
},
|
|
78
|
-
} as unknown as PayloadRequest
|
|
79
|
-
}
|
|
@@ -1,139 +0,0 @@
|
|
|
1
|
-
import type { CollectionSlug, File, FileData, PayloadRequest } from 'payload'
|
|
2
|
-
|
|
3
|
-
import { APIError } from 'payload'
|
|
4
|
-
import { getExternalFile, isURLAllowed } from 'payload/internal'
|
|
5
|
-
import { sanitizeFilename } from 'payload/shared'
|
|
6
|
-
import { z } from 'zod'
|
|
7
|
-
|
|
8
|
-
const mimeTypeSchema = z
|
|
9
|
-
.string()
|
|
10
|
-
.regex(/^[!#$%&'*+.^`|~\w-]+\/[!#$%&'*+.^`|~\w-]+$/, 'MIME type must use the type/subtype format')
|
|
11
|
-
|
|
12
|
-
export const fileInputSchema = z
|
|
13
|
-
.discriminatedUnion('source', [
|
|
14
|
-
z.object({
|
|
15
|
-
name: z.string().min(1).describe('The file name, including its extension'),
|
|
16
|
-
data: z.string().describe('The base64-encoded file bytes, without a data URL prefix'),
|
|
17
|
-
mimeType: mimeTypeSchema.describe('The file MIME type, for example image/png'),
|
|
18
|
-
source: z.literal('base64'),
|
|
19
|
-
}),
|
|
20
|
-
z.object({
|
|
21
|
-
name: z.string().min(1).describe('Optional file name override').optional(),
|
|
22
|
-
source: z.literal('url'),
|
|
23
|
-
url: z.url().describe('The http or https URL to download'),
|
|
24
|
-
}),
|
|
25
|
-
])
|
|
26
|
-
.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.',
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
type FileInput = z.infer<typeof fileInputSchema>
|
|
31
|
-
|
|
32
|
-
export async function resolveFileInput({
|
|
33
|
-
collectionSlug,
|
|
34
|
-
input,
|
|
35
|
-
req,
|
|
36
|
-
}: {
|
|
37
|
-
collectionSlug: CollectionSlug
|
|
38
|
-
input?: FileInput
|
|
39
|
-
req: PayloadRequest
|
|
40
|
-
}): Promise<File | undefined> {
|
|
41
|
-
if (!input) {
|
|
42
|
-
return undefined
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const uploadConfig = req.payload.collections[collectionSlug]?.config.upload
|
|
46
|
-
|
|
47
|
-
if (!uploadConfig) {
|
|
48
|
-
throw new APIError(`Collection "${collectionSlug}" does not support file uploads.`, 400)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const maxFileSize = req.payload.config.upload.limits?.fileSize
|
|
52
|
-
let file: File
|
|
53
|
-
|
|
54
|
-
if (input.source === 'base64') {
|
|
55
|
-
const data = decodeBase64({ maxFileSize, value: input.data })
|
|
56
|
-
|
|
57
|
-
file = {
|
|
58
|
-
name: sanitizeFilename(input.name),
|
|
59
|
-
data,
|
|
60
|
-
mimetype: input.mimeType,
|
|
61
|
-
size: data.length,
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
if (uploadConfig.pasteURL === false) {
|
|
65
|
-
throw new APIError(
|
|
66
|
-
`Uploading files from URLs is disabled for collection "${collectionSlug}".`,
|
|
67
|
-
400,
|
|
68
|
-
)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const url = new URL(input.url)
|
|
72
|
-
|
|
73
|
-
if (!['http:', 'https:'].includes(url.protocol)) {
|
|
74
|
-
throw new APIError('File URLs must use http or https.', 400)
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (
|
|
78
|
-
typeof uploadConfig.pasteURL === 'object' &&
|
|
79
|
-
!isURLAllowed(input.url, uploadConfig.pasteURL.allowList)
|
|
80
|
-
) {
|
|
81
|
-
throw new APIError('The provided file URL is not allowed.', 400)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
file = await getExternalFile({
|
|
85
|
-
data: {
|
|
86
|
-
filename: sanitizeFilename(input.name || getURLFilename(url)),
|
|
87
|
-
url: input.url,
|
|
88
|
-
} as FileData,
|
|
89
|
-
req,
|
|
90
|
-
uploadConfig: {
|
|
91
|
-
...uploadConfig,
|
|
92
|
-
externalFileHeaderFilter: uploadConfig.externalFileHeaderFilter ?? (() => ({})),
|
|
93
|
-
},
|
|
94
|
-
})
|
|
95
|
-
file.mimetype = file.mimetype?.split(';')[0] || 'application/octet-stream'
|
|
96
|
-
file.size = file.data.length
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (maxFileSize !== undefined && Number.isFinite(maxFileSize) && file.size > maxFileSize) {
|
|
100
|
-
throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return file
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function decodeBase64({ maxFileSize, value }: { maxFileSize?: number; value: string }): Buffer {
|
|
107
|
-
const normalized = value.replace(/\s/g, '')
|
|
108
|
-
|
|
109
|
-
if (!/^[a-z0-9+/]*={0,2}$/i.test(normalized) || normalized.length % 4 === 1) {
|
|
110
|
-
throw new APIError('File data must be valid base64.', 400)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (maxFileSize !== undefined && Number.isFinite(maxFileSize)) {
|
|
114
|
-
const paddingLength = normalized.endsWith('==') ? 2 : normalized.endsWith('=') ? 1 : 0
|
|
115
|
-
const decodedSize = Math.floor((normalized.length * 3) / 4) - paddingLength
|
|
116
|
-
|
|
117
|
-
if (decodedSize > maxFileSize) {
|
|
118
|
-
throw new APIError(`File exceeds the ${maxFileSize} byte upload limit.`, 400)
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const data = Buffer.from(normalized, 'base64')
|
|
123
|
-
|
|
124
|
-
if (data.toString('base64').replace(/=+$/, '') !== normalized.replace(/=+$/, '')) {
|
|
125
|
-
throw new APIError('File data must be valid base64.', 400)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return data
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function getURLFilename(url: URL): string {
|
|
132
|
-
const pathSegment = url.pathname.split('/').pop() || 'upload'
|
|
133
|
-
|
|
134
|
-
try {
|
|
135
|
-
return decodeURIComponent(pathSegment)
|
|
136
|
-
} catch {
|
|
137
|
-
return pathSegment
|
|
138
|
-
}
|
|
139
|
-
}
|