@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,210 @@
1
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
2
+ import type { PayloadRequest, TypedUser } from 'payload'
3
+
4
+ import type { PluginMCPServerConfig } from '../../../types.js'
5
+
6
+ import { toCamelCase } from '../../../utils/camelCase.js'
7
+ import { toolSchemas } from '../schemas.js'
8
+
9
+ export const deleteResourceTool = (
10
+ server: McpServer,
11
+ req: PayloadRequest,
12
+ user: TypedUser,
13
+ verboseLogs: boolean,
14
+ collectionSlug: string,
15
+ collections: PluginMCPServerConfig['collections'],
16
+ ) => {
17
+ const tool = async (
18
+ id?: string,
19
+ where?: string,
20
+ depth: number = 0,
21
+ ): Promise<{
22
+ content: Array<{
23
+ text: string
24
+ type: 'text'
25
+ }>
26
+ }> => {
27
+ const payload = req.payload
28
+
29
+ if (verboseLogs) {
30
+ payload.logger.info(
31
+ `[payload-mcp] Deleting resource from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}`,
32
+ )
33
+ }
34
+
35
+ try {
36
+ // Validate that either id or where is provided
37
+ if (!id && !where) {
38
+ payload.logger.error('[payload-mcp] Either id or where clause must be provided')
39
+ const response = {
40
+ content: [
41
+ { type: 'text' as const, text: 'Error: Either id or where clause must be provided' },
42
+ ],
43
+ }
44
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
45
+ response) as {
46
+ content: Array<{
47
+ text: string
48
+ type: 'text'
49
+ }>
50
+ }
51
+ }
52
+
53
+ // Parse where clause if provided
54
+ let whereClause = {}
55
+ if (where) {
56
+ try {
57
+ whereClause = JSON.parse(where)
58
+ if (verboseLogs) {
59
+ payload.logger.info(`[payload-mcp] Using where clause: ${where}`)
60
+ }
61
+ } catch (_parseError) {
62
+ payload.logger.warn(`[payload-mcp] Invalid where clause JSON: ${where}`)
63
+ const response = {
64
+ content: [{ type: 'text' as const, text: 'Error: Invalid JSON in where clause' }],
65
+ }
66
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
67
+ response) as {
68
+ content: Array<{
69
+ text: string
70
+ type: 'text'
71
+ }>
72
+ }
73
+ }
74
+ }
75
+
76
+ // Build delete options
77
+ const deleteOptions: Record<string, unknown> = {
78
+ collection: collectionSlug,
79
+ depth,
80
+ user,
81
+ }
82
+
83
+ // Delete by ID or where clause
84
+ if (id) {
85
+ deleteOptions.id = id
86
+ if (verboseLogs) {
87
+ payload.logger.info(`[payload-mcp] Deleting single document with ID: ${id}`)
88
+ }
89
+ } else {
90
+ deleteOptions.where = whereClause
91
+ if (verboseLogs) {
92
+ payload.logger.info(`[payload-mcp] Deleting multiple documents with where clause`)
93
+ }
94
+ }
95
+
96
+ const result = await payload.delete(deleteOptions as Parameters<typeof payload.delete>[0])
97
+
98
+ // Handle different result types
99
+ if (id) {
100
+ // Single document deletion
101
+ if (verboseLogs) {
102
+ payload.logger.info(`[payload-mcp] Successfully deleted document with ID: ${id}`)
103
+ }
104
+
105
+ const response = {
106
+ content: [
107
+ {
108
+ type: 'text' as const,
109
+ text: `Document deleted successfully from collection "${collectionSlug}"!
110
+ Deleted document:
111
+ \`\`\`json
112
+ ${JSON.stringify(result, null, 2)}
113
+ \`\`\``,
114
+ },
115
+ ],
116
+ }
117
+
118
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||
119
+ response) as {
120
+ content: Array<{
121
+ text: string
122
+ type: 'text'
123
+ }>
124
+ }
125
+ } else {
126
+ // Multiple documents deletion
127
+ const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
128
+ const docs = bulkResult.docs || []
129
+ const errors = bulkResult.errors || []
130
+
131
+ if (verboseLogs) {
132
+ payload.logger.info(
133
+ `[payload-mcp] Successfully deleted ${docs.length} documents, ${errors.length} errors`,
134
+ )
135
+ }
136
+
137
+ let responseText = `Document deleted successfully from collection "${collectionSlug}"!
138
+ Deleted: ${docs.length} documents
139
+ Errors: ${errors.length}
140
+ ---`
141
+
142
+ if (docs.length > 0) {
143
+ responseText += `\n\nDeleted documents:
144
+ \`\`\`json
145
+ ${JSON.stringify(docs, null, 2)}
146
+ \`\`\``
147
+ }
148
+
149
+ if (errors.length > 0) {
150
+ responseText += `\n\nErrors:
151
+ \`\`\`json
152
+ ${JSON.stringify(errors, null, 2)}
153
+ \`\`\``
154
+ }
155
+
156
+ const response = {
157
+ content: [
158
+ {
159
+ type: 'text' as const,
160
+ text: responseText,
161
+ },
162
+ ],
163
+ }
164
+
165
+ return (collections?.[collectionSlug]?.overrideResponse?.(
166
+ response,
167
+ { docs, errors },
168
+ req,
169
+ ) || response) as {
170
+ content: Array<{
171
+ text: string
172
+ type: 'text'
173
+ }>
174
+ }
175
+ }
176
+ } catch (error) {
177
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
178
+ payload.logger.error(
179
+ `[payload-mcp] Error deleting resource from ${collectionSlug}: ${errorMessage}`,
180
+ )
181
+
182
+ const response = {
183
+ content: [
184
+ {
185
+ type: 'text' as const,
186
+ text: `Error deleting resource from collection "${collectionSlug}": ${errorMessage}`,
187
+ },
188
+ ],
189
+ }
190
+
191
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {
192
+ content: Array<{
193
+ text: string
194
+ type: 'text'
195
+ }>
196
+ }
197
+ }
198
+ }
199
+
200
+ if (collections?.[collectionSlug]?.enabled) {
201
+ server.tool(
202
+ `delete${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,
203
+ `${toolSchemas.deleteResource.description.trim()}\n\n${collections?.[collectionSlug]?.description || ''}`,
204
+ toolSchemas.deleteResource.parameters.shape,
205
+ async ({ id, depth, where }) => {
206
+ return await tool(id, where, depth)
207
+ },
208
+ )
209
+ }
210
+ }
@@ -0,0 +1,194 @@
1
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
2
+ import type { PayloadRequest, TypedUser } from 'payload'
3
+
4
+ import type { PluginMCPServerConfig } from '../../../types.js'
5
+
6
+ import { toCamelCase } from '../../../utils/camelCase.js'
7
+ import { toolSchemas } from '../schemas.js'
8
+
9
+ export const findResourceTool = (
10
+ server: McpServer,
11
+ req: PayloadRequest,
12
+ user: TypedUser,
13
+ verboseLogs: boolean,
14
+ collectionSlug: string,
15
+ collections: PluginMCPServerConfig['collections'],
16
+ ) => {
17
+ const tool = async (
18
+ id?: string,
19
+ limit: number = 10,
20
+ page: number = 1,
21
+ sort?: string,
22
+ where?: string,
23
+ ): Promise<{
24
+ content: Array<{
25
+ text: string
26
+ type: 'text'
27
+ }>
28
+ }> => {
29
+ const payload = req.payload
30
+
31
+ if (verboseLogs) {
32
+ payload.logger.info(
33
+ `[payload-mcp] Reading resource from collection: ${collectionSlug}${id ? ` with ID: ${id}` : ''}, limit: ${limit}, page: ${page}`,
34
+ )
35
+ }
36
+
37
+ try {
38
+ // Parse where clause if provided
39
+ let whereClause = {}
40
+ if (where) {
41
+ try {
42
+ whereClause = JSON.parse(where)
43
+ if (verboseLogs) {
44
+ payload.logger.info(`[payload-mcp] Using where clause: ${where}`)
45
+ }
46
+ } catch (_parseError) {
47
+ payload.logger.warn(`[payload-mcp] Invalid where clause JSON: ${where}`)
48
+ const response = {
49
+ content: [{ type: 'text' as const, text: 'Error: Invalid JSON in where clause' }],
50
+ }
51
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
52
+ response) as {
53
+ content: Array<{
54
+ text: string
55
+ type: 'text'
56
+ }>
57
+ }
58
+ }
59
+ }
60
+
61
+ // If ID is provided, use findByID
62
+ if (id) {
63
+ try {
64
+ const doc = await payload.findByID({
65
+ id,
66
+ collection: collectionSlug,
67
+ user,
68
+ })
69
+
70
+ if (verboseLogs) {
71
+ payload.logger.info(`[payload-mcp] Found document with ID: ${id}`)
72
+ }
73
+
74
+ const response = {
75
+ content: [
76
+ {
77
+ type: 'text' as const,
78
+ text: `Resource from collection "${collectionSlug}":
79
+ ${JSON.stringify(doc, null, 2)}`,
80
+ },
81
+ ],
82
+ }
83
+
84
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, doc, req) ||
85
+ response) as {
86
+ content: Array<{
87
+ text: string
88
+ type: 'text'
89
+ }>
90
+ }
91
+ } catch (_findError) {
92
+ payload.logger.warn(
93
+ `[payload-mcp] Document not found with ID: ${id} in collection: ${collectionSlug}`,
94
+ )
95
+ const response = {
96
+ content: [
97
+ {
98
+ type: 'text' as const,
99
+ text: `Error: Document with ID "${id}" not found in collection "${collectionSlug}"`,
100
+ },
101
+ ],
102
+ }
103
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
104
+ response) as {
105
+ content: Array<{
106
+ text: string
107
+ type: 'text'
108
+ }>
109
+ }
110
+ }
111
+ }
112
+
113
+ // Otherwise, use find to get multiple documents
114
+ const findOptions: Parameters<typeof payload.find>[0] = {
115
+ collection: collectionSlug,
116
+ limit,
117
+ page,
118
+ user,
119
+ }
120
+
121
+ if (sort) {
122
+ findOptions.sort = sort
123
+ }
124
+
125
+ if (Object.keys(whereClause).length > 0) {
126
+ findOptions.where = whereClause
127
+ }
128
+
129
+ const result = await payload.find(findOptions)
130
+
131
+ if (verboseLogs) {
132
+ payload.logger.info(
133
+ `[payload-mcp] Found ${result.docs.length} documents in collection: ${collectionSlug}`,
134
+ )
135
+ }
136
+
137
+ let responseText = `Collection: "${collectionSlug}"
138
+ Total: ${result.totalDocs} documents
139
+ Page: ${result.page} of ${result.totalPages}
140
+ `
141
+
142
+ for (const doc of result.docs) {
143
+ responseText += `\n\`\`\`json\n${JSON.stringify(doc, null, 2)}\n\`\`\``
144
+ }
145
+
146
+ const response = {
147
+ content: [
148
+ {
149
+ type: 'text' as const,
150
+ text: responseText,
151
+ },
152
+ ],
153
+ }
154
+
155
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||
156
+ response) as {
157
+ content: Array<{
158
+ text: string
159
+ type: 'text'
160
+ }>
161
+ }
162
+ } catch (error) {
163
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
164
+ payload.logger.error(
165
+ `[payload-mcp] Error reading resources from collection ${collectionSlug}: ${errorMessage}`,
166
+ )
167
+ const response = {
168
+ content: [
169
+ {
170
+ type: 'text' as const,
171
+ text: `❌ **Error reading resources from collection "${collectionSlug}":** ${errorMessage}`,
172
+ },
173
+ ],
174
+ }
175
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {
176
+ content: Array<{
177
+ text: string
178
+ type: 'text'
179
+ }>
180
+ }
181
+ }
182
+ }
183
+
184
+ if (collections?.[collectionSlug]?.enabled) {
185
+ server.tool(
186
+ `find${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,
187
+ `${toolSchemas.findResources.description.trim()}\n\n${collections?.[collectionSlug]?.description || ''}`,
188
+ toolSchemas.findResources.parameters.shape,
189
+ async ({ id, limit, page, sort, where }) => {
190
+ return await tool(id, limit, page, sort, where)
191
+ },
192
+ )
193
+ }
194
+ }
@@ -0,0 +1,314 @@
1
+ import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
2
+ import type { JSONSchema4 } from 'json-schema'
3
+ import type { PayloadRequest, TypedUser } from 'payload'
4
+
5
+ import { z } from 'zod'
6
+
7
+ import type { PluginMCPServerConfig } from '../../../types.js'
8
+
9
+ import { toCamelCase } from '../../../utils/camelCase.js'
10
+ import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'
11
+ import { toolSchemas } from '../schemas.js'
12
+ export const updateResourceTool = (
13
+ server: McpServer,
14
+ req: PayloadRequest,
15
+ user: TypedUser,
16
+ verboseLogs: boolean,
17
+ collectionSlug: string,
18
+ collections: PluginMCPServerConfig['collections'],
19
+ schema: JSONSchema4,
20
+ ) => {
21
+ const tool = async (
22
+ data: string,
23
+ id?: string,
24
+ where?: string,
25
+ draft: boolean = false,
26
+ depth: number = 0,
27
+ overrideLock: boolean = true,
28
+ filePath?: string,
29
+ overwriteExistingFiles: boolean = false,
30
+ ): Promise<{
31
+ content: Array<{
32
+ text: string
33
+ type: 'text'
34
+ }>
35
+ }> => {
36
+ const payload = req.payload
37
+
38
+ if (verboseLogs) {
39
+ payload.logger.info(
40
+ `[payload-mcp] Updating resource in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}`,
41
+ )
42
+ }
43
+
44
+ try {
45
+ // Parse the data JSON
46
+ let parsedData: Record<string, unknown>
47
+ try {
48
+ parsedData = JSON.parse(data)
49
+ if (verboseLogs) {
50
+ payload.logger.info(
51
+ `[payload-mcp] Parsed data for ${collectionSlug}: ${JSON.stringify(parsedData)}`,
52
+ )
53
+ }
54
+ } catch (_parseError) {
55
+ payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)
56
+ const response = {
57
+ content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],
58
+ }
59
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
60
+ response) as {
61
+ content: Array<{
62
+ text: string
63
+ type: 'text'
64
+ }>
65
+ }
66
+ }
67
+
68
+ // Validate that either id or where is provided
69
+ if (!id && !where) {
70
+ payload.logger.error('[payload-mcp] Either id or where clause must be provided')
71
+ const response = {
72
+ content: [
73
+ { type: 'text' as const, text: 'Error: Either id or where clause must be provided' },
74
+ ],
75
+ }
76
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
77
+ response) as {
78
+ content: Array<{
79
+ text: string
80
+ type: 'text'
81
+ }>
82
+ }
83
+ }
84
+
85
+ // Parse where clause if provided
86
+ let whereClause = {}
87
+ if (where) {
88
+ try {
89
+ whereClause = JSON.parse(where)
90
+ if (verboseLogs) {
91
+ payload.logger.info(`[payload-mcp] Using where clause: ${where}`)
92
+ }
93
+ } catch (_parseError) {
94
+ payload.logger.error(`[payload-mcp] Invalid where clause JSON: ${where}`)
95
+ const response = {
96
+ content: [{ type: 'text' as const, text: 'Error: Invalid JSON in where clause' }],
97
+ }
98
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||
99
+ response) as {
100
+ content: Array<{
101
+ text: string
102
+ type: 'text'
103
+ }>
104
+ }
105
+ }
106
+ }
107
+
108
+ // Update by ID or where clause
109
+ if (id) {
110
+ // Single document update
111
+ const updateOptions = {
112
+ id,
113
+ collection: collectionSlug,
114
+ data: parsedData,
115
+ depth,
116
+ draft,
117
+ overrideLock,
118
+ user,
119
+ ...(filePath && { filePath }),
120
+ ...(overwriteExistingFiles && { overwriteExistingFiles }),
121
+ }
122
+
123
+ if (verboseLogs) {
124
+ payload.logger.info(`[payload-mcp] Updating single document with ID: ${id}`)
125
+ }
126
+ const result = await payload.update({
127
+ ...updateOptions,
128
+ data: collections?.[collectionSlug]?.override?.(parsedData, req) || parsedData,
129
+ } as any)
130
+
131
+ if (verboseLogs) {
132
+ payload.logger.info(`[payload-mcp] Successfully updated document with ID: ${id}`)
133
+ }
134
+
135
+ const response = {
136
+ content: [
137
+ {
138
+ type: 'text' as const,
139
+ text: `Document updated successfully in collection "${collectionSlug}"!
140
+ Updated document:
141
+ \`\`\`json
142
+ ${JSON.stringify(result, null, 2)}
143
+ \`\`\``,
144
+ },
145
+ ],
146
+ }
147
+
148
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||
149
+ response) as {
150
+ content: Array<{
151
+ text: string
152
+ type: 'text'
153
+ }>
154
+ }
155
+ } else {
156
+ // Multiple documents update
157
+ const updateOptions = {
158
+ collection: collectionSlug,
159
+ data: parsedData,
160
+ depth,
161
+ draft,
162
+ overrideAccess: true,
163
+ overrideLock,
164
+ where: whereClause,
165
+ ...(filePath && { filePath }),
166
+ ...(overwriteExistingFiles && { overwriteExistingFiles }),
167
+ }
168
+
169
+ if (verboseLogs) {
170
+ payload.logger.info(`[payload-mcp] Updating multiple documents with where clause`)
171
+ }
172
+ const result = await payload.update({
173
+ ...updateOptions,
174
+ data: collections?.[collectionSlug]?.override?.(parsedData, req) || parsedData,
175
+ } as any)
176
+
177
+ const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }
178
+ const docs = bulkResult.docs || []
179
+ const errors = bulkResult.errors || []
180
+
181
+ if (verboseLogs) {
182
+ payload.logger.info(
183
+ `[payload-mcp] Successfully updated ${docs.length} documents, ${errors.length} errors`,
184
+ )
185
+ }
186
+
187
+ let responseText = `Multiple documents updated in collection "${collectionSlug}"!
188
+ Updated: ${docs.length} documents
189
+ Errors: ${errors.length}
190
+ ---`
191
+
192
+ if (docs.length > 0) {
193
+ responseText += `\n\nUpdated documents:
194
+ \`\`\`json
195
+ ${JSON.stringify(docs, null, 2)}
196
+ \`\`\``
197
+ }
198
+
199
+ if (errors.length > 0) {
200
+ responseText += `\n\nErrors:
201
+ \`\`\`json
202
+ ${JSON.stringify(errors, null, 2)}
203
+ \`\`\``
204
+ }
205
+
206
+ const response = {
207
+ content: [
208
+ {
209
+ type: 'text' as const,
210
+ text: responseText,
211
+ },
212
+ ],
213
+ }
214
+
215
+ return (collections?.[collectionSlug]?.overrideResponse?.(
216
+ response,
217
+ { docs, errors },
218
+ req,
219
+ ) || response) as {
220
+ content: Array<{
221
+ text: string
222
+ type: 'text'
223
+ }>
224
+ }
225
+ }
226
+ } catch (error) {
227
+ const errorMessage = error instanceof Error ? error.message : 'Unknown error'
228
+ payload.logger.error(
229
+ `[payload-mcp] Error updating resource in ${collectionSlug}: ${errorMessage}`,
230
+ )
231
+
232
+ const response = {
233
+ content: [
234
+ {
235
+ type: 'text' as const,
236
+ text: `Error updating resource in collection "${collectionSlug}": ${errorMessage}`,
237
+ },
238
+ ],
239
+ }
240
+
241
+ return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {
242
+ content: Array<{
243
+ text: string
244
+ type: 'text'
245
+ }>
246
+ }
247
+ }
248
+ }
249
+
250
+ if (collections?.[collectionSlug]?.enabled) {
251
+ const convertedFields = convertCollectionSchemaToZod(schema)
252
+
253
+ // Create a new schema that combines the converted fields with update-specific parameters
254
+ const updateResourceSchema = z.object({
255
+ ...convertedFields.shape,
256
+ id: z.string().optional().describe('The ID of the document to update'),
257
+ depth: z
258
+ .number()
259
+ .optional()
260
+ .default(0)
261
+ .describe('How many levels deep to populate relationships'),
262
+ draft: z
263
+ .boolean()
264
+ .optional()
265
+ .default(false)
266
+ .describe('Whether to update the document as a draft'),
267
+ filePath: z.string().optional().describe('File path for file uploads'),
268
+ overrideLock: z
269
+ .boolean()
270
+ .optional()
271
+ .default(true)
272
+ .describe('Whether to override document locks'),
273
+ overwriteExistingFiles: z
274
+ .boolean()
275
+ .optional()
276
+ .default(false)
277
+ .describe('Whether to overwrite existing files'),
278
+ where: z
279
+ .string()
280
+ .optional()
281
+ .describe('JSON string for where clause to update multiple documents'),
282
+ })
283
+
284
+ server.tool(
285
+ `update${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,
286
+ `${toolSchemas.updateResource.description.trim()}\n\n${collections?.[collectionSlug]?.description || ''}`,
287
+ updateResourceSchema.shape,
288
+ async (params: Record<string, unknown>) => {
289
+ const {
290
+ id,
291
+ depth,
292
+ draft,
293
+ filePath,
294
+ overrideLock,
295
+ overwriteExistingFiles,
296
+ where,
297
+ ...fieldData
298
+ } = params
299
+ // Convert field data back to JSON string format expected by the tool
300
+ const data = JSON.stringify(fieldData)
301
+ return await tool(
302
+ data,
303
+ id as string | undefined,
304
+ where as string | undefined,
305
+ draft as boolean,
306
+ depth as number,
307
+ overrideLock as boolean,
308
+ filePath as string | undefined,
309
+ overwriteExistingFiles as boolean,
310
+ )
311
+ },
312
+ )
313
+ }
314
+ }