@payloadcms/plugin-mcp 4.0.0-internal.5b1e7cd → 4.0.0-internal.5f0cd13

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 (328) hide show
  1. package/bin.js +39 -0
  2. package/dist/collection/getAccessField.d.ts +12 -0
  3. package/dist/collection/getAccessField.d.ts.map +1 -0
  4. package/dist/collection/getAccessField.js +57 -0
  5. package/dist/collection/getAccessField.js.map +1 -0
  6. package/dist/collection/index.d.ts +6 -0
  7. package/dist/collection/index.d.ts.map +1 -0
  8. package/dist/collection/index.js +59 -0
  9. package/dist/collection/index.js.map +1 -0
  10. package/dist/components/AccessField/index.client.d.ts +10 -0
  11. package/dist/components/AccessField/index.client.d.ts.map +1 -0
  12. package/dist/components/AccessField/index.client.js +305 -0
  13. package/dist/components/AccessField/index.client.js.map +1 -0
  14. package/dist/components/AccessField/index.css +93 -0
  15. package/dist/defineTool.d.ts +26 -0
  16. package/dist/defineTool.d.ts.map +1 -0
  17. package/dist/defineTool.js +37 -0
  18. package/dist/defineTool.js.map +1 -0
  19. package/dist/endpoint/access.d.ts +10 -0
  20. package/dist/endpoint/access.d.ts.map +1 -0
  21. package/dist/endpoint/access.js +106 -0
  22. package/dist/endpoint/access.js.map +1 -0
  23. package/dist/endpoint/index.d.ts +3 -0
  24. package/dist/endpoint/index.d.ts.map +1 -0
  25. package/dist/endpoint/index.js +36 -0
  26. package/dist/endpoint/index.js.map +1 -0
  27. package/dist/exports/client.d.ts +2 -0
  28. package/dist/exports/client.d.ts.map +1 -0
  29. package/dist/exports/client.js +4 -0
  30. package/dist/exports/client.js.map +1 -0
  31. package/dist/index.d.ts +4 -7
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +44 -67
  34. package/dist/index.js.map +1 -1
  35. package/dist/mcp/buildMcpServer.d.ts +19 -0
  36. package/dist/mcp/buildMcpServer.d.ts.map +1 -0
  37. package/dist/mcp/buildMcpServer.js +163 -0
  38. package/dist/mcp/buildMcpServer.js.map +1 -0
  39. package/dist/mcp/builtin/collections/authTools.d.ts +7 -0
  40. package/dist/mcp/builtin/collections/authTools.d.ts.map +1 -0
  41. package/dist/mcp/builtin/collections/authTools.js +250 -0
  42. package/dist/mcp/builtin/collections/authTools.js.map +1 -0
  43. package/dist/mcp/builtin/collections/createTool.d.ts +2 -0
  44. package/dist/mcp/builtin/collections/createTool.d.ts.map +1 -0
  45. package/dist/mcp/builtin/collections/createTool.js +87 -0
  46. package/dist/mcp/builtin/collections/createTool.js.map +1 -0
  47. package/dist/mcp/builtin/collections/deleteTool.d.ts +2 -0
  48. package/dist/mcp/builtin/collections/deleteTool.d.ts.map +1 -0
  49. package/dist/mcp/builtin/collections/deleteTool.js +117 -0
  50. package/dist/mcp/builtin/collections/deleteTool.js.map +1 -0
  51. package/dist/mcp/builtin/collections/findTool.d.ts +2 -0
  52. package/dist/mcp/builtin/collections/findTool.d.ts.map +1 -0
  53. package/dist/mcp/builtin/collections/findTool.js +159 -0
  54. package/dist/mcp/builtin/collections/findTool.js.map +1 -0
  55. package/dist/mcp/builtin/collections/updateTool.d.ts +2 -0
  56. package/dist/mcp/builtin/collections/updateTool.d.ts.map +1 -0
  57. package/dist/mcp/builtin/collections/updateTool.js +187 -0
  58. package/dist/mcp/builtin/collections/updateTool.js.map +1 -0
  59. package/dist/mcp/builtin/globals/findTool.d.ts +2 -0
  60. package/dist/mcp/builtin/globals/findTool.d.ts.map +1 -0
  61. package/dist/mcp/builtin/globals/findTool.js +76 -0
  62. package/dist/mcp/builtin/globals/findTool.js.map +1 -0
  63. package/dist/mcp/builtin/globals/updateTool.d.ts +2 -0
  64. package/dist/mcp/builtin/globals/updateTool.d.ts.map +1 -0
  65. package/dist/mcp/builtin/globals/updateTool.js +92 -0
  66. package/dist/mcp/builtin/globals/updateTool.js.map +1 -0
  67. package/dist/mcp/builtinTools.d.ts +37 -0
  68. package/dist/mcp/builtinTools.d.ts.map +1 -0
  69. package/dist/mcp/builtinTools.js +64 -0
  70. package/dist/mcp/builtinTools.js.map +1 -0
  71. package/dist/mcp/sanitizeMCPConfig.d.ts +17 -0
  72. package/dist/mcp/sanitizeMCPConfig.d.ts.map +1 -0
  73. package/dist/mcp/sanitizeMCPConfig.js +167 -0
  74. package/dist/mcp/sanitizeMCPConfig.js.map +1 -0
  75. package/dist/stdio.d.ts +8 -0
  76. package/dist/stdio.d.ts.map +1 -0
  77. package/dist/stdio.js +89 -0
  78. package/dist/stdio.js.map +1 -0
  79. package/dist/types.d.ts +262 -455
  80. package/dist/types.d.ts.map +1 -1
  81. package/dist/types.js +6 -1
  82. package/dist/types.js.map +1 -1
  83. package/dist/utils/camelCase.d.ts.map +1 -1
  84. package/dist/utils/getLogger.d.ts +10 -0
  85. package/dist/utils/getLogger.d.ts.map +1 -0
  86. package/dist/utils/getLogger.js +22 -0
  87. package/dist/utils/getLogger.js.map +1 -0
  88. package/dist/utils/getPluginConfig.d.ts +12 -0
  89. package/dist/utils/getPluginConfig.d.ts.map +1 -0
  90. package/dist/utils/getPluginConfig.js +15 -0
  91. package/dist/utils/getPluginConfig.js.map +1 -0
  92. package/dist/utils/localAPIDefaults.d.ts +20 -0
  93. package/dist/utils/localAPIDefaults.d.ts.map +1 -0
  94. package/dist/utils/localAPIDefaults.js +19 -0
  95. package/dist/utils/localAPIDefaults.js.map +1 -0
  96. package/dist/utils/resolveProjectRoot.d.ts +7 -0
  97. package/dist/utils/resolveProjectRoot.d.ts.map +1 -0
  98. package/dist/utils/resolveProjectRoot.js +15 -0
  99. package/dist/utils/resolveProjectRoot.js.map +1 -0
  100. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts +7 -0
  101. package/dist/utils/schemaConversion/prepareCollectionSchema.d.ts.map +1 -0
  102. package/dist/utils/schemaConversion/prepareCollectionSchema.js +37 -0
  103. package/dist/utils/schemaConversion/prepareCollectionSchema.js.map +1 -0
  104. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts +2 -2
  105. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.d.ts.map +1 -1
  106. package/dist/utils/schemaConversion/removeVirtualFieldsFromSchema.js.map +1 -1
  107. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +2 -2
  108. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +1 -1
  109. package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +1 -1
  110. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +2 -2
  111. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +1 -1
  112. package/dist/utils/schemaConversion/simplifyRelationshipFields.js +7 -4
  113. package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +1 -1
  114. package/dist/utils/schemaConversion/transformPointFields.d.ts +2 -2
  115. package/dist/utils/schemaConversion/transformPointFields.d.ts.map +1 -1
  116. package/dist/utils/schemaConversion/transformPointFields.js +7 -1
  117. package/dist/utils/schemaConversion/transformPointFields.js.map +1 -1
  118. package/dist/utils/toStandardSchema.d.ts +5 -0
  119. package/dist/utils/toStandardSchema.d.ts.map +1 -0
  120. package/dist/utils/toStandardSchema.js +4 -0
  121. package/dist/utils/toStandardSchema.js.map +1 -0
  122. package/package.json +35 -10
  123. package/src/collection/getAccessField.ts +64 -0
  124. package/src/collection/index.ts +63 -0
  125. package/src/components/AccessField/index.client.tsx +344 -0
  126. package/src/components/AccessField/index.css +93 -0
  127. package/src/defineTool.ts +44 -0
  128. package/src/endpoint/access.ts +132 -0
  129. package/src/endpoint/index.ts +35 -0
  130. package/src/exports/client.ts +2 -0
  131. package/src/index.ts +35 -85
  132. package/src/mcp/buildMcpServer.ts +224 -0
  133. package/src/mcp/builtin/collections/authTools.ts +233 -0
  134. package/src/mcp/builtin/collections/createTool.ts +116 -0
  135. package/src/mcp/builtin/collections/deleteTool.ts +123 -0
  136. package/src/mcp/builtin/collections/findTool.ts +187 -0
  137. package/src/mcp/builtin/collections/updateTool.ts +205 -0
  138. package/src/mcp/builtin/globals/findTool.ts +96 -0
  139. package/src/mcp/builtin/globals/updateTool.ts +118 -0
  140. package/src/mcp/builtinTools.ts +84 -0
  141. package/src/mcp/sanitizeMCPConfig.ts +239 -0
  142. package/src/stdio.ts +98 -0
  143. package/src/types.ts +295 -490
  144. package/src/utils/getLogger.ts +22 -0
  145. package/src/utils/getPluginConfig.ts +24 -0
  146. package/src/utils/localAPIDefaults.ts +22 -0
  147. package/src/utils/resolveProjectRoot.ts +17 -0
  148. package/src/utils/schemaConversion/prepareCollectionSchema.ts +39 -0
  149. package/src/utils/schemaConversion/removeVirtualFieldsFromSchema.ts +3 -3
  150. package/src/utils/schemaConversion/sanitizeJsonSchema.ts +4 -4
  151. package/src/utils/schemaConversion/simplifyRelationshipFields.ts +11 -6
  152. package/src/utils/schemaConversion/transformPointFields.ts +6 -5
  153. package/src/utils/toStandardSchema.ts +9 -0
  154. package/dist/collections/createApiKeysCollection.d.ts +0 -7
  155. package/dist/collections/createApiKeysCollection.d.ts.map +0 -1
  156. package/dist/collections/createApiKeysCollection.js +0 -317
  157. package/dist/collections/createApiKeysCollection.js.map +0 -1
  158. package/dist/defaults.d.ts +0 -4
  159. package/dist/defaults.d.ts.map +0 -1
  160. package/dist/defaults.js +0 -5
  161. package/dist/defaults.js.map +0 -1
  162. package/dist/endpoints/mcp.d.ts +0 -4
  163. package/dist/endpoints/mcp.d.ts.map +0 -1
  164. package/dist/endpoints/mcp.js +0 -71
  165. package/dist/endpoints/mcp.js.map +0 -1
  166. package/dist/mcp/createRequest.d.ts +0 -3
  167. package/dist/mcp/createRequest.d.ts.map +0 -1
  168. package/dist/mcp/createRequest.js +0 -14
  169. package/dist/mcp/createRequest.js.map +0 -1
  170. package/dist/mcp/getMcpHandler.d.ts +0 -4
  171. package/dist/mcp/getMcpHandler.d.ts.map +0 -1
  172. package/dist/mcp/getMcpHandler.js +0 -231
  173. package/dist/mcp/getMcpHandler.js.map +0 -1
  174. package/dist/mcp/helpers/config.d.ts +0 -22
  175. package/dist/mcp/helpers/config.d.ts.map +0 -1
  176. package/dist/mcp/helpers/config.js +0 -153
  177. package/dist/mcp/helpers/config.js.map +0 -1
  178. package/dist/mcp/helpers/fields.d.ts +0 -19
  179. package/dist/mcp/helpers/fields.d.ts.map +0 -1
  180. package/dist/mcp/helpers/fields.js +0 -102
  181. package/dist/mcp/helpers/fields.js.map +0 -1
  182. package/dist/mcp/helpers/fileValidation.d.ts +0 -67
  183. package/dist/mcp/helpers/fileValidation.d.ts.map +0 -1
  184. package/dist/mcp/helpers/fileValidation.js +0 -267
  185. package/dist/mcp/helpers/fileValidation.js.map +0 -1
  186. package/dist/mcp/registerTool.d.ts +0 -6
  187. package/dist/mcp/registerTool.d.ts.map +0 -1
  188. package/dist/mcp/registerTool.js +0 -18
  189. package/dist/mcp/registerTool.js.map +0 -1
  190. package/dist/mcp/tools/auth/auth.d.ts +0 -4
  191. package/dist/mcp/tools/auth/auth.d.ts.map +0 -1
  192. package/dist/mcp/tools/auth/auth.js +0 -57
  193. package/dist/mcp/tools/auth/auth.js.map +0 -1
  194. package/dist/mcp/tools/auth/forgotPassword.d.ts +0 -4
  195. package/dist/mcp/tools/auth/forgotPassword.d.ts.map +0 -1
  196. package/dist/mcp/tools/auth/forgotPassword.js +0 -48
  197. package/dist/mcp/tools/auth/forgotPassword.js.map +0 -1
  198. package/dist/mcp/tools/auth/login.d.ts +0 -4
  199. package/dist/mcp/tools/auth/login.d.ts.map +0 -1
  200. package/dist/mcp/tools/auth/login.js +0 -51
  201. package/dist/mcp/tools/auth/login.js.map +0 -1
  202. package/dist/mcp/tools/auth/resetPassword.d.ts +0 -4
  203. package/dist/mcp/tools/auth/resetPassword.d.ts.map +0 -1
  204. package/dist/mcp/tools/auth/resetPassword.js +0 -49
  205. package/dist/mcp/tools/auth/resetPassword.js.map +0 -1
  206. package/dist/mcp/tools/auth/unlock.d.ts +0 -4
  207. package/dist/mcp/tools/auth/unlock.d.ts.map +0 -1
  208. package/dist/mcp/tools/auth/unlock.js +0 -48
  209. package/dist/mcp/tools/auth/unlock.js.map +0 -1
  210. package/dist/mcp/tools/auth/verify.d.ts +0 -4
  211. package/dist/mcp/tools/auth/verify.d.ts.map +0 -1
  212. package/dist/mcp/tools/auth/verify.js +0 -45
  213. package/dist/mcp/tools/auth/verify.js.map +0 -1
  214. package/dist/mcp/tools/collection/create.d.ts +0 -10
  215. package/dist/mcp/tools/collection/create.d.ts.map +0 -1
  216. package/dist/mcp/tools/collection/create.js +0 -139
  217. package/dist/mcp/tools/collection/create.js.map +0 -1
  218. package/dist/mcp/tools/collection/delete.d.ts +0 -10
  219. package/dist/mcp/tools/collection/delete.d.ts.map +0 -1
  220. package/dist/mcp/tools/collection/delete.js +0 -154
  221. package/dist/mcp/tools/collection/delete.js.map +0 -1
  222. package/dist/mcp/tools/collection/find.d.ts +0 -10
  223. package/dist/mcp/tools/collection/find.d.ts.map +0 -1
  224. package/dist/mcp/tools/collection/find.js +0 -165
  225. package/dist/mcp/tools/collection/find.js.map +0 -1
  226. package/dist/mcp/tools/collection/update.d.ts +0 -10
  227. package/dist/mcp/tools/collection/update.d.ts.map +0 -1
  228. package/dist/mcp/tools/collection/update.js +0 -209
  229. package/dist/mcp/tools/collection/update.js.map +0 -1
  230. package/dist/mcp/tools/config/find.d.ts +0 -10
  231. package/dist/mcp/tools/config/find.d.ts.map +0 -1
  232. package/dist/mcp/tools/config/find.js +0 -97
  233. package/dist/mcp/tools/config/find.js.map +0 -1
  234. package/dist/mcp/tools/config/update.d.ts +0 -10
  235. package/dist/mcp/tools/config/update.d.ts.map +0 -1
  236. package/dist/mcp/tools/config/update.js +0 -215
  237. package/dist/mcp/tools/config/update.js.map +0 -1
  238. package/dist/mcp/tools/global/find.d.ts +0 -5
  239. package/dist/mcp/tools/global/find.d.ts.map +0 -1
  240. package/dist/mcp/tools/global/find.js +0 -82
  241. package/dist/mcp/tools/global/find.js.map +0 -1
  242. package/dist/mcp/tools/global/update.d.ts +0 -6
  243. package/dist/mcp/tools/global/update.d.ts.map +0 -1
  244. package/dist/mcp/tools/global/update.js +0 -124
  245. package/dist/mcp/tools/global/update.js.map +0 -1
  246. package/dist/mcp/tools/job/create.d.ts +0 -10
  247. package/dist/mcp/tools/job/create.d.ts.map +0 -1
  248. package/dist/mcp/tools/job/create.js +0 -293
  249. package/dist/mcp/tools/job/create.js.map +0 -1
  250. package/dist/mcp/tools/job/run.d.ts +0 -10
  251. package/dist/mcp/tools/job/run.d.ts.map +0 -1
  252. package/dist/mcp/tools/job/run.js +0 -129
  253. package/dist/mcp/tools/job/run.js.map +0 -1
  254. package/dist/mcp/tools/job/update.d.ts +0 -11
  255. package/dist/mcp/tools/job/update.d.ts.map +0 -1
  256. package/dist/mcp/tools/job/update.js +0 -186
  257. package/dist/mcp/tools/job/update.js.map +0 -1
  258. package/dist/mcp/tools/resource/create.d.ts +0 -6
  259. package/dist/mcp/tools/resource/create.d.ts.map +0 -1
  260. package/dist/mcp/tools/resource/create.js +0 -124
  261. package/dist/mcp/tools/resource/create.js.map +0 -1
  262. package/dist/mcp/tools/resource/delete.d.ts +0 -5
  263. package/dist/mcp/tools/resource/delete.d.ts.map +0 -1
  264. package/dist/mcp/tools/resource/delete.js +0 -151
  265. package/dist/mcp/tools/resource/delete.js.map +0 -1
  266. package/dist/mcp/tools/resource/find.d.ts +0 -5
  267. package/dist/mcp/tools/resource/find.d.ts.map +0 -1
  268. package/dist/mcp/tools/resource/find.js +0 -170
  269. package/dist/mcp/tools/resource/find.js.map +0 -1
  270. package/dist/mcp/tools/resource/update.d.ts +0 -6
  271. package/dist/mcp/tools/resource/update.d.ts.map +0 -1
  272. package/dist/mcp/tools/resource/update.js +0 -256
  273. package/dist/mcp/tools/resource/update.js.map +0 -1
  274. package/dist/mcp/tools/schemas.d.ts +0 -457
  275. package/dist/mcp/tools/schemas.d.ts.map +0 -1
  276. package/dist/mcp/tools/schemas.js +0 -243
  277. package/dist/mcp/tools/schemas.js.map +0 -1
  278. package/dist/utils/adminEntitySettings.d.ts +0 -17
  279. package/dist/utils/adminEntitySettings.d.ts.map +0 -1
  280. package/dist/utils/adminEntitySettings.js +0 -41
  281. package/dist/utils/adminEntitySettings.js.map +0 -1
  282. package/dist/utils/createApiKeyFields.d.ts +0 -15
  283. package/dist/utils/createApiKeyFields.d.ts.map +0 -1
  284. package/dist/utils/createApiKeyFields.js +0 -57
  285. package/dist/utils/createApiKeyFields.js.map +0 -1
  286. package/dist/utils/getEnabledSlugs.d.ts +0 -13
  287. package/dist/utils/getEnabledSlugs.d.ts.map +0 -1
  288. package/dist/utils/getEnabledSlugs.js +0 -32
  289. package/dist/utils/getEnabledSlugs.js.map +0 -1
  290. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts +0 -3
  291. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts.map +0 -1
  292. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js +0 -43
  293. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js.map +0 -1
  294. package/src/collections/createApiKeysCollection.ts +0 -373
  295. package/src/defaults.ts +0 -3
  296. package/src/endpoints/mcp.ts +0 -91
  297. package/src/mcp/createRequest.ts +0 -13
  298. package/src/mcp/getMcpHandler.ts +0 -545
  299. package/src/mcp/helpers/config.ts +0 -213
  300. package/src/mcp/helpers/fields.ts +0 -154
  301. package/src/mcp/helpers/fileValidation.ts +0 -362
  302. package/src/mcp/registerTool.ts +0 -22
  303. package/src/mcp/tools/auth/auth.ts +0 -71
  304. package/src/mcp/tools/auth/forgotPassword.ts +0 -70
  305. package/src/mcp/tools/auth/login.ts +0 -72
  306. package/src/mcp/tools/auth/resetPassword.ts +0 -61
  307. package/src/mcp/tools/auth/unlock.ts +0 -64
  308. package/src/mcp/tools/auth/verify.ts +0 -57
  309. package/src/mcp/tools/collection/create.ts +0 -210
  310. package/src/mcp/tools/collection/delete.ts +0 -211
  311. package/src/mcp/tools/collection/find.ts +0 -224
  312. package/src/mcp/tools/collection/update.ts +0 -290
  313. package/src/mcp/tools/config/find.ts +0 -128
  314. package/src/mcp/tools/config/update.ts +0 -280
  315. package/src/mcp/tools/global/find.ts +0 -128
  316. package/src/mcp/tools/global/update.ts +0 -207
  317. package/src/mcp/tools/job/create.ts +0 -416
  318. package/src/mcp/tools/job/run.ts +0 -167
  319. package/src/mcp/tools/job/update.ts +0 -274
  320. package/src/mcp/tools/resource/create.ts +0 -211
  321. package/src/mcp/tools/resource/delete.ts +0 -218
  322. package/src/mcp/tools/resource/find.ts +0 -246
  323. package/src/mcp/tools/resource/update.ts +0 -383
  324. package/src/mcp/tools/schemas.ts +0 -520
  325. package/src/utils/adminEntitySettings.ts +0 -40
  326. package/src/utils/createApiKeyFields.ts +0 -72
  327. package/src/utils/getEnabledSlugs.ts +0 -42
  328. package/src/utils/schemaConversion/convertCollectionSchemaToZod.ts +0 -52
@@ -0,0 +1,233 @@
1
+ import { z } from 'zod'
2
+
3
+ import type { MCPToolResponse } from '../../../types.js'
4
+
5
+ import { defineCollectionTool } from '../../../defineTool.js'
6
+ import { getLogger } from '../../../utils/getLogger.js'
7
+
8
+ /**
9
+ * Auth tools surfaced under `collections.<auth-collection>.tools`. Opt-in: they
10
+ * default off (an LLM with `login` access can probe passwords; not something
11
+ * to expose by default). Enable in plugin config via `tools: { login: true }`
12
+ * (or `{ description: '...' }`).
13
+ *
14
+ * `auth` (check current token) doesn't actually depend on the collection at
15
+ * runtime — `payload.auth({ headers })` is global — but it's grouped under the
16
+ * users collection so all auth-shaped tools live in one place.
17
+ */
18
+
19
+ const emailSchema = z.string().email().describe('The user email address')
20
+
21
+ const wrapError =
22
+ (name: string) =>
23
+ ({ collectionSlug, message }: { collectionSlug: string; message: string }): MCPToolResponse => {
24
+ return {
25
+ content: [
26
+ {
27
+ type: 'text',
28
+ text: `❌ **Error in ${name}** on ${collectionSlug}: ${message}`,
29
+ },
30
+ ],
31
+ }
32
+ }
33
+
34
+ export const authCollectionTool = defineCollectionTool({
35
+ description: 'Checks authentication status for the current user.',
36
+ input: z.object({
37
+ headers: z
38
+ .string()
39
+ .describe(
40
+ 'Optional JSON string containing custom headers to send with the authentication request',
41
+ )
42
+ .optional(),
43
+ }),
44
+ }).handler(async ({ collectionSlug, input, req }) => {
45
+ const logger = getLogger({ payload: req.payload })
46
+ try {
47
+ let authHeaders = new Headers()
48
+ if (input.headers) {
49
+ try {
50
+ authHeaders = new Headers(JSON.parse(input.headers) as Record<string, string>)
51
+ } catch {
52
+ logger.warn(`Invalid headers JSON for auth: ${input.headers}, using empty headers`)
53
+ }
54
+ }
55
+ const result = await req.payload.auth({ headers: authHeaders })
56
+ return {
57
+ content: [
58
+ {
59
+ type: 'text',
60
+ text: `# Authentication Status\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
61
+ },
62
+ ],
63
+ doc: result as unknown as Record<string, unknown>,
64
+ }
65
+ } catch (error) {
66
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
67
+ logger.error(`Error in auth tool on ${collectionSlug}: ${errorMessage}`)
68
+ return wrapError('auth')({ collectionSlug, message: errorMessage })
69
+ }
70
+ })
71
+
72
+ export const forgotPasswordCollectionTool = defineCollectionTool({
73
+ description: 'Sends a password reset email to a user.',
74
+ input: z.object({
75
+ disableEmail: z
76
+ .boolean()
77
+ .describe('Whether to disable sending the email (for testing)')
78
+ .optional()
79
+ .default(false),
80
+ email: emailSchema,
81
+ }),
82
+ }).handler(async ({ collectionSlug, input, req }) => {
83
+ const logger = getLogger({ payload: req.payload })
84
+ try {
85
+ const result = await req.payload.forgotPassword({
86
+ collection: collectionSlug,
87
+ data: { email: input.email },
88
+ disableEmail: input.disableEmail,
89
+ })
90
+ return {
91
+ content: [
92
+ {
93
+ type: 'text',
94
+ text: `# Forgot Password Email Sent\n\n**Collection:** ${collectionSlug}\n**Email:** ${input.email}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
95
+ },
96
+ ],
97
+ doc: { result } as Record<string, unknown>,
98
+ }
99
+ } catch (error) {
100
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
101
+ logger.error(`Error in forgotPassword tool on ${collectionSlug}: ${errorMessage}`)
102
+ return wrapError('forgotPassword')({ collectionSlug, message: errorMessage })
103
+ }
104
+ })
105
+
106
+ export const loginCollectionTool = defineCollectionTool({
107
+ description: 'Authenticates a user with email and password.',
108
+ input: z.object({
109
+ depth: z
110
+ .number()
111
+ .int()
112
+ .min(0)
113
+ .max(10)
114
+ .describe('Depth of population for relationships')
115
+ .optional()
116
+ .default(0),
117
+ email: emailSchema,
118
+ password: z.string().describe('The user password'),
119
+ showHiddenFields: z
120
+ .boolean()
121
+ .describe('Whether to show hidden fields in the response')
122
+ .optional()
123
+ .default(false),
124
+ }),
125
+ }).handler(async ({ collectionSlug, input, req }) => {
126
+ const logger = getLogger({ payload: req.payload })
127
+ try {
128
+ const result = await req.payload.login({
129
+ collection: collectionSlug,
130
+ data: { email: input.email, password: input.password },
131
+ depth: input.depth,
132
+ showHiddenFields: input.showHiddenFields,
133
+ })
134
+ return {
135
+ content: [
136
+ {
137
+ type: 'text',
138
+ text: `# Login Successful\n\n**User:** ${input.email}\n**Collection:** ${collectionSlug}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
139
+ },
140
+ ],
141
+ doc: result as unknown as Record<string, unknown>,
142
+ }
143
+ } catch (error) {
144
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
145
+ logger.error(`Error in login tool on ${collectionSlug}: ${errorMessage}`)
146
+ return wrapError('login')({ collectionSlug, message: errorMessage })
147
+ }
148
+ })
149
+
150
+ export const resetPasswordCollectionTool = defineCollectionTool({
151
+ description: 'Resets a user password with a reset token.',
152
+ input: z.object({
153
+ password: z.string().describe('The new password for the user'),
154
+ token: z.string().describe('The password reset token sent to the user email'),
155
+ }),
156
+ }).handler(async ({ collectionSlug, input, req }) => {
157
+ const logger = getLogger({ payload: req.payload })
158
+ try {
159
+ const result = await req.payload.resetPassword({
160
+ collection: collectionSlug,
161
+ data: { password: input.password, token: input.token },
162
+ overrideAccess: true,
163
+ })
164
+ return {
165
+ content: [
166
+ {
167
+ type: 'text',
168
+ text: `# Password Reset Successful\n\n**Collection:** ${collectionSlug}\n\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
169
+ },
170
+ ],
171
+ doc: result as unknown as Record<string, unknown>,
172
+ }
173
+ } catch (error) {
174
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
175
+ logger.error(`Error in resetPassword tool on ${collectionSlug}: ${errorMessage}`)
176
+ return wrapError('resetPassword')({ collectionSlug, message: errorMessage })
177
+ }
178
+ })
179
+
180
+ export const unlockCollectionTool = defineCollectionTool({
181
+ description: 'Unlocks a user account that has been locked due to failed login attempts.',
182
+ input: z.object({ email: emailSchema }),
183
+ }).handler(async ({ collectionSlug, input, req }) => {
184
+ const logger = getLogger({ payload: req.payload })
185
+ try {
186
+ const result = await req.payload.unlock({
187
+ collection: collectionSlug,
188
+ data: { email: input.email },
189
+ overrideAccess: true,
190
+ })
191
+ return {
192
+ content: [
193
+ {
194
+ type: 'text',
195
+ text: `# Account Unlocked\n\n**Collection:** ${collectionSlug}\n**Email:** ${input.email}\n**Result:** ${result ? 'Success' : 'Failed'}`,
196
+ },
197
+ ],
198
+ doc: { result } as Record<string, unknown>,
199
+ }
200
+ } catch (error) {
201
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
202
+ logger.error(`Error in unlock tool on ${collectionSlug}: ${errorMessage}`)
203
+ return wrapError('unlock')({ collectionSlug, message: errorMessage })
204
+ }
205
+ })
206
+
207
+ export const verifyCollectionTool = defineCollectionTool({
208
+ description: 'Verifies a user email with a verification token.',
209
+ input: z.object({
210
+ token: z.string().describe('The verification token sent to the user email'),
211
+ }),
212
+ }).handler(async ({ collectionSlug, input, req }) => {
213
+ const logger = getLogger({ payload: req.payload })
214
+ try {
215
+ const result = await req.payload.verifyEmail({
216
+ collection: collectionSlug,
217
+ token: input.token,
218
+ })
219
+ return {
220
+ content: [
221
+ {
222
+ type: 'text',
223
+ text: `# Email Verification Successful\n\n**Collection:** ${collectionSlug}\n**Result:** ${result ? 'Success' : 'Failed'}`,
224
+ },
225
+ ],
226
+ doc: { result } as Record<string, unknown>,
227
+ }
228
+ } catch (error) {
229
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
230
+ logger.error(`Error in verify tool on ${collectionSlug}: ${errorMessage}`)
231
+ return wrapError('verify')({ collectionSlug, message: errorMessage })
232
+ }
233
+ })
@@ -0,0 +1,116 @@
1
+ import type { SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defineCollectionTool } from '../../../defineTool.js'
6
+ import { getLogger } from '../../../utils/getLogger.js'
7
+ import {
8
+ getCollectionVirtualFieldNames,
9
+ stripVirtualFields,
10
+ } from '../../../utils/getVirtualFieldNames.js'
11
+ import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
12
+ import { prepareCollectionSchema } from '../../../utils/schemaConversion/prepareCollectionSchema.js'
13
+ import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
14
+
15
+ const DEFAULT_DESCRIPTION = 'Create a document in a collection.'
16
+
17
+ export const createCollectionTool = defineCollectionTool({
18
+ description: DEFAULT_DESCRIPTION,
19
+ input: ({ collectionSchema }) =>
20
+ z.object({
21
+ data: z
22
+ .fromJSONSchema(
23
+ prepareCollectionSchema(collectionSchema) as unknown as z.core.JSONSchema.JSONSchema,
24
+ )
25
+ .describe('The document fields to create'),
26
+ depth: z
27
+ .number()
28
+ .int()
29
+ .min(0)
30
+ .max(10)
31
+ .describe('How many levels deep to populate relationships in response')
32
+ .optional()
33
+ .default(0),
34
+ draft: z
35
+ .boolean()
36
+ .describe('Whether to create the document as a draft')
37
+ .optional()
38
+ .default(false),
39
+ fallbackLocale: z
40
+ .string()
41
+ .describe('Optional: fallback locale code to use when requested locale is not available')
42
+ .optional(),
43
+ locale: z
44
+ .string()
45
+ .describe(
46
+ 'Optional: locale code to create the document in (e.g., "en", "es"). Defaults to the default locale',
47
+ )
48
+ .optional(),
49
+ select: z
50
+ .string()
51
+ .describe(
52
+ 'Optional: define exactly which fields you\'d like to create (JSON), e.g., \'{"title": "My Post"}\'',
53
+ )
54
+ .optional(),
55
+ }),
56
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
57
+ const payload = req.payload
58
+ const logger = getLogger({ payload })
59
+
60
+ const { data, depth, draft, fallbackLocale, locale, select } = input
61
+
62
+ logger.info(
63
+ `Creating document in collection: ${collectionSlug}${locale ? ` with locale: ${locale}` : ''}`,
64
+ )
65
+
66
+ try {
67
+ let parsedData = transformPointDataToPayload(data as Record<string, unknown>)
68
+ const virtualFieldNames = getCollectionVirtualFieldNames(payload.config, collectionSlug)
69
+ parsedData = stripVirtualFields(parsedData, virtualFieldNames)
70
+
71
+ let selectClause: SelectType | undefined
72
+ if (select) {
73
+ try {
74
+ selectClause = JSON.parse(select) as SelectType
75
+ } catch {
76
+ logger.warn(`Invalid select clause JSON: ${select}`)
77
+ return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
78
+ }
79
+ }
80
+
81
+ const result = await payload.create({
82
+ collection: collectionSlug,
83
+ data: parsedData,
84
+ depth,
85
+ draft,
86
+ req,
87
+ ...localAPIDefaults(authorizedMCP),
88
+ ...(locale ? { locale } : {}),
89
+ ...(fallbackLocale ? { fallbackLocale } : {}),
90
+ ...(selectClause ? { select: selectClause } : {}),
91
+ })
92
+
93
+ logger.info(`Successfully created document in ${collectionSlug} with ID: ${result.id}`)
94
+
95
+ return {
96
+ content: [
97
+ {
98
+ type: 'text',
99
+ text: `Document created successfully in collection "${collectionSlug}"!\nCreated document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
100
+ },
101
+ ],
102
+ doc: result as Record<string, unknown>,
103
+ }
104
+ } catch (error) {
105
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
106
+ logger.error(`Error creating document in ${collectionSlug}: ${errorMessage}`)
107
+ return {
108
+ content: [
109
+ {
110
+ type: 'text',
111
+ text: `Error creating document in collection "${collectionSlug}": ${errorMessage}`,
112
+ },
113
+ ],
114
+ }
115
+ }
116
+ })
@@ -0,0 +1,123 @@
1
+ import { z } from 'zod'
2
+
3
+ import { defineCollectionTool } from '../../../defineTool.js'
4
+ import { getLogger } from '../../../utils/getLogger.js'
5
+ import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
6
+
7
+ const DEFAULT_DESCRIPTION = 'Delete documents in a collection by ID or where clause.'
8
+
9
+ export const deleteCollectionTool = defineCollectionTool({
10
+ description: DEFAULT_DESCRIPTION,
11
+ input: z.object({
12
+ id: z
13
+ .union([z.string(), z.number()])
14
+ .describe('Optional: specific document ID to delete')
15
+ .optional(),
16
+ depth: z
17
+ .number()
18
+ .int()
19
+ .min(0)
20
+ .max(10)
21
+ .describe('Depth of population for relationships in response')
22
+ .optional()
23
+ .default(0),
24
+ fallbackLocale: z
25
+ .string()
26
+ .describe('Optional: fallback locale code to use when requested locale is not available')
27
+ .optional(),
28
+ locale: z
29
+ .string()
30
+ .describe(
31
+ 'Optional: locale code for the operation (e.g., "en", "es"). Defaults to the default locale',
32
+ )
33
+ .optional(),
34
+ where: z
35
+ .string()
36
+ .describe('Optional: JSON string for where clause to delete multiple documents')
37
+ .optional(),
38
+ }),
39
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
40
+ const payload = req.payload
41
+ const logger = getLogger({ payload })
42
+
43
+ const { id, depth, fallbackLocale, locale, where } = input
44
+
45
+ logger.info(
46
+ `Deleting document from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}${locale ? `, locale: ${locale}` : ''}`,
47
+ )
48
+
49
+ try {
50
+ if (!id && !where) {
51
+ return {
52
+ content: [{ type: 'text', text: 'Error: Either id or where clause must be provided' }],
53
+ }
54
+ }
55
+
56
+ let whereClause: Record<string, unknown> = {}
57
+ if (where) {
58
+ try {
59
+ whereClause = JSON.parse(where) as Record<string, unknown>
60
+ } catch {
61
+ logger.warn(`Invalid where clause JSON: ${where}`)
62
+ return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
63
+ }
64
+ }
65
+
66
+ const deleteOptions: Record<string, unknown> = {
67
+ collection: collectionSlug,
68
+ depth,
69
+ req,
70
+ ...localAPIDefaults(authorizedMCP),
71
+ ...(locale && { locale }),
72
+ ...(fallbackLocale && { fallbackLocale }),
73
+ }
74
+
75
+ if (id) {
76
+ deleteOptions.id = id
77
+ } else {
78
+ deleteOptions.where = whereClause
79
+ }
80
+
81
+ const result = await payload.delete(deleteOptions as Parameters<typeof payload.delete>[0])
82
+
83
+ if (id) {
84
+ return {
85
+ content: [
86
+ {
87
+ type: 'text',
88
+ text: `Document deleted successfully from collection "${collectionSlug}"!\nDeleted document:\n\`\`\`json\n${JSON.stringify(result)}\n\`\`\``,
89
+ },
90
+ ],
91
+ doc: result as Record<string, unknown>,
92
+ }
93
+ }
94
+
95
+ const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
96
+ const docs = bulkResult.docs || []
97
+ const errors = bulkResult.errors || []
98
+
99
+ let responseText = `Document deleted successfully from collection "${collectionSlug}"!\nDeleted: ${docs.length} documents\nErrors: ${errors.length}\n---`
100
+ if (docs.length > 0) {
101
+ responseText += `\n\nDeleted documents:\n\`\`\`json\n${JSON.stringify(docs)}\n\`\`\``
102
+ }
103
+ if (errors.length > 0) {
104
+ responseText += `\n\nErrors:\n\`\`\`json\n${JSON.stringify(errors)}\n\`\`\``
105
+ }
106
+
107
+ return {
108
+ content: [{ type: 'text', text: responseText }],
109
+ doc: { docs, errors } as unknown as Record<string, unknown>,
110
+ }
111
+ } catch (error) {
112
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
113
+ logger.error(`Error deleting document from ${collectionSlug}: ${errorMessage}`)
114
+ return {
115
+ content: [
116
+ {
117
+ type: 'text',
118
+ text: `Error deleting document from collection "${collectionSlug}": ${errorMessage}`,
119
+ },
120
+ ],
121
+ }
122
+ }
123
+ })
@@ -0,0 +1,187 @@
1
+ import type { SelectType } from 'payload'
2
+
3
+ import { z } from 'zod'
4
+
5
+ import { defineCollectionTool } from '../../../defineTool.js'
6
+ import { getLogger } from '../../../utils/getLogger.js'
7
+ import { localAPIDefaults } from '../../../utils/localAPIDefaults.js'
8
+
9
+ const DEFAULT_DESCRIPTION =
10
+ 'Find documents in a collection by ID or where clause using Find or FindByID.'
11
+
12
+ export const findCollectionTool = defineCollectionTool({
13
+ description: DEFAULT_DESCRIPTION,
14
+ input: z.object({
15
+ id: z
16
+ .union([z.string(), z.number()])
17
+ .describe(
18
+ 'Optional: specific document ID to retrieve. If not provided, returns all documents',
19
+ )
20
+ .optional(),
21
+ depth: z
22
+ .number()
23
+ .int()
24
+ .min(0)
25
+ .max(10)
26
+ .describe('How many levels deep to populate relationships (default: 0)')
27
+ .optional()
28
+ .default(0),
29
+ draft: z
30
+ .boolean()
31
+ .describe(
32
+ 'Optional: whether the document should be queried from the versions table/collection or not.',
33
+ )
34
+ .optional(),
35
+ fallbackLocale: z
36
+ .string()
37
+ .describe('Optional: fallback locale code to use when requested locale is not available')
38
+ .optional(),
39
+ limit: z
40
+ .number()
41
+ .int()
42
+ .min(1)
43
+ .max(100)
44
+ .describe('Maximum number of documents to return (default: 10, max: 100)')
45
+ .optional()
46
+ .default(10),
47
+ locale: z
48
+ .string()
49
+ .describe(
50
+ 'Optional: locale code to retrieve data in (e.g., "en", "es"). Use "all" to retrieve all locales for localized fields',
51
+ )
52
+ .optional(),
53
+ page: z
54
+ .number()
55
+ .int()
56
+ .min(1)
57
+ .describe('Page number for pagination (default: 1)')
58
+ .optional()
59
+ .default(1),
60
+ select: z
61
+ .string()
62
+ .describe(
63
+ "Optional: define exactly which fields you'd like to return in the response (JSON), e.g., '{\"title\": true}'",
64
+ )
65
+ .optional(),
66
+ sort: z
67
+ .string()
68
+ .describe('Field to sort by (e.g., "createdAt", "-updatedAt" for descending)')
69
+ .optional(),
70
+ where: z
71
+ .string()
72
+ .describe(
73
+ 'Optional JSON string for where clause filtering (e.g., \'{"title": {"contains": "test"}}\')',
74
+ )
75
+ .optional(),
76
+ }),
77
+ }).handler(async ({ authorizedMCP, collectionSlug, input, req }) => {
78
+ const payload = req.payload
79
+ const logger = getLogger({ payload })
80
+
81
+ const { id, depth, draft, fallbackLocale, limit, locale, page, select, sort, where } = input
82
+
83
+ logger.info(
84
+ `Reading document from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ''}, limit: ${limit}, page: ${page}${locale ? `, locale: ${locale}` : ''}`,
85
+ )
86
+
87
+ try {
88
+ let whereClause: Record<string, unknown> = {}
89
+ if (where) {
90
+ try {
91
+ whereClause = JSON.parse(where) as Record<string, unknown>
92
+ } catch {
93
+ logger.warn(`Invalid where clause JSON: ${where}`)
94
+ return { content: [{ type: 'text', text: 'Error: Invalid JSON in where clause' }] }
95
+ }
96
+ }
97
+
98
+ let selectClause: SelectType | undefined
99
+ if (select) {
100
+ try {
101
+ selectClause = JSON.parse(select) as SelectType
102
+ } catch {
103
+ logger.warn(`Invalid select clause JSON: ${select}`)
104
+ return { content: [{ type: 'text', text: 'Error: Invalid JSON in select clause' }] }
105
+ }
106
+ }
107
+
108
+ if (id) {
109
+ try {
110
+ const doc = await payload.findByID({
111
+ id,
112
+ collection: collectionSlug,
113
+ depth,
114
+ req,
115
+ ...localAPIDefaults(authorizedMCP),
116
+ ...(selectClause && { select: selectClause }),
117
+ ...(locale && { locale }),
118
+ ...(fallbackLocale && { fallbackLocale }),
119
+ ...(draft !== undefined && { draft }),
120
+ })
121
+
122
+ return {
123
+ content: [
124
+ {
125
+ type: 'text',
126
+ text: `Document from collection "${collectionSlug}":\n${JSON.stringify(doc)}`,
127
+ },
128
+ ],
129
+ doc: doc as Record<string, unknown>,
130
+ }
131
+ } catch {
132
+ logger.warn(`Document not found with ID: ${id} in collection: ${collectionSlug}`)
133
+ return {
134
+ content: [
135
+ {
136
+ type: 'text',
137
+ text: `Error: Document with ID "${id}" not found in collection "${collectionSlug}"`,
138
+ },
139
+ ],
140
+ }
141
+ }
142
+ }
143
+
144
+ const findOptions: Parameters<typeof payload.find>[0] = {
145
+ collection: collectionSlug,
146
+ depth,
147
+ limit,
148
+ page,
149
+ req,
150
+ ...localAPIDefaults(authorizedMCP),
151
+ ...(selectClause && { select: selectClause }),
152
+ ...(locale && { locale }),
153
+ ...(fallbackLocale && { fallbackLocale }),
154
+ ...(draft !== undefined && { draft }),
155
+ }
156
+
157
+ if (sort) {
158
+ findOptions.sort = sort
159
+ }
160
+ if (Object.keys(whereClause).length > 0) {
161
+ findOptions.where = whereClause as Parameters<typeof payload.find>[0]['where']
162
+ }
163
+
164
+ const result = await payload.find(findOptions)
165
+
166
+ let responseText = `Collection: "${collectionSlug}"\nTotal: ${result.totalDocs} documents\nPage: ${result.page} of ${result.totalPages}\n`
167
+ for (const doc of result.docs) {
168
+ responseText += `\n\`\`\`json\n${JSON.stringify(doc)}\n\`\`\``
169
+ }
170
+
171
+ return {
172
+ content: [{ type: 'text', text: responseText }],
173
+ doc: result as unknown as Record<string, unknown>,
174
+ }
175
+ } catch (error) {
176
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
177
+ logger.error(`Error reading documents from collection ${collectionSlug}: ${errorMessage}`)
178
+ return {
179
+ content: [
180
+ {
181
+ type: 'text',
182
+ text: `❌ **Error reading documents from collection "${collectionSlug}":** ${errorMessage}`,
183
+ },
184
+ ],
185
+ }
186
+ }
187
+ })