@payloadcms/plugin-mcp 3.86.0-internal.ac46214 → 4.0.0-canary.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 (329) hide show
  1. package/README.md +2 -2
  2. package/bin.js +39 -0
  3. package/dist/collection/getAccessField.d.ts +12 -0
  4. package/dist/collection/getAccessField.d.ts.map +1 -0
  5. package/dist/collection/getAccessField.js +57 -0
  6. package/dist/collection/getAccessField.js.map +1 -0
  7. package/dist/collection/index.d.ts +6 -0
  8. package/dist/collection/index.d.ts.map +1 -0
  9. package/dist/collection/index.js +59 -0
  10. package/dist/collection/index.js.map +1 -0
  11. package/dist/components/AccessField/index.client.d.ts +10 -0
  12. package/dist/components/AccessField/index.client.d.ts.map +1 -0
  13. package/dist/components/AccessField/index.client.js +305 -0
  14. package/dist/components/AccessField/index.client.js.map +1 -0
  15. package/dist/components/AccessField/index.css +93 -0
  16. package/dist/defineTool.d.ts +26 -0
  17. package/dist/defineTool.d.ts.map +1 -0
  18. package/dist/defineTool.js +37 -0
  19. package/dist/defineTool.js.map +1 -0
  20. package/dist/endpoint/access.d.ts +10 -0
  21. package/dist/endpoint/access.d.ts.map +1 -0
  22. package/dist/endpoint/access.js +106 -0
  23. package/dist/endpoint/access.js.map +1 -0
  24. package/dist/endpoint/index.d.ts +3 -0
  25. package/dist/endpoint/index.d.ts.map +1 -0
  26. package/dist/endpoint/index.js +36 -0
  27. package/dist/endpoint/index.js.map +1 -0
  28. package/dist/exports/client.d.ts +2 -0
  29. package/dist/exports/client.d.ts.map +1 -0
  30. package/dist/exports/client.js +4 -0
  31. package/dist/exports/client.js.map +1 -0
  32. package/dist/index.d.ts +4 -7
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +44 -67
  35. package/dist/index.js.map +1 -1
  36. package/dist/mcp/buildMcpServer.d.ts +19 -0
  37. package/dist/mcp/buildMcpServer.d.ts.map +1 -0
  38. package/dist/mcp/buildMcpServer.js +163 -0
  39. package/dist/mcp/buildMcpServer.js.map +1 -0
  40. package/dist/mcp/builtin/collections/authTools.d.ts +7 -0
  41. package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -0
  42. package/dist/mcp/builtin/collections/authTools.js +250 -0
  43. package/dist/mcp/builtin/collections/authTools.js.map +1 -0
  44. package/dist/mcp/builtin/collections/createTool.d.ts +2 -0
  45. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -0
  46. package/dist/mcp/builtin/collections/createTool.js +87 -0
  47. package/dist/mcp/builtin/collections/createTool.js.map +1 -0
  48. package/dist/mcp/builtin/collections/deleteTool.d.ts +2 -0
  49. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -0
  50. package/dist/mcp/builtin/collections/deleteTool.js +117 -0
  51. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -0
  52. package/dist/mcp/builtin/collections/findTool.d.ts +2 -0
  53. package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -0
  54. package/dist/mcp/builtin/collections/findTool.js +159 -0
  55. package/dist/mcp/builtin/collections/findTool.js.map +1 -0
  56. package/dist/mcp/builtin/collections/updateTool.d.ts +2 -0
  57. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -0
  58. package/dist/mcp/builtin/collections/updateTool.js +187 -0
  59. package/dist/mcp/builtin/collections/updateTool.js.map +1 -0
  60. package/dist/mcp/builtin/globals/findTool.d.ts +2 -0
  61. package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -0
  62. package/dist/mcp/builtin/globals/findTool.js +76 -0
  63. package/dist/mcp/builtin/globals/findTool.js.map +1 -0
  64. package/dist/mcp/builtin/globals/updateTool.d.ts +2 -0
  65. package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -0
  66. package/dist/mcp/builtin/globals/updateTool.js +92 -0
  67. package/dist/mcp/builtin/globals/updateTool.js.map +1 -0
  68. package/dist/mcp/builtinTools.d.ts +37 -0
  69. package/dist/mcp/builtinTools.d.ts.map +1 -0
  70. package/dist/mcp/builtinTools.js +64 -0
  71. package/dist/mcp/builtinTools.js.map +1 -0
  72. package/dist/mcp/sanitizeMCPConfig.d.ts +17 -0
  73. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -0
  74. package/dist/mcp/sanitizeMCPConfig.js +167 -0
  75. package/dist/mcp/sanitizeMCPConfig.js.map +1 -0
  76. package/dist/stdio.d.ts +8 -0
  77. package/dist/stdio.d.ts.map +1 -0
  78. package/dist/stdio.js +89 -0
  79. package/dist/stdio.js.map +1 -0
  80. package/dist/types.d.ts +262 -455
  81. package/dist/types.d.ts.map +1 -1
  82. package/dist/types.js +6 -1
  83. package/dist/types.js.map +1 -1
  84. package/dist/utils/camelCase.d.ts.map +1 -1
  85. package/dist/utils/getLogger.d.ts +10 -0
  86. package/dist/utils/getLogger.d.ts.map +1 -0
  87. package/dist/utils/getLogger.js +22 -0
  88. package/dist/utils/getLogger.js.map +1 -0
  89. package/dist/utils/getPluginConfig.d.ts +12 -0
  90. package/dist/utils/getPluginConfig.d.ts.map +1 -0
  91. package/dist/utils/getPluginConfig.js +15 -0
  92. package/dist/utils/getPluginConfig.js.map +1 -0
  93. package/dist/utils/localAPIDefaults.d.ts +20 -0
  94. package/dist/utils/localAPIDefaults.d.ts.map +1 -0
  95. package/dist/utils/localAPIDefaults.js +19 -0
  96. package/dist/utils/localAPIDefaults.js.map +1 -0
  97. package/dist/utils/resolveProjectRoot.d.ts +7 -0
  98. package/dist/utils/resolveProjectRoot.d.ts.map +1 -0
  99. package/dist/utils/resolveProjectRoot.js +15 -0
  100. package/dist/utils/resolveProjectRoot.js.map +1 -0
  101. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +7 -0
  102. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +1 -0
  103. package/dist/utils/schemaConversion/prepareCollectionSchema.js +37 -0
  104. package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +1 -0
  105. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +2 -2
  106. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +1 -1
  107. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +1 -1
  108. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +2 -2
  109. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +1 -1
  110. package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +1 -1
  111. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +2 -2
  112. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +1 -1
  113. package/dist/utils/schemaConversion/simplifyRelationshipFields.js +7 -4
  114. package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +1 -1
  115. package/dist/utils/schemaConversion/transformPointFields.d.ts +2 -2
  116. package/dist/utils/schemaConversion/transformPointFields.d.ts.map +1 -1
  117. package/dist/utils/schemaConversion/transformPointFields.js +7 -1
  118. package/dist/utils/schemaConversion/transformPointFields.js.map +1 -1
  119. package/dist/utils/toStandardSchema.d.ts +5 -0
  120. package/dist/utils/toStandardSchema.d.ts.map +1 -0
  121. package/dist/utils/toStandardSchema.js +4 -0
  122. package/dist/utils/toStandardSchema.js.map +1 -0
  123. package/package.json +35 -10
  124. package/src/collection/getAccessField.ts +64 -0
  125. package/src/collection/index.ts +63 -0
  126. package/src/components/AccessField/index.client.tsx +344 -0
  127. package/src/components/AccessField/index.css +93 -0
  128. package/src/defineTool.ts +44 -0
  129. package/src/endpoint/access.ts +132 -0
  130. package/src/endpoint/index.ts +35 -0
  131. package/src/exports/client.ts +2 -0
  132. package/src/index.ts +35 -85
  133. package/src/mcp/buildMcpServer.ts +224 -0
  134. package/src/mcp/builtin/collections/authTools.ts +233 -0
  135. package/src/mcp/builtin/collections/createTool.ts +116 -0
  136. package/src/mcp/builtin/collections/deleteTool.ts +123 -0
  137. package/src/mcp/builtin/collections/findTool.ts +187 -0
  138. package/src/mcp/builtin/collections/updateTool.ts +205 -0
  139. package/src/mcp/builtin/globals/findTool.ts +96 -0
  140. package/src/mcp/builtin/globals/updateTool.ts +118 -0
  141. package/src/mcp/builtinTools.ts +84 -0
  142. package/src/mcp/sanitizeMCPConfig.ts +239 -0
  143. package/src/stdio.ts +98 -0
  144. package/src/types.ts +295 -490
  145. package/src/utils/getLogger.ts +22 -0
  146. package/src/utils/getPluginConfig.ts +24 -0
  147. package/src/utils/localAPIDefaults.ts +22 -0
  148. package/src/utils/resolveProjectRoot.ts +17 -0
  149. package/src/utils/schemaConversion/prepareCollectionSchema.ts +39 -0
  150. package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +3 -3
  151. package/src/utils/schemaConversion/sanitizeJsonSchema.ts +4 -4
  152. package/src/utils/schemaConversion/simplifyRelationshipFields.ts +11 -6
  153. package/src/utils/schemaConversion/transformPointFields.ts +6 -5
  154. package/src/utils/toStandardSchema.ts +9 -0
  155. package/dist/collections/createApiKeysCollection.d.ts +0 -7
  156. package/dist/collections/createApiKeysCollection.d.ts.map +0 -1
  157. package/dist/collections/createApiKeysCollection.js +0 -317
  158. package/dist/collections/createApiKeysCollection.js.map +0 -1
  159. package/dist/defaults.d.ts +0 -4
  160. package/dist/defaults.d.ts.map +0 -1
  161. package/dist/defaults.js +0 -5
  162. package/dist/defaults.js.map +0 -1
  163. package/dist/endpoints/mcp.d.ts +0 -4
  164. package/dist/endpoints/mcp.d.ts.map +0 -1
  165. package/dist/endpoints/mcp.js +0 -71
  166. package/dist/endpoints/mcp.js.map +0 -1
  167. package/dist/mcp/createRequest.d.ts +0 -3
  168. package/dist/mcp/createRequest.d.ts.map +0 -1
  169. package/dist/mcp/createRequest.js +0 -14
  170. package/dist/mcp/createRequest.js.map +0 -1
  171. package/dist/mcp/getMcpHandler.d.ts +0 -4
  172. package/dist/mcp/getMcpHandler.d.ts.map +0 -1
  173. package/dist/mcp/getMcpHandler.js +0 -231
  174. package/dist/mcp/getMcpHandler.js.map +0 -1
  175. package/dist/mcp/helpers/config.d.ts +0 -22
  176. package/dist/mcp/helpers/config.d.ts.map +0 -1
  177. package/dist/mcp/helpers/config.js +0 -153
  178. package/dist/mcp/helpers/config.js.map +0 -1
  179. package/dist/mcp/helpers/fields.d.ts +0 -19
  180. package/dist/mcp/helpers/fields.d.ts.map +0 -1
  181. package/dist/mcp/helpers/fields.js +0 -102
  182. package/dist/mcp/helpers/fields.js.map +0 -1
  183. package/dist/mcp/helpers/fileValidation.d.ts +0 -67
  184. package/dist/mcp/helpers/fileValidation.d.ts.map +0 -1
  185. package/dist/mcp/helpers/fileValidation.js +0 -267
  186. package/dist/mcp/helpers/fileValidation.js.map +0 -1
  187. package/dist/mcp/registerTool.d.ts +0 -6
  188. package/dist/mcp/registerTool.d.ts.map +0 -1
  189. package/dist/mcp/registerTool.js +0 -18
  190. package/dist/mcp/registerTool.js.map +0 -1
  191. package/dist/mcp/tools/auth/auth.d.ts +0 -4
  192. package/dist/mcp/tools/auth/auth.d.ts.map +0 -1
  193. package/dist/mcp/tools/auth/auth.js +0 -57
  194. package/dist/mcp/tools/auth/auth.js.map +0 -1
  195. package/dist/mcp/tools/auth/forgotPassword.d.ts +0 -4
  196. package/dist/mcp/tools/auth/forgotPassword.d.ts.map +0 -1
  197. package/dist/mcp/tools/auth/forgotPassword.js +0 -48
  198. package/dist/mcp/tools/auth/forgotPassword.js.map +0 -1
  199. package/dist/mcp/tools/auth/login.d.ts +0 -4
  200. package/dist/mcp/tools/auth/login.d.ts.map +0 -1
  201. package/dist/mcp/tools/auth/login.js +0 -51
  202. package/dist/mcp/tools/auth/login.js.map +0 -1
  203. package/dist/mcp/tools/auth/resetPassword.d.ts +0 -4
  204. package/dist/mcp/tools/auth/resetPassword.d.ts.map +0 -1
  205. package/dist/mcp/tools/auth/resetPassword.js +0 -49
  206. package/dist/mcp/tools/auth/resetPassword.js.map +0 -1
  207. package/dist/mcp/tools/auth/unlock.d.ts +0 -4
  208. package/dist/mcp/tools/auth/unlock.d.ts.map +0 -1
  209. package/dist/mcp/tools/auth/unlock.js +0 -48
  210. package/dist/mcp/tools/auth/unlock.js.map +0 -1
  211. package/dist/mcp/tools/auth/verify.d.ts +0 -4
  212. package/dist/mcp/tools/auth/verify.d.ts.map +0 -1
  213. package/dist/mcp/tools/auth/verify.js +0 -45
  214. package/dist/mcp/tools/auth/verify.js.map +0 -1
  215. package/dist/mcp/tools/collection/create.d.ts +0 -10
  216. package/dist/mcp/tools/collection/create.d.ts.map +0 -1
  217. package/dist/mcp/tools/collection/create.js +0 -139
  218. package/dist/mcp/tools/collection/create.js.map +0 -1
  219. package/dist/mcp/tools/collection/delete.d.ts +0 -10
  220. package/dist/mcp/tools/collection/delete.d.ts.map +0 -1
  221. package/dist/mcp/tools/collection/delete.js +0 -154
  222. package/dist/mcp/tools/collection/delete.js.map +0 -1
  223. package/dist/mcp/tools/collection/find.d.ts +0 -10
  224. package/dist/mcp/tools/collection/find.d.ts.map +0 -1
  225. package/dist/mcp/tools/collection/find.js +0 -165
  226. package/dist/mcp/tools/collection/find.js.map +0 -1
  227. package/dist/mcp/tools/collection/update.d.ts +0 -10
  228. package/dist/mcp/tools/collection/update.d.ts.map +0 -1
  229. package/dist/mcp/tools/collection/update.js +0 -209
  230. package/dist/mcp/tools/collection/update.js.map +0 -1
  231. package/dist/mcp/tools/config/find.d.ts +0 -10
  232. package/dist/mcp/tools/config/find.d.ts.map +0 -1
  233. package/dist/mcp/tools/config/find.js +0 -97
  234. package/dist/mcp/tools/config/find.js.map +0 -1
  235. package/dist/mcp/tools/config/update.d.ts +0 -10
  236. package/dist/mcp/tools/config/update.d.ts.map +0 -1
  237. package/dist/mcp/tools/config/update.js +0 -215
  238. package/dist/mcp/tools/config/update.js.map +0 -1
  239. package/dist/mcp/tools/global/find.d.ts +0 -5
  240. package/dist/mcp/tools/global/find.d.ts.map +0 -1
  241. package/dist/mcp/tools/global/find.js +0 -82
  242. package/dist/mcp/tools/global/find.js.map +0 -1
  243. package/dist/mcp/tools/global/update.d.ts +0 -6
  244. package/dist/mcp/tools/global/update.d.ts.map +0 -1
  245. package/dist/mcp/tools/global/update.js +0 -124
  246. package/dist/mcp/tools/global/update.js.map +0 -1
  247. package/dist/mcp/tools/job/create.d.ts +0 -10
  248. package/dist/mcp/tools/job/create.d.ts.map +0 -1
  249. package/dist/mcp/tools/job/create.js +0 -293
  250. package/dist/mcp/tools/job/create.js.map +0 -1
  251. package/dist/mcp/tools/job/run.d.ts +0 -10
  252. package/dist/mcp/tools/job/run.d.ts.map +0 -1
  253. package/dist/mcp/tools/job/run.js +0 -129
  254. package/dist/mcp/tools/job/run.js.map +0 -1
  255. package/dist/mcp/tools/job/update.d.ts +0 -11
  256. package/dist/mcp/tools/job/update.d.ts.map +0 -1
  257. package/dist/mcp/tools/job/update.js +0 -186
  258. package/dist/mcp/tools/job/update.js.map +0 -1
  259. package/dist/mcp/tools/resource/create.d.ts +0 -6
  260. package/dist/mcp/tools/resource/create.d.ts.map +0 -1
  261. package/dist/mcp/tools/resource/create.js +0 -124
  262. package/dist/mcp/tools/resource/create.js.map +0 -1
  263. package/dist/mcp/tools/resource/delete.d.ts +0 -5
  264. package/dist/mcp/tools/resource/delete.d.ts.map +0 -1
  265. package/dist/mcp/tools/resource/delete.js +0 -151
  266. package/dist/mcp/tools/resource/delete.js.map +0 -1
  267. package/dist/mcp/tools/resource/find.d.ts +0 -5
  268. package/dist/mcp/tools/resource/find.d.ts.map +0 -1
  269. package/dist/mcp/tools/resource/find.js +0 -170
  270. package/dist/mcp/tools/resource/find.js.map +0 -1
  271. package/dist/mcp/tools/resource/update.d.ts +0 -6
  272. package/dist/mcp/tools/resource/update.d.ts.map +0 -1
  273. package/dist/mcp/tools/resource/update.js +0 -256
  274. package/dist/mcp/tools/resource/update.js.map +0 -1
  275. package/dist/mcp/tools/schemas.d.ts +0 -457
  276. package/dist/mcp/tools/schemas.d.ts.map +0 -1
  277. package/dist/mcp/tools/schemas.js +0 -243
  278. package/dist/mcp/tools/schemas.js.map +0 -1
  279. package/dist/utils/adminEntitySettings.d.ts +0 -17
  280. package/dist/utils/adminEntitySettings.d.ts.map +0 -1
  281. package/dist/utils/adminEntitySettings.js +0 -41
  282. package/dist/utils/adminEntitySettings.js.map +0 -1
  283. package/dist/utils/createApiKeyFields.d.ts +0 -15
  284. package/dist/utils/createApiKeyFields.d.ts.map +0 -1
  285. package/dist/utils/createApiKeyFields.js +0 -57
  286. package/dist/utils/createApiKeyFields.js.map +0 -1
  287. package/dist/utils/getEnabledSlugs.d.ts +0 -13
  288. package/dist/utils/getEnabledSlugs.d.ts.map +0 -1
  289. package/dist/utils/getEnabledSlugs.js +0 -32
  290. package/dist/utils/getEnabledSlugs.js.map +0 -1
  291. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts +0 -3
  292. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts.map +0 -1
  293. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js +0 -43
  294. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js.map +0 -1
  295. package/src/collections/createApiKeysCollection.ts +0 -373
  296. package/src/defaults.ts +0 -3
  297. package/src/endpoints/mcp.ts +0 -91
  298. package/src/mcp/createRequest.ts +0 -13
  299. package/src/mcp/getMcpHandler.ts +0 -545
  300. package/src/mcp/helpers/config.ts +0 -213
  301. package/src/mcp/helpers/fields.ts +0 -154
  302. package/src/mcp/helpers/fileValidation.ts +0 -362
  303. package/src/mcp/registerTool.ts +0 -22
  304. package/src/mcp/tools/auth/auth.ts +0 -71
  305. package/src/mcp/tools/auth/forgotPassword.ts +0 -70
  306. package/src/mcp/tools/auth/login.ts +0 -72
  307. package/src/mcp/tools/auth/resetPassword.ts +0 -61
  308. package/src/mcp/tools/auth/unlock.ts +0 -64
  309. package/src/mcp/tools/auth/verify.ts +0 -57
  310. package/src/mcp/tools/collection/create.ts +0 -210
  311. package/src/mcp/tools/collection/delete.ts +0 -211
  312. package/src/mcp/tools/collection/find.ts +0 -224
  313. package/src/mcp/tools/collection/update.ts +0 -290
  314. package/src/mcp/tools/config/find.ts +0 -128
  315. package/src/mcp/tools/config/update.ts +0 -280
  316. package/src/mcp/tools/global/find.ts +0 -128
  317. package/src/mcp/tools/global/update.ts +0 -207
  318. package/src/mcp/tools/job/create.ts +0 -416
  319. package/src/mcp/tools/job/run.ts +0 -167
  320. package/src/mcp/tools/job/update.ts +0 -274
  321. package/src/mcp/tools/resource/create.ts +0 -211
  322. package/src/mcp/tools/resource/delete.ts +0 -218
  323. package/src/mcp/tools/resource/find.ts +0 -246
  324. package/src/mcp/tools/resource/update.ts +0 -383
  325. package/src/mcp/tools/schemas.ts +0 -520
  326. package/src/utils/adminEntitySettings.ts +0 -40
  327. package/src/utils/createApiKeyFields.ts +0 -72
  328. package/src/utils/getEnabledSlugs.ts +0 -42
  329. package/src/utils/schemaConversion/convertCollectionSchemaToZod.ts +0 -52
@@ -1,416 +0,0 @@
1
- import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
2
- import type { PayloadRequest } from 'payload'
3
-
4
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
5
- import { join } from 'path'
6
-
7
- import { toCamelCase } from '../../../utils/camelCase.js'
8
- import { validatePayloadFile } from '../../helpers/fileValidation.js'
9
- import { toolSchemas } from '../schemas.js'
10
-
11
- const createOrUpdateJobFile = (
12
- req: PayloadRequest,
13
- verboseLogs: boolean,
14
- jobsDir: string,
15
- jobName: string,
16
- jobType: 'task' | 'workflow',
17
- jobSlug: string,
18
- camelCaseJobSlug: string,
19
- ) => {
20
- const payload = req.payload
21
- const jobFilePath = join(jobsDir, `${jobName}.ts`)
22
- const importName = `${camelCaseJobSlug}${jobType === 'task' ? 'Task' : 'Workflow'}`
23
- const importPath = `./${jobType === 'task' ? 'tasks' : 'workflows'}/${camelCaseJobSlug}`
24
-
25
- if (verboseLogs) {
26
- payload.logger.info(`[payload-mcp] Processing job file: ${jobFilePath}`)
27
- }
28
-
29
- if (existsSync(jobFilePath)) {
30
- if (verboseLogs) {
31
- payload.logger.info(`[payload-mcp] Updating existing job file: ${jobFilePath}`)
32
- }
33
-
34
- // Update existing job file
35
- let content = readFileSync(jobFilePath, 'utf8')
36
-
37
- // Add import if not already present
38
- const importStatement = `import { ${importName} } from '${importPath}'`
39
- if (!content.includes(importStatement)) {
40
- if (verboseLogs) {
41
- payload.logger.info(`[payload-mcp] Adding import: ${importStatement}`)
42
- }
43
-
44
- // Find the last import statement and add after it
45
- const importRegex = /import\s+(?:\S.*)?from\s+['"].*['"];?\s*\n/g
46
- let lastImportMatch
47
- let match
48
- while ((match = importRegex.exec(content)) !== null) {
49
- lastImportMatch = match
50
- }
51
-
52
- if (lastImportMatch) {
53
- const insertIndex = lastImportMatch.index + lastImportMatch[0].length
54
- content =
55
- content.slice(0, insertIndex) + importStatement + '\n' + content.slice(insertIndex)
56
- } else {
57
- // No imports found, add at the beginning
58
- content = importStatement + '\n\n' + content
59
- }
60
- }
61
-
62
- // Add to the appropriate array
63
- const arrayName = jobType === 'task' ? 'tasks' : 'workflows'
64
- const arrayRegex = new RegExp(`(${arrayName}:\\s*\\[)([^\\]]*)(\\])`, 's')
65
- const arrayMatch = content.match(arrayRegex)
66
-
67
- if (arrayMatch && arrayMatch[2]) {
68
- const existingItems = arrayMatch[2].trim()
69
- const newItem = existingItems ? `${existingItems},\n ${importName}` : `\n ${importName}`
70
- content = content.replace(arrayRegex, `$1${newItem}\n $3`)
71
-
72
- if (verboseLogs) {
73
- payload.logger.info(`[payload-mcp] Added ${importName} to ${arrayName} array`)
74
- }
75
- } else {
76
- // Array doesn't exist, add it
77
- const jobsConfigRegex = /(export\s+const\s.*JobsConfig\s*=\s*\{)([^}]*)(\})/s
78
- const jobsConfigMatch = content.match(jobsConfigRegex)
79
-
80
- if (jobsConfigMatch && jobsConfigMatch[2]) {
81
- const existingConfig = jobsConfigMatch[2].trim()
82
- const newConfig = existingConfig
83
- ? `${existingConfig},\n ${arrayName}: [\n ${importName}\n ]`
84
- : `\n ${arrayName}: [\n ${importName}\n ]`
85
- content = content.replace(jobsConfigRegex, `$1${newConfig}\n$3`)
86
-
87
- if (verboseLogs) {
88
- payload.logger.info(`[payload-mcp] Created new ${arrayName} array with ${importName}`)
89
- }
90
- }
91
- }
92
-
93
- writeFileSync(jobFilePath, content)
94
- if (verboseLogs) {
95
- payload.logger.info(`[payload-mcp] Successfully updated job file: ${jobFilePath}`)
96
- }
97
- } else {
98
- if (verboseLogs) {
99
- payload.logger.info(`[payload-mcp] Creating new job file: ${jobFilePath}`)
100
- }
101
-
102
- // Create new job file
103
- const camelCaseJobName = toCamelCase(jobName)
104
- const jobFileContent = `import type { JobsConfig } from 'payload'
105
- import { ${importName} } from '${importPath}'
106
-
107
- export const ${camelCaseJobName}JobsConfig: JobsConfig = {
108
- ${jobType === 'task' ? 'tasks' : 'workflows'}: [
109
- ${importName}
110
- ]
111
- }
112
- `
113
- writeFileSync(jobFilePath, jobFileContent)
114
- if (verboseLogs) {
115
- payload.logger.info(`[payload-mcp] Successfully created new job file: ${jobFilePath}`)
116
- }
117
- }
118
- }
119
-
120
- // Reusable function for creating jobs
121
- export const createJob = async (
122
- req: PayloadRequest,
123
- verboseLogs: boolean,
124
- jobsDir: string,
125
- jobName: string,
126
- jobType: 'task' | 'workflow',
127
- jobSlug: string,
128
- description: string,
129
- inputSchema: any,
130
- outputSchema: any,
131
- jobData: Record<string, any>,
132
- ) => {
133
- const payload = req.payload
134
-
135
- if (verboseLogs) {
136
- payload.logger.info(`[payload-mcp] Creating ${jobType}: ${jobName}`)
137
- }
138
-
139
- try {
140
- // Ensure jobs directory exists
141
- if (!existsSync(jobsDir)) {
142
- if (verboseLogs) {
143
- payload.logger.info(`[payload-mcp] Creating jobs directory: ${jobsDir}`)
144
- }
145
- mkdirSync(jobsDir, { recursive: true })
146
- }
147
-
148
- // Ensure subdirectories exist
149
- const tasksDir = join(jobsDir, 'tasks')
150
- const workflowsDir = join(jobsDir, 'workflows')
151
-
152
- if (!existsSync(tasksDir)) {
153
- mkdirSync(tasksDir, { recursive: true })
154
- }
155
- if (!existsSync(workflowsDir)) {
156
- mkdirSync(workflowsDir, { recursive: true })
157
- }
158
-
159
- const camelCaseJobSlug = toCamelCase(jobSlug)
160
- const targetDir = jobType === 'task' ? tasksDir : workflowsDir
161
- const fileName = `${camelCaseJobSlug}.ts`
162
- const filePath = join(targetDir, fileName)
163
-
164
- if (verboseLogs) {
165
- payload.logger.info(`[payload-mcp] Target file path: ${filePath}`)
166
- }
167
-
168
- // Security check: ensure we're working with the jobs directory
169
- if (!filePath.startsWith(jobsDir)) {
170
- payload.logger.error(`[payload-mcp] Invalid job path attempted: ${filePath}`)
171
- return {
172
- content: [
173
- {
174
- type: 'text' as const,
175
- text: '❌ **Error**: Invalid job path',
176
- },
177
- ],
178
- }
179
- }
180
-
181
- // Check if file already exists
182
- if (existsSync(filePath)) {
183
- if (verboseLogs) {
184
- payload.logger.info(`[payload-mcp] Job file already exists: ${fileName}`)
185
- }
186
- return {
187
- content: [
188
- {
189
- type: 'text' as const,
190
- text: `❌ **Error**: Job file already exists: ${fileName}`,
191
- },
192
- ],
193
- }
194
- }
195
-
196
- // Generate job content based on type
197
- let jobContent: string
198
- if (jobType === 'task') {
199
- jobContent = generateTaskContent(
200
- jobName,
201
- jobSlug,
202
- description,
203
- inputSchema,
204
- outputSchema,
205
- jobData,
206
- )
207
- } else {
208
- jobContent = generateWorkflowContent(
209
- jobName,
210
- jobSlug,
211
- description,
212
- inputSchema,
213
- outputSchema,
214
- jobData,
215
- )
216
- }
217
-
218
- // Write the job file
219
- writeFileSync(filePath, jobContent, 'utf8')
220
- if (verboseLogs) {
221
- payload.logger.info(`[payload-mcp] Successfully created job file: ${filePath}`)
222
- }
223
-
224
- // Update the main job file
225
- createOrUpdateJobFile(req, verboseLogs, jobsDir, jobName, jobType, jobSlug, camelCaseJobSlug)
226
-
227
- // Validate the generated file
228
- const validationResult = await validatePayloadFile(fileName, jobType)
229
- if (validationResult.error) {
230
- return {
231
- content: [
232
- {
233
- type: 'text' as const,
234
- text: `❌ **Error**: Generated job has validation issues:\n\n${validationResult.error}`,
235
- },
236
- ],
237
- }
238
- }
239
-
240
- return {
241
- content: [
242
- {
243
- type: 'text' as const,
244
- text: `✅ **Job created successfully!**
245
-
246
- **File**: \`${fileName}\`
247
- **Type**: \`${jobType}\`
248
- **Slug**: \`${jobSlug}\`
249
- **Description**: ${description}
250
-
251
- **Generated Job Code:**
252
- \`\`\`typescript
253
- ${jobContent}
254
- \`\`\``,
255
- },
256
- ],
257
- }
258
- } catch (error) {
259
- const errorMessage = (error as Error).message
260
- payload.logger.error(`[payload-mcp] Error creating job: ${errorMessage}`)
261
-
262
- return {
263
- content: [
264
- {
265
- type: 'text' as const,
266
- text: `❌ **Error creating job**: ${errorMessage}`,
267
- },
268
- ],
269
- }
270
- }
271
- }
272
-
273
- // Helper function to generate task content
274
- function generateTaskContent(
275
- _jobName: string,
276
- jobSlug: string,
277
- description: string,
278
- inputSchema: any,
279
- outputSchema: any,
280
- _jobData: Record<string, any>,
281
- ): string {
282
- const camelCaseJobSlug = toCamelCase(jobSlug)
283
-
284
- return `import type { Task } from 'payload'
285
-
286
- export const ${camelCaseJobSlug}Task: Task = {
287
- slug: '${jobSlug}',
288
- description: '${description}',
289
- inputSchema: ${JSON.stringify(inputSchema, null, 2)},
290
- outputSchema: ${JSON.stringify(outputSchema, null, 2)},
291
- handler: async (input, context) => {
292
- // TODO: Implement your task logic here
293
- // Access input data: input.fieldName
294
- // Access context: context.payload, context.req, etc.
295
-
296
- // Example implementation:
297
- const result = {
298
- message: 'Task executed successfully',
299
- input,
300
- timestamp: new Date().toISOString(),
301
- }
302
-
303
- return result
304
- },
305
- }
306
- `
307
- }
308
-
309
- // Helper function to generate workflow content
310
- function generateWorkflowContent(
311
- _jobName: string,
312
- jobSlug: string,
313
- description: string,
314
- inputSchema: any,
315
- outputSchema: any,
316
- _jobData: Record<string, any>,
317
- ): string {
318
- const camelCaseJobSlug = toCamelCase(jobSlug)
319
-
320
- return `import type { Workflow } from 'payload'
321
-
322
- export const ${camelCaseJobSlug}Workflow: Workflow = {
323
- slug: '${jobSlug}',
324
- description: '${description}',
325
- inputSchema: ${JSON.stringify(inputSchema, null, 2)},
326
- outputSchema: ${JSON.stringify(outputSchema, null, 2)},
327
- steps: [
328
- // TODO: Define your workflow steps here
329
- // Each step should be a function that returns a result
330
- // Example:
331
- // {
332
- // name: 'step1',
333
- // handler: async (input, context) => {
334
- // // Step logic here
335
- // return { result: 'step1 completed' }
336
- // }
337
- // }
338
- ],
339
- }
340
- `
341
- }
342
-
343
- export const createJobTool = (
344
- server: McpServer,
345
- req: PayloadRequest,
346
- verboseLogs: boolean,
347
- jobsDir: string,
348
- ) => {
349
- const tool = async (
350
- jobName: string,
351
- jobType: 'task' | 'workflow',
352
- jobSlug: string,
353
- description: string,
354
- inputSchema: any = {},
355
- outputSchema: any = {},
356
- jobData: Record<string, any> = {},
357
- ) => {
358
- if (verboseLogs) {
359
- req.payload.logger.info(
360
- `[payload-mcp] Create Job Tool called with: ${jobName}, ${jobType}, ${jobSlug}`,
361
- )
362
- }
363
-
364
- try {
365
- const result = await createJob(
366
- req,
367
- verboseLogs,
368
- jobsDir,
369
- jobName,
370
- jobType,
371
- jobSlug,
372
- description,
373
- inputSchema,
374
- outputSchema,
375
- jobData,
376
- )
377
-
378
- if (verboseLogs) {
379
- req.payload.logger.info(`[payload-mcp] Create Job Tool completed successfully`)
380
- }
381
-
382
- return result
383
- } catch (error) {
384
- const errorMessage = error instanceof Error ? error.message : 'Unknown error'
385
- req.payload.logger.error(`[payload-mcp] Error in Create Job Tool: ${errorMessage}`)
386
-
387
- return {
388
- content: [
389
- {
390
- type: 'text' as const,
391
- text: `❌ **Error in Create Job Tool**: ${errorMessage}`,
392
- },
393
- ],
394
- }
395
- }
396
- }
397
-
398
- server.registerTool(
399
- 'createJob',
400
- {
401
- description: 'Creates a new Payload job (task or workflow) with specified configuration',
402
- inputSchema: toolSchemas.createJob.parameters.shape,
403
- },
404
- async (args) => {
405
- return tool(
406
- args.jobName,
407
- args.jobType,
408
- args.jobSlug,
409
- args.description,
410
- args.inputSchema,
411
- args.outputSchema,
412
- args.jobData,
413
- )
414
- },
415
- )
416
- }
@@ -1,167 +0,0 @@
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
- // Reusable function for running jobs
7
- export const runJob = async (
8
- req: PayloadRequest,
9
- verboseLogs: boolean,
10
- jobSlug: string,
11
- input: Record<string, unknown>,
12
- queue?: string,
13
- priority?: number,
14
- delay?: number,
15
- ) => {
16
- const payload = req.payload
17
-
18
- if (verboseLogs) {
19
- payload.logger.info(`[payload-mcp] Running job: ${jobSlug}`)
20
- }
21
-
22
- try {
23
- // Actually run the job using Payload's job queue
24
- const jobQueueOptions: Record<string, unknown> = {
25
- input,
26
- task: jobSlug,
27
- }
28
-
29
- if (queue && queue !== 'default') {
30
- jobQueueOptions.queue = queue
31
- if (verboseLogs) {
32
- payload.logger.info(`[payload-mcp] Using custom queue: ${queue}`)
33
- }
34
- }
35
-
36
- if (priority && priority > 0) {
37
- jobQueueOptions.priority = priority
38
- if (verboseLogs) {
39
- payload.logger.info(`[payload-mcp] Setting job priority: ${priority}`)
40
- }
41
- }
42
-
43
- if (delay && delay > 0) {
44
- jobQueueOptions.waitUntil = new Date(Date.now() + delay)
45
- if (verboseLogs) {
46
- payload.logger.info(`[payload-mcp] Setting job delay: ${delay}ms`)
47
- }
48
- }
49
-
50
- if (verboseLogs) {
51
- payload.logger.info(
52
- `[payload-mcp] Queuing job with options: ${JSON.stringify(jobQueueOptions)}`,
53
- )
54
- }
55
-
56
- const job = await payload.jobs.queue(
57
- jobQueueOptions as Parameters<typeof payload.jobs.queue>[0],
58
- )
59
-
60
- const jobId = (job as { id?: string })?.id || 'unknown'
61
-
62
- if (verboseLogs) {
63
- payload.logger.info(`[payload-mcp] Job created successfully: ${jobId}`)
64
- }
65
-
66
- return {
67
- content: [
68
- {
69
- type: 'text' as const,
70
- text: `# Job Queued Successfully: ${jobSlug}
71
-
72
- ## Job Details
73
- - **Job ID**: ${jobId}
74
- - **Job Slug**: ${jobSlug}
75
- - **Queue**: ${queue || 'default'}
76
- - **Priority**: ${priority || 'default'}
77
- - **Delay**: ${delay ? `${delay}ms` : 'none'}
78
- - **Status**: Queued and Running
79
-
80
- ## Input Data
81
- \`\`\`json
82
- ${JSON.stringify(input)}
83
- \`\`\`
84
-
85
- ## Job Status
86
- The job has been successfully queued and will be processed according to the queue settings.
87
-
88
- ## Monitoring the Job
89
- You can monitor the job status using:
90
-
91
- \`\`\`typescript
92
- // Check job status
93
- const jobStatus = await payload.jobs.status('${jobId}')
94
- console.log('Job status:', jobStatus)
95
-
96
- // Wait for completion
97
- const result = await payload.jobs.wait('${jobId}')
98
- console.log('Job result:', result)
99
- \`\`\`
100
-
101
- ✅ Job successfully queued with ID: ${jobId}`,
102
- },
103
- ],
104
- }
105
- } catch (error) {
106
- const errorMsg = (error as Error).message
107
- payload.logger.error(`[payload-mcp] Error running job "${jobSlug}": ${errorMsg}`)
108
-
109
- return {
110
- content: [
111
- {
112
- type: 'text' as const,
113
- text: `❌ Error running job "${jobSlug}": ${errorMsg}
114
-
115
- ## Common Issues:
116
- 1. **Job not found**: The job "${jobSlug}" may not be registered in your Payload configuration
117
- 2. **Invalid input format**: Ensure the input matches the job's input schema
118
- 3. **Queue not configured**: The queue "${queue || 'default'}" may not be properly set up
119
- 4. **Permission issues**: Ensure proper access rights for job execution
120
- 5. **Job handler error**: The job implementation may have errors
121
-
122
- ## Input Data Provided:
123
- \`\`\`json
124
- ${JSON.stringify(input)}
125
- \`\`\`
126
-
127
- ## Next Steps:
128
- 1. **Verify job exists**: Check that the job "${jobSlug}" is properly registered
129
- 2. **Check input format**: Ensure the input data matches the expected schema
130
- 3. **Review job configuration**: Verify the job is properly configured in your Payload setup
131
- 4. **Check permissions**: Ensure you have the necessary permissions to run jobs
132
- 5. **Review error logs**: Check the server logs for more detailed error information
133
-
134
- ## Troubleshooting:
135
- - **Job not found**: Verify the job slug and check your jobs configuration
136
- - **Schema mismatch**: Ensure input data matches the job's input schema
137
- - **Queue issues**: Check that the specified queue is properly configured
138
- - **Permission errors**: Verify user permissions for job execution`,
139
- },
140
- ],
141
- }
142
- }
143
- }
144
-
145
- export const runJobTool = (server: McpServer, req: PayloadRequest, verboseLogs: boolean) => {
146
- server.registerTool(
147
- 'runJob',
148
- {
149
- description: 'Runs a Payload job with specified input data and queue options',
150
- inputSchema: toolSchemas.runJob.parameters.shape,
151
- },
152
- async ({ delay, input, jobSlug, priority, queue }) => {
153
- if (verboseLogs) {
154
- req.payload.logger.info(`[payload-mcp] Run Job Tool called with: ${jobSlug}`)
155
- }
156
- return runJob(
157
- req,
158
- verboseLogs,
159
- jobSlug,
160
- input as Record<string, unknown>,
161
- queue,
162
- priority,
163
- delay,
164
- )
165
- },
166
- )
167
- }