@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
|
@@ -3,35 +3,30 @@ import { defineCollectionTool } from '../../../defineTool.js';
|
|
|
3
3
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
4
4
|
import { getCollectionVirtualFieldNames, stripVirtualFields } from '../../../utils/getVirtualFieldNames.js';
|
|
5
5
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js';
|
|
6
|
-
import {
|
|
6
|
+
import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js';
|
|
7
7
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
import { whereSchema } from '../../../utils/whereSchema.js';
|
|
9
|
+
import { validateCollectionData } from '../validateEntityData.js';
|
|
10
|
+
import { formatCollectionError } from './formatCollectionError.js';
|
|
11
|
+
const DEFAULT_DESCRIPTION = 'Update documents in any collection by passing the collection slug and data.';
|
|
12
|
+
export const updateDocumentTool = defineCollectionTool({
|
|
10
13
|
description: DEFAULT_DESCRIPTION,
|
|
11
|
-
input: ({
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
filePath: z.string().describe('File path for file uploads').optional(),
|
|
28
|
-
locale: z.string().describe('Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
29
|
-
overrideLock: z.boolean().describe('Whether to override document locks').optional().default(true),
|
|
30
|
-
overwriteExistingFiles: z.boolean().describe('Whether to overwrite existing files').optional().default(false),
|
|
31
|
-
select: z.string().describe('Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"title": "My Post"}\'').optional(),
|
|
32
|
-
where: z.string().describe('JSON string for where clause to update multiple documents').optional()
|
|
33
|
-
});
|
|
34
|
-
}
|
|
14
|
+
input: z.object({
|
|
15
|
+
id: z.union([
|
|
16
|
+
z.string(),
|
|
17
|
+
z.number()
|
|
18
|
+
]).describe('The ID of the document to update').optional(),
|
|
19
|
+
data: z.record(z.string(), z.unknown()).describe('The fields to update'),
|
|
20
|
+
depth: z.number().describe('How many levels deep to populate relationships').optional().default(0),
|
|
21
|
+
draft: z.boolean().describe('Whether to update the document as a draft').optional().default(false),
|
|
22
|
+
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
23
|
+
filePath: z.string().describe('File path for file uploads').optional(),
|
|
24
|
+
locale: z.string().describe('Optional: locale code to update the document in (e.g., "en", "es"). Defaults to the default locale').optional(),
|
|
25
|
+
overrideLock: z.boolean().describe('Whether to override document locks').optional().default(true),
|
|
26
|
+
overwriteExistingFiles: z.boolean().describe('Whether to overwrite existing files').optional().default(false),
|
|
27
|
+
select: z.string().describe("Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'").optional(),
|
|
28
|
+
where: whereSchema.describe('Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}').optional()
|
|
29
|
+
})
|
|
35
30
|
}).handler(async ({ authorizedMCP, collectionSlug, input, req })=>{
|
|
36
31
|
const payload = req.payload;
|
|
37
32
|
const logger = getLogger({
|
|
@@ -50,25 +45,19 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
50
45
|
]
|
|
51
46
|
};
|
|
52
47
|
}
|
|
53
|
-
let parsedData = transformPointDataToPayload(data);
|
|
54
48
|
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
{
|
|
65
|
-
type: 'text',
|
|
66
|
-
text: 'Error: Invalid JSON in where clause'
|
|
67
|
-
}
|
|
68
|
-
]
|
|
69
|
-
};
|
|
70
|
-
}
|
|
49
|
+
const inputData = stripVirtualFields(data, virtualFieldNames);
|
|
50
|
+
const validationError = validateCollectionData({
|
|
51
|
+
collectionSlug,
|
|
52
|
+
data: inputData,
|
|
53
|
+
partial: true,
|
|
54
|
+
req
|
|
55
|
+
});
|
|
56
|
+
if (validationError) {
|
|
57
|
+
return validationError;
|
|
71
58
|
}
|
|
59
|
+
const parsedData = transformPointDataToPayload(inputData);
|
|
60
|
+
const whereClause = where ?? {};
|
|
72
61
|
let selectClause;
|
|
73
62
|
if (select) {
|
|
74
63
|
try {
|
|
@@ -86,7 +75,7 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
86
75
|
}
|
|
87
76
|
}
|
|
88
77
|
if (id) {
|
|
89
|
-
const
|
|
78
|
+
const result = await payload.update({
|
|
90
79
|
id,
|
|
91
80
|
collection: collectionSlug,
|
|
92
81
|
data: parsedData,
|
|
@@ -110,8 +99,7 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
110
99
|
...selectClause ? {
|
|
111
100
|
select: selectClause
|
|
112
101
|
} : {}
|
|
113
|
-
};
|
|
114
|
-
const result = await payload.update(updateOptions);
|
|
102
|
+
});
|
|
115
103
|
return {
|
|
116
104
|
content: [
|
|
117
105
|
{
|
|
@@ -122,15 +110,15 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
122
110
|
doc: result
|
|
123
111
|
};
|
|
124
112
|
}
|
|
125
|
-
const
|
|
113
|
+
const result = await payload.update({
|
|
126
114
|
collection: collectionSlug,
|
|
127
115
|
data: parsedData,
|
|
128
116
|
depth,
|
|
129
117
|
draft,
|
|
130
118
|
overrideLock,
|
|
131
119
|
req,
|
|
132
|
-
...localAPIDefaults(authorizedMCP),
|
|
133
120
|
where: whereClause,
|
|
121
|
+
...localAPIDefaults(authorizedMCP),
|
|
134
122
|
...filePath ? {
|
|
135
123
|
filePath
|
|
136
124
|
} : {},
|
|
@@ -146,17 +134,43 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
146
134
|
...selectClause ? {
|
|
147
135
|
select: selectClause
|
|
148
136
|
} : {}
|
|
149
|
-
};
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
const docs = bulkResult.docs || [];
|
|
153
|
-
const errors = bulkResult.errors || [];
|
|
137
|
+
});
|
|
138
|
+
const docs = result.docs || [];
|
|
139
|
+
const errors = result.errors || [];
|
|
154
140
|
let responseText = `Multiple documents updated in collection "${collectionSlug}"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`;
|
|
155
141
|
if (docs.length > 0) {
|
|
156
142
|
responseText += `\n\nUpdated documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``;
|
|
157
143
|
}
|
|
158
144
|
if (errors.length > 0) {
|
|
159
145
|
responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``;
|
|
146
|
+
const errorSchema = getCollectionInputSchema({
|
|
147
|
+
collectionSlug,
|
|
148
|
+
req
|
|
149
|
+
});
|
|
150
|
+
if (errorSchema) {
|
|
151
|
+
responseText += `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(errorSchema)}\n\`\`\``;
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
content: [
|
|
155
|
+
{
|
|
156
|
+
type: 'text',
|
|
157
|
+
text: responseText
|
|
158
|
+
}
|
|
159
|
+
],
|
|
160
|
+
doc: {
|
|
161
|
+
docs,
|
|
162
|
+
errors
|
|
163
|
+
},
|
|
164
|
+
isError: true,
|
|
165
|
+
...errorSchema ? {
|
|
166
|
+
structuredContent: {
|
|
167
|
+
collectionSlug,
|
|
168
|
+
docs,
|
|
169
|
+
errors,
|
|
170
|
+
schema: errorSchema
|
|
171
|
+
}
|
|
172
|
+
} : {}
|
|
173
|
+
};
|
|
160
174
|
}
|
|
161
175
|
return {
|
|
162
176
|
content: [
|
|
@@ -173,14 +187,12 @@ export const updateCollectionTool = defineCollectionTool({
|
|
|
173
187
|
} catch (error) {
|
|
174
188
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
175
189
|
logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`);
|
|
176
|
-
return {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
]
|
|
183
|
-
};
|
|
190
|
+
return formatCollectionError({
|
|
191
|
+
action: 'updating',
|
|
192
|
+
collectionSlug,
|
|
193
|
+
error,
|
|
194
|
+
req
|
|
195
|
+
});
|
|
184
196
|
}
|
|
185
197
|
});
|
|
186
198
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/collections/updateTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getCollectionVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\nimport { prepareCollectionSchema } from '../../../utils/schemaConversion/prepareCollectionSchema.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\n\nconst DEFAULT_DESCRIPTION = 'Update documents in a collection by ID or where clause.'\n\nexport const updateCollectionTool = defineCollectionTool({\n description: DEFAULT_DESCRIPTION,\n input: ({ collectionSchema }) => {\n const partialSchema = prepareCollectionSchema(collectionSchema)\n\n // Collection updates do not require all required fields to be passed => delete .required.\n //\n // Local API equivalent: packages/payload/src/collections/operations/local/update.ts#BaseOptions#data:\n // data: DeepPartial<RequiredDataFromCollectionSlug<TSlug>>\n delete partialSchema.required\n\n return z.object({\n id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),\n data: z\n .fromJSONSchema(partialSchema as unknown as z.core.JSONSchema.JSONSchema)\n .describe('The fields to update'),\n depth: z\n .number()\n .describe('How many levels deep to populate relationships')\n .optional()\n .default(0),\n draft: z\n .boolean()\n .describe('Whether to update the document as a draft')\n .optional()\n .default(false),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n filePath: z.string().describe('File path for file uploads').optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n )\n .optional(),\n overrideLock: z\n .boolean()\n .describe('Whether to override document locks')\n .optional()\n .default(true),\n overwriteExistingFiles: z\n .boolean()\n .describe('Whether to overwrite existing files')\n .optional()\n .default(false),\n select: z\n .string()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n )\n .optional(),\n where: z\n .string()\n .describe('JSON string for where clause to update multiple documents')\n .optional(),\n })\n },\n}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const {\n id,\n data,\n depth,\n draft,\n fallbackLocale,\n filePath,\n locale,\n overrideLock,\n overwriteExistingFiles,\n select,\n where,\n } = input\n\n logger.info(\n `Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n if (!id && !where) {\n return {\n content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],\n }\n }\n\n let parsedData = transformPointDataToPayload(data as Record<string, unknown>)\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)\n parsedData = stripVirtualFields(parsedData, virtualFieldNames)\n\n let whereClause: Record<string, unknown> = {}\n if (where) {\n try {\n whereClause = JSON.parse(where) as Record<string, unknown>\n } catch {\n logger.error(`Invalid where clause JSON: ${where}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch {\n logger.warn(`Invalid select clause JSON: ${select}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }\n }\n }\n\n if (id) {\n const updateOptions = {\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n ...localAPIDefaults(authorizedMCP),\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n }\n\n const result = await payload.update(updateOptions as any)\n\n return {\n content: [\n {\n type: 'text',\n text: `Document updated successfully in collection \"${collectionSlug}\"!\\nUpdated document:\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n }\n\n const updateOptions = {\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n ...localAPIDefaults(authorizedMCP),\n where: whereClause,\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n }\n\n const result = await payload.update(updateOptions as any)\n\n const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }\n const docs = bulkResult.docs || []\n const errors = bulkResult.errors || []\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\\nUpdated: ${docs.length} documents\\nErrors: ${errors.length}\\n---`\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\\n\\`\\`\\`json\\n${JSON.stringify(docs)}\\n\\`\\`\\``\n }\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\\n\\`\\`\\`json\\n${JSON.stringify(errors)}\\n\\`\\`\\``\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)\n return {\n content: [\n {\n type: 'text',\n text: `Error updating document in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n }\n})\n"],"names":["z","defineCollectionTool","getLogger","getCollectionVirtualFieldNames","stripVirtualFields","localAPIDefaults","prepareCollectionSchema","transformPointDataToPayload","DEFAULT_DESCRIPTION","updateCollectionTool","description","input","collectionSchema","partialSchema","required","object","id","union","string","number","describe","optional","data","fromJSONSchema","depth","default","draft","boolean","fallbackLocale","filePath","locale","overrideLock","overwriteExistingFiles","select","where","handler","authorizedMCP","collectionSlug","req","payload","logger","info","content","type","text","parsedData","virtualFieldNames","config","whereClause","JSON","parse","error","selectClause","warn","updateOptions","collection","result","update","stringify","doc","bulkResult","docs","errors","responseText","length","errorMessage","Error","message"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,oBAAoB,QAAQ,yBAAwB;AAC7D,SAASC,SAAS,QAAQ,8BAA6B;AACvD,SACEC,8BAA8B,EAC9BC,kBAAkB,QACb,yCAAwC;AAC/C,SAASC,gBAAgB,QAAQ,qCAAoC;AACrE,SAASC,uBAAuB,QAAQ,6DAA4D;AACpG,SAASC,2BAA2B,QAAQ,gDAA+C;AAE3F,MAAMC,sBAAsB;AAE5B,OAAO,MAAMC,uBAAuBR,qBAAqB;IACvDS,aAAaF;IACbG,OAAO,CAAC,EAAEC,gBAAgB,EAAE;QAC1B,MAAMC,gBAAgBP,wBAAwBM;QAE9C,0FAA0F;QAC1F,EAAE;QACF,sGAAsG;QACtG,2DAA2D;QAC3D,OAAOC,cAAcC,QAAQ;QAE7B,OAAOd,EAAEe,MAAM,CAAC;YACdC,IAAIhB,EAAEiB,KAAK,CAAC;gBAACjB,EAAEkB,MAAM;gBAAIlB,EAAEmB,MAAM;aAAG,EAAEC,QAAQ,CAAC,oCAAoCC,QAAQ;YAC3FC,MAAMtB,EACHuB,cAAc,CAACV,eACfO,QAAQ,CAAC;YACZI,OAAOxB,EACJmB,MAAM,GACNC,QAAQ,CAAC,kDACTC,QAAQ,GACRI,OAAO,CAAC;YACXC,OAAO1B,EACJ2B,OAAO,GACPP,QAAQ,CAAC,6CACTC,QAAQ,GACRI,OAAO,CAAC;YACXG,gBAAgB5B,EACbkB,MAAM,GACNE,QAAQ,CAAC,gFACTC,QAAQ;YACXQ,UAAU7B,EAAEkB,MAAM,GAAGE,QAAQ,CAAC,8BAA8BC,QAAQ;YACpES,QAAQ9B,EACLkB,MAAM,GACNE,QAAQ,CACP,sGAEDC,QAAQ;YACXU,cAAc/B,EAC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;YAC5D,GAAIF,SAAS;gBAAEA;YAAO,IAAI,CAAC,CAAC;YAC5B,GAAIF,iBAAiB;gBAAEA;YAAe,IAAI,CAAC,CAAC;YAC5C,GAAIwB,eAAe;gBAAEnB,QAAQmB;YAAa,IAAI,CAAC,CAAC;QAClD;QAEA,MAAMI,SAAS,MAAMjB,QAAQkB,MAAM,CAACH;QAEpC,MAAMM,aAAaJ;QACnB,MAAMK,OAAOD,WAAWC,IAAI,IAAI,EAAE;QAClC,MAAMC,SAASF,WAAWE,MAAM,IAAI,EAAE;QAEtC,IAAIC,eAAe,CAAC,0CAA0C,EAAE1B,eAAe,aAAa,EAAEwB,KAAKG,MAAM,CAAC,oBAAoB,EAAEF,OAAOE,MAAM,CAAC,KAAK,CAAC;QACpJ,IAAIH,KAAKG,MAAM,GAAG,GAAG;YACnBD,gBAAgB,CAAC,oCAAoC,EAAEd,KAAKS,SAAS,CAACG,MAAM,QAAQ,CAAC;QACvF;QACA,IAAIC,OAAOE,MAAM,GAAG,GAAG;YACrBD,gBAAgB,CAAC,yBAAyB,EAAEd,KAAKS,SAAS,CAACI,QAAQ,QAAQ,CAAC;QAC9E;QAEA,OAAO;YACLpB,SAAS;gBAAC;oBAAEC,MAAM;oBAAQC,MAAMmB;gBAAa;aAAE;YAC/CJ,KAAK;gBAAEE;gBAAMC;YAAO;QACtB;IACF,EAAE,OAAOX,OAAO;QACd,MAAMc,eAAed,iBAAiBe,QAAQf,MAAMgB,OAAO,GAAG;QAC9D3B,OAAOW,KAAK,CAAC,CAAC,2BAA2B,EAAEd,eAAe,EAAE,EAAE4B,cAAc;QAC5E,OAAO;YACLvB,SAAS;gBACP;oBACEC,MAAM;oBACNC,MAAM,CAAC,uCAAuC,EAAEP,eAAe,GAAG,EAAE4B,cAAc;gBACpF;aACD;QACH;IACF;AACF,GAAE"}
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/collections/updateTool.ts"],"sourcesContent":["import type { SelectType, Where } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineCollectionTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getCollectionVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\nimport { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { whereSchema } from '../../../utils/whereSchema.js'\nimport { validateCollectionData } from '../validateEntityData.js'\nimport { formatCollectionError } from './formatCollectionError.js'\n\nconst DEFAULT_DESCRIPTION =\n 'Update documents in any collection by passing the collection slug and data.'\n\nexport const updateDocumentTool = defineCollectionTool({\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n id: z.union([z.string(), z.number()]).describe('The ID of the document to update').optional(),\n data: z.record(z.string(), z.unknown()).describe('The fields to update'),\n depth: z\n .number()\n .describe('How many levels deep to populate relationships')\n .optional()\n .default(0),\n draft: z\n .boolean()\n .describe('Whether to update the document as a draft')\n .optional()\n .default(false),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n filePath: z.string().describe('File path for file uploads').optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n )\n .optional(),\n overrideLock: z\n .boolean()\n .describe('Whether to override document locks')\n .optional()\n .default(true),\n overwriteExistingFiles: z\n .boolean()\n .describe('Whether to overwrite existing files')\n .optional()\n .default(false),\n select: z\n .string()\n .describe(\n \"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\\\"title\\\": true}'\",\n )\n .optional(),\n where: whereSchema\n .describe(\n 'Where clause to update multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {\"title\":{\"contains\":\"test\"}}',\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const {\n id,\n data,\n depth,\n draft,\n fallbackLocale,\n filePath,\n locale,\n overrideLock,\n overwriteExistingFiles,\n select,\n where,\n } = input\n\n logger.info(\n `Updating document in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n if (!id && !where) {\n return {\n content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],\n }\n }\n\n const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)\n const inputData = stripVirtualFields(data, virtualFieldNames)\n const validationError = validateCollectionData({\n collectionSlug,\n data: inputData,\n partial: true,\n req,\n })\n\n if (validationError) {\n return validationError\n }\n\n const parsedData = transformPointDataToPayload(inputData)\n\n const whereClause: Where = where ?? {}\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch {\n logger.warn(`Invalid select clause JSON: ${select}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }\n }\n }\n\n if (id) {\n const result = await payload.update({\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n ...localAPIDefaults(authorizedMCP),\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n })\n\n return {\n content: [\n {\n type: 'text',\n text: `Document updated successfully in collection \"${collectionSlug}\"!\\nUpdated document:\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n }\n\n const result = await payload.update({\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideLock,\n req,\n where: whereClause,\n ...localAPIDefaults(authorizedMCP),\n ...(filePath ? { filePath } : {}),\n ...(overwriteExistingFiles ? { overwriteExistingFiles } : {}),\n ...(locale ? { locale } : {}),\n ...(fallbackLocale ? { fallbackLocale } : {}),\n ...(selectClause ? { select: selectClause } : {}),\n })\n\n const docs = result.docs || []\n const errors = result.errors || []\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\\nUpdated: ${docs.length} documents\\nErrors: ${errors.length}\\n---`\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\\n\\`\\`\\`json\\n${JSON.stringify(docs)}\\n\\`\\`\\``\n }\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\\n\\`\\`\\`json\\n${JSON.stringify(errors)}\\n\\`\\`\\``\n\n const errorSchema = getCollectionInputSchema({ collectionSlug, req })\n\n if (errorSchema) {\n responseText += `\\n\\nUse this schema for data:\\n\\`\\`\\`json\\n${JSON.stringify(errorSchema)}\\n\\`\\`\\``\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n isError: true,\n ...(errorSchema\n ? {\n structuredContent: {\n collectionSlug,\n docs,\n errors,\n schema: errorSchema,\n },\n }\n : {}),\n }\n }\n\n return {\n content: [{ type: 'text', text: responseText }],\n doc: { docs, errors } as unknown as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error updating document in ${collectionSlug}: ${errorMessage}`)\n return formatCollectionError({ action: 'updating', collectionSlug, error, req })\n }\n})\n"],"names":["z","defineCollectionTool","getLogger","getCollectionVirtualFieldNames","stripVirtualFields","localAPIDefaults","getCollectionInputSchema","transformPointDataToPayload","whereSchema","validateCollectionData","formatCollectionError","DEFAULT_DESCRIPTION","updateDocumentTool","description","input","object","id","union","string","number","describe","optional","data","record","unknown","depth","default","draft","boolean","fallbackLocale","filePath","locale","overrideLock","overwriteExistingFiles","select","where","handler","authorizedMCP","collectionSlug","req","payload","logger","info","content","type","text","virtualFieldNames","config","inputData","validationError","partial","parsedData","whereClause","selectClause","JSON","parse","warn","result","update","collection","stringify","doc","docs","errors","responseText","length","errorSchema","isError","struct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ts","sourceRoot":"","sources":["../../../src/mcp/builtin/getConfigInfoTool.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,iBAAiB,+BAuC5B,CAAA"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getAccessResults, isEntityHidden } from 'payload';
|
|
2
|
+
import { defineTool } from '../../defineTool.js';
|
|
3
|
+
export const getConfigInfoTool = defineTool({
|
|
4
|
+
description: 'List the Payload collection and global slugs visible to this MCP client.'
|
|
5
|
+
}).handler(async ({ authorizedMCP, req })=>{
|
|
6
|
+
const user = authorizedMCP.user ?? req.user ?? null;
|
|
7
|
+
const permissions = user ? await getAccessResults({
|
|
8
|
+
req: {
|
|
9
|
+
...req,
|
|
10
|
+
user
|
|
11
|
+
}
|
|
12
|
+
}) : null;
|
|
13
|
+
const collections = [];
|
|
14
|
+
const globals = [];
|
|
15
|
+
for (const collection of req.payload.config.collections){
|
|
16
|
+
if (user && isEntityHidden({
|
|
17
|
+
hidden: collection.admin.hidden,
|
|
18
|
+
user
|
|
19
|
+
})) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
if (user && !permissions?.collections?.[collection.slug]?.read) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
collections.push(collection.slug);
|
|
26
|
+
}
|
|
27
|
+
for (const global of req.payload.config.globals){
|
|
28
|
+
if (user && isEntityHidden({
|
|
29
|
+
hidden: global.admin.hidden,
|
|
30
|
+
user
|
|
31
|
+
})) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (user && !permissions?.globals?.[global.slug]?.read) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
globals.push(global.slug);
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
content: [
|
|
41
|
+
{
|
|
42
|
+
type: 'text',
|
|
43
|
+
text: `Collections: ${collections.join(', ') || 'none'}\nGlobals: ${globals.join(', ') || 'none'}`
|
|
44
|
+
}
|
|
45
|
+
]
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
//# sourceMappingURL=getConfigInfoTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/mcp/builtin/getConfigInfoTool.ts"],"sourcesContent":["import { getAccessResults, isEntityHidden } from 'payload'\n\nimport { defineTool } from '../../defineTool.js'\n\nexport const getConfigInfoTool = defineTool({\n description: 'List the Payload collection and global slugs visible to this MCP client.',\n}).handler(async ({ authorizedMCP, req }) => {\n const user = authorizedMCP.user ?? req.user ?? null\n const permissions = user ? await getAccessResults({ req: { ...req, user } }) : null\n\n const collections: string[] = []\n const globals: string[] = []\n\n for (const collection of req.payload.config.collections) {\n if (user && isEntityHidden({ hidden: collection.admin.hidden, user })) {\n continue\n }\n if (user && !permissions?.collections?.[collection.slug]?.read) {\n continue\n }\n\n collections.push(collection.slug)\n }\n\n for (const global of req.payload.config.globals) {\n if (user && isEntityHidden({ hidden: global.admin.hidden, user })) {\n continue\n }\n if (user && !permissions?.globals?.[global.slug]?.read) {\n continue\n }\n\n globals.push(global.slug)\n }\n\n return {\n content: [\n {\n type: 'text',\n text: `Collections: ${collections.join(', ') || 'none'}\\nGlobals: ${globals.join(', ') || 'none'}`,\n },\n ],\n }\n})\n"],"names":["getAccessResults","isEntityHidden","defineTool","getConfigInfoTool","description","handler","authorizedMCP","req","user","permissions","collections","globals","collection","payload","config","hidden","admin","slug","read","push","global","content","type","text","join"],"mappings":"AAAA,SAASA,gBAAgB,EAAEC,cAAc,QAAQ,UAAS;AAE1D,SAASC,UAAU,QAAQ,sBAAqB;AAEhD,OAAO,MAAMC,oBAAoBD,WAAW;IAC1CE,aAAa;AACf,GAAGC,OAAO,CAAC,OAAO,EAAEC,aAAa,EAAEC,GAAG,EAAE;IACtC,MAAMC,OAAOF,cAAcE,IAAI,IAAID,IAAIC,IAAI,IAAI;IAC/C,MAAMC,cAAcD,OAAO,MAAMR,iBAAiB;QAAEO,KAAK;YAAE,GAAGA,GAAG;YAAEC;QAAK;IAAE,KAAK;IAE/E,MAAME,cAAwB,EAAE;IAChC,MAAMC,UAAoB,EAAE;IAE5B,KAAK,MAAMC,cAAcL,IAAIM,OAAO,CAACC,MAAM,CAACJ,WAAW,CAAE;QACvD,IAAIF,QAAQP,eAAe;YAAEc,QAAQH,WAAWI,KAAK,CAACD,MAAM;YAAEP;QAAK,IAAI;YACrE;QACF;QACA,IAAIA,QAAQ,CAACC,aAAaC,aAAa,CAACE,WAAWK,IAAI,CAAC,EAAEC,MAAM;YAC9D;QACF;QAEAR,YAAYS,IAAI,CAACP,WAAWK,IAAI;IAClC;IAEA,KAAK,MAAMG,UAAUb,IAAIM,OAAO,CAACC,MAAM,CAACH,OAAO,CAAE;QAC/C,IAAIH,QAAQP,eAAe;YAAEc,QAAQK,OAAOJ,KAAK,CAACD,MAAM;YAAEP;QAAK,IAAI;YACjE;QACF;QACA,IAAIA,QAAQ,CAACC,aAAaE,SAAS,CAACS,OAAOH,IAAI,CAAC,EAAEC,MAAM;YACtD;QACF;QAEAP,QAAQQ,IAAI,CAACC,OAAOH,IAAI;IAC1B;IAEA,OAAO;QACLI,SAAS;YACP;gBACEC,MAAM;gBACNC,MAAM,CAAC,aAAa,EAAEb,YAAYc,IAAI,CAAC,SAAS,OAAO,WAAW,EAAEb,QAAQa,IAAI,CAAC,SAAS,QAAQ;YACpG;SACD;IACH;AACF,GAAE"}
|
|
@@ -2,7 +2,7 @@ import { z } from 'zod';
|
|
|
2
2
|
import { defineGlobalTool } from '../../../defineTool.js';
|
|
3
3
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
4
4
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js';
|
|
5
|
-
const DEFAULT_DESCRIPTION = 'Find
|
|
5
|
+
const DEFAULT_DESCRIPTION = 'Find any Payload global by passing the global slug.';
|
|
6
6
|
export const findGlobalTool = defineGlobalTool({
|
|
7
7
|
description: DEFAULT_DESCRIPTION,
|
|
8
8
|
input: z.object({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/globals/findTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineGlobalTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\n\nconst DEFAULT_DESCRIPTION = 'Find
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/globals/findTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineGlobalTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\n\nconst DEFAULT_DESCRIPTION = 'Find any Payload global by passing the global slug.'\n\nexport const findGlobalTool = defineGlobalTool({\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n depth: z\n .number()\n .int()\n .min(0)\n .max(10)\n .describe('Depth of population for relationships')\n .optional()\n .default(0),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to retrieve data in (e.g., \"en\", \"es\"). Use \"all\" to retrieve all locales for localized fields',\n )\n .optional(),\n select: z\n .string()\n .describe(\n \"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\\\"title\\\": true}'\",\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, globalSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const { depth, fallbackLocale, locale, select } = input\n\n logger.info(\n `Reading global: ${globalSlug}, depth: ${depth}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n const findOptions: Parameters<typeof payload.findGlobal>[0] = {\n slug: globalSlug,\n depth,\n ...localAPIDefaults(authorizedMCP),\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch {\n logger.warn(`Invalid select clause JSON for global: ${select}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }\n }\n }\n\n if (locale) {\n findOptions.locale = locale\n }\n if (fallbackLocale) {\n findOptions.fallbackLocale = fallbackLocale\n }\n if (selectClause) {\n findOptions.select = selectClause\n }\n\n const result = await payload.findGlobal(findOptions)\n\n return {\n content: [\n {\n type: 'text',\n text: `Global \"${globalSlug}\":\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error reading global ${globalSlug}: ${errorMessage}`)\n return {\n content: [\n { type: 'text', text: `❌ **Error reading global \"${globalSlug}\":** ${errorMessage}` },\n ],\n }\n }\n})\n"],"names":["z","defineGlobalTool","getLogger","localAPIDefaults","DEFAULT_DESCRIPTION","findGlobalTool","description","input","object","depth","number","int","min","max","describe","optional","default","fallbackLocale","string","locale","select","handler","authorizedMCP","globalSlug","req","payload","logger","info","findOptions","slug","selectClause","JSON","parse","warn","content","type","text","result","findGlobal","stringify","doc","error","errorMessage","Error","message"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,SAAS,QAAQ,8BAA6B;AACvD,SAASC,gBAAgB,QAAQ,qCAAoC;AAErE,MAAMC,sBAAsB;AAE5B,OAAO,MAAMC,iBAAiBJ,iBAAiB;IAC7CK,aAAaF;IACbG,OAAOP,EAAEQ,MAAM,CAAC;QACdC,OAAOT,EACJU,MAAM,GACNC,GAAG,GACHC,GAAG,CAAC,GACJC,GAAG,CAAC,IACJC,QAAQ,CAAC,yCACTC,QAAQ,GACRC,OAAO,CAAC;QACXC,gBAAgBjB,EACbkB,MAAM,GACNJ,QAAQ,CAAC,gFACTC,QAAQ;QACXI,QAAQnB,EACLkB,MAAM,GACNJ,QAAQ,CACP,wHAEDC,QAAQ;QACX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AG;QAC9DlB,OAAOe,KAAK,CAAC,CAAC,qBAAqB,EAAElB,WAAW,EAAE,EAAEmB,cAAc;QAClE,OAAO;YACLR,SAAS;gBACP;oBAAEC,MAAM;oBAAQC,MAAM,CAAC,0BAA0B,EAAEb,WAAW,KAAK,EAAEmB,cAAc;gBAAC;aACrF;QACH;IACF;AACF,GAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getGlobalSchemaTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/getGlobalSchemaTool.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,mBAAmB,wCAwB9B,CAAA"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { defineGlobalTool } from '../../../defineTool.js';
|
|
2
|
+
import { getGlobalInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js';
|
|
3
|
+
export const getGlobalSchemaTool = defineGlobalTool({
|
|
4
|
+
description: 'Get the input schema for updating a global.'
|
|
5
|
+
}).handler(({ globalSlug, req })=>{
|
|
6
|
+
const inputSchema = getGlobalInputSchema({
|
|
7
|
+
globalSlug,
|
|
8
|
+
req
|
|
9
|
+
});
|
|
10
|
+
if (!inputSchema) {
|
|
11
|
+
return {
|
|
12
|
+
content: [
|
|
13
|
+
{
|
|
14
|
+
type: 'text',
|
|
15
|
+
text: `Error: Global "${globalSlug}" not found`
|
|
16
|
+
}
|
|
17
|
+
],
|
|
18
|
+
isError: true
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
content: [
|
|
23
|
+
{
|
|
24
|
+
type: 'text',
|
|
25
|
+
text: `Schema for global "${globalSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
structuredContent: {
|
|
29
|
+
globalSlug,
|
|
30
|
+
schema: inputSchema
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=getGlobalSchemaTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/globals/getGlobalSchemaTool.ts"],"sourcesContent":["import { defineGlobalTool } from '../../../defineTool.js'\nimport { getGlobalInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'\n\nexport const getGlobalSchemaTool = defineGlobalTool({\n description: 'Get the input schema for updating a global.',\n}).handler(({ globalSlug, req }) => {\n const inputSchema = getGlobalInputSchema({ globalSlug, req })\n\n if (!inputSchema) {\n return {\n content: [{ type: 'text', text: `Error: Global \"${globalSlug}\" not found` }],\n isError: true,\n }\n }\n\n return {\n content: [\n {\n type: 'text',\n text: `Schema for global \"${globalSlug}\":\\n\\`\\`\\`json\\n${JSON.stringify(inputSchema)}\\n\\`\\`\\``,\n },\n ],\n structuredContent: {\n globalSlug,\n schema: inputSchema,\n },\n }\n})\n"],"names":["defineGlobalTool","getGlobalInputSchema","getGlobalSchemaTool","description","handler","globalSlug","req","inputSchema","content","type","text","isError","JSON","stringify","structuredContent","schema"],"mappings":"AAAA,SAASA,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,oBAAoB,QAAQ,0DAAyD;AAE9F,OAAO,MAAMC,sBAAsBF,iBAAiB;IAClDG,aAAa;AACf,GAAGC,OAAO,CAAC,CAAC,EAAEC,UAAU,EAAEC,GAAG,EAAE;IAC7B,MAAMC,cAAcN,qBAAqB;QAAEI;QAAYC;IAAI;IAE3D,IAAI,CAACC,aAAa;QAChB,OAAO;YACLC,SAAS;gBAAC;oBAAEC,MAAM;oBAAQC,MAAM,CAAC,eAAe,EAAEL,WAAW,WAAW,CAAC;gBAAC;aAAE;YAC5EM,SAAS;QACX;IACF;IAEA,OAAO;QACLH,SAAS;YACP;gBACEC,MAAM;gBACNC,MAAM,CAAC,mBAAmB,EAAEL,WAAW,gBAAgB,EAAEO,KAAKC,SAAS,CAACN,aAAa,QAAQ,CAAC;YAChG;SACD;QACDO,mBAAmB;YACjBT;YACAU,QAAQR;QACV;IACF;AACF,GAAE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/updateTool.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"updateTool.d.ts","sourceRoot":"","sources":["../../../../src/mcp/builtin/globals/updateTool.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,gBAAgB,wCAkG3B,CAAA"}
|
|
@@ -3,26 +3,19 @@ import { defineGlobalTool } from '../../../defineTool.js';
|
|
|
3
3
|
import { getLogger } from '../../../utils/getLogger.js';
|
|
4
4
|
import { getGlobalVirtualFieldNames, stripVirtualFields } from '../../../utils/getVirtualFieldNames.js';
|
|
5
5
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js';
|
|
6
|
-
import {
|
|
7
|
-
|
|
6
|
+
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
|
|
7
|
+
import { validateGlobalData } from '../validateEntityData.js';
|
|
8
|
+
const DEFAULT_DESCRIPTION = 'Update any Payload global by passing the global slug and data.';
|
|
8
9
|
export const updateGlobalTool = defineGlobalTool({
|
|
9
10
|
description: DEFAULT_DESCRIPTION,
|
|
10
|
-
input: ({
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
data: z.fromJSONSchema(partialSchema).describe('The fields to update'),
|
|
19
|
-
depth: z.number().describe('Optional: Depth of relationships to populate').optional().default(0),
|
|
20
|
-
draft: z.boolean().describe('Optional: Whether to save as draft (default: false)').optional().default(false),
|
|
21
|
-
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
22
|
-
locale: z.string().describe('Optional: locale code to update data in (e.g., "en", "es"). Use "all" to update all locales for localized fields').optional(),
|
|
23
|
-
select: z.string().describe('Optional: define exactly which fields you\'d like to return in the response (JSON), e.g., \'{"siteName": "My Site"}\'').optional()
|
|
24
|
-
});
|
|
25
|
-
}
|
|
11
|
+
input: z.object({
|
|
12
|
+
data: z.record(z.string(), z.unknown()).describe('The global fields to update'),
|
|
13
|
+
depth: z.number().describe('Optional: Depth of relationships to populate').optional().default(0),
|
|
14
|
+
draft: z.boolean().describe('Optional: Whether to save as draft (default: false)').optional().default(false),
|
|
15
|
+
fallbackLocale: z.string().describe('Optional: fallback locale code to use when requested locale is not available').optional(),
|
|
16
|
+
locale: z.string().describe('Optional: locale code to update data in (e.g., "en", "es"). Use "all" to update all locales for localized fields').optional(),
|
|
17
|
+
select: z.string().describe("Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"siteName\": true}'").optional()
|
|
18
|
+
})
|
|
26
19
|
}).handler(async ({ authorizedMCP, globalSlug, input, req })=>{
|
|
27
20
|
const payload = req.payload;
|
|
28
21
|
const logger = getLogger({
|
|
@@ -32,7 +25,16 @@ export const updateGlobalTool = defineGlobalTool({
|
|
|
32
25
|
logger.info(`Updating global: ${globalSlug}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`);
|
|
33
26
|
try {
|
|
34
27
|
const virtualFieldNames = getGlobalVirtualFieldNames(payload.config, globalSlug);
|
|
35
|
-
const
|
|
28
|
+
const inputData = stripVirtualFields(data, virtualFieldNames);
|
|
29
|
+
const validationError = validateGlobalData({
|
|
30
|
+
data: inputData,
|
|
31
|
+
globalSlug,
|
|
32
|
+
req
|
|
33
|
+
});
|
|
34
|
+
if (validationError) {
|
|
35
|
+
return validationError;
|
|
36
|
+
}
|
|
37
|
+
const parsedData = transformPointDataToPayload(inputData);
|
|
36
38
|
let selectClause;
|
|
37
39
|
if (select) {
|
|
38
40
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/mcp/builtin/globals/updateTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineGlobalTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getGlobalVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../../../../src/mcp/builtin/globals/updateTool.ts"],"sourcesContent":["import type { SelectType } from 'payload'\n\nimport { z } from 'zod'\n\nimport { defineGlobalTool } from '../../../defineTool.js'\nimport { getLogger } from '../../../utils/getLogger.js'\nimport {\n getGlobalVirtualFieldNames,\n stripVirtualFields,\n} from '../../../utils/getVirtualFieldNames.js'\nimport { localAPIDefaults } from '../../../utils/localAPIDefaults.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { validateGlobalData } from '../validateEntityData.js'\n\nconst DEFAULT_DESCRIPTION = 'Update any Payload global by passing the global slug and data.'\n\nexport const updateGlobalTool = defineGlobalTool({\n description: DEFAULT_DESCRIPTION,\n input: z.object({\n data: z.record(z.string(), z.unknown()).describe('The global fields to update'),\n depth: z\n .number()\n .describe('Optional: Depth of relationships to populate')\n .optional()\n .default(0),\n draft: z\n .boolean()\n .describe('Optional: Whether to save as draft (default: false)')\n .optional()\n .default(false),\n fallbackLocale: z\n .string()\n .describe('Optional: fallback locale code to use when requested locale is not available')\n .optional(),\n locale: z\n .string()\n .describe(\n 'Optional: locale code to update data in (e.g., \"en\", \"es\"). Use \"all\" to update all locales for localized fields',\n )\n .optional(),\n select: z\n .string()\n .describe(\n \"Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\\\"siteName\\\": true}'\",\n )\n .optional(),\n }),\n}).handler(async ({ authorizedMCP, globalSlug, input, req }) => {\n const payload = req.payload\n const logger = getLogger({ payload })\n\n const { data, depth, draft, fallbackLocale, locale, select } = input\n\n logger.info(\n `Updating global: ${globalSlug}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n\n try {\n const virtualFieldNames = getGlobalVirtualFieldNames(payload.config, globalSlug)\n const inputData = stripVirtualFields(data, virtualFieldNames)\n const validationError = validateGlobalData({ data: inputData, globalSlug, req })\n\n if (validationError) {\n return validationError\n }\n\n const parsedData = transformPointDataToPayload(inputData)\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch {\n logger.warn(`Invalid select clause JSON for global: ${select}`)\n return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }\n }\n }\n\n const updateOptions: Parameters<typeof payload.updateGlobal>[0] = {\n slug: globalSlug,\n data: parsedData,\n depth,\n draft,\n ...localAPIDefaults(authorizedMCP),\n }\n\n if (locale) {\n updateOptions.locale = locale\n }\n if (fallbackLocale) {\n updateOptions.fallbackLocale = fallbackLocale\n }\n if (selectClause) {\n updateOptions.select = selectClause\n }\n\n const result = await payload.updateGlobal(updateOptions)\n\n return {\n content: [\n {\n type: 'text',\n text: `Global \"${globalSlug}\" updated successfully!\\n\\`\\`\\`json\\n${JSON.stringify(result)}\\n\\`\\`\\``,\n },\n ],\n doc: result as Record<string, unknown>,\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n logger.error(`Error updating global ${globalSlug}: ${errorMessage}`)\n return {\n content: [{ type: 'text', text: `Error updating global \"${globalSlug}\": ${errorMessage}` }],\n }\n }\n})\n"],"names":["z","defineGlobalTool","getLogger","getGlobalVirtualFieldNames","stripVirtualFields","localAPIDefaults","transformPointDataToPayload","validateGlobalData","DEFAULT_DESCRIPTION","updateGlobalTool","description","input","object","data","record","string","unknown","describe","depth","number","optional","default","draft","boolean","fallbackLocale","locale","select","handler","authorizedMCP","globalSlug","req","payload","logger","info","virtualFieldNames","config","inputData","validationError","parsedData","selectClause","JSON","parse","warn","content","type","text","updateOptions","slug","result","updateGlobal","stringify","doc","error","errorMessage","Error","message"],"mappings":"AAEA,SAASA,CAAC,QAAQ,MAAK;AAEvB,SAASC,gBAAgB,QAAQ,yBAAwB;AACzD,SAASC,SAAS,QAAQ,8BAA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loadRequest } from 'payload';
|
|
2
|
+
import type { MCPToolResponse } from '../../types.js';
|
|
3
|
+
export declare const validateCollectionData: ({ collectionSlug, data, partial, req, }: {
|
|
4
|
+
collectionSlug: CollectionSlug;
|
|
5
|
+
data: Record<string, unknown>;
|
|
6
|
+
partial?: boolean;
|
|
7
|
+
req: PayloadRequest;
|
|
8
|
+
}) => MCPToolResponse | null;
|
|
9
|
+
export declare const validateGlobalData: ({ data, globalSlug, req, }: {
|
|
10
|
+
data: Record<string, unknown>;
|
|
11
|
+
globalSlug: GlobalSlug;
|
|
12
|
+
req: PayloadRequest;
|
|
13
|
+
}) => MCPToolResponse | null;
|
|
14
|
+
//# sourceMappingURL=validateEntityData.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validateEntityData.d.ts","sourceRoot":"","sources":["../../../src/mcp/builtin/validateEntityData.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,cAAc,EAAmB,MAAM,SAAS,CAAA;AAI1F,OAAO,KAAK,EAAkB,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAuBrE,eAAO,MAAM,sBAAsB,GAAI,yCAKpC;IACD,cAAc,EAAE,cAAc,CAAA;IAC9B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,eAAe,GAAG,IAQlB,CAAA;AAEJ,eAAO,MAAM,kBAAkB,GAAI,4BAIhC;IACD,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,UAAU,EAAE,UAAU,CAAA;IACtB,GAAG,EAAE,cAAc,CAAA;CACpB,KAAG,eAAe,GAAG,IAQlB,CAAA"}
|