@payloadcms/plugin-mcp 0.0.1-alpha.0
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/LICENSE.md +22 -0
- package/README.md +7 -0
- package/dist/collections/createApiKeysCollection.d.ts +7 -0
- package/dist/collections/createApiKeysCollection.d.ts.map +1 -0
- package/dist/collections/createApiKeysCollection.js +315 -0
- package/dist/collections/createApiKeysCollection.js.map +1 -0
- package/dist/endpoints/mcp.d.ts +4 -0
- package/dist/endpoints/mcp.d.ts.map +1 -0
- package/dist/endpoints/mcp.js +44 -0
- package/dist/endpoints/mcp.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/createRequest.d.ts +3 -0
- package/dist/mcp/createRequest.d.ts.map +1 -0
- package/dist/mcp/createRequest.js +14 -0
- package/dist/mcp/createRequest.js.map +1 -0
- package/dist/mcp/getMcpHandler.d.ts +4 -0
- package/dist/mcp/getMcpHandler.d.ts.map +1 -0
- package/dist/mcp/getMcpHandler.js +179 -0
- package/dist/mcp/getMcpHandler.js.map +1 -0
- package/dist/mcp/helpers/config.d.ts +30 -0
- package/dist/mcp/helpers/config.d.ts.map +1 -0
- package/dist/mcp/helpers/config.js +217 -0
- package/dist/mcp/helpers/config.js.map +1 -0
- package/dist/mcp/helpers/conversion.d.ts +2 -0
- package/dist/mcp/helpers/conversion.d.ts.map +1 -0
- package/dist/mcp/helpers/conversion.js +5 -0
- package/dist/mcp/helpers/conversion.js.map +1 -0
- package/dist/mcp/helpers/fields.d.ts +38 -0
- package/dist/mcp/helpers/fields.d.ts.map +1 -0
- package/dist/mcp/helpers/fields.js +96 -0
- package/dist/mcp/helpers/fields.js.map +1 -0
- package/dist/mcp/helpers/fileValidation.d.ts +69 -0
- package/dist/mcp/helpers/fileValidation.d.ts.map +1 -0
- package/dist/mcp/helpers/fileValidation.js +305 -0
- package/dist/mcp/helpers/fileValidation.js.map +1 -0
- package/dist/mcp/helpers/validation.d.ts +9 -0
- package/dist/mcp/helpers/validation.d.ts.map +1 -0
- package/dist/mcp/helpers/validation.js +22 -0
- package/dist/mcp/helpers/validation.js.map +1 -0
- package/dist/mcp/registerTool.d.ts +6 -0
- package/dist/mcp/registerTool.d.ts.map +1 -0
- package/dist/mcp/registerTool.js +18 -0
- package/dist/mcp/registerTool.js.map +1 -0
- package/dist/mcp/tools/auth/auth.d.ts +4 -0
- package/dist/mcp/tools/auth/auth.d.ts.map +1 -0
- package/dist/mcp/tools/auth/auth.js +54 -0
- package/dist/mcp/tools/auth/auth.js.map +1 -0
- package/dist/mcp/tools/auth/forgotPassword.d.ts +4 -0
- package/dist/mcp/tools/auth/forgotPassword.d.ts.map +1 -0
- package/dist/mcp/tools/auth/forgotPassword.js +45 -0
- package/dist/mcp/tools/auth/forgotPassword.js.map +1 -0
- package/dist/mcp/tools/auth/login.d.ts +4 -0
- package/dist/mcp/tools/auth/login.d.ts.map +1 -0
- package/dist/mcp/tools/auth/login.js +48 -0
- package/dist/mcp/tools/auth/login.js.map +1 -0
- package/dist/mcp/tools/auth/resetPassword.d.ts +4 -0
- package/dist/mcp/tools/auth/resetPassword.d.ts.map +1 -0
- package/dist/mcp/tools/auth/resetPassword.js +46 -0
- package/dist/mcp/tools/auth/resetPassword.js.map +1 -0
- package/dist/mcp/tools/auth/unlock.d.ts +4 -0
- package/dist/mcp/tools/auth/unlock.d.ts.map +1 -0
- package/dist/mcp/tools/auth/unlock.js +45 -0
- package/dist/mcp/tools/auth/unlock.js.map +1 -0
- package/dist/mcp/tools/auth/verify.d.ts +4 -0
- package/dist/mcp/tools/auth/verify.d.ts.map +1 -0
- package/dist/mcp/tools/auth/verify.js +42 -0
- package/dist/mcp/tools/auth/verify.js.map +1 -0
- package/dist/mcp/tools/collection/create.d.ts +10 -0
- package/dist/mcp/tools/collection/create.d.ts.map +1 -0
- package/dist/mcp/tools/collection/create.js +159 -0
- package/dist/mcp/tools/collection/create.js.map +1 -0
- package/dist/mcp/tools/collection/delete.d.ts +10 -0
- package/dist/mcp/tools/collection/delete.d.ts.map +1 -0
- package/dist/mcp/tools/collection/delete.js +162 -0
- package/dist/mcp/tools/collection/delete.js.map +1 -0
- package/dist/mcp/tools/collection/find.d.ts +10 -0
- package/dist/mcp/tools/collection/find.d.ts.map +1 -0
- package/dist/mcp/tools/collection/find.js +162 -0
- package/dist/mcp/tools/collection/find.js.map +1 -0
- package/dist/mcp/tools/collection/update.d.ts +10 -0
- package/dist/mcp/tools/collection/update.d.ts.map +1 -0
- package/dist/mcp/tools/collection/update.js +206 -0
- package/dist/mcp/tools/collection/update.js.map +1 -0
- package/dist/mcp/tools/config/find.d.ts +10 -0
- package/dist/mcp/tools/config/find.d.ts.map +1 -0
- package/dist/mcp/tools/config/find.js +94 -0
- package/dist/mcp/tools/config/find.js.map +1 -0
- package/dist/mcp/tools/config/update.d.ts +10 -0
- package/dist/mcp/tools/config/update.d.ts.map +1 -0
- package/dist/mcp/tools/config/update.js +212 -0
- package/dist/mcp/tools/config/update.js.map +1 -0
- package/dist/mcp/tools/job/create.d.ts +10 -0
- package/dist/mcp/tools/job/create.d.ts.map +1 -0
- package/dist/mcp/tools/job/create.js +293 -0
- package/dist/mcp/tools/job/create.js.map +1 -0
- package/dist/mcp/tools/job/run.d.ts +10 -0
- package/dist/mcp/tools/job/run.d.ts.map +1 -0
- package/dist/mcp/tools/job/run.js +147 -0
- package/dist/mcp/tools/job/run.js.map +1 -0
- package/dist/mcp/tools/job/update.d.ts +11 -0
- package/dist/mcp/tools/job/update.d.ts.map +1 -0
- package/dist/mcp/tools/job/update.js +211 -0
- package/dist/mcp/tools/job/update.js.map +1 -0
- package/dist/mcp/tools/resource/create.d.ts +6 -0
- package/dist/mcp/tools/resource/create.d.ts.map +1 -0
- package/dist/mcp/tools/resource/create.js +75 -0
- package/dist/mcp/tools/resource/create.js.map +1 -0
- package/dist/mcp/tools/resource/delete.d.ts +5 -0
- package/dist/mcp/tools/resource/delete.d.ts.map +1 -0
- package/dist/mcp/tools/resource/delete.js +140 -0
- package/dist/mcp/tools/resource/delete.js.map +1 -0
- package/dist/mcp/tools/resource/find.d.ts +5 -0
- package/dist/mcp/tools/resource/find.d.ts.map +1 -0
- package/dist/mcp/tools/resource/find.js +119 -0
- package/dist/mcp/tools/resource/find.js.map +1 -0
- package/dist/mcp/tools/resource/update.d.ts +6 -0
- package/dist/mcp/tools/resource/update.d.ts.map +1 -0
- package/dist/mcp/tools/resource/update.js +201 -0
- package/dist/mcp/tools/resource/update.js.map +1 -0
- package/dist/mcp/tools/schemas.d.ts +374 -0
- package/dist/mcp/tools/schemas.d.ts.map +1 -0
- package/dist/mcp/tools/schemas.js +201 -0
- package/dist/mcp/tools/schemas.js.map +1 -0
- package/dist/types.d.ts +379 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/camelCase.d.ts +9 -0
- package/dist/utils/camelCase.d.ts.map +1 -0
- package/dist/utils/camelCase.js +11 -0
- package/dist/utils/camelCase.js.map +1 -0
- package/dist/utils/convertCollectionSchemaToZod.d.ts +3 -0
- package/dist/utils/convertCollectionSchemaToZod.d.ts.map +1 -0
- package/dist/utils/convertCollectionSchemaToZod.js +30 -0
- package/dist/utils/convertCollectionSchemaToZod.js.map +1 -0
- package/package.json +64 -0
- package/src/collections/createApiKeysCollection.ts +393 -0
- package/src/endpoints/mcp.ts +60 -0
- package/src/index.ts +86 -0
- package/src/mcp/createRequest.ts +13 -0
- package/src/mcp/getMcpHandler.ts +433 -0
- package/src/mcp/helpers/config.ts +326 -0
- package/src/mcp/helpers/conversion.ts +3 -0
- package/src/mcp/helpers/fields.ts +158 -0
- package/src/mcp/helpers/fileValidation.ts +417 -0
- package/src/mcp/helpers/validation.ts +32 -0
- package/src/mcp/registerTool.ts +22 -0
- package/src/mcp/tools/auth/auth.ts +69 -0
- package/src/mcp/tools/auth/forgotPassword.ts +68 -0
- package/src/mcp/tools/auth/login.ts +70 -0
- package/src/mcp/tools/auth/resetPassword.ts +59 -0
- package/src/mcp/tools/auth/unlock.ts +62 -0
- package/src/mcp/tools/auth/verify.ts +55 -0
- package/src/mcp/tools/collection/create.ts +236 -0
- package/src/mcp/tools/collection/delete.ts +227 -0
- package/src/mcp/tools/collection/find.ts +222 -0
- package/src/mcp/tools/collection/update.ts +288 -0
- package/src/mcp/tools/config/find.ts +126 -0
- package/src/mcp/tools/config/update.ts +282 -0
- package/src/mcp/tools/job/create.ts +420 -0
- package/src/mcp/tools/job/run.ts +189 -0
- package/src/mcp/tools/job/update.ts +319 -0
- package/src/mcp/tools/resource/create.ts +121 -0
- package/src/mcp/tools/resource/delete.ts +210 -0
- package/src/mcp/tools/resource/find.ts +194 -0
- package/src/mcp/tools/resource/update.ts +314 -0
- package/src/mcp/tools/schemas.ts +373 -0
- package/src/types.ts +405 -0
- package/src/utils/camelCase.ts +12 -0
- package/src/utils/convertCollectionSchemaToZod.ts +35 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import type { CollectionConfig } from 'payload'
|
|
2
|
+
|
|
3
|
+
import { existsSync } from 'fs'
|
|
4
|
+
import { join } from 'path'
|
|
5
|
+
|
|
6
|
+
export type ValidationType = 'collection' | 'task' | 'workflow'
|
|
7
|
+
|
|
8
|
+
export interface ValidationResult<T = unknown> {
|
|
9
|
+
config?: T
|
|
10
|
+
error?: string
|
|
11
|
+
success: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Custom task config interface that matches what we're creating
|
|
15
|
+
export interface TaskConfig {
|
|
16
|
+
handler: (args: {
|
|
17
|
+
input: Record<string, unknown>
|
|
18
|
+
job: Record<string, unknown>
|
|
19
|
+
tasks: Record<string, unknown>
|
|
20
|
+
}) => Record<string, unknown>
|
|
21
|
+
inputSchema?: Array<{
|
|
22
|
+
label?: string
|
|
23
|
+
name: string
|
|
24
|
+
options?: Array<{ label: string; value: string }>
|
|
25
|
+
required?: boolean
|
|
26
|
+
type: string
|
|
27
|
+
}>
|
|
28
|
+
label?: string
|
|
29
|
+
outputSchema?: Array<{
|
|
30
|
+
label?: string
|
|
31
|
+
name: string
|
|
32
|
+
options?: Array<{ label: string; value: string }>
|
|
33
|
+
required?: boolean
|
|
34
|
+
type: string
|
|
35
|
+
}>
|
|
36
|
+
retries?: number
|
|
37
|
+
slug: string
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Custom workflow config interface that matches what we're creating
|
|
41
|
+
export interface WorkflowConfig {
|
|
42
|
+
handler: (args: {
|
|
43
|
+
input: Record<string, unknown>
|
|
44
|
+
job: Record<string, unknown>
|
|
45
|
+
tasks: Record<string, unknown>
|
|
46
|
+
}) => void
|
|
47
|
+
inputSchema?: Array<{
|
|
48
|
+
label?: string
|
|
49
|
+
name: string
|
|
50
|
+
options?: Array<{ label: string; value: string }>
|
|
51
|
+
required?: boolean
|
|
52
|
+
type: string
|
|
53
|
+
}>
|
|
54
|
+
label?: string
|
|
55
|
+
queue?: string
|
|
56
|
+
retries?: number
|
|
57
|
+
slug: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Generic validation function for Payload configuration files
|
|
62
|
+
* @param fileName - The name of the file (e.g., 'Users.ts', 'my-task.ts')
|
|
63
|
+
* @param type - The type of validation to perform ('collection', 'task', or 'workflow')
|
|
64
|
+
* @returns Object containing success status and any error messages
|
|
65
|
+
*/
|
|
66
|
+
export const validatePayloadFile = async <T = CollectionConfig | TaskConfig | WorkflowConfig>(
|
|
67
|
+
fileName: string,
|
|
68
|
+
type: ValidationType,
|
|
69
|
+
): Promise<ValidationResult<T>> => {
|
|
70
|
+
try {
|
|
71
|
+
const basePath = type === 'collection' ? 'collections' : type === 'task' ? 'tasks' : 'workflows'
|
|
72
|
+
const fullPath = join(process.cwd(), 'src', basePath)
|
|
73
|
+
const filePath = join(fullPath, fileName)
|
|
74
|
+
|
|
75
|
+
// Check if file exists
|
|
76
|
+
if (!existsSync(filePath)) {
|
|
77
|
+
return {
|
|
78
|
+
error: `${type} file does not exist: ${fileName}`,
|
|
79
|
+
success: false,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Clear require cache to ensure fresh import
|
|
84
|
+
delete require.cache[filePath]
|
|
85
|
+
|
|
86
|
+
// Use relative path for webpack compatibility
|
|
87
|
+
const moduleName = fileName.replace('.ts', '')
|
|
88
|
+
const relativePath = `../${basePath}/${moduleName}`
|
|
89
|
+
|
|
90
|
+
// Dynamic import with relative path
|
|
91
|
+
const importedModule = await import(/* webpackIgnore: true */ relativePath)
|
|
92
|
+
|
|
93
|
+
// Get the configuration based on type
|
|
94
|
+
let config: T | undefined
|
|
95
|
+
|
|
96
|
+
if (type === 'collection') {
|
|
97
|
+
config = getCollectionConfig(importedModule, moduleName) as T
|
|
98
|
+
} else if (type === 'task') {
|
|
99
|
+
config = getTaskConfig(importedModule) as T
|
|
100
|
+
} else if (type === 'workflow') {
|
|
101
|
+
config = getWorkflowConfig(importedModule) as T
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!config) {
|
|
105
|
+
return {
|
|
106
|
+
error: `${type} file does not export a valid ${type} config`,
|
|
107
|
+
success: false,
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Validate the configuration
|
|
112
|
+
let validationResult: ValidationResult<unknown>
|
|
113
|
+
if (type === 'collection') {
|
|
114
|
+
validationResult = validateCollectionConfig(config as unknown as CollectionConfig)
|
|
115
|
+
} else if (type === 'task') {
|
|
116
|
+
validationResult = validateTaskConfig(config as unknown as TaskConfig)
|
|
117
|
+
} else if (type === 'workflow') {
|
|
118
|
+
validationResult = validateWorkflowConfig(config as unknown as WorkflowConfig)
|
|
119
|
+
} else {
|
|
120
|
+
return {
|
|
121
|
+
error: `Unknown validation type: ${type}`,
|
|
122
|
+
success: false,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (!validationResult.success) {
|
|
127
|
+
return validationResult as ValidationResult<T>
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
config,
|
|
132
|
+
success: true,
|
|
133
|
+
}
|
|
134
|
+
} catch (error) {
|
|
135
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error during validation'
|
|
136
|
+
return {
|
|
137
|
+
error: `Failed to validate ${type} file: ${errorMessage}`,
|
|
138
|
+
success: false,
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Extract collection configuration from module exports
|
|
145
|
+
*/
|
|
146
|
+
function getCollectionConfig(
|
|
147
|
+
importedModule: Record<string, unknown>,
|
|
148
|
+
moduleName: string,
|
|
149
|
+
): CollectionConfig | undefined {
|
|
150
|
+
if (importedModule.default) {
|
|
151
|
+
return importedModule.default as CollectionConfig
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (importedModule[moduleName]) {
|
|
155
|
+
return importedModule[moduleName] as CollectionConfig
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return undefined
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Extract task configuration from module exports
|
|
163
|
+
*/
|
|
164
|
+
function getTaskConfig(importedModule: Record<string, unknown>): TaskConfig | undefined {
|
|
165
|
+
// First check for default export
|
|
166
|
+
if (importedModule.default) {
|
|
167
|
+
return importedModule.default as TaskConfig
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Look for named exports ending with "Task"
|
|
171
|
+
const exportNames = Object.keys(importedModule)
|
|
172
|
+
const taskExport = exportNames.find((name) => name.endsWith('Task'))
|
|
173
|
+
|
|
174
|
+
if (taskExport) {
|
|
175
|
+
return importedModule[taskExport] as TaskConfig
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return undefined
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Extract workflow configuration from module exports
|
|
183
|
+
*/
|
|
184
|
+
function getWorkflowConfig(importedModule: Record<string, unknown>): undefined | WorkflowConfig {
|
|
185
|
+
// First check for default export
|
|
186
|
+
if (importedModule.default) {
|
|
187
|
+
return importedModule.default as WorkflowConfig
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// Look for named exports ending with "Workflow"
|
|
191
|
+
const exportNames = Object.keys(importedModule)
|
|
192
|
+
const workflowExport = exportNames.find((name) => name.endsWith('Workflow'))
|
|
193
|
+
|
|
194
|
+
if (workflowExport) {
|
|
195
|
+
return importedModule[workflowExport] as WorkflowConfig
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return undefined
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Validate collection configuration structure
|
|
203
|
+
*/
|
|
204
|
+
function validateCollectionConfig(config: CollectionConfig): ValidationResult<CollectionConfig> {
|
|
205
|
+
if (!config || typeof config !== 'object') {
|
|
206
|
+
return {
|
|
207
|
+
error: 'Collection config is not a valid object',
|
|
208
|
+
success: false,
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (!config.slug || typeof config.slug !== 'string') {
|
|
213
|
+
return {
|
|
214
|
+
error: 'Collection config must have a valid slug property',
|
|
215
|
+
success: false,
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Validate each field has required properties
|
|
220
|
+
if (config.fields) {
|
|
221
|
+
for (let i = 0; i < config.fields.length; i++) {
|
|
222
|
+
const field = config.fields[i] as Record<string, unknown>
|
|
223
|
+
if (!field || typeof field !== 'object') {
|
|
224
|
+
return {
|
|
225
|
+
error: `Field at index ${i} is not a valid object`,
|
|
226
|
+
success: false,
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Check if field has type property
|
|
231
|
+
if ('type' in field && field.type && typeof field.type !== 'string') {
|
|
232
|
+
return {
|
|
233
|
+
error: `Field at index ${i} has invalid type property`,
|
|
234
|
+
success: false,
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return { config, success: true }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Validate task configuration structure
|
|
245
|
+
*/
|
|
246
|
+
function validateTaskConfig(config: TaskConfig): ValidationResult<TaskConfig> {
|
|
247
|
+
if (!config || typeof config !== 'object') {
|
|
248
|
+
return {
|
|
249
|
+
error: 'Task config is not a valid object',
|
|
250
|
+
success: false,
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (!config.slug || typeof config.slug !== 'string') {
|
|
255
|
+
return {
|
|
256
|
+
error: 'Task config must have a valid slug property',
|
|
257
|
+
success: false,
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
if (!config.handler || typeof config.handler !== 'function') {
|
|
262
|
+
return {
|
|
263
|
+
error: 'Task config must have a valid handler function',
|
|
264
|
+
success: false,
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// Validate optional properties
|
|
269
|
+
if (config.retries !== undefined && (typeof config.retries !== 'number' || config.retries < 0)) {
|
|
270
|
+
return {
|
|
271
|
+
error: 'Task config retries must be a non-negative number',
|
|
272
|
+
success: false,
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// Validate schemas if present
|
|
277
|
+
if (config.inputSchema && Array.isArray(config.inputSchema)) {
|
|
278
|
+
for (let i = 0; i < config.inputSchema.length; i++) {
|
|
279
|
+
const field = config.inputSchema[i]
|
|
280
|
+
if (!field || typeof field !== 'object') {
|
|
281
|
+
return {
|
|
282
|
+
error: `Input schema field at index ${i} is not a valid object`,
|
|
283
|
+
success: false,
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (!field.name || typeof field.name !== 'string') {
|
|
288
|
+
return {
|
|
289
|
+
error: `Input schema field at index ${i} must have a valid name property`,
|
|
290
|
+
success: false,
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (!field.type || typeof field.type !== 'string') {
|
|
295
|
+
return {
|
|
296
|
+
error: `Input schema field at index ${i} must have a valid type property`,
|
|
297
|
+
success: false,
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (config.outputSchema && Array.isArray(config.outputSchema)) {
|
|
304
|
+
for (let i = 0; i < config.outputSchema.length; i++) {
|
|
305
|
+
const field = config.outputSchema[i]
|
|
306
|
+
if (!field || typeof field !== 'object') {
|
|
307
|
+
return {
|
|
308
|
+
error: `Output schema field at index ${i} is not a valid object`,
|
|
309
|
+
success: false,
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
if (!field.name || typeof field.name !== 'string') {
|
|
314
|
+
return {
|
|
315
|
+
error: `Output schema field at index ${i} must have a valid name property`,
|
|
316
|
+
success: false,
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (!field.type || typeof field.type !== 'string') {
|
|
321
|
+
return {
|
|
322
|
+
error: `Output schema field at index ${i} must have a valid type property`,
|
|
323
|
+
success: false,
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return { config, success: true }
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Validate workflow configuration structure
|
|
334
|
+
*/
|
|
335
|
+
function validateWorkflowConfig(config: WorkflowConfig): ValidationResult<WorkflowConfig> {
|
|
336
|
+
if (!config || typeof config !== 'object') {
|
|
337
|
+
return {
|
|
338
|
+
error: 'Workflow config is not a valid object',
|
|
339
|
+
success: false,
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!config.slug || typeof config.slug !== 'string') {
|
|
344
|
+
return {
|
|
345
|
+
error: 'Workflow config must have a valid slug property',
|
|
346
|
+
success: false,
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if (!config.handler || typeof config.handler !== 'function') {
|
|
351
|
+
return {
|
|
352
|
+
error: 'Workflow config must have a valid handler function',
|
|
353
|
+
success: false,
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Validate optional properties
|
|
358
|
+
if (config.queue && typeof config.queue !== 'string') {
|
|
359
|
+
return {
|
|
360
|
+
error: 'Workflow config queue must be a string',
|
|
361
|
+
success: false,
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (config.retries !== undefined && (typeof config.retries !== 'number' || config.retries < 0)) {
|
|
366
|
+
return {
|
|
367
|
+
error: 'Workflow config retries must be a non-negative number',
|
|
368
|
+
success: false,
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Validate schema if present
|
|
373
|
+
if (config.inputSchema && Array.isArray(config.inputSchema)) {
|
|
374
|
+
for (let i = 0; i < config.inputSchema.length; i++) {
|
|
375
|
+
const field = config.inputSchema[i]
|
|
376
|
+
if (!field || typeof field !== 'object') {
|
|
377
|
+
return {
|
|
378
|
+
error: `Input schema field at index ${i} is not a valid object`,
|
|
379
|
+
success: false,
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (!field.name || typeof field.name !== 'string') {
|
|
384
|
+
return {
|
|
385
|
+
error: `Input schema field at index ${i} must have a valid name property`,
|
|
386
|
+
success: false,
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
if (!field.type || typeof field.type !== 'string') {
|
|
391
|
+
return {
|
|
392
|
+
error: `Input schema field at index ${i} must have a valid type property`,
|
|
393
|
+
success: false,
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return { config, success: true }
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Convenience functions for backward compatibility
|
|
403
|
+
export const validateCollectionFile = async (
|
|
404
|
+
fileName: string,
|
|
405
|
+
): Promise<ValidationResult<CollectionConfig>> => {
|
|
406
|
+
return validatePayloadFile<CollectionConfig>(fileName, 'collection')
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
export const validateTaskFile = async (fileName: string): Promise<ValidationResult<TaskConfig>> => {
|
|
410
|
+
return validatePayloadFile<TaskConfig>(fileName, 'task')
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export const validateWorkflowFile = async (
|
|
414
|
+
fileName: string,
|
|
415
|
+
): Promise<ValidationResult<WorkflowConfig>> => {
|
|
416
|
+
return validatePayloadFile<WorkflowConfig>(fileName, 'workflow')
|
|
417
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates collection-specific data for resource creation
|
|
3
|
+
*/
|
|
4
|
+
export function validateCollectionData(
|
|
5
|
+
collection: string,
|
|
6
|
+
data: Record<string, unknown>,
|
|
7
|
+
availableCollections: string[],
|
|
8
|
+
): null | string {
|
|
9
|
+
// Check if collection exists
|
|
10
|
+
if (!availableCollections.includes(collection)) {
|
|
11
|
+
return `Unknown collection: ${collection}. Available collections: ${availableCollections.join(', ')}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!data || typeof data !== 'object' || Object.keys(data).length === 0) {
|
|
15
|
+
return `Collection "${collection}" requires data to be provided`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return null
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks if a collection slug is valid
|
|
23
|
+
*/
|
|
24
|
+
export function validateCollectionSlug(
|
|
25
|
+
collection: string,
|
|
26
|
+
collections: Partial<Record<string, true>>,
|
|
27
|
+
) {
|
|
28
|
+
const collectionSlugs = Object.keys(collections)
|
|
29
|
+
if (!collectionSlugs.includes(collection)) {
|
|
30
|
+
return `Collection "${collection}" is not valid`
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const registerTool = (
|
|
2
|
+
isEnabled: boolean | undefined,
|
|
3
|
+
toolType: string,
|
|
4
|
+
registrationFn: () => void,
|
|
5
|
+
payload: { logger: { info: (message: string) => void } },
|
|
6
|
+
useVerboseLogs: boolean,
|
|
7
|
+
) => {
|
|
8
|
+
if (isEnabled) {
|
|
9
|
+
try {
|
|
10
|
+
registrationFn()
|
|
11
|
+
if (useVerboseLogs) {
|
|
12
|
+
payload.logger.info(`[payload-mcp] ✅ Tool: ${toolType} Registered.`)
|
|
13
|
+
}
|
|
14
|
+
} catch (error) {
|
|
15
|
+
// Log the error and re-throw
|
|
16
|
+
payload.logger.info(`[payload-mcp] ❌ Tool: ${toolType} Failed to register.`)
|
|
17
|
+
throw error
|
|
18
|
+
}
|
|
19
|
+
} else if (useVerboseLogs) {
|
|
20
|
+
payload.logger.info(`[payload-mcp] ⏭️ Tool: ${toolType} Skipped.`)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import type { PayloadRequest } from 'payload'
|
|
3
|
+
|
|
4
|
+
import { toolSchemas } from '../schemas.js'
|
|
5
|
+
|
|
6
|
+
export const authTool = (server: McpServer, req: PayloadRequest, verboseLogs: boolean) => {
|
|
7
|
+
const tool = async (headers?: string) => {
|
|
8
|
+
const payload = req.payload
|
|
9
|
+
|
|
10
|
+
if (verboseLogs) {
|
|
11
|
+
payload.logger.info('[payload-mcp] Checking authentication status')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
// Parse custom headers if provided, otherwise use empty headers
|
|
16
|
+
let authHeaders = new Headers()
|
|
17
|
+
|
|
18
|
+
if (headers) {
|
|
19
|
+
try {
|
|
20
|
+
const parsedHeaders = JSON.parse(headers)
|
|
21
|
+
authHeaders = new Headers(parsedHeaders)
|
|
22
|
+
if (verboseLogs) {
|
|
23
|
+
payload.logger.info(`[payload-mcp] Using custom headers: ${headers}`)
|
|
24
|
+
}
|
|
25
|
+
} catch (_ignore) {
|
|
26
|
+
payload.logger.warn(`[payload-mcp] Invalid headers JSON: ${headers}, using empty headers`)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const result = await payload.auth({
|
|
31
|
+
headers: authHeaders,
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
if (verboseLogs) {
|
|
35
|
+
payload.logger.info('[payload-mcp] Authentication check completed successfully')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
content: [
|
|
40
|
+
{
|
|
41
|
+
type: 'text' as const,
|
|
42
|
+
text: `# Authentication Status\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
} catch (error) {
|
|
47
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
48
|
+
payload.logger.error(`[payload-mcp] Error checking authentication: ${errorMessage}`)
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
content: [
|
|
52
|
+
{
|
|
53
|
+
type: 'text' as const,
|
|
54
|
+
text: `❌ **Error checking authentication**: ${errorMessage}`,
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
server.tool(
|
|
62
|
+
'auth',
|
|
63
|
+
toolSchemas.auth.description,
|
|
64
|
+
toolSchemas.auth.parameters.shape,
|
|
65
|
+
async ({ headers }) => {
|
|
66
|
+
return await tool(headers)
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import type { PayloadRequest } from 'payload'
|
|
3
|
+
|
|
4
|
+
import { toolSchemas } from '../schemas.js'
|
|
5
|
+
|
|
6
|
+
export const forgotPasswordTool = (
|
|
7
|
+
server: McpServer,
|
|
8
|
+
req: PayloadRequest,
|
|
9
|
+
verboseLogs: boolean,
|
|
10
|
+
) => {
|
|
11
|
+
const tool = async (collection: string, email: string, disableEmail: boolean = false) => {
|
|
12
|
+
const payload = req.payload
|
|
13
|
+
|
|
14
|
+
if (verboseLogs) {
|
|
15
|
+
payload.logger.info(
|
|
16
|
+
`[payload-mcp] Sending password reset email for user: ${email} in collection: ${collection}`,
|
|
17
|
+
)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const result = await payload.forgotPassword({
|
|
22
|
+
collection,
|
|
23
|
+
data: {
|
|
24
|
+
email,
|
|
25
|
+
},
|
|
26
|
+
disableEmail,
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
if (verboseLogs) {
|
|
30
|
+
payload.logger.info(
|
|
31
|
+
`[payload-mcp] Password reset email sent successfully for user: ${email}`,
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
content: [
|
|
37
|
+
{
|
|
38
|
+
type: 'text' as const,
|
|
39
|
+
text: `# Password Reset Email Sent\n\n**User:** ${email}\n**Collection:** ${collection}\n**Email Disabled:** ${disableEmail}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
}
|
|
43
|
+
} catch (error) {
|
|
44
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
45
|
+
payload.logger.error(
|
|
46
|
+
`[payload-mcp] Error sending password reset email for user ${email}: ${errorMessage}`,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
content: [
|
|
51
|
+
{
|
|
52
|
+
type: 'text' as const,
|
|
53
|
+
text: `❌ **Error sending password reset email for user "${email}"**: ${errorMessage}`,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
server.tool(
|
|
61
|
+
'forgotPassword',
|
|
62
|
+
toolSchemas.forgotPassword.description,
|
|
63
|
+
toolSchemas.forgotPassword.parameters.shape,
|
|
64
|
+
async ({ collection, disableEmail, email }) => {
|
|
65
|
+
return await tool(collection, email, disableEmail)
|
|
66
|
+
},
|
|
67
|
+
)
|
|
68
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import type { PayloadRequest } from 'payload'
|
|
3
|
+
|
|
4
|
+
import { toolSchemas } from '../schemas.js'
|
|
5
|
+
|
|
6
|
+
export const loginTool = (server: McpServer, req: PayloadRequest, verboseLogs: boolean) => {
|
|
7
|
+
const tool = async (
|
|
8
|
+
collection: string,
|
|
9
|
+
email: string,
|
|
10
|
+
password: string,
|
|
11
|
+
depth: number = 0,
|
|
12
|
+
overrideAccess: boolean = false,
|
|
13
|
+
showHiddenFields: boolean = false,
|
|
14
|
+
) => {
|
|
15
|
+
const payload = req.payload
|
|
16
|
+
|
|
17
|
+
if (verboseLogs) {
|
|
18
|
+
payload.logger.info(
|
|
19
|
+
`[payload-mcp] Attempting login for user: ${email} in collection: ${collection}`,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const result = await payload.login({
|
|
25
|
+
collection,
|
|
26
|
+
data: {
|
|
27
|
+
email,
|
|
28
|
+
password,
|
|
29
|
+
},
|
|
30
|
+
depth,
|
|
31
|
+
overrideAccess,
|
|
32
|
+
showHiddenFields,
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
if (verboseLogs) {
|
|
36
|
+
payload.logger.info(`[payload-mcp] Login successful for user: ${email}`)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
content: [
|
|
41
|
+
{
|
|
42
|
+
type: 'text' as const,
|
|
43
|
+
text: `# Login Successful\n\n**User:** ${email}\n**Collection:** ${collection}\n\n\`\`\`json\n${JSON.stringify(result, null, 2)}\n\`\`\``,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
}
|
|
47
|
+
} catch (error) {
|
|
48
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error'
|
|
49
|
+
payload.logger.error(`[payload-mcp] Login failed for user ${email}: ${errorMessage}`)
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
content: [
|
|
53
|
+
{
|
|
54
|
+
type: 'text' as const,
|
|
55
|
+
text: `❌ **Login failed for user "${email}"**: ${errorMessage}`,
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
server.tool(
|
|
63
|
+
'login',
|
|
64
|
+
toolSchemas.login.description,
|
|
65
|
+
toolSchemas.login.parameters.shape,
|
|
66
|
+
async ({ collection, depth, email, overrideAccess, password, showHiddenFields }) => {
|
|
67
|
+
return await tool(collection, email, password, depth, overrideAccess, showHiddenFields)
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
}
|