@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.
Files changed (173) hide show
  1. package/LICENSE.md +22 -0
  2. package/README.md +7 -0
  3. package/dist/collections/createApiKeysCollection.d.ts +7 -0
  4. package/dist/collections/createApiKeysCollection.d.ts.map +1 -0
  5. package/dist/collections/createApiKeysCollection.js +315 -0
  6. package/dist/collections/createApiKeysCollection.js.map +1 -0
  7. package/dist/endpoints/mcp.d.ts +4 -0
  8. package/dist/endpoints/mcp.d.ts.map +1 -0
  9. package/dist/endpoints/mcp.js +44 -0
  10. package/dist/endpoints/mcp.js.map +1 -0
  11. package/dist/index.d.ts +10 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +67 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/mcp/createRequest.d.ts +3 -0
  16. package/dist/mcp/createRequest.d.ts.map +1 -0
  17. package/dist/mcp/createRequest.js +14 -0
  18. package/dist/mcp/createRequest.js.map +1 -0
  19. package/dist/mcp/getMcpHandler.d.ts +4 -0
  20. package/dist/mcp/getMcpHandler.d.ts.map +1 -0
  21. package/dist/mcp/getMcpHandler.js +179 -0
  22. package/dist/mcp/getMcpHandler.js.map +1 -0
  23. package/dist/mcp/helpers/config.d.ts +30 -0
  24. package/dist/mcp/helpers/config.d.ts.map +1 -0
  25. package/dist/mcp/helpers/config.js +217 -0
  26. package/dist/mcp/helpers/config.js.map +1 -0
  27. package/dist/mcp/helpers/conversion.d.ts +2 -0
  28. package/dist/mcp/helpers/conversion.d.ts.map +1 -0
  29. package/dist/mcp/helpers/conversion.js +5 -0
  30. package/dist/mcp/helpers/conversion.js.map +1 -0
  31. package/dist/mcp/helpers/fields.d.ts +38 -0
  32. package/dist/mcp/helpers/fields.d.ts.map +1 -0
  33. package/dist/mcp/helpers/fields.js +96 -0
  34. package/dist/mcp/helpers/fields.js.map +1 -0
  35. package/dist/mcp/helpers/fileValidation.d.ts +69 -0
  36. package/dist/mcp/helpers/fileValidation.d.ts.map +1 -0
  37. package/dist/mcp/helpers/fileValidation.js +305 -0
  38. package/dist/mcp/helpers/fileValidation.js.map +1 -0
  39. package/dist/mcp/helpers/validation.d.ts +9 -0
  40. package/dist/mcp/helpers/validation.d.ts.map +1 -0
  41. package/dist/mcp/helpers/validation.js +22 -0
  42. package/dist/mcp/helpers/validation.js.map +1 -0
  43. package/dist/mcp/registerTool.d.ts +6 -0
  44. package/dist/mcp/registerTool.d.ts.map +1 -0
  45. package/dist/mcp/registerTool.js +18 -0
  46. package/dist/mcp/registerTool.js.map +1 -0
  47. package/dist/mcp/tools/auth/auth.d.ts +4 -0
  48. package/dist/mcp/tools/auth/auth.d.ts.map +1 -0
  49. package/dist/mcp/tools/auth/auth.js +54 -0
  50. package/dist/mcp/tools/auth/auth.js.map +1 -0
  51. package/dist/mcp/tools/auth/forgotPassword.d.ts +4 -0
  52. package/dist/mcp/tools/auth/forgotPassword.d.ts.map +1 -0
  53. package/dist/mcp/tools/auth/forgotPassword.js +45 -0
  54. package/dist/mcp/tools/auth/forgotPassword.js.map +1 -0
  55. package/dist/mcp/tools/auth/login.d.ts +4 -0
  56. package/dist/mcp/tools/auth/login.d.ts.map +1 -0
  57. package/dist/mcp/tools/auth/login.js +48 -0
  58. package/dist/mcp/tools/auth/login.js.map +1 -0
  59. package/dist/mcp/tools/auth/resetPassword.d.ts +4 -0
  60. package/dist/mcp/tools/auth/resetPassword.d.ts.map +1 -0
  61. package/dist/mcp/tools/auth/resetPassword.js +46 -0
  62. package/dist/mcp/tools/auth/resetPassword.js.map +1 -0
  63. package/dist/mcp/tools/auth/unlock.d.ts +4 -0
  64. package/dist/mcp/tools/auth/unlock.d.ts.map +1 -0
  65. package/dist/mcp/tools/auth/unlock.js +45 -0
  66. package/dist/mcp/tools/auth/unlock.js.map +1 -0
  67. package/dist/mcp/tools/auth/verify.d.ts +4 -0
  68. package/dist/mcp/tools/auth/verify.d.ts.map +1 -0
  69. package/dist/mcp/tools/auth/verify.js +42 -0
  70. package/dist/mcp/tools/auth/verify.js.map +1 -0
  71. package/dist/mcp/tools/collection/create.d.ts +10 -0
  72. package/dist/mcp/tools/collection/create.d.ts.map +1 -0
  73. package/dist/mcp/tools/collection/create.js +159 -0
  74. package/dist/mcp/tools/collection/create.js.map +1 -0
  75. package/dist/mcp/tools/collection/delete.d.ts +10 -0
  76. package/dist/mcp/tools/collection/delete.d.ts.map +1 -0
  77. package/dist/mcp/tools/collection/delete.js +162 -0
  78. package/dist/mcp/tools/collection/delete.js.map +1 -0
  79. package/dist/mcp/tools/collection/find.d.ts +10 -0
  80. package/dist/mcp/tools/collection/find.d.ts.map +1 -0
  81. package/dist/mcp/tools/collection/find.js +162 -0
  82. package/dist/mcp/tools/collection/find.js.map +1 -0
  83. package/dist/mcp/tools/collection/update.d.ts +10 -0
  84. package/dist/mcp/tools/collection/update.d.ts.map +1 -0
  85. package/dist/mcp/tools/collection/update.js +206 -0
  86. package/dist/mcp/tools/collection/update.js.map +1 -0
  87. package/dist/mcp/tools/config/find.d.ts +10 -0
  88. package/dist/mcp/tools/config/find.d.ts.map +1 -0
  89. package/dist/mcp/tools/config/find.js +94 -0
  90. package/dist/mcp/tools/config/find.js.map +1 -0
  91. package/dist/mcp/tools/config/update.d.ts +10 -0
  92. package/dist/mcp/tools/config/update.d.ts.map +1 -0
  93. package/dist/mcp/tools/config/update.js +212 -0
  94. package/dist/mcp/tools/config/update.js.map +1 -0
  95. package/dist/mcp/tools/job/create.d.ts +10 -0
  96. package/dist/mcp/tools/job/create.d.ts.map +1 -0
  97. package/dist/mcp/tools/job/create.js +293 -0
  98. package/dist/mcp/tools/job/create.js.map +1 -0
  99. package/dist/mcp/tools/job/run.d.ts +10 -0
  100. package/dist/mcp/tools/job/run.d.ts.map +1 -0
  101. package/dist/mcp/tools/job/run.js +147 -0
  102. package/dist/mcp/tools/job/run.js.map +1 -0
  103. package/dist/mcp/tools/job/update.d.ts +11 -0
  104. package/dist/mcp/tools/job/update.d.ts.map +1 -0
  105. package/dist/mcp/tools/job/update.js +211 -0
  106. package/dist/mcp/tools/job/update.js.map +1 -0
  107. package/dist/mcp/tools/resource/create.d.ts +6 -0
  108. package/dist/mcp/tools/resource/create.d.ts.map +1 -0
  109. package/dist/mcp/tools/resource/create.js +75 -0
  110. package/dist/mcp/tools/resource/create.js.map +1 -0
  111. package/dist/mcp/tools/resource/delete.d.ts +5 -0
  112. package/dist/mcp/tools/resource/delete.d.ts.map +1 -0
  113. package/dist/mcp/tools/resource/delete.js +140 -0
  114. package/dist/mcp/tools/resource/delete.js.map +1 -0
  115. package/dist/mcp/tools/resource/find.d.ts +5 -0
  116. package/dist/mcp/tools/resource/find.d.ts.map +1 -0
  117. package/dist/mcp/tools/resource/find.js +119 -0
  118. package/dist/mcp/tools/resource/find.js.map +1 -0
  119. package/dist/mcp/tools/resource/update.d.ts +6 -0
  120. package/dist/mcp/tools/resource/update.d.ts.map +1 -0
  121. package/dist/mcp/tools/resource/update.js +201 -0
  122. package/dist/mcp/tools/resource/update.js.map +1 -0
  123. package/dist/mcp/tools/schemas.d.ts +374 -0
  124. package/dist/mcp/tools/schemas.d.ts.map +1 -0
  125. package/dist/mcp/tools/schemas.js +201 -0
  126. package/dist/mcp/tools/schemas.js.map +1 -0
  127. package/dist/types.d.ts +379 -0
  128. package/dist/types.d.ts.map +1 -0
  129. package/dist/types.js +3 -0
  130. package/dist/types.js.map +1 -0
  131. package/dist/utils/camelCase.d.ts +9 -0
  132. package/dist/utils/camelCase.d.ts.map +1 -0
  133. package/dist/utils/camelCase.js +11 -0
  134. package/dist/utils/camelCase.js.map +1 -0
  135. package/dist/utils/convertCollectionSchemaToZod.d.ts +3 -0
  136. package/dist/utils/convertCollectionSchemaToZod.d.ts.map +1 -0
  137. package/dist/utils/convertCollectionSchemaToZod.js +30 -0
  138. package/dist/utils/convertCollectionSchemaToZod.js.map +1 -0
  139. package/package.json +64 -0
  140. package/src/collections/createApiKeysCollection.ts +393 -0
  141. package/src/endpoints/mcp.ts +60 -0
  142. package/src/index.ts +86 -0
  143. package/src/mcp/createRequest.ts +13 -0
  144. package/src/mcp/getMcpHandler.ts +433 -0
  145. package/src/mcp/helpers/config.ts +326 -0
  146. package/src/mcp/helpers/conversion.ts +3 -0
  147. package/src/mcp/helpers/fields.ts +158 -0
  148. package/src/mcp/helpers/fileValidation.ts +417 -0
  149. package/src/mcp/helpers/validation.ts +32 -0
  150. package/src/mcp/registerTool.ts +22 -0
  151. package/src/mcp/tools/auth/auth.ts +69 -0
  152. package/src/mcp/tools/auth/forgotPassword.ts +68 -0
  153. package/src/mcp/tools/auth/login.ts +70 -0
  154. package/src/mcp/tools/auth/resetPassword.ts +59 -0
  155. package/src/mcp/tools/auth/unlock.ts +62 -0
  156. package/src/mcp/tools/auth/verify.ts +55 -0
  157. package/src/mcp/tools/collection/create.ts +236 -0
  158. package/src/mcp/tools/collection/delete.ts +227 -0
  159. package/src/mcp/tools/collection/find.ts +222 -0
  160. package/src/mcp/tools/collection/update.ts +288 -0
  161. package/src/mcp/tools/config/find.ts +126 -0
  162. package/src/mcp/tools/config/update.ts +282 -0
  163. package/src/mcp/tools/job/create.ts +420 -0
  164. package/src/mcp/tools/job/run.ts +189 -0
  165. package/src/mcp/tools/job/update.ts +319 -0
  166. package/src/mcp/tools/resource/create.ts +121 -0
  167. package/src/mcp/tools/resource/delete.ts +210 -0
  168. package/src/mcp/tools/resource/find.ts +194 -0
  169. package/src/mcp/tools/resource/update.ts +314 -0
  170. package/src/mcp/tools/schemas.ts +373 -0
  171. package/src/types.ts +405 -0
  172. package/src/utils/camelCase.ts +12 -0
  173. package/src/utils/convertCollectionSchemaToZod.ts +35 -0
@@ -0,0 +1,393 @@
1
+ import type { CollectionConfig } from 'payload'
2
+
3
+ import type { PluginMCPServerConfig } from '../types.js'
4
+
5
+ import { toCamelCase } from '../utils/camelCase.js'
6
+
7
+ const addEnabledCollectionTools = (collections: PluginMCPServerConfig['collections']) => {
8
+ const enabledCollectionSlugs = Object.keys(collections || {}).filter((collection) => {
9
+ const fullyEnabled =
10
+ typeof collections?.[collection]?.enabled === 'boolean' && collections?.[collection]?.enabled
11
+
12
+ if (fullyEnabled) {
13
+ return true
14
+ }
15
+
16
+ const partiallyEnabled =
17
+ typeof collections?.[collection]?.enabled !== 'boolean' &&
18
+ ((typeof collections?.[collection]?.enabled?.find === 'boolean' &&
19
+ collections?.[collection]?.enabled?.find === true) ||
20
+ (typeof collections?.[collection]?.enabled?.create === 'boolean' &&
21
+ collections?.[collection]?.enabled?.create === true) ||
22
+ (typeof collections?.[collection]?.enabled?.update === 'boolean' &&
23
+ collections?.[collection]?.enabled?.update === true) ||
24
+ (typeof collections?.[collection]?.enabled?.delete === 'boolean' &&
25
+ collections?.[collection]?.enabled?.delete === true))
26
+
27
+ if (partiallyEnabled) {
28
+ return true
29
+ }
30
+ })
31
+ return enabledCollectionSlugs.map((enabledCollectionSlug) => ({
32
+ type: 'collapsible' as const,
33
+ admin: {
34
+ position: 'sidebar' as const,
35
+ },
36
+ fields: [
37
+ {
38
+ name: `${toCamelCase(enabledCollectionSlug)}`,
39
+ type: 'group' as const,
40
+ fields: [
41
+ ...(collections?.[enabledCollectionSlug]?.enabled === true ||
42
+ (typeof collections?.[enabledCollectionSlug]?.enabled !== 'boolean' &&
43
+ typeof collections?.[enabledCollectionSlug]?.enabled?.find === 'boolean' &&
44
+ collections?.[enabledCollectionSlug]?.enabled?.find === true)
45
+ ? [
46
+ {
47
+ name: `find`,
48
+ type: 'checkbox' as const,
49
+ admin: {
50
+ description: `Allow clients to find ${enabledCollectionSlug}.`,
51
+ },
52
+ defaultValue: false,
53
+ label: 'Find',
54
+ },
55
+ ]
56
+ : []),
57
+
58
+ ...(collections?.[enabledCollectionSlug]?.enabled === true ||
59
+ (typeof collections?.[enabledCollectionSlug]?.enabled !== 'boolean' &&
60
+ typeof collections?.[enabledCollectionSlug]?.enabled?.create === 'boolean' &&
61
+ collections?.[enabledCollectionSlug]?.enabled?.create === true)
62
+ ? [
63
+ {
64
+ name: `create`,
65
+ type: 'checkbox' as const,
66
+ admin: {
67
+ description: `Allow clients to create ${enabledCollectionSlug}.`,
68
+ },
69
+ defaultValue: false,
70
+ label: 'Create',
71
+ },
72
+ ]
73
+ : []),
74
+
75
+ ...(collections?.[enabledCollectionSlug]?.enabled === true ||
76
+ (typeof collections?.[enabledCollectionSlug]?.enabled !== 'boolean' &&
77
+ typeof collections?.[enabledCollectionSlug]?.enabled?.update === 'boolean' &&
78
+ collections?.[enabledCollectionSlug]?.enabled?.update === true)
79
+ ? [
80
+ {
81
+ name: `update`,
82
+ type: 'checkbox' as const,
83
+ admin: {
84
+ description: `Allow clients to update ${enabledCollectionSlug}.`,
85
+ },
86
+ defaultValue: false,
87
+ label: 'Update',
88
+ },
89
+ ]
90
+ : []),
91
+
92
+ ...(collections?.[enabledCollectionSlug]?.enabled === true ||
93
+ (typeof collections?.[enabledCollectionSlug]?.enabled !== 'boolean' &&
94
+ typeof collections?.[enabledCollectionSlug]?.enabled?.delete === 'boolean' &&
95
+ collections?.[enabledCollectionSlug]?.enabled?.delete === true)
96
+ ? [
97
+ {
98
+ name: `delete`,
99
+ type: 'checkbox' as const,
100
+ admin: {
101
+ description: `Allow clients to delete ${enabledCollectionSlug}.`,
102
+ },
103
+ defaultValue: false,
104
+ label: 'Delete',
105
+ },
106
+ ]
107
+ : []),
108
+ ],
109
+ },
110
+ ],
111
+ label: `${enabledCollectionSlug.charAt(0).toUpperCase() + toCamelCase(enabledCollectionSlug).slice(1)}`,
112
+ }))
113
+ }
114
+
115
+ export const createAPIKeysCollection = (
116
+ collections: PluginMCPServerConfig['collections'],
117
+ customTools: Array<{ description: string; name: string }> = [],
118
+ experimentalTools: NonNullable<PluginMCPServerConfig['experimental']>['tools'] = {},
119
+ ): CollectionConfig => {
120
+ const customToolsFields = customTools.map((tool) => {
121
+ const camelCasedName = toCamelCase(tool.name)
122
+ return {
123
+ name: camelCasedName,
124
+ type: 'checkbox' as const,
125
+ admin: {
126
+ description: tool.description,
127
+ },
128
+ defaultValue: true,
129
+ label: camelCasedName,
130
+ }
131
+ })
132
+
133
+ return {
134
+ slug: 'payload-mcp-api-keys',
135
+ admin: {
136
+ group: 'MCP',
137
+ useAsTitle: 'label',
138
+ },
139
+ auth: {
140
+ disableLocalStrategy: true,
141
+ useAPIKey: true,
142
+ },
143
+ fields: [
144
+ {
145
+ name: 'user',
146
+ type: 'relationship',
147
+ admin: {
148
+ description: 'The user that the API key is associated with.',
149
+ },
150
+ relationTo: 'users',
151
+ required: true,
152
+ },
153
+ {
154
+ name: 'label',
155
+ type: 'text',
156
+ admin: {
157
+ description: 'A useful label for the API key.',
158
+ },
159
+ },
160
+ {
161
+ name: 'description',
162
+ type: 'text',
163
+ admin: {
164
+ description: 'The purpose of the API key.',
165
+ },
166
+ },
167
+
168
+ ...addEnabledCollectionTools(collections),
169
+
170
+ ...(customTools.length > 0
171
+ ? [
172
+ {
173
+ type: 'collapsible' as const,
174
+ admin: {
175
+ position: 'sidebar' as const,
176
+ },
177
+ fields: [
178
+ {
179
+ name: 'custom',
180
+ type: 'group' as const,
181
+ fields: customToolsFields,
182
+ },
183
+ ],
184
+ label: 'Custom Tools',
185
+ },
186
+ ]
187
+ : []),
188
+
189
+ // Experimental Tools
190
+ ...(process.env.NODE_ENV === 'development' &&
191
+ (experimentalTools?.collections?.enabled ||
192
+ experimentalTools?.jobs?.enabled ||
193
+ experimentalTools?.config?.enabled ||
194
+ experimentalTools?.auth?.enabled)
195
+ ? [
196
+ {
197
+ type: 'collapsible' as const,
198
+ admin: {
199
+ position: 'sidebar' as const,
200
+ },
201
+ fields: [
202
+ ...(experimentalTools?.collections?.enabled
203
+ ? [
204
+ {
205
+ name: 'collections',
206
+ type: 'group' as const,
207
+ fields: [
208
+ {
209
+ name: 'find',
210
+ type: 'checkbox' as const,
211
+ admin: {
212
+ description:
213
+ 'Allow LLMs to find and list Payload collections with optional content and document counts.',
214
+ },
215
+ defaultValue: false,
216
+ },
217
+ {
218
+ name: 'create',
219
+ type: 'checkbox' as const,
220
+ admin: {
221
+ description:
222
+ 'Allow LLMs to create new Payload collections with specified fields and configuration.',
223
+ },
224
+ defaultValue: false,
225
+ },
226
+ {
227
+ name: 'update',
228
+ type: 'checkbox' as const,
229
+ admin: {
230
+ description:
231
+ 'Allow LLMs to update existing Payload collections with new fields, modifications, or configuration changes.',
232
+ },
233
+ defaultValue: false,
234
+ },
235
+ {
236
+ name: 'delete',
237
+ type: 'checkbox' as const,
238
+ admin: {
239
+ description:
240
+ 'Allow LLMs to delete Payload collections and optionally update the configuration.',
241
+ },
242
+ defaultValue: false,
243
+ },
244
+ ],
245
+ },
246
+ ]
247
+ : []),
248
+ ...(experimentalTools?.jobs?.enabled
249
+ ? [
250
+ {
251
+ name: 'jobs',
252
+ type: 'group' as const,
253
+ fields: [
254
+ {
255
+ name: 'create',
256
+ type: 'checkbox' as const,
257
+ admin: {
258
+ description:
259
+ 'Allow LLMs to create new Payload jobs (tasks and workflows) with custom schemas and configuration.',
260
+ },
261
+ defaultValue: false,
262
+ },
263
+ {
264
+ name: 'run',
265
+ type: 'checkbox' as const,
266
+ admin: {
267
+ description:
268
+ 'Allow LLMs to execute Payload jobs with custom input data and queue options.',
269
+ },
270
+ defaultValue: false,
271
+ },
272
+ {
273
+ name: 'update',
274
+ type: 'checkbox' as const,
275
+ admin: {
276
+ description:
277
+ 'Allow LLMs to update existing Payload jobs with new schemas, configuration, or handler code.',
278
+ },
279
+ defaultValue: false,
280
+ },
281
+ ],
282
+ },
283
+ ]
284
+ : []),
285
+ ...(experimentalTools?.config?.enabled
286
+ ? [
287
+ {
288
+ name: 'config',
289
+ type: 'group' as const,
290
+ fields: [
291
+ {
292
+ name: 'find',
293
+ type: 'checkbox' as const,
294
+ admin: {
295
+ description:
296
+ 'Allow LLMs to read and display a Payload configuration file.',
297
+ },
298
+ defaultValue: false,
299
+ },
300
+ {
301
+ name: 'update',
302
+ type: 'checkbox' as const,
303
+ admin: {
304
+ description:
305
+ 'Allow LLMs to update a Payload configuration file with various modifications.',
306
+ },
307
+ defaultValue: false,
308
+ },
309
+ ],
310
+ },
311
+ ]
312
+ : []),
313
+ ...(experimentalTools?.auth?.enabled
314
+ ? [
315
+ {
316
+ name: 'auth',
317
+ type: 'group' as const,
318
+ fields: [
319
+ {
320
+ name: 'auth',
321
+ type: 'checkbox' as const,
322
+ admin: {
323
+ description:
324
+ 'Allow LLMs to check authentication status for a user by setting custom headers. (e.g. {"Authorization": "Bearer <token>"})',
325
+ },
326
+ defaultValue: false,
327
+ label: 'Check Auth Status',
328
+ },
329
+ {
330
+ name: 'login',
331
+ type: 'checkbox' as const,
332
+ admin: {
333
+ description:
334
+ 'Allow LLMs to authenticate a user with email and password.',
335
+ },
336
+ defaultValue: false,
337
+ label: 'User Login',
338
+ },
339
+ {
340
+ name: 'verify',
341
+ type: 'checkbox' as const,
342
+ admin: {
343
+ description:
344
+ 'Allow LLMs to verify a user email with a verification token.',
345
+ },
346
+ defaultValue: false,
347
+ label: 'Email Verification',
348
+ },
349
+ {
350
+ name: 'resetPassword',
351
+ type: 'checkbox' as const,
352
+ admin: {
353
+ description:
354
+ 'Allow LLMs to reset a user password with a reset token.',
355
+ },
356
+ defaultValue: false,
357
+ label: 'Reset Password',
358
+ },
359
+ {
360
+ name: 'forgotPassword',
361
+ type: 'checkbox' as const,
362
+ admin: {
363
+ description: 'Allow LLMs to send a password reset email to a user.',
364
+ },
365
+ defaultValue: false,
366
+ label: 'Forgot Password',
367
+ },
368
+ {
369
+ name: 'unlock',
370
+ type: 'checkbox' as const,
371
+ admin: {
372
+ description:
373
+ 'Allow LLMs to unlock a user account that has been locked due to failed login attempts.',
374
+ },
375
+ defaultValue: false,
376
+ label: 'Unlock Account',
377
+ },
378
+ ],
379
+ },
380
+ ]
381
+ : []),
382
+ ],
383
+ label: 'Experimental Tools',
384
+ },
385
+ ]
386
+ : []),
387
+ ],
388
+ labels: {
389
+ plural: 'API Keys',
390
+ singular: 'API Key',
391
+ },
392
+ }
393
+ }
@@ -0,0 +1,60 @@
1
+ import crypto from 'crypto'
2
+ import { APIError, type PayloadHandler, type Where } from 'payload'
3
+
4
+ import type { PluginMCPServerConfig, ToolSettings } from '../types.js'
5
+
6
+ import { createRequestFromPayloadRequest } from '../mcp/createRequest.js'
7
+ import { getMCPHandler } from '../mcp/getMcpHandler.js'
8
+
9
+ export const initializeMCPHandler = (pluginOptions: PluginMCPServerConfig) => {
10
+ const mcpHandler: PayloadHandler = async (req) => {
11
+ const { payload } = req
12
+ const MCPOptions = pluginOptions.mcp || {}
13
+ const MCPHandlerOptions = MCPOptions.handlerOptions || {}
14
+ const useVerboseLogs = MCPHandlerOptions.verboseLogs ?? false
15
+
16
+ const apiKey = req.headers.get('Authorization')?.startsWith('Bearer ')
17
+ ? req.headers.get('Authorization')?.replace('Bearer ', '').trim()
18
+ : null
19
+
20
+ if (apiKey === null) {
21
+ throw new APIError('API Key is required', 401)
22
+ }
23
+
24
+ const sha256APIKeyIndex = crypto
25
+ .createHmac('sha256', payload.secret)
26
+ .update(apiKey || '')
27
+ .digest('hex')
28
+
29
+ const apiKeyConstraints = [
30
+ {
31
+ apiKeyIndex: {
32
+ equals: sha256APIKeyIndex,
33
+ },
34
+ },
35
+ ]
36
+ const where: Where = {
37
+ or: apiKeyConstraints,
38
+ }
39
+
40
+ const { docs } = await payload.find({
41
+ collection: 'payload-mcp-api-keys',
42
+ where,
43
+ })
44
+
45
+ if (docs.length === 0) {
46
+ throw new APIError('API Key is invalid', 401)
47
+ }
48
+
49
+ const toolSettings = docs[0] as ToolSettings
50
+
51
+ if (useVerboseLogs) {
52
+ payload.logger.info('[payload-mcp] API Key is valid')
53
+ }
54
+
55
+ const handler = getMCPHandler(pluginOptions, toolSettings, req)
56
+ const request = createRequestFromPayloadRequest(req)
57
+ return await handler(request)
58
+ }
59
+ return mcpHandler
60
+ }
package/src/index.ts ADDED
@@ -0,0 +1,86 @@
1
+ import type { Config } from 'payload'
2
+
3
+ import type { PluginMCPServerConfig } from './types.js'
4
+
5
+ import { createAPIKeysCollection } from './collections/createApiKeysCollection.js'
6
+ import { initializeMCPHandler } from './endpoints/mcp.js'
7
+
8
+ /**
9
+ * The MCP Plugin for Payload. This plugin allows you to add MCP capabilities to your Payload project.
10
+ *
11
+ * @param pluginOptions - The options for the MCP plugin.
12
+ * @experimental This plugin is experimental and may change in the future.
13
+ */
14
+ export const mcpPlugin =
15
+ (pluginOptions: PluginMCPServerConfig) =>
16
+ (config: Config): Config => {
17
+ if (!config.collections) {
18
+ config.collections = []
19
+ }
20
+
21
+ // Collections
22
+ const collections = pluginOptions.collections || {}
23
+ // Extract custom tools for the global config
24
+ const customTools =
25
+ pluginOptions.mcp?.tools?.map((tool) => ({
26
+ name: tool.name,
27
+ description: tool.description,
28
+ })) || []
29
+
30
+ const experimentalTools = pluginOptions?.experimental?.tools || {}
31
+
32
+ /**
33
+ * API Keys
34
+ * --------
35
+ * High resolution control over MCP capabilities is crucial when using Payload with LLMs.
36
+ *
37
+ * This API Keys collection has ways for admins to create API keys and allow or disallow the MCP capabilities.
38
+ * This is useful when Admins want to allow or disallow the use of the MCP capabilities in real time.
39
+ * For example:
40
+ * - If a collection has all of its capabilities enabled, admins can allow or disallow the create, update, delete, and find capabilities on that collection.
41
+ * - If a collection only has the find capability enabled, admins can only allow or disallow the find capability on that collection.
42
+ * - If a custom tool has gone haywire, admins can disallow that tool.
43
+ *
44
+ */
45
+ const apiKeyCollection = createAPIKeysCollection(collections, customTools, experimentalTools)
46
+ if (pluginOptions.overrideApiKeyCollection) {
47
+ config.collections.push(pluginOptions.overrideApiKeyCollection(apiKeyCollection))
48
+ } else {
49
+ config.collections.push(apiKeyCollection)
50
+ }
51
+
52
+ /**
53
+ * If the plugin is disabled, we still want to keep added collections/fields so the database schema is consistent which is important for migrations.
54
+ * If your plugin heavily modifies the database schema, you may want to remove this property.
55
+ */
56
+ if (pluginOptions.disabled) {
57
+ return config
58
+ }
59
+
60
+ if (!config.endpoints) {
61
+ config.endpoints = []
62
+ }
63
+
64
+ /**
65
+ * This is the primary MCP Server Endpoint.
66
+ * Payload will automatically add the /api prefix to the path, so the full path is `/api/mcp`
67
+ * NOTE: This is only transport method until we add full support for SSE which will be another endpoint at `/api/sse`
68
+ */
69
+ config.endpoints.push({
70
+ handler: initializeMCPHandler(pluginOptions),
71
+ method: 'post',
72
+ path: '/mcp',
73
+ })
74
+
75
+ /**
76
+ * The GET response is always: {"jsonrpc":"2.0","error":{"code":-32000,"message":"Method not allowed."},"id":null} -- even with an API key
77
+ * This is expected behavior and MCP clients should always use the POST endpoint.
78
+ */
79
+ config.endpoints.push({
80
+ handler: initializeMCPHandler(pluginOptions),
81
+ method: 'get',
82
+ path: '/mcp',
83
+ })
84
+
85
+ return config
86
+ }
@@ -0,0 +1,13 @@
1
+ import { APIError, type PayloadRequest } from 'payload'
2
+
3
+ export const createRequestFromPayloadRequest = (req: PayloadRequest) => {
4
+ if (!req.url) {
5
+ throw new APIError('Request URL is required', 500)
6
+ }
7
+ return new Request(req.url, {
8
+ body: req.body,
9
+ duplex: 'half',
10
+ headers: req.headers,
11
+ method: req.method,
12
+ } as { duplex: 'half' } & RequestInit)
13
+ }