@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,29 +1,21 @@
|
|
|
1
1
|
import { McpServer, type ServerContext } from '@modelcontextprotocol/server'
|
|
2
|
-
import { APIError,
|
|
2
|
+
import { APIError, type PayloadRequest } from 'payload'
|
|
3
|
+
import { z } from 'zod'
|
|
3
4
|
|
|
4
5
|
import type {
|
|
5
6
|
AuthorizedMCP,
|
|
7
|
+
CollectionMCPItem,
|
|
8
|
+
GlobalMCPItem,
|
|
6
9
|
JsonSchemaType,
|
|
7
10
|
MCPResponseOverride,
|
|
8
11
|
MCPToolResponse,
|
|
9
12
|
SanitizedMCPPluginConfig,
|
|
13
|
+
ToolInputSchema,
|
|
10
14
|
} from '../types.js'
|
|
11
15
|
|
|
12
|
-
import { toCamelCase } from '../utils/camelCase.js'
|
|
13
16
|
import { getLogger } from '../utils/getLogger.js'
|
|
14
|
-
import {
|
|
15
|
-
getCollectionVirtualFieldNames,
|
|
16
|
-
getGlobalVirtualFieldNames,
|
|
17
|
-
} from '../utils/getVirtualFieldNames.js'
|
|
18
|
-
import { removeVirtualFieldsFromSchema } from '../utils/schemaConversion/removeVirtualFieldsFromSchema.js'
|
|
19
17
|
import { toStandardSchema } from '../utils/toStandardSchema.js'
|
|
20
18
|
|
|
21
|
-
/** `findPosts`, `updateSiteSettings` — auto-prefixed wire name for collection/global tools. */
|
|
22
|
-
const wireName = (key: string, slug: string): string => {
|
|
23
|
-
const camel = toCamelCase(slug)
|
|
24
|
-
return `${key}${camel.charAt(0).toUpperCase()}${camel.slice(1)}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
19
|
/**
|
|
28
20
|
* Transport-agnostic core: registers every authorized MCP item onto a fresh
|
|
29
21
|
* `McpServer` and returns it. The caller is responsible for picking a transport
|
|
@@ -64,96 +56,100 @@ export const buildMcpServer = ({
|
|
|
64
56
|
return rest
|
|
65
57
|
}
|
|
66
58
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Runs a collection/global tool call:
|
|
61
|
+
* - reads `collectionSlug` / `globalSlug` from the input
|
|
62
|
+
* - runs access control: errors if `authorizedMCP.items` has no entry for this tool + slug
|
|
63
|
+
* - runs the tool handler and finalizes its response
|
|
64
|
+
*/
|
|
65
|
+
const callEntityTool = async ({
|
|
66
|
+
input,
|
|
67
|
+
item,
|
|
68
|
+
serverContext,
|
|
69
|
+
}: {
|
|
70
|
+
input: unknown
|
|
71
|
+
item: CollectionMCPItem | GlobalMCPItem
|
|
72
|
+
serverContext: ServerContext
|
|
73
|
+
}): Promise<MCPToolResponse> => {
|
|
74
|
+
const entity = item.type === 'collectionTool' ? 'collection' : 'global'
|
|
75
|
+
const slugKey = item.type === 'collectionTool' ? 'collectionSlug' : 'globalSlug'
|
|
76
|
+
const toolInput = (input ?? {}) as Record<string, unknown>
|
|
77
|
+
const slug = toolInput[slugKey] as string | undefined
|
|
78
|
+
|
|
79
|
+
if (!slug) {
|
|
80
|
+
return {
|
|
81
|
+
content: [
|
|
82
|
+
{
|
|
83
|
+
type: 'text',
|
|
84
|
+
text: `Error: "${item.mcpName}" requires ${slugKey}. Use getConfigInfo to inspect ${entity} slugs.`,
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
isError: true,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const match = authorizedMCP.items.find(
|
|
92
|
+
(candidate): candidate is CollectionMCPItem | GlobalMCPItem =>
|
|
93
|
+
candidate.type === item.type &&
|
|
94
|
+
candidate.mcpName === item.mcpName &&
|
|
95
|
+
(candidate.type === 'collectionTool'
|
|
96
|
+
? candidate.collectionSlug === slug
|
|
97
|
+
: candidate.type === 'globalTool' && candidate.globalSlug === slug),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
if (!match) {
|
|
101
|
+
return {
|
|
102
|
+
content: [
|
|
103
|
+
{
|
|
104
|
+
type: 'text',
|
|
105
|
+
text: `Error: MCP access to "${item.mcpName}" is not enabled for ${entity} "${slug}"`,
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
isError: true,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const handlerArgs = { authorizedMCP, input: toolInput, req, serverContext }
|
|
113
|
+
const response = await (match.type === 'collectionTool'
|
|
114
|
+
? match.tool.handler({ ...handlerArgs, collectionSlug: slug })
|
|
115
|
+
: match.tool.handler({ ...handlerArgs, globalSlug: slug }))
|
|
116
|
+
|
|
117
|
+
return finalizeToolResponse(response, match.tool.overrideResponse)
|
|
118
|
+
}
|
|
73
119
|
|
|
74
120
|
try {
|
|
121
|
+
const registeredEntityTools = new Set<string>()
|
|
122
|
+
|
|
75
123
|
for (const item of authorizedMCP.items) {
|
|
76
124
|
switch (item.type) {
|
|
77
|
-
case 'collectionTool':
|
|
78
|
-
const tool = item.tool
|
|
79
|
-
const name = wireName(item.key, item.collectionSlug)
|
|
80
|
-
let inputSchema = tool.input
|
|
81
|
-
if (typeof inputSchema === 'function') {
|
|
82
|
-
const raw = configSchema.$defs?.[item.collectionSlug]
|
|
83
|
-
if (!raw) {
|
|
84
|
-
throw new APIError(
|
|
85
|
-
`Collection schema not found for slug: ${item.collectionSlug}`,
|
|
86
|
-
500,
|
|
87
|
-
)
|
|
88
|
-
}
|
|
89
|
-
const collectionSchema = removeVirtualFieldsFromSchema(
|
|
90
|
-
JSON.parse(JSON.stringify(raw)) as JsonSchemaType,
|
|
91
|
-
getCollectionVirtualFieldNames(req.payload.config, item.collectionSlug),
|
|
92
|
-
)
|
|
93
|
-
inputSchema = inputSchema({ collectionSchema })
|
|
94
|
-
}
|
|
95
|
-
server.registerTool(
|
|
96
|
-
name,
|
|
97
|
-
{
|
|
98
|
-
description: tool.description,
|
|
99
|
-
inputSchema: inputSchema ? toStandardSchema(inputSchema) : undefined,
|
|
100
|
-
},
|
|
101
|
-
async (input: unknown, ctx: ServerContext) =>
|
|
102
|
-
finalizeToolResponse(
|
|
103
|
-
await tool.handler({
|
|
104
|
-
authorizedMCP,
|
|
105
|
-
collectionSlug: item.collectionSlug,
|
|
106
|
-
input: (input ?? {}) as Record<string, unknown>,
|
|
107
|
-
req,
|
|
108
|
-
serverContext: ctx,
|
|
109
|
-
}),
|
|
110
|
-
tool.overrideResponse,
|
|
111
|
-
),
|
|
112
|
-
)
|
|
113
|
-
logger.info(`✅ Tool: ${name} Registered.`)
|
|
114
|
-
break
|
|
115
|
-
}
|
|
125
|
+
case 'collectionTool':
|
|
116
126
|
case 'globalTool': {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let inputSchema = tool.input
|
|
120
|
-
if (typeof inputSchema === 'function') {
|
|
121
|
-
const raw = configSchema.$defs?.[item.globalSlug]
|
|
122
|
-
if (!raw) {
|
|
123
|
-
throw new APIError(`Global schema not found for slug: ${item.globalSlug}`, 500)
|
|
124
|
-
}
|
|
125
|
-
const globalSchema = removeVirtualFieldsFromSchema(
|
|
126
|
-
JSON.parse(JSON.stringify(raw)) as JsonSchemaType,
|
|
127
|
-
getGlobalVirtualFieldNames(req.payload.config, item.globalSlug),
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
inputSchema = inputSchema({ globalSchema })
|
|
127
|
+
if (registeredEntityTools.has(item.mcpName)) {
|
|
128
|
+
break
|
|
131
129
|
}
|
|
130
|
+
registeredEntityTools.add(item.mcpName)
|
|
131
|
+
|
|
132
|
+
const inputSchema = withSlugInput({
|
|
133
|
+
name: item.type === 'collectionTool' ? 'collectionSlug' : 'globalSlug',
|
|
134
|
+
input: item.tool.input,
|
|
135
|
+
})
|
|
136
|
+
|
|
132
137
|
server.registerTool(
|
|
133
|
-
|
|
138
|
+
item.mcpName,
|
|
134
139
|
{
|
|
135
|
-
description: tool.description,
|
|
136
|
-
inputSchema:
|
|
140
|
+
description: item.tool.description,
|
|
141
|
+
inputSchema: toStandardSchema(inputSchema),
|
|
137
142
|
},
|
|
138
143
|
async (input: unknown, ctx: ServerContext) =>
|
|
139
|
-
|
|
140
|
-
await tool.handler({
|
|
141
|
-
authorizedMCP,
|
|
142
|
-
globalSlug: item.globalSlug,
|
|
143
|
-
input: (input ?? {}) as Record<string, unknown>,
|
|
144
|
-
req,
|
|
145
|
-
serverContext: ctx,
|
|
146
|
-
}),
|
|
147
|
-
tool.overrideResponse,
|
|
148
|
-
),
|
|
144
|
+
callEntityTool({ input, item, serverContext: ctx }),
|
|
149
145
|
)
|
|
150
|
-
logger.info(`✅ Tool: ${
|
|
146
|
+
logger.info(`✅ Tool: ${item.mcpName} Registered.`)
|
|
151
147
|
break
|
|
152
148
|
}
|
|
153
149
|
case 'prompt': {
|
|
154
150
|
const prompt = item.prompt
|
|
155
151
|
server.registerPrompt(
|
|
156
|
-
item.
|
|
152
|
+
item.mcpName,
|
|
157
153
|
{
|
|
158
154
|
argsSchema: prompt.argsSchema ? toStandardSchema(prompt.argsSchema) : undefined,
|
|
159
155
|
description: prompt.description,
|
|
@@ -172,7 +168,7 @@ export const buildMcpServer = ({
|
|
|
172
168
|
case 'resource': {
|
|
173
169
|
const resource = item.resource
|
|
174
170
|
server.registerResource(
|
|
175
|
-
item.
|
|
171
|
+
item.mcpName,
|
|
176
172
|
// @ts-expect-error - Overload type ambiguity (string OR ResourceTemplate is valid)
|
|
177
173
|
resource.uri,
|
|
178
174
|
{
|
|
@@ -195,7 +191,7 @@ export const buildMcpServer = ({
|
|
|
195
191
|
case 'tool': {
|
|
196
192
|
const tool = item.tool
|
|
197
193
|
server.registerTool(
|
|
198
|
-
item.
|
|
194
|
+
item.mcpName,
|
|
199
195
|
{
|
|
200
196
|
description: tool.description,
|
|
201
197
|
inputSchema: tool.input ? toStandardSchema(tool.input) : undefined,
|
|
@@ -211,7 +207,7 @@ export const buildMcpServer = ({
|
|
|
211
207
|
tool.overrideResponse,
|
|
212
208
|
),
|
|
213
209
|
)
|
|
214
|
-
logger.info(`✅ Tool: ${item.
|
|
210
|
+
logger.info(`✅ Tool: ${item.mcpName} Registered.`)
|
|
215
211
|
break
|
|
216
212
|
}
|
|
217
213
|
}
|
|
@@ -222,3 +218,40 @@ export const buildMcpServer = ({
|
|
|
222
218
|
|
|
223
219
|
return server
|
|
224
220
|
}
|
|
221
|
+
|
|
222
|
+
const withSlugInput = ({
|
|
223
|
+
name,
|
|
224
|
+
input,
|
|
225
|
+
}: {
|
|
226
|
+
input?: ToolInputSchema
|
|
227
|
+
name: 'collectionSlug' | 'globalSlug'
|
|
228
|
+
}): ToolInputSchema => {
|
|
229
|
+
const description = name === 'collectionSlug' ? 'The collection slug' : 'The global slug'
|
|
230
|
+
const slugSchema = z.string().describe(description)
|
|
231
|
+
|
|
232
|
+
if (!input) {
|
|
233
|
+
return z.object({ [name]: slugSchema })
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (input instanceof z.ZodObject) {
|
|
237
|
+
return input.extend({ [name]: slugSchema })
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const schema = input as {
|
|
241
|
+
properties?: Record<string, JsonSchemaType>
|
|
242
|
+
required?: string[]
|
|
243
|
+
} & JsonSchemaType
|
|
244
|
+
|
|
245
|
+
return {
|
|
246
|
+
...schema,
|
|
247
|
+
type: 'object',
|
|
248
|
+
properties: {
|
|
249
|
+
...schema.properties,
|
|
250
|
+
[name]: {
|
|
251
|
+
type: 'string',
|
|
252
|
+
description,
|
|
253
|
+
},
|
|
254
|
+
},
|
|
255
|
+
required: Array.from(new Set([name, ...(schema.required ?? [])])),
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -9,50 +9,47 @@ import {
|
|
|
9
9
|
stripVirtualFields,
|
|
10
10
|
} from '../../../utils/getVirtualFieldNames.js'
|
|
11
11
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
12
|
-
import { prepareCollectionSchema } from '../../../utils/schemaConversion/prepareCollectionSchema.js'
|
|
13
12
|
import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
|
|
13
|
+
import { validateCollectionData } from '../validateEntityData.js'
|
|
14
|
+
import { formatCollectionError } from './formatCollectionError.js'
|
|
14
15
|
|
|
15
|
-
const DEFAULT_DESCRIPTION =
|
|
16
|
+
const DEFAULT_DESCRIPTION =
|
|
17
|
+
'Create a document in any collection by passing the collection slug and data.'
|
|
16
18
|
|
|
17
|
-
export const
|
|
19
|
+
export const createDocumentTool = defineCollectionTool({
|
|
18
20
|
description: DEFAULT_DESCRIPTION,
|
|
19
|
-
input: ({
|
|
20
|
-
z.
|
|
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
|
-
.describe(
|
|
52
|
-
'Optional: define exactly which fields you\'d like to create (JSON), e.g., \'{"title": "My Post"}\'',
|
|
53
|
-
)
|
|
54
|
-
.optional(),
|
|
55
|
-
}),
|
|
21
|
+
input: z.object({
|
|
22
|
+
data: z.record(z.string(), z.unknown()).describe('The document fields to create'),
|
|
23
|
+
depth: z
|
|
24
|
+
.number()
|
|
25
|
+
.int()
|
|
26
|
+
.min(0)
|
|
27
|
+
.max(10)
|
|
28
|
+
.describe('How many levels deep to populate relationships in response')
|
|
29
|
+
.optional()
|
|
30
|
+
.default(0),
|
|
31
|
+
draft: z
|
|
32
|
+
.boolean()
|
|
33
|
+
.describe('Whether to create 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
|
+
locale: z
|
|
41
|
+
.string()
|
|
42
|
+
.describe(
|
|
43
|
+
'Optional: locale code to create the document in (e.g., "en", "es"). Defaults to the default locale',
|
|
44
|
+
)
|
|
45
|
+
.optional(),
|
|
46
|
+
select: z
|
|
47
|
+
.string()
|
|
48
|
+
.describe(
|
|
49
|
+
"Optional: define exactly which fields you'd like to return (JSON), e.g., '{\"title\": true}'",
|
|
50
|
+
)
|
|
51
|
+
.optional(),
|
|
52
|
+
}),
|
|
56
53
|
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
57
54
|
const payload = req.payload
|
|
58
55
|
const logger = getLogger({ payload })
|
|
@@ -64,9 +61,15 @@ export const createCollectionTool = defineCollectionTool({
|
|
|
64
61
|
)
|
|
65
62
|
|
|
66
63
|
try {
|
|
67
|
-
let parsedData = transformPointDataToPayload(data as Record<string, unknown>)
|
|
68
64
|
const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)
|
|
69
|
-
|
|
65
|
+
const inputData = stripVirtualFields(data, virtualFieldNames)
|
|
66
|
+
const validationError = validateCollectionData({ collectionSlug, data: inputData, req })
|
|
67
|
+
|
|
68
|
+
if (validationError) {
|
|
69
|
+
return validationError
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const parsedData = transformPointDataToPayload(inputData)
|
|
70
73
|
|
|
71
74
|
let selectClause: SelectType | undefined
|
|
72
75
|
if (select) {
|
|
@@ -104,13 +107,6 @@ export const createCollectionTool = defineCollectionTool({
|
|
|
104
107
|
} catch (error) {
|
|
105
108
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
106
109
|
logger.error(`Error creating document in ${collectionSlug}: ${errorMessage}`)
|
|
107
|
-
return {
|
|
108
|
-
content: [
|
|
109
|
-
{
|
|
110
|
-
type: 'text',
|
|
111
|
-
text: `Error creating document in collection "${collectionSlug}": ${errorMessage}`,
|
|
112
|
-
},
|
|
113
|
-
],
|
|
114
|
-
}
|
|
110
|
+
return formatCollectionError({ action: 'creating', collectionSlug, error, req })
|
|
115
111
|
}
|
|
116
112
|
})
|
|
@@ -3,10 +3,12 @@ import { z } from 'zod'
|
|
|
3
3
|
import { defineCollectionTool } from '../../../defineTool.js'
|
|
4
4
|
import { getLogger } from '../../../utils/getLogger.js'
|
|
5
5
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
6
|
+
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
6
7
|
|
|
7
|
-
const DEFAULT_DESCRIPTION =
|
|
8
|
+
const DEFAULT_DESCRIPTION =
|
|
9
|
+
'Delete documents in any collection by passing the collection slug and ID or where clause.'
|
|
8
10
|
|
|
9
|
-
export const
|
|
11
|
+
export const deleteDocumentsTool = defineCollectionTool({
|
|
10
12
|
description: DEFAULT_DESCRIPTION,
|
|
11
13
|
input: z.object({
|
|
12
14
|
id: z
|
|
@@ -31,9 +33,10 @@ export const deleteCollectionTool = defineCollectionTool({
|
|
|
31
33
|
'Optional: locale code for the operation (e.g., "en", "es"). Defaults to the default locale',
|
|
32
34
|
)
|
|
33
35
|
.optional(),
|
|
34
|
-
where:
|
|
35
|
-
.
|
|
36
|
-
|
|
36
|
+
where: whereSchema
|
|
37
|
+
.describe(
|
|
38
|
+
'Optional: where clause to delete multiple documents. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}',
|
|
39
|
+
)
|
|
37
40
|
.optional(),
|
|
38
41
|
}),
|
|
39
42
|
}).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
|
|
@@ -53,16 +56,6 @@ export const deleteCollectionTool = defineCollectionTool({
|
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
let whereClause: Record<string, unknown> = {}
|
|
57
|
-
if (where) {
|
|
58
|
-
try {
|
|
59
|
-
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
60
|
-
} catch {
|
|
61
|
-
logger.warn(`Invalid where clause JSON: ${where}`)
|
|
62
|
-
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
59
|
const deleteOptions: Record<string, unknown> = {
|
|
67
60
|
collection: collectionSlug,
|
|
68
61
|
depth,
|
|
@@ -75,7 +68,7 @@ export const deleteCollectionTool = defineCollectionTool({
|
|
|
75
68
|
if (id) {
|
|
76
69
|
deleteOptions.id = id
|
|
77
70
|
} else {
|
|
78
|
-
deleteOptions.where =
|
|
71
|
+
deleteOptions.where = where
|
|
79
72
|
}
|
|
80
73
|
|
|
81
74
|
const result = await payload.delete(deleteOptions as Parameters<typeof payload.delete>[0])
|
|
@@ -5,11 +5,12 @@ import { z } from 'zod'
|
|
|
5
5
|
import { defineCollectionTool } from '../../../defineTool.js'
|
|
6
6
|
import { getLogger } from '../../../utils/getLogger.js'
|
|
7
7
|
import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
|
|
8
|
+
import { whereSchema } from '../../../utils/whereSchema.js'
|
|
8
9
|
|
|
9
10
|
const DEFAULT_DESCRIPTION =
|
|
10
|
-
'Find documents in
|
|
11
|
+
'Find documents in any collection by passing the collection slug and optional ID or where clause.'
|
|
11
12
|
|
|
12
|
-
export const
|
|
13
|
+
export const findDocumentsTool = defineCollectionTool({
|
|
13
14
|
description: DEFAULT_DESCRIPTION,
|
|
14
15
|
input: z.object({
|
|
15
16
|
id: z
|
|
@@ -67,10 +68,9 @@ export const findCollectionTool = defineCollectionTool({
|
|
|
67
68
|
.string()
|
|
68
69
|
.describe('Field to sort by (e.g., "createdAt", "-updatedAt" for descending)')
|
|
69
70
|
.optional(),
|
|
70
|
-
where:
|
|
71
|
-
.string()
|
|
71
|
+
where: whereSchema
|
|
72
72
|
.describe(
|
|
73
|
-
'Optional
|
|
73
|
+
'Optional: where clause for filtering. Use field names with Payload operators, and/or arrays for grouping. Example: {"title":{"contains":"test"}}',
|
|
74
74
|
)
|
|
75
75
|
.optional(),
|
|
76
76
|
}),
|
|
@@ -85,16 +85,6 @@ export const findCollectionTool = defineCollectionTool({
|
|
|
85
85
|
)
|
|
86
86
|
|
|
87
87
|
try {
|
|
88
|
-
let whereClause: Record<string, unknown> = {}
|
|
89
|
-
if (where) {
|
|
90
|
-
try {
|
|
91
|
-
whereClause = JSON.parse(where) as Record<string, unknown>
|
|
92
|
-
} catch {
|
|
93
|
-
logger.warn(`Invalid where clause JSON: ${where}`)
|
|
94
|
-
return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
88
|
let selectClause: SelectType | undefined
|
|
99
89
|
if (select) {
|
|
100
90
|
try {
|
|
@@ -157,8 +147,8 @@ export const findCollectionTool = defineCollectionTool({
|
|
|
157
147
|
if (sort) {
|
|
158
148
|
findOptions.sort = sort
|
|
159
149
|
}
|
|
160
|
-
if (
|
|
161
|
-
findOptions.where =
|
|
150
|
+
if (where) {
|
|
151
|
+
findOptions.where = where
|
|
162
152
|
}
|
|
163
153
|
|
|
164
154
|
const result = await payload.find(findOptions)
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { CollectionSlug, PayloadRequest } from 'payload'
|
|
2
|
+
|
|
3
|
+
import type { MCPToolResponse } from '../../../types.js'
|
|
4
|
+
|
|
5
|
+
import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
|
|
6
|
+
|
|
7
|
+
const getValidationErrors = (error: unknown): undefined | unknown[] => {
|
|
8
|
+
if (!error || typeof error !== 'object') {
|
|
9
|
+
return undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const data = 'data' in error ? error.data : undefined
|
|
13
|
+
if (!data || typeof data !== 'object' || !('errors' in data) || !Array.isArray(data.errors)) {
|
|
14
|
+
return undefined
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return data.errors
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const isSchemaError = (error: unknown, message: string): boolean => {
|
|
21
|
+
if (getValidationErrors(error)) {
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const name = error && typeof error === 'object' && 'name' in error ? error.name : undefined
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
name === 'CastError' ||
|
|
29
|
+
name === 'ValidationError' ||
|
|
30
|
+
message.includes('Cast to ') ||
|
|
31
|
+
message.includes('validation failed')
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const formatCollectionError = ({
|
|
36
|
+
action,
|
|
37
|
+
collectionSlug,
|
|
38
|
+
error,
|
|
39
|
+
req,
|
|
40
|
+
}: {
|
|
41
|
+
action: 'creating' | 'updating'
|
|
42
|
+
collectionSlug: CollectionSlug
|
|
43
|
+
error: unknown
|
|
44
|
+
req: PayloadRequest
|
|
45
|
+
}): MCPToolResponse => {
|
|
46
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
47
|
+
const errors = getValidationErrors(error) ?? [{ message: errorMessage }]
|
|
48
|
+
|
|
49
|
+
if (!isSchemaError(error, errorMessage)) {
|
|
50
|
+
return {
|
|
51
|
+
content: [
|
|
52
|
+
{
|
|
53
|
+
type: 'text',
|
|
54
|
+
text: `Error ${action} document in collection "${collectionSlug}": ${errorMessage}`,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
isError: true,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const schema = getCollectionInputSchema({ collectionSlug, req })
|
|
62
|
+
const schemaText = schema
|
|
63
|
+
? `\n\nUse this schema for data:\n\`\`\`json\n${JSON.stringify(schema)}\n\`\`\``
|
|
64
|
+
: ''
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
content: [
|
|
68
|
+
{
|
|
69
|
+
type: 'text',
|
|
70
|
+
text: `Error ${action} document in collection "${collectionSlug}": ${errorMessage}${schemaText}`,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
isError: true,
|
|
74
|
+
...(schema
|
|
75
|
+
? {
|
|
76
|
+
structuredContent: {
|
|
77
|
+
collectionSlug,
|
|
78
|
+
errors,
|
|
79
|
+
schema,
|
|
80
|
+
},
|
|
81
|
+
}
|
|
82
|
+
: {}),
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { defineCollectionTool } from '../../../defineTool.js'
|
|
2
|
+
import { getCollectionInputSchema } from '../../../utils/schemaConversion/getEntityInputSchema.js'
|
|
3
|
+
|
|
4
|
+
export const getCollectionSchemaTool = defineCollectionTool({
|
|
5
|
+
description: 'Get the input schema for creating or updating documents in a collection.',
|
|
6
|
+
}).handler(({ collectionSlug, req }) => {
|
|
7
|
+
const inputSchema = getCollectionInputSchema({ collectionSlug, req })
|
|
8
|
+
|
|
9
|
+
if (!inputSchema) {
|
|
10
|
+
return {
|
|
11
|
+
content: [{ type: 'text', text: `Error: Collection "${collectionSlug}" not found` }],
|
|
12
|
+
isError: true,
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
content: [
|
|
18
|
+
{
|
|
19
|
+
type: 'text',
|
|
20
|
+
text: `Schema for collection "${collectionSlug}":\n\`\`\`json\n${JSON.stringify(inputSchema)}\n\`\`\``,
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
structuredContent: {
|
|
24
|
+
collectionSlug,
|
|
25
|
+
schema: inputSchema,
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
})
|