@payloadcms/plugin-mcp 4.0.0-canary.0 → 4.0.0-canary.1
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/dist/collection/getAccessField.js +1 -1
- package/dist/collection/getAccessField.js.map +1 -1
- package/dist/collection/index.d.ts.map +1 -1
- package/dist/collection/index.js +2 -1
- package/dist/collection/index.js.map +1 -1
- package/dist/components/AccessField/index.client.d.ts.map +1 -1
- package/dist/components/AccessField/index.client.js +30 -30
- package/dist/components/AccessField/index.client.js.map +1 -1
- package/dist/endpoint/access.js +5 -5
- package/dist/endpoint/access.js.map +1 -1
- package/dist/mcp/buildMcpServer.d.ts.map +1 -1
- package/dist/mcp/buildMcpServer.js +100 -64
- package/dist/mcp/buildMcpServer.js.map +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/createTool.js +28 -21
- package/dist/mcp/builtin/collections/createTool.js.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/deleteTool.js +5 -20
- package/dist/mcp/builtin/collections/deleteTool.js.map +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/findTool.js +6 -21
- package/dist/mcp/builtin/collections/findTool.js.map +1 -1
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts +9 -0
- package/dist/mcp/builtin/collections/formatCollectionError.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/formatCollectionError.js +60 -0
- package/dist/mcp/builtin/collections/formatCollectionError.js.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js +35 -0
- package/dist/mcp/builtin/collections/getCollectionSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/collections/updateTool.d.ts +1 -1
- package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/collections/updateTool.js +74 -62
- package/dist/mcp/builtin/collections/updateTool.js.map +1 -1
- package/dist/mcp/builtin/getConfigInfoTool.d.ts +2 -0
- package/dist/mcp/builtin/getConfigInfoTool.d.ts.map +1 -0
- package/dist/mcp/builtin/getConfigInfoTool.js +49 -0
- package/dist/mcp/builtin/getConfigInfoTool.js.map +1 -0
- package/dist/mcp/builtin/globals/findTool.js +1 -1
- package/dist/mcp/builtin/globals/findTool.js.map +1 -1
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts +2 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.d.ts.map +1 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js +35 -0
- package/dist/mcp/builtin/globals/getGlobalSchemaTool.js.map +1 -0
- package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -1
- package/dist/mcp/builtin/globals/updateTool.js +21 -19
- package/dist/mcp/builtin/globals/updateTool.js.map +1 -1
- package/dist/mcp/builtin/validateEntityData.d.ts +14 -0
- package/dist/mcp/builtin/validateEntityData.d.ts.map +1 -0
- package/dist/mcp/builtin/validateEntityData.js +82 -0
- package/dist/mcp/builtin/validateEntityData.js.map +1 -0
- package/dist/mcp/builtinTools.d.ts +84 -16
- package/dist/mcp/builtinTools.d.ts.map +1 -1
- package/dist/mcp/builtinTools.js +54 -11
- package/dist/mcp/builtinTools.js.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -1
- package/dist/mcp/sanitizeMCPConfig.js +61 -40
- package/dist/mcp/sanitizeMCPConfig.js.map +1 -1
- package/dist/types.d.ts +16 -27
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts +11 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js +34 -0
- package/dist/utils/schemaConversion/getEntityInputSchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts +15 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.d.ts.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js +464 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.js.map +1 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js +158 -0
- package/dist/utils/schemaConversion/sanitizeEntitySchema.spec.js.map +1 -0
- package/dist/utils/whereSchema.d.ts +9 -0
- package/dist/utils/whereSchema.d.ts.map +1 -0
- package/dist/utils/whereSchema.js +13 -0
- package/dist/utils/whereSchema.js.map +1 -0
- package/package.json +5 -5
- package/src/collection/getAccessField.ts +1 -1
- package/src/collection/index.ts +1 -0
- package/src/components/AccessField/index.client.tsx +34 -31
- package/src/endpoint/access.ts +5 -5
- package/src/mcp/buildMcpServer.ts +123 -90
- package/src/mcp/builtin/collections/createTool.ts +46 -50
- package/src/mcp/builtin/collections/deleteTool.ts +9 -16
- package/src/mcp/builtin/collections/findTool.ts +7 -17
- package/src/mcp/builtin/collections/formatCollectionError.ts +84 -0
- package/src/mcp/builtin/collections/getCollectionSchemaTool.ts +28 -0
- package/src/mcp/builtin/collections/updateTool.ts +97 -91
- package/src/mcp/builtin/getConfigInfoTool.ts +44 -0
- package/src/mcp/builtin/globals/findTool.ts +1 -1
- package/src/mcp/builtin/globals/getGlobalSchemaTool.ts +28 -0
- package/src/mcp/builtin/globals/updateTool.ts +40 -43
- package/src/mcp/builtin/validateEntityData.ts +132 -0
- package/src/mcp/builtinTools.ts +52 -38
- package/src/mcp/sanitizeMCPConfig.ts +78 -57
- package/src/types.ts +24 -29
- package/src/utils/schemaConversion/getEntityInputSchema.ts +78 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.spec.ts +103 -0
- package/src/utils/schemaConversion/sanitizeEntitySchema.ts +529 -0
- package/src/utils/whereSchema.ts +24 -0
- package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +0 -7
- package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/prepareCollectionSchema.js +0 -37
- package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +0 -13
- package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +0 -1
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js +0 -56
- package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +0 -20
- package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js +0 -56
- package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.d.ts +0 -3
- package/dist/utils/schemaConversion/transformPointFields.d.ts.map +0 -1
- package/dist/utils/schemaConversion/transformPointFields.js +0 -57
- package/dist/utils/schemaConversion/transformPointFields.js.map +0 -1
- package/src/utils/schemaConversion/prepareCollectionSchema.ts +0 -39
- package/src/utils/schemaConversion/sanitizeJsonSchema.ts +0 -62
- package/src/utils/schemaConversion/simplifyRelationshipFields.ts +0 -70
- package/src/utils/schemaConversion/transformPointFields.ts +0 -56
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { SelectType } from 'payload'
|
|
1
|
+
import type { SelectType, Where } from 'payload'
|
|
2
2
|
|
|
3
3
|
import { z } from 'zod'
|
|
4
4
|
|
|
@@ -9,70 +9,63 @@ import {
|
|
|
9
9
|
stripVirtualFields,
|
|
10
10
|
} from '../../../utils/getVirtualFieldNames.js'
|
|
11
11
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
12
|
-
import {
|
|
12
|
+
import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
|
|
13
13
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
14
|
+
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
15
|
+
import { validateCollectionData } from '../validateEntityData.js'
|
|
16
|
+
import { formatCollectionError } from './formatCollectionError.js'
|
|
14
17
|
|
|
15
|
-
const DEFAULT_DESCRIPTION =
|
|
18
|
+
const DEFAULT_DESCRIPTION =
|
|
19
|
+
'Update documents in any collection by passing the collection slug and data.'
|
|
16
20
|
|
|
17
|
-
export const
|
|
21
|
+
export const updateDocumentTool = defineCollectionTool({
|
|
18
22
|
description: DEFAULT_DESCRIPTION,
|
|
19
|
-
input: ({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
.string()
|
|
66
|
-
.describe(
|
|
67
|
-
'Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"title": "My Post"}\'',
|
|
68
|
-
)
|
|
69
|
-
.optional(),
|
|
70
|
-
where: z
|
|
71
|
-
.string()
|
|
72
|
-
.describe('JSON string for where clause to update multiple documents')
|
|
73
|
-
.optional(),
|
|
74
|
-
})
|
|
75
|
-
},
|
|
23
|
+
input: z.object({
|
|
24
|
+
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'),
|
|
26
|
+
depth: z
|
|
27
|
+
.number()
|
|
28
|
+
.describe('How many levels deep to populate relationships')
|
|
29
|
+
.optional()
|
|
30
|
+
.default(0),
|
|
31
|
+
draft: z
|
|
32
|
+
.boolean()
|
|
33
|
+
.describe('Whether to update the document as a draft')
|
|
34
|
+
.optional()
|
|
35
|
+
.default(false),
|
|
36
|
+
fallbackLocale: z
|
|
37
|
+
.string()
|
|
38
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
39
|
+
.optional(),
|
|
40
|
+
filePath: z.string().describe('File path for file uploads').optional(),
|
|
41
|
+
locale: z
|
|
42
|
+
.string()
|
|
43
|
+
.describe(
|
|
44
|
+
'Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale',
|
|
45
|
+
)
|
|
46
|
+
.optional(),
|
|
47
|
+
overrideLock: z
|
|
48
|
+
.boolean()
|
|
49
|
+
.describe('Whether to override document locks')
|
|
50
|
+
.optional()
|
|
51
|
+
.default(true),
|
|
52
|
+
overwriteExistingFiles: z
|
|
53
|
+
.boolean()
|
|
54
|
+
.describe('Whether to overwrite existing files')
|
|
55
|
+
.optional()
|
|
56
|
+
.default(false),
|
|
57
|
+
select: z
|
|
58
|
+
.string()
|
|
59
|
+
.describe(
|
|
60
|
+
"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'",
|
|
61
|
+
)
|
|
62
|
+
.optional(),
|
|
63
|
+
where: whereSchema
|
|
64
|
+
.describe(
|
|
65
|
+
'Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}',
|
|
66
|
+
)
|
|
67
|
+
.optional(),
|
|
68
|
+
}),
|
|
76
69
|
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
77
70
|
const payload = req.payload
|
|
78
71
|
const logger = getLogger({ payload })
|
|
@@ -102,20 +95,23 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
102
95
|
}
|
|
103
96
|
}
|
|
104
97
|
|
|
105
|
-
let parsedData = transformPointDataToPayload(data as Record<string, unknown>)
|
|
106
98
|
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)
|
|
107
|
-
|
|
99
|
+
const inputData = stripVirtualFields(data, virtualFieldNames)
|
|
100
|
+
const validationError = validateCollectionData({
|
|
101
|
+
collectionSlug,
|
|
102
|
+
data: inputData,
|
|
103
|
+
partial: true,
|
|
104
|
+
req,
|
|
105
|
+
})
|
|
108
106
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
113
|
-
} catch {
|
|
114
|
-
logger.error(`Invalid where clause JSON: ${where}`)
|
|
115
|
-
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
116
|
-
}
|
|
107
|
+
if (validationError) {
|
|
108
|
+
return validationError
|
|
117
109
|
}
|
|
118
110
|
|
|
111
|
+
const parsedData = transformPointDataToPayload(inputData)
|
|
112
|
+
|
|
113
|
+
const whereClause: Where = where ?? {}
|
|
114
|
+
|
|
119
115
|
let selectClause: SelectType | undefined
|
|
120
116
|
if (select) {
|
|
121
117
|
try {
|
|
@@ -127,7 +123,7 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
127
123
|
}
|
|
128
124
|
|
|
129
125
|
if (id) {
|
|
130
|
-
const
|
|
126
|
+
const result = await payload.update({
|
|
131
127
|
id,
|
|
132
128
|
collection: collectionSlug,
|
|
133
129
|
data: parsedData,
|
|
@@ -141,9 +137,7 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
141
137
|
...(locale ? { locale } : {}),
|
|
142
138
|
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
143
139
|
...(selectClause ? { select: selectClause } : {}),
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const result = await payload.update(updateOptions as any)
|
|
140
|
+
})
|
|
147
141
|
|
|
148
142
|
return {
|
|
149
143
|
content: [
|
|
@@ -156,27 +150,24 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
156
150
|
}
|
|
157
151
|
}
|
|
158
152
|
|
|
159
|
-
const
|
|
153
|
+
const result = await payload.update({
|
|
160
154
|
collection: collectionSlug,
|
|
161
155
|
data: parsedData,
|
|
162
156
|
depth,
|
|
163
157
|
draft,
|
|
164
158
|
overrideLock,
|
|
165
159
|
req,
|
|
166
|
-
...localAPIDefaults(authorizedMCP),
|
|
167
160
|
where: whereClause,
|
|
161
|
+
...localAPIDefaults(authorizedMCP),
|
|
168
162
|
...(filePath ? { filePath } : {}),
|
|
169
163
|
...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),
|
|
170
164
|
...(locale ? { locale } : {}),
|
|
171
165
|
...(fallbackLocale ? { fallbackLocale } : {}),
|
|
172
166
|
...(selectClause ? { select: selectClause } : {}),
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
const result = await payload.update(updateOptions as any)
|
|
167
|
+
})
|
|
176
168
|
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
const errors = bulkResult.errors || []
|
|
169
|
+
const docs = result.docs || []
|
|
170
|
+
const errors = result.errors || []
|
|
180
171
|
|
|
181
172
|
let responseText = `Multiple documents updated in collection "${collectionSlug}"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`
|
|
182
173
|
if (docs.length > 0) {
|
|
@@ -184,6 +175,28 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
184
175
|
}
|
|
185
176
|
if (errors.length > 0) {
|
|
186
177
|
responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``
|
|
178
|
+
|
|
179
|
+
const errorSchema = getCollectionInputSchema({ collectionSlug, req })
|
|
180
|
+
|
|
181
|
+
if (errorSchema) {
|
|
182
|
+
responseText += `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(errorSchema)}\n\`\`\``
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
content: [{ type: 'text', text: responseText }],
|
|
187
|
+
doc: { docs, errors } as unknown as Record<string, unknown>,
|
|
188
|
+
isError: true,
|
|
189
|
+
...(errorSchema
|
|
190
|
+
? {
|
|
191
|
+
structuredContent: {
|
|
192
|
+
collectionSlug,
|
|
193
|
+
docs,
|
|
194
|
+
errors,
|
|
195
|
+
schema: errorSchema,
|
|
196
|
+
},
|
|
197
|
+
}
|
|
198
|
+
: {}),
|
|
199
|
+
}
|
|
187
200
|
}
|
|
188
201
|
|
|
189
202
|
return {
|
|
@@ -193,13 +206,6 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
193
206
|
} catch (error) {
|
|
194
207
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
195
208
|
logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)
|
|
196
|
-
return {
|
|
197
|
-
content: [
|
|
198
|
-
{
|
|
199
|
-
type: 'text',
|
|
200
|
-
text: `Error updating document in collection "${collectionSlug}": ${errorMessage}`,
|
|
201
|
-
},
|
|
202
|
-
],
|
|
203
|
-
}
|
|
209
|
+
return formatCollectionError({ action: 'updating', collectionSlug, error, req })
|
|
204
210
|
}
|
|
205
211
|
})
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getAccessResults, isEntityHidden } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { defineTool } from '../../defineTool.js'
|
|
4
|
+
|
|
5
|
+
export const getConfigInfoTool = defineTool({
|
|
6
|
+
description: 'List the Payload collection and global slugs visible to this MCP client.',
|
|
7
|
+
}).handler(async ({ authorizedMCP, req }) => {
|
|
8
|
+
const user = authorizedMCP.user ?? req.user ?? null
|
|
9
|
+
const permissions = user ? await getAccessResults({ req: { ...req, user } }) : null
|
|
10
|
+
|
|
11
|
+
const collections: string[] = []
|
|
12
|
+
const globals: string[] = []
|
|
13
|
+
|
|
14
|
+
for (const collection of req.payload.config.collections) {
|
|
15
|
+
if (user && isEntityHidden({ hidden: collection.admin.hidden, user })) {
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
if (user && !permissions?.collections?.[collection.slug]?.read) {
|
|
19
|
+
continue
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
collections.push(collection.slug)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
for (const global of req.payload.config.globals) {
|
|
26
|
+
if (user && isEntityHidden({ hidden: global.admin.hidden, user })) {
|
|
27
|
+
continue
|
|
28
|
+
}
|
|
29
|
+
if (user && !permissions?.globals?.[global.slug]?.read) {
|
|
30
|
+
continue
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
globals.push(global.slug)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
content: [
|
|
38
|
+
{
|
|
39
|
+
type: 'text',
|
|
40
|
+
text: `Collections: ${collections.join(', ') || 'none'}\nGlobals: ${globals.join(', ') || 'none'}`,
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
}
|
|
44
|
+
})
|
|
@@ -6,7 +6,7 @@ import { defineGlobalTool } from '../../../defineTool.js'
|
|
|
6
6
|
import { getLogger } from '../../../utils/getLogger.js'
|
|
7
7
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
8
8
|
|
|
9
|
-
const DEFAULT_DESCRIPTION = 'Find
|
|
9
|
+
const DEFAULT_DESCRIPTION = 'Find any Payload global by passing the global slug.'
|
|
10
10
|
|
|
11
11
|
export const findGlobalTool = defineGlobalTool({
|
|
12
12
|
description: DEFAULT_DESCRIPTION,
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineGlobalTool } from '../../../defineTool.js'
|
|
2
|
+
import { getGlobalInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
|
|
3
|
+
|
|
4
|
+
export const getGlobalSchemaTool = defineGlobalTool({
|
|
5
|
+
description: 'Get the input schema for updating a global.',
|
|
6
|
+
}).handler(({ globalSlug, req }) => {
|
|
7
|
+
const inputSchema = getGlobalInputSchema({ globalSlug, req })
|
|
8
|
+
|
|
9
|
+
if (!inputSchema) {
|
|
10
|
+
return {
|
|
11
|
+
content: [{ type: 'text', text: `Error: Global "${globalSlug}" not found` }],
|
|
12
|
+
isError: true,
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
content: [
|
|
18
|
+
{
|
|
19
|
+
type: 'text',
|
|
20
|
+
text: `Schema for global "${globalSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``,
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
structuredContent: {
|
|
24
|
+
globalSlug,
|
|
25
|
+
schema: inputSchema,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
})
|
|
@@ -9,52 +9,42 @@ import {
|
|
|
9
9
|
stripVirtualFields,
|
|
10
10
|
} from '../../../utils/getVirtualFieldNames.js'
|
|
11
11
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
12
|
-
import {
|
|
12
|
+
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
13
|
+
import { validateGlobalData } from '../validateEntityData.js'
|
|
13
14
|
|
|
14
|
-
const DEFAULT_DESCRIPTION = 'Update
|
|
15
|
+
const DEFAULT_DESCRIPTION = 'Update any Payload global by passing the global slug and data.'
|
|
15
16
|
|
|
16
17
|
export const updateGlobalTool = defineGlobalTool({
|
|
17
18
|
description: DEFAULT_DESCRIPTION,
|
|
18
|
-
input: ({
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
'Optional: locale code to update data in (e.g., "en", "es"). Use "all" to update all locales for localized fields',
|
|
48
|
-
)
|
|
49
|
-
.optional(),
|
|
50
|
-
select: z
|
|
51
|
-
.string()
|
|
52
|
-
.describe(
|
|
53
|
-
'Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"siteName": "My Site"}\'',
|
|
54
|
-
)
|
|
55
|
-
.optional(),
|
|
56
|
-
})
|
|
57
|
-
},
|
|
19
|
+
input: z.object({
|
|
20
|
+
data: z.record(z.string(), z.unknown()).describe('The global fields to update'),
|
|
21
|
+
depth: z
|
|
22
|
+
.number()
|
|
23
|
+
.describe('Optional: Depth of relationships to populate')
|
|
24
|
+
.optional()
|
|
25
|
+
.default(0),
|
|
26
|
+
draft: z
|
|
27
|
+
.boolean()
|
|
28
|
+
.describe('Optional: Whether to save as draft (default: false)')
|
|
29
|
+
.optional()
|
|
30
|
+
.default(false),
|
|
31
|
+
fallbackLocale: z
|
|
32
|
+
.string()
|
|
33
|
+
.describe('Optional: fallback locale code to use when requested locale is not available')
|
|
34
|
+
.optional(),
|
|
35
|
+
locale: z
|
|
36
|
+
.string()
|
|
37
|
+
.describe(
|
|
38
|
+
'Optional: locale code to update data in (e.g., "en", "es"). Use "all" to update all locales for localized fields',
|
|
39
|
+
)
|
|
40
|
+
.optional(),
|
|
41
|
+
select: z
|
|
42
|
+
.string()
|
|
43
|
+
.describe(
|
|
44
|
+
"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"siteName\": true}'",
|
|
45
|
+
)
|
|
46
|
+
.optional(),
|
|
47
|
+
}),
|
|
58
48
|
}).handler(async ({ authorizedMCP, globalSlug, input, req }) => {
|
|
59
49
|
const payload = req.payload
|
|
60
50
|
const logger = getLogger({ payload })
|
|
@@ -67,7 +57,14 @@ export const updateGlobalTool = defineGlobalTool({
|
|
|
67
57
|
|
|
68
58
|
try {
|
|
69
59
|
const virtualFieldNames = getGlobalVirtualFieldNames(payload.config, globalSlug)
|
|
70
|
-
const
|
|
60
|
+
const inputData = stripVirtualFields(data, virtualFieldNames)
|
|
61
|
+
const validationError = validateGlobalData({ data: inputData, globalSlug, req })
|
|
62
|
+
|
|
63
|
+
if (validationError) {
|
|
64
|
+
return validationError
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const parsedData = transformPointDataToPayload(inputData)
|
|
71
68
|
|
|
72
69
|
let selectClause: SelectType | undefined
|
|
73
70
|
if (select) {
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import type { CollectionSlug, GlobalSlug, PayloadRequest, SanitizedConfig } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { CfWorkerJsonSchemaValidator } from '@modelcontextprotocol/server'
|
|
4
|
+
|
|
5
|
+
import type { JsonSchemaType, MCPToolResponse } from '../../types.js'
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
getCollectionInputSchema,
|
|
9
|
+
getGlobalInputSchema,
|
|
10
|
+
} from '../../utils/schemaConversion/getEntityInputSchema.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Keep the default `shortcircuit: true` - `false` makes validating nested rich text exponentially slow.
|
|
14
|
+
*/
|
|
15
|
+
const validator = new CfWorkerJsonSchemaValidator({ shortcircuit: true })
|
|
16
|
+
|
|
17
|
+
type EntityValidator = {
|
|
18
|
+
schema: JsonSchemaType
|
|
19
|
+
validate: ReturnType<(typeof validator)['getValidator']>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* - Caches each entity's schema and validator, so both are only built on the first request.
|
|
24
|
+
* - Keyed by payload config, then by entity type, slug, partial and language.
|
|
25
|
+
*/
|
|
26
|
+
const validatorCache = new WeakMap<SanitizedConfig, Map<string, EntityValidator>>()
|
|
27
|
+
|
|
28
|
+
export const validateCollectionData = ({
|
|
29
|
+
collectionSlug,
|
|
30
|
+
data,
|
|
31
|
+
partial,
|
|
32
|
+
req,
|
|
33
|
+
}: {
|
|
34
|
+
collectionSlug: CollectionSlug
|
|
35
|
+
data: Record<string, unknown>
|
|
36
|
+
partial?: boolean
|
|
37
|
+
req: PayloadRequest
|
|
38
|
+
}): MCPToolResponse | null =>
|
|
39
|
+
validateData({
|
|
40
|
+
slug: collectionSlug,
|
|
41
|
+
buildSchema: () => getCollectionInputSchema({ collectionSlug, req }),
|
|
42
|
+
data,
|
|
43
|
+
entity: 'collection',
|
|
44
|
+
partial,
|
|
45
|
+
req,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
export const validateGlobalData = ({
|
|
49
|
+
data,
|
|
50
|
+
globalSlug,
|
|
51
|
+
req,
|
|
52
|
+
}: {
|
|
53
|
+
data: Record<string, unknown>
|
|
54
|
+
globalSlug: GlobalSlug
|
|
55
|
+
req: PayloadRequest
|
|
56
|
+
}): MCPToolResponse | null =>
|
|
57
|
+
validateData({
|
|
58
|
+
slug: globalSlug,
|
|
59
|
+
buildSchema: () => getGlobalInputSchema({ globalSlug, req }),
|
|
60
|
+
data,
|
|
61
|
+
entity: 'global',
|
|
62
|
+
partial: true,
|
|
63
|
+
req,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const validateData = ({
|
|
67
|
+
slug,
|
|
68
|
+
buildSchema,
|
|
69
|
+
data,
|
|
70
|
+
entity,
|
|
71
|
+
partial,
|
|
72
|
+
req,
|
|
73
|
+
}: {
|
|
74
|
+
buildSchema: () => JsonSchemaType | null
|
|
75
|
+
data: Record<string, unknown>
|
|
76
|
+
entity: 'collection' | 'global'
|
|
77
|
+
partial?: boolean
|
|
78
|
+
req: PayloadRequest
|
|
79
|
+
slug: string
|
|
80
|
+
}): MCPToolResponse | null => {
|
|
81
|
+
let cache = validatorCache.get(req.payload.config)
|
|
82
|
+
if (!cache) {
|
|
83
|
+
cache = new Map()
|
|
84
|
+
validatorCache.set(req.payload.config, cache)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const cacheKey = `${entity}:${slug}:${partial ? 'partial' : 'full'}:${req.i18n.language}`
|
|
88
|
+
let entityValidator = cache.get(cacheKey)
|
|
89
|
+
|
|
90
|
+
if (!entityValidator) {
|
|
91
|
+
const schema = buildSchema()
|
|
92
|
+
|
|
93
|
+
if (!schema) {
|
|
94
|
+
return null
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
entityValidator = {
|
|
98
|
+
schema,
|
|
99
|
+
validate: validator.getValidator(partial ? withoutRequired(schema) : schema),
|
|
100
|
+
}
|
|
101
|
+
cache.set(cacheKey, entityValidator)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const result = entityValidator.validate(data)
|
|
105
|
+
|
|
106
|
+
if (result.valid) {
|
|
107
|
+
return null
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const { schema } = entityValidator
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
content: [
|
|
114
|
+
{
|
|
115
|
+
type: 'text',
|
|
116
|
+
text: `Invalid data for ${entity} "${slug}": ${result.errorMessage}\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(schema)}\n\`\`\``,
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
isError: true,
|
|
120
|
+
structuredContent: {
|
|
121
|
+
[`${entity}Slug`]: slug,
|
|
122
|
+
errors: [{ message: result.errorMessage }],
|
|
123
|
+
schema,
|
|
124
|
+
},
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Updates are partial — drop the schema's top-level `required` before validating. */
|
|
129
|
+
const withoutRequired = (schema: JsonSchemaType): JsonSchemaType => {
|
|
130
|
+
const { required: _required, ...rest } = schema
|
|
131
|
+
return rest
|
|
132
|
+
}
|