@payloadcms/plugin-mcp 3.77.0-canary.7 → 3.77.0-canary.8

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 (36) hide show
  1. package/dist/mcp/tools/global/update.js +1 -1
  2. package/dist/mcp/tools/global/update.js.map +1 -1
  3. package/dist/mcp/tools/resource/create.js +1 -1
  4. package/dist/mcp/tools/resource/create.js.map +1 -1
  5. package/dist/mcp/tools/resource/update.js +1 -1
  6. package/dist/mcp/tools/resource/update.js.map +1 -1
  7. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.d.ts.map +1 -0
  8. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js +34 -0
  9. package/dist/utils/schemaConversion/convertCollectionSchemaToZod.js.map +1 -0
  10. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts +8 -0
  11. package/dist/utils/schemaConversion/sanitizeJsonSchema.d.ts.map +1 -0
  12. package/dist/utils/schemaConversion/sanitizeJsonSchema.js +18 -0
  13. package/dist/utils/schemaConversion/sanitizeJsonSchema.js.map +1 -0
  14. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts +20 -0
  15. package/dist/utils/schemaConversion/simplifyRelationshipFields.d.ts.map +1 -0
  16. package/dist/utils/schemaConversion/simplifyRelationshipFields.js +53 -0
  17. package/dist/utils/schemaConversion/simplifyRelationshipFields.js.map +1 -0
  18. package/dist/utils/schemaConversion/transformPointFields.d.ts.map +1 -0
  19. package/dist/utils/schemaConversion/transformPointFields.js.map +1 -0
  20. package/package.json +3 -3
  21. package/src/mcp/tools/global/update.ts +1 -1
  22. package/src/mcp/tools/resource/create.ts +1 -1
  23. package/src/mcp/tools/resource/update.ts +1 -1
  24. package/src/utils/schemaConversion/convertCollectionSchemaToZod.ts +41 -0
  25. package/src/utils/schemaConversion/sanitizeJsonSchema.ts +21 -0
  26. package/src/utils/schemaConversion/simplifyRelationshipFields.ts +65 -0
  27. package/dist/utils/convertCollectionSchemaToZod.d.ts.map +0 -1
  28. package/dist/utils/convertCollectionSchemaToZod.js +0 -80
  29. package/dist/utils/convertCollectionSchemaToZod.js.map +0 -1
  30. package/dist/utils/transformPointFields.d.ts.map +0 -1
  31. package/dist/utils/transformPointFields.js.map +0 -1
  32. package/src/utils/convertCollectionSchemaToZod.ts +0 -95
  33. /package/dist/utils/{convertCollectionSchemaToZod.d.ts → schemaConversion/convertCollectionSchemaToZod.d.ts} +0 -0
  34. /package/dist/utils/{transformPointFields.d.ts → schemaConversion/transformPointFields.d.ts} +0 -0
  35. /package/dist/utils/{transformPointFields.js → schemaConversion/transformPointFields.js} +0 -0
  36. /package/src/utils/{transformPointFields.ts → schemaConversion/transformPointFields.ts} +0 -0
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { toCamelCase } from '../../../utils/camelCase.js';
3
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js';
3
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js';
4
4
  import { toolSchemas } from '../schemas.js';
5
5
  export const updateGlobalTool = (server, req, user, verboseLogs, globalSlug, globals, schema)=>{
6
6
  const tool = async (data, draft = false, depth = 0, locale, fallbackLocale, select)=>{
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/mcp/tools/global/update.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'\nimport { toolSchemas } from '../schemas.js'\n\nexport const updateGlobalTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n globalSlug: string,\n globals: PluginMCPServerConfig['globals'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n draft: boolean = false,\n depth: number = 0,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Updating global: ${globalSlug}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${globalSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON for global: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n const updateOptions: Parameters<typeof payload.updateGlobal>[0] = {\n slug: globalSlug,\n data: parsedData,\n depth,\n draft,\n user,\n }\n\n // Add locale parameters if provided\n if (locale) {\n updateOptions.locale = locale\n }\n if (fallbackLocale) {\n updateOptions.fallbackLocale = fallbackLocale\n }\n if (selectClause) {\n updateOptions.select = selectClause\n }\n\n const result = await payload.updateGlobal(updateOptions)\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Successfully updated global: ${globalSlug}`)\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Global \"${globalSlug}\" updated successfully!\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (globals?.[globalSlug]?.overrideResponse?.(response, result, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(`[payload-mcp] Error updating global ${globalSlug}: ${errorMessage}`)\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error updating global \"${globalSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (globals?.[globalSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Make all fields optional for partial updates (PATCH-style)\n const optionalFields = Object.fromEntries(\n Object.entries(convertedFields.shape).map(([key, value]) => [key, (value as any).optional()]),\n )\n\n const updateGlobalSchema = z.object({\n ...optionalFields,\n depth: z.number().optional().describe('Optional: Depth of relationships to populate'),\n draft: z.boolean().optional().describe('Optional: Whether to save as draft (default: false)'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to update data in (e.g., \"en\", \"es\"). Use \"all\" to update all locales for localized fields',\n ),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"siteName\": \"My Site\"}\\'',\n ),\n })\n\n server.tool(\n `update${globalSlug.charAt(0).toUpperCase() + toCamelCase(globalSlug).slice(1)}`,\n `${toolSchemas.updateGlobal.description.trim()}\\n\\n${globals?.[globalSlug]?.description || ''}`,\n updateGlobalSchema.shape,\n async (params: Record<string, unknown>) => {\n const { depth, draft, fallbackLocale, locale, select, ...rest } = params\n const data = JSON.stringify(rest)\n return await tool(\n data,\n draft as boolean,\n depth as number,\n locale as string,\n fallbackLocale as string,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","toolSchemas","updateGlobalTool","server","req","user","verboseLogs","globalSlug","globals","schema","tool","data","draft","depth","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","response","content","type","text","overrideResponse","selectClause","warn","updateOptions","slug","result","updateGlobal","errorMessage","Error","message","enabled","convertedFields","optionalFields","Object","fromEntries","entries","shape","map","key","value","optional","updateGlobalSchema","object","number","describe","boolean","string","charAt","toUpperCase","slice","description","trim","params","rest"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,4BAA4B,QAAQ,iDAAgD;AAC7F,SAASC,WAAW,QAAQ,gBAAe;AAE3C,OAAO,MAAMC,mBAAmB,CAC9BC,QACAC,KACAC,MACAC,aACAC,YACAC,SACAC;IAEA,MAAMC,OAAO,OACXC,MACAC,QAAiB,KAAK,EACtBC,QAAgB,CAAC,EACjBC,QACAC,gBACAC;QAOA,MAAMC,UAAUb,IAAIa,OAAO;QAE3B,IAAIX,aAAa;YACfW,QAAQC,MAAM,CAACC,IAAI,CACjB,CAAC,+BAA+B,EAAEZ,WAAW,SAAS,EAAEK,QAAQE,SAAS,CAAC,UAAU,EAAEA,QAAQ,GAAG,IAAI;QAEzG;QAEA,IAAI;YACF,sBAAsB;YACtB,IAAIM;YACJ,IAAI;gBACFA,aAAaC,KAAKC,KAAK,CAACX;gBACxB,IAAIL,aAAa;oBACfW,QAAQC,MAAM,CAACC,IAAI,CACjB,CAAC,8BAA8B,EAAEZ,WAAW,EAAE,EAAEc,KAAKE,SAAS,CAACH,aAAa;gBAEhF;YACF,EAAE,OAAOI,aAAa;gBACpBP,QAAQC,MAAM,CAACO,KAAK,CAAC,CAAC,0CAA0C,EAAEd,MAAM;gBACxE,MAAMe,WAAW;oBACfC,SAAS;wBAAC;4BAAEC,MAAM;4BAAiBC,MAAM;wBAAoC;qBAAE;g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AAK,EAAED,KAAK,EAAEG,cAAc,EAAED,MAAM,EAAEE,MAAM,EAAE,GAAG+C,MAAM,GAAGD;YAClE,MAAMnD,OAAOU,KAAKE,SAAS,CAACwC;YAC5B,OAAO,MAAMrD,KACXC,MACAC,OACAC,OACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../../src/mcp/tools/global/update.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'\nimport { toolSchemas } from '../schemas.js'\n\nexport const updateGlobalTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n globalSlug: string,\n globals: PluginMCPServerConfig['globals'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n draft: boolean = false,\n depth: number = 0,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Updating global: ${globalSlug}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${globalSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON for global: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n const updateOptions: Parameters<typeof payload.updateGlobal>[0] = {\n slug: globalSlug,\n data: parsedData,\n depth,\n draft,\n user,\n }\n\n // Add locale parameters if provided\n if (locale) {\n updateOptions.locale = locale\n }\n if (fallbackLocale) {\n updateOptions.fallbackLocale = fallbackLocale\n }\n if (selectClause) {\n updateOptions.select = selectClause\n }\n\n const result = await payload.updateGlobal(updateOptions)\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Successfully updated global: ${globalSlug}`)\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Global \"${globalSlug}\" updated successfully!\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (globals?.[globalSlug]?.overrideResponse?.(response, result, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(`[payload-mcp] Error updating global ${globalSlug}: ${errorMessage}`)\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error updating global \"${globalSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (globals?.[globalSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (globals?.[globalSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Make all fields optional for partial updates (PATCH-style)\n const optionalFields = Object.fromEntries(\n Object.entries(convertedFields.shape).map(([key, value]) => [key, (value as any).optional()]),\n )\n\n const updateGlobalSchema = z.object({\n ...optionalFields,\n depth: z.number().optional().describe('Optional: Depth of relationships to populate'),\n draft: z.boolean().optional().describe('Optional: Whether to save as draft (default: false)'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to update data in (e.g., \"en\", \"es\"). Use \"all\" to update all locales for localized fields',\n ),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"siteName\": \"My Site\"}\\'',\n ),\n })\n\n server.tool(\n `update${globalSlug.charAt(0).toUpperCase() + toCamelCase(globalSlug).slice(1)}`,\n `${toolSchemas.updateGlobal.description.trim()}\\n\\n${globals?.[globalSlug]?.description || ''}`,\n updateGlobalSchema.shape,\n async (params: Record<string, unknown>) => {\n const { depth, draft, fallbackLocale, locale, select, ...rest } = params\n const data = JSON.stringify(rest)\n return await tool(\n data,\n draft as boolean,\n depth as number,\n locale as string,\n fallbackLocale as string,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","toolSchemas","updateGlobalTool","server","req","user","verboseLogs","globalSlug","globals","schema","tool","data","draft","depth","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","response","content","type","text","overrideResponse","selectClause","warn","updateOptions","slug","result","updateGlobal","errorMessage","Error","message","enabled","convertedFields","optionalFields","Object","fromEntries","entries","shape","map","key","value","optional","updateGlobalSchema","object","number","describe","boolean","string","charAt","toUpperCase","slice","description","trim","params","rest"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,4BAA4B,QAAQ,kEAAiE;AAC9G,SAASC,WAAW,QAAQ,gBAAe;AAE3C,OAAO,MAAMC,mBAAmB,CAC9BC,QACAC,KACAC,MACAC,aACAC,YACAC,SACAC;IAEA,MAAM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BAAqBrD,EAAEsD,MAAM,CAAC;YAClC,GAAGV,cAAc;YACjB7B,OAAOf,EAAEuD,MAAM,GAAGH,QAAQ,GAAGI,QAAQ,CAAC;YACtC1C,OAAOd,EAAEyD,OAAO,GAAGL,QAAQ,GAAGI,QAAQ,CAAC;YACvCvC,gBAAgBjB,EACb0D,MAAM,GACNN,QAAQ,GACRI,QAAQ,CAAC;YACZxC,QAAQhB,EACL0D,MAAM,GACNN,QAAQ,GACRI,QAAQ,CACP;YAEJtC,QAAQlB,EACL0D,MAAM,GACNN,QAAQ,GACRI,QAAQ,CACP;QAEN;QAEAnD,OAAOO,IAAI,CACT,CAAC,MAAM,EAAEH,WAAWkD,MAAM,CAAC,GAAGC,WAAW,KAAK3D,YAAYQ,YAAYoD,KAAK,CAAC,IAAI,EAChF,GAAG1D,YAAYmC,YAAY,CAACwB,WAAW,CAACC,IAAI,GAAG,IAAI,EAAErD,SAAS,CAACD,WAAW,EAAEqD,eAAe,IAAI,EAC/FT,mBAAmBL,KAAK,EACxB,OAAOgB;YACL,MAAM,EAAEjD,KAAK,EAAED,KAAK,EAAEG,cAAc,EAAED,MAAM,EAAEE,MAAM,EAAE,GAAG+C,MAAM,GAAGD;YAClE,MAAMnD,OAAOU,KAAKE,SAAS,CAACwC;YAC5B,OAAO,MAAMrD,KACXC,MACAC,OACAC,OACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { toCamelCase } from '../../../utils/camelCase.js';
3
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js';
3
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js';
4
4
  import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
5
5
  import { toolSchemas } from '../schemas.js';
6
6
  export const createResourceTool = (server, req, user, verboseLogs, collectionSlug, collections, schema)=>{
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/mcp/tools/resource/create.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { toolSchemas } from '../schemas.js'\nexport const createResourceTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n collectionSlug: string,\n collections: PluginMCPServerConfig['collections'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n depth: number = 0,\n draft: boolean,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Creating resource in collection: ${collectionSlug}${locale ? ` with locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n\n // Transform point fields from object format to tuple array\n parsedData = transformPointDataToPayload(parsedData)\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${collectionSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n return {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n // Create the resource\n const result = await payload.create({\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n req,\n user,\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n })\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Successfully created resource in ${collectionSlug} with ID: ${result.id}`,\n )\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Resource created successfully in collection \"${collectionSlug}\"!\nCreated resource:\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(\n `[payload-mcp] Error creating resource in ${collectionSlug}: ${errorMessage}`,\n )\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error creating resource in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (collections?.[collectionSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Create a new schema that combines the converted fields with create-specific parameters\n const createResourceSchema = z.object({\n ...convertedFields.shape,\n depth: z\n .number()\n .int()\n .min(0)\n .max(10)\n .optional()\n .default(0)\n .describe('How many levels deep to populate relationships in response'),\n draft: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to create the document as a draft'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to create the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n ),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to create (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n ),\n })\n\n server.tool(\n `create${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,\n `${collections?.[collectionSlug]?.description || toolSchemas.createResource.description.trim()}`,\n createResourceSchema.shape,\n async (params: Record<string, unknown>) => {\n const { depth, draft, fallbackLocale, locale, select, ...fieldData } = params\n const data = JSON.stringify(fieldData)\n return await tool(\n data,\n depth as number,\n draft as boolean,\n locale as string | undefined,\n fallbackLocale as string | undefined,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","transformPointDataToPayload","toolSchemas","createResourceTool","server","req","user","verboseLogs","collectionSlug","collections","schema","tool","data","depth","draft","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","content","type","text","selectClause","warn","response","overrideResponse","result","create","collection","overrideAccess","id","errorMessage","Error","message","enabled","convertedFields","createResourceSchema","object","shape","number","int","min","max","optional","default","describe","boolean","string","charAt","toUpperCase","slice","description","createResource","trim","params","fieldData"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,4BAA4B,QAAQ,iDAAgD;AAC7F,SAASC,2BAA2B,QAAQ,gDAA+C;AAC3F,SAASC,WAAW,QAAQ,gBAAe;AAC3C,OAAO,MAAMC,qBAAqB,CAChCC,QACAC,KACAC,MACAC,aACAC,gBACAC,aACAC;IAEA,MAAMC,OAAO,OACXC,MACAC,QAAgB,CAAC,EACjBC,OACAC,QACAC,gBACAC;QAOA,MAAMC,UAAUb,IAAIa,OAAO;QAE3B,IAAIX,aAAa;YACfW,QAAQC,MAAM,CAACC,IAAI,CACjB,CAAC,+CAA+C,EAAEZ,iBAAiBO,SAAS,CAAC,cAAc,EAAEA,QAAQ,GAAG,IAAI;QAEhH;QAEA,IAAI;YACF,sBAAsB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EH,eAAegD,MAAM,CAAC,GAAGC,WAAW,KAAK1D,YAAYS,gBAAgBkD,KAAK,CAAC,IAAI,EACxF,GAAGjD,aAAa,CAACD,eAAe,EAAEmD,eAAezD,YAAY0D,cAAc,CAACD,WAAW,CAACE,IAAI,IAAI,EAChGjB,qBAAqBE,KAAK,EAC1B,OAAOgB;YACL,MAAM,EAAEjD,KAAK,EAAEC,KAAK,EAAEE,cAAc,EAAED,MAAM,EAAEE,MAAM,EAAE,GAAG8C,WAAW,GAAGD;YACvE,MAAMlD,OAAOU,KAAKE,SAAS,CAACuC;YAC5B,OAAO,MAAMpD,KACXC,MACAC,OACAC,OACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../../src/mcp/tools/resource/create.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { toolSchemas } from '../schemas.js'\nexport const createResourceTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n collectionSlug: string,\n collections: PluginMCPServerConfig['collections'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n depth: number = 0,\n draft: boolean,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Creating resource in collection: ${collectionSlug}${locale ? ` with locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n\n // Transform point fields from object format to tuple array\n parsedData = transformPointDataToPayload(parsedData)\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${collectionSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n return {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n // Create the resource\n const result = await payload.create({\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n req,\n user,\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n })\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Successfully created resource in ${collectionSlug} with ID: ${result.id}`,\n )\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Resource created successfully in collection \"${collectionSlug}\"!\nCreated resource:\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(\n `[payload-mcp] Error creating resource in ${collectionSlug}: ${errorMessage}`,\n )\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error creating resource in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (collections?.[collectionSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Create a new schema that combines the converted fields with create-specific parameters\n const createResourceSchema = z.object({\n ...convertedFields.shape,\n depth: z\n .number()\n .int()\n .min(0)\n .max(10)\n .optional()\n .default(0)\n .describe('How many levels deep to populate relationships in response'),\n draft: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to create the document as a draft'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to create the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n ),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to create (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n ),\n })\n\n server.tool(\n `create${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,\n `${collections?.[collectionSlug]?.description || toolSchemas.createResource.description.trim()}`,\n createResourceSchema.shape,\n async (params: Record<string, unknown>) => {\n const { depth, draft, fallbackLocale, locale, select, ...fieldData } = params\n const data = JSON.stringify(fieldData)\n return await tool(\n data,\n depth as number,\n draft as boolean,\n locale as string | undefined,\n fallbackLocale as string | undefined,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","transformPointDataToPayload","toolSchemas","createResourceTool","server","req","user","verboseLogs","collectionSlug","collections","schema","tool","data","depth","draft","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","content","type","text","selectClause","warn","response","overrideResponse","result","create","collection","overrideAccess","id","errorMessage","Error","message","enabled","convertedFields","createResourceSchema","object","shape","number","int","min","max","optional","default","describe","boolean","string","charAt","toUpperCase","slice","description","createResource","trim","params","fieldData"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAAS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C;YACpC,GAAGF,gBAAgBG,KAAK;YACxBjC,OAAOf,EACJiD,MAAM,GACNC,GAAG,GACHC,GAAG,CAAC,GACJC,GAAG,CAAC,IACJC,QAAQ,GACRC,OAAO,CAAC,GACRC,QAAQ,CAAC;YACZvC,OAAOhB,EACJwD,OAAO,GACPH,QAAQ,GACRC,OAAO,CAAC,OACRC,QAAQ,CAAC;YACZrC,gBAAgBlB,EACbyD,MAAM,GACNJ,QAAQ,GACRE,QAAQ,CAAC;YACZtC,QAAQjB,EACLyD,MAAM,GACNJ,QAAQ,GACRE,QAAQ,CACP;YAEJpC,QAAQnB,EACLyD,MAAM,GACNJ,QAAQ,GACRE,QAAQ,CACP;QAEN;QAEAjD,OAAOO,IAAI,CACT,CAAC,MAAM,EAAEH,eAAegD,MAAM,CAAC,GAAGC,WAAW,KAAK1D,YAAYS,gBAAgBkD,KAAK,CAAC,IAAI,EACxF,GAAGjD,aAAa,CAACD,eAAe,EAAEmD,eAAezD,YAAY0D,cAAc,CAACD,WAAW,CAACE,IAAI,IAAI,EAChGjB,qBAAqBE,KAAK,EAC1B,OAAOgB;YACL,MAAM,EAAEjD,KAAK,EAAEC,KAAK,EAAEE,cAAc,EAAED,MAAM,EAAEE,MAAM,EAAE,GAAG8C,WAAW,GAAGD;YACvE,MAAMlD,OAAOU,KAAKE,SAAS,CAACuC;YAC5B,OAAO,MAAMpD,KACXC,MACAC,OACAC,OACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { toCamelCase } from '../../../utils/camelCase.js';
3
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js';
3
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js';
4
4
  import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js';
5
5
  import { toolSchemas } from '../schemas.js';
6
6
  export const updateResourceTool = (server, req, user, verboseLogs, collectionSlug, collections, schema)=>{
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/mcp/tools/resource/update.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { toolSchemas } from '../schemas.js'\nexport const updateResourceTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n collectionSlug: string,\n collections: PluginMCPServerConfig['collections'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n id?: number | string,\n where?: string,\n draft: boolean = false,\n depth: number = 0,\n overrideLock: boolean = true,\n filePath?: string,\n overwriteExistingFiles: boolean = false,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Updating resource in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n\n // Transform point fields from object format to tuple array\n parsedData = transformPointDataToPayload(parsedData)\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${collectionSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n // Validate that either id or where is provided\n if (!id && !where) {\n payload.logger.error('[payload-mcp] Either id or where clause must be provided')\n const response = {\n content: [\n { type: 'text' as const, text: 'Error: Either id or where clause must be provided' },\n ],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n // Parse where clause if provided\n let whereClause = {}\n if (where) {\n try {\n whereClause = JSON.parse(where)\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Using where clause: ${where}`)\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid where clause JSON: ${where}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in where clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n // Update by ID or where clause\n if (id) {\n // Single document update\n const updateOptions = {\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n overrideLock,\n req,\n user,\n ...(filePath && { filePath }),\n ...(overwriteExistingFiles && { overwriteExistingFiles }),\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n }\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Updating single document with ID: ${id}`)\n }\n const result = await payload.update({\n ...updateOptions,\n data: parsedData,\n } as any)\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Successfully updated document with ID: ${id}`)\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Document updated successfully in collection \"${collectionSlug}\"!\nUpdated document:\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } else {\n // Multiple documents update\n const updateOptions = {\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n overrideLock,\n req,\n user,\n where: whereClause,\n ...(filePath && { filePath }),\n ...(overwriteExistingFiles && { overwriteExistingFiles }),\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n }\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Updating multiple documents with where clause`)\n }\n const result = await payload.update({\n ...updateOptions,\n data: parsedData,\n } as any)\n\n const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }\n const docs = bulkResult.docs || []\n const errors = bulkResult.errors || []\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Successfully updated ${docs.length} documents, ${errors.length} errors`,\n )\n }\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`\n\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\n\\`\\`\\`json\n${JSON.stringify(docs, null, 2)}\n\\`\\`\\``\n }\n\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\n\\`\\`\\`json\n${JSON.stringify(errors, null, 2)}\n\\`\\`\\``\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: responseText,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(\n response,\n { docs, errors },\n req,\n ) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(\n `[payload-mcp] Error updating resource in ${collectionSlug}: ${errorMessage}`,\n )\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error updating resource in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (collections?.[collectionSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Create a new schema that combines the converted fields with update-specific parameters\n // Use .partial() to make all fields optional for partial updates\n const updateResourceSchema = z.object({\n ...convertedFields.partial().shape,\n id: z.union([z.string(), z.number()]).optional().describe('The ID of the document to update'),\n depth: z\n .number()\n .optional()\n .default(0)\n .describe('How many levels deep to populate relationships'),\n draft: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to update the document as a draft'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n filePath: z.string().optional().describe('File path for file uploads'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n ),\n overrideLock: z\n .boolean()\n .optional()\n .default(true)\n .describe('Whether to override document locks'),\n overwriteExistingFiles: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to overwrite existing files'),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n ),\n where: z\n .string()\n .optional()\n .describe('JSON string for where clause to update multiple documents'),\n })\n\n server.tool(\n `update${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,\n `${collections?.[collectionSlug]?.description || toolSchemas.updateResource.description.trim()}`,\n updateResourceSchema.shape,\n async (params: Record<string, unknown>) => {\n const {\n id,\n depth,\n draft,\n fallbackLocale,\n filePath,\n locale,\n overrideLock,\n overwriteExistingFiles,\n select,\n where,\n ...fieldData\n } = params\n // Convert field data back to JSON string format expected by the tool\n const data = JSON.stringify(fieldData)\n return await tool(\n data,\n id as number | string | undefined,\n where as string | undefined,\n draft as boolean,\n depth as number,\n overrideLock as boolean,\n filePath as string | undefined,\n overwriteExistingFiles as boolean,\n locale as string | undefined,\n fallbackLocale as string | undefined,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","transformPointDataToPayload","toolSchemas","updateResourceTool","server","req","user","verboseLogs","collectionSlug","collections","schema","tool","data","id","where","draft","depth","overrideLock","filePath","overwriteExistingFiles","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","response","content","type","text","overrideResponse","whereClause","selectClause","warn","updateOptions","collection","overrideAccess","result","update","bulkResult","docs","errors","length","responseText","errorMessage","Error","message","enabled","convertedFields","updateResourceSchema","object","partial","shape","union","string","number","optional","describe","default","boolean","charAt","toUpperCase","slice","description","updateResource","trim","params","fieldData"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,4BAA4B,QAAQ,iDAAgD;AAC7F,SAASC,2BAA2B,QAAQ,gDAA+C;AAC3F,SAASC,WAAW,QAAQ,gBAAe;AAC3C,OAAO,MAAMC,qBAAqB,CAChCC,QACAC,KACAC,MACAC,aACAC,gBACAC,aACAC;IAEA,MAAMC,OAAO,OACXC,MACAC,IACAC,OACAC,QAAiB,KAAK,EACtBC,QAAgB,CAAC,EACjBC,eAAwB,IAAI,EAC5BC,UACAC,yBAAkC,KAAK,EACvCC,QACAC,gBACAC;QAOA,MAAMC,UAAUlB,IAAIkB,OAAO;QAE3B,IAAIhB,aAAa;YACfgB,QAAQC,MAAM,CAACC,IAAI,CACjB,CAAC,+CAA+C,EAAEjB,iBAAiBK,KAAK,CAAC,UAAU,EAAEA,IAAI,GAAG,qBAAqB,SAAS,EAAEE,QAAQK,SAAS,CAAC,UAAU,EAAEA,QAAQ,GAAG,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PH,QAAQ,GACRE,OAAO,CAAC,OACRD,QAAQ,CAAC;YACZ1C,gBAAgBvB,EACb8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CAAC;YACZ7C,UAAUpB,EAAE8D,MAAM,GAAGE,QAAQ,GAAGC,QAAQ,CAAC;YACzC3C,QAAQtB,EACL8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CACP;YAEJ9C,cAAcnB,EACXmE,OAAO,GACPH,QAAQ,GACRE,OAAO,CAAC,MACRD,QAAQ,CAAC;YACZ5C,wBAAwBrB,EACrBmE,OAAO,GACPH,QAAQ,GACRE,OAAO,CAAC,OACRD,QAAQ,CAAC;YACZzC,QAAQxB,EACL8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CACP;YAEJjD,OAAOhB,EACJ8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CAAC;QACd;QAEA3D,OAAOO,IAAI,CACT,CAAC,MAAM,EAAEH,eAAe0D,MAAM,CAAC,GAAGC,WAAW,KAAKpE,YAAYS,gBAAgB4D,KAAK,CAAC,IAAI,EACxF,GAAG3D,aAAa,CAACD,eAAe,EAAE6D,eAAenE,YAAYoE,cAAc,CAACD,WAAW,CAACE,IAAI,IAAI,EAChGhB,qBAAqBG,KAAK,EAC1B,OAAOc;YACL,MAAM,EACJ3D,EAAE,EACFG,KAAK,EACLD,KAAK,EACLM,cAAc,EACdH,QAAQ,EACRE,MAAM,EACNH,YAAY,EACZE,sBAAsB,EACtBG,MAAM,EACNR,KAAK,EACL,GAAG2D,WACJ,GAAGD;YACJ,qEAAqE;YACrE,MAAM5D,OAAOe,KAAKE,SAAS,CAAC4C;YAC5B,OAAO,MAAM9D,KACXC,MACAC,IACAC,OACAC,OACAC,OACAC,cACAC,UACAC,wBACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
1
+ {"version":3,"sources":["../../../../src/mcp/tools/resource/update.ts"],"sourcesContent":["import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'\nimport type { JSONSchema4 } from 'json-schema'\nimport type { PayloadRequest, SelectType, TypedUser } from 'payload'\n\nimport { z } from 'zod'\n\nimport type { PluginMCPServerConfig } from '../../../types.js'\n\nimport { toCamelCase } from '../../../utils/camelCase.js'\nimport { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'\nimport { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'\nimport { toolSchemas } from '../schemas.js'\nexport const updateResourceTool = (\n server: McpServer,\n req: PayloadRequest,\n user: TypedUser,\n verboseLogs: boolean,\n collectionSlug: string,\n collections: PluginMCPServerConfig['collections'],\n schema: JSONSchema4,\n) => {\n const tool = async (\n data: string,\n id?: number | string,\n where?: string,\n draft: boolean = false,\n depth: number = 0,\n overrideLock: boolean = true,\n filePath?: string,\n overwriteExistingFiles: boolean = false,\n locale?: string,\n fallbackLocale?: string,\n select?: string,\n ): Promise<{\n content: Array<{\n text: string\n type: 'text'\n }>\n }> => {\n const payload = req.payload\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Updating resource in collection: ${collectionSlug}${id ? ` with ID: ${id}` : ' with where clause'}, draft: ${draft}${locale ? `, locale: ${locale}` : ''}`,\n )\n }\n\n try {\n // Parse the data JSON\n let parsedData: Record<string, unknown>\n try {\n parsedData = JSON.parse(data)\n\n // Transform point fields from object format to tuple array\n parsedData = transformPointDataToPayload(parsedData)\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Parsed data for ${collectionSlug}: ${JSON.stringify(parsedData)}`,\n )\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid JSON data provided: ${data}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON data provided' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n // Validate that either id or where is provided\n if (!id && !where) {\n payload.logger.error('[payload-mcp] Either id or where clause must be provided')\n const response = {\n content: [\n { type: 'text' as const, text: 'Error: Either id or where clause must be provided' },\n ],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n\n // Parse where clause if provided\n let whereClause = {}\n if (where) {\n try {\n whereClause = JSON.parse(where)\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Using where clause: ${where}`)\n }\n } catch (_parseError) {\n payload.logger.error(`[payload-mcp] Invalid where clause JSON: ${where}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in where clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n let selectClause: SelectType | undefined\n if (select) {\n try {\n selectClause = JSON.parse(select) as SelectType\n } catch (_parseError) {\n payload.logger.warn(`[payload-mcp] Invalid select clause JSON: ${select}`)\n const response = {\n content: [{ type: 'text' as const, text: 'Error: Invalid JSON in select clause' }],\n }\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n // Update by ID or where clause\n if (id) {\n // Single document update\n const updateOptions = {\n id,\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n overrideLock,\n req,\n user,\n ...(filePath && { filePath }),\n ...(overwriteExistingFiles && { overwriteExistingFiles }),\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n }\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Updating single document with ID: ${id}`)\n }\n const result = await payload.update({\n ...updateOptions,\n data: parsedData,\n } as any)\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Successfully updated document with ID: ${id}`)\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Document updated successfully in collection \"${collectionSlug}\"!\nUpdated document:\n\\`\\`\\`json\n${JSON.stringify(result, null, 2)}\n\\`\\`\\``,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, result, req) ||\n response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n } else {\n // Multiple documents update\n const updateOptions = {\n collection: collectionSlug,\n data: parsedData,\n depth,\n draft,\n overrideAccess: false,\n overrideLock,\n req,\n user,\n where: whereClause,\n ...(filePath && { filePath }),\n ...(overwriteExistingFiles && { overwriteExistingFiles }),\n ...(locale && { locale }),\n ...(fallbackLocale && { fallbackLocale }),\n ...(selectClause && { select: selectClause }),\n }\n\n if (verboseLogs) {\n payload.logger.info(`[payload-mcp] Updating multiple documents with where clause`)\n }\n const result = await payload.update({\n ...updateOptions,\n data: parsedData,\n } as any)\n\n const bulkResult = result as { docs?: unknown[]; errors?: unknown[] }\n const docs = bulkResult.docs || []\n const errors = bulkResult.errors || []\n\n if (verboseLogs) {\n payload.logger.info(\n `[payload-mcp] Successfully updated ${docs.length} documents, ${errors.length} errors`,\n )\n }\n\n let responseText = `Multiple documents updated in collection \"${collectionSlug}\"!\nUpdated: ${docs.length} documents\nErrors: ${errors.length}\n---`\n\n if (docs.length > 0) {\n responseText += `\\n\\nUpdated documents:\n\\`\\`\\`json\n${JSON.stringify(docs, null, 2)}\n\\`\\`\\``\n }\n\n if (errors.length > 0) {\n responseText += `\\n\\nErrors:\n\\`\\`\\`json\n${JSON.stringify(errors, null, 2)}\n\\`\\`\\``\n }\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: responseText,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(\n response,\n { docs, errors },\n req,\n ) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error'\n payload.logger.error(\n `[payload-mcp] Error updating resource in ${collectionSlug}: ${errorMessage}`,\n )\n\n const response = {\n content: [\n {\n type: 'text' as const,\n text: `Error updating resource in collection \"${collectionSlug}\": ${errorMessage}`,\n },\n ],\n }\n\n return (collections?.[collectionSlug]?.overrideResponse?.(response, {}, req) || response) as {\n content: Array<{\n text: string\n type: 'text'\n }>\n }\n }\n }\n\n if (collections?.[collectionSlug]?.enabled) {\n const convertedFields = convertCollectionSchemaToZod(schema)\n\n // Create a new schema that combines the converted fields with update-specific parameters\n // Use .partial() to make all fields optional for partial updates\n const updateResourceSchema = z.object({\n ...convertedFields.partial().shape,\n id: z.union([z.string(), z.number()]).optional().describe('The ID of the document to update'),\n depth: z\n .number()\n .optional()\n .default(0)\n .describe('How many levels deep to populate relationships'),\n draft: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to update the document as a draft'),\n fallbackLocale: z\n .string()\n .optional()\n .describe('Optional: fallback locale code to use when requested locale is not available'),\n filePath: z.string().optional().describe('File path for file uploads'),\n locale: z\n .string()\n .optional()\n .describe(\n 'Optional: locale code to update the document in (e.g., \"en\", \"es\"). Defaults to the default locale',\n ),\n overrideLock: z\n .boolean()\n .optional()\n .default(true)\n .describe('Whether to override document locks'),\n overwriteExistingFiles: z\n .boolean()\n .optional()\n .default(false)\n .describe('Whether to overwrite existing files'),\n select: z\n .string()\n .optional()\n .describe(\n 'Optional: define exactly which fields you\\'d like to return in the response (JSON), e.g., \\'{\"title\": \"My Post\"}\\'',\n ),\n where: z\n .string()\n .optional()\n .describe('JSON string for where clause to update multiple documents'),\n })\n\n server.tool(\n `update${collectionSlug.charAt(0).toUpperCase() + toCamelCase(collectionSlug).slice(1)}`,\n `${collections?.[collectionSlug]?.description || toolSchemas.updateResource.description.trim()}`,\n updateResourceSchema.shape,\n async (params: Record<string, unknown>) => {\n const {\n id,\n depth,\n draft,\n fallbackLocale,\n filePath,\n locale,\n overrideLock,\n overwriteExistingFiles,\n select,\n where,\n ...fieldData\n } = params\n // Convert field data back to JSON string format expected by the tool\n const data = JSON.stringify(fieldData)\n return await tool(\n data,\n id as number | string | undefined,\n where as string | undefined,\n draft as boolean,\n depth as number,\n overrideLock as boolean,\n filePath as string | undefined,\n overwriteExistingFiles as boolean,\n locale as string | undefined,\n fallbackLocale as string | undefined,\n select as string | undefined,\n )\n },\n )\n }\n}\n"],"names":["z","toCamelCase","convertCollectionSchemaToZod","transformPointDataToPayload","toolSchemas","updateResourceTool","server","req","user","verboseLogs","collectionSlug","collections","schema","tool","data","id","where","draft","depth","overrideLock","filePath","overwriteExistingFiles","locale","fallbackLocale","select","payload","logger","info","parsedData","JSON","parse","stringify","_parseError","error","response","content","type","text","overrideResponse","whereClause","selectClause","warn","updateOptions","collection","overrideAccess","result","update","bulkResult","docs","errors","length","responseText","errorMessage","Error","message","enabled","convertedFields","updateResourceSchema","object","partial","shape","union","string","number","optional","describe","default","boolean","charAt","toUpperCase","slice","description","updateResource","trim","params","fieldData"],"mappings":"AAIA,SAASA,CAAC,QAAQ,MAAK;AAIvB,SAASC,WAAW,QAAQ,8BAA6B;AACzD,SAASC,4BAA4B,QAAQ,kEAAiE;AAC9G,SAASC,2BAA2B,QAAQ,gDAA+C;AAC3F,SAASC,WAAW,QAAQ,gBAAe;AAC3C,OAAO,MAAMC,qBAAqB,CAChCC,QACAC,KACAC,MACAC,aACAC,gBACAC,aACAC;IAEA,MAAMC,OAAO,OACXC,MACAC,IACAC,OACAC,QAAiB,KAAK,EACtBC,QAAgB,CAAC,EACjBC,eAAwB,IAAI,EAC5BC,UACAC,yBAAkC,KAAK,EACvCC,QACAC,gBACAC;QAOA,MAAMC,UAAUlB,IAAIkB,OAAO;QAE3B,IAAIhB,aAAa;YACfgB,QAAQC,MAAM,CAACC,IAAI,CACjB,CAAC,+CAA+C,EAAEjB,iBAAiBK,KAAK,CAAC,UAAU,EAAEA,IAAI,GAAG,qBAAqB,SAAS,EAAEE,QAAQK,SAAS,CAAC,UAAU,EAAEA,QAAQ,GAAG,IAAI;QAE7K;QAEA,IAAI;YACF,sBAAsB;YACtB,IAAIM;YACJ,IAAI;gBACFA,aAAaC,KAAKC,KAAK,CAAChB;gBAExB,2DAA2D;gBA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7C,UAAUpB,EAAE8D,MAAM,GAAGE,QAAQ,GAAGC,QAAQ,CAAC;YACzC3C,QAAQtB,EACL8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CACP;YAEJ9C,cAAcnB,EACXmE,OAAO,GACPH,QAAQ,GACRE,OAAO,CAAC,MACRD,QAAQ,CAAC;YACZ5C,wBAAwBrB,EACrBmE,OAAO,GACPH,QAAQ,GACRE,OAAO,CAAC,OACRD,QAAQ,CAAC;YACZzC,QAAQxB,EACL8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CACP;YAEJjD,OAAOhB,EACJ8D,MAAM,GACNE,QAAQ,GACRC,QAAQ,CAAC;QACd;QAEA3D,OAAOO,IAAI,CACT,CAAC,MAAM,EAAEH,eAAe0D,MAAM,CAAC,GAAGC,WAAW,KAAKpE,YAAYS,gBAAgB4D,KAAK,CAAC,IAAI,EACxF,GAAG3D,aAAa,CAACD,eAAe,EAAE6D,eAAenE,YAAYoE,cAAc,CAACD,WAAW,CAACE,IAAI,IAAI,EAChGhB,qBAAqBG,KAAK,EAC1B,OAAOc;YACL,MAAM,EACJ3D,EAAE,EACFG,KAAK,EACLD,KAAK,EACLM,cAAc,EACdH,QAAQ,EACRE,MAAM,EACNH,YAAY,EACZE,sBAAsB,EACtBG,MAAM,EACNR,KAAK,EACL,GAAG2D,WACJ,GAAGD;YACJ,qEAAqE;YACrE,MAAM5D,OAAOe,KAAKE,SAAS,CAAC4C;YAC5B,OAAO,MAAM9D,KACXC,MACAC,IACAC,OACAC,OACAC,OACAC,cACAC,UACAC,wBACAC,QACAC,gBACAC;QAEJ;IAEJ;AACF,EAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convertCollectionSchemaToZod.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/convertCollectionSchemaToZod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAU9C,eAAO,MAAM,4BAA4B,WAAY,WAAW,QA8B/D,CAAA"}
@@ -0,0 +1,34 @@
1
+ import { jsonSchemaToZod } from 'json-schema-to-zod';
2
+ import * as ts from 'typescript';
3
+ import { z } from 'zod';
4
+ import { sanitizeJsonSchema } from './sanitizeJsonSchema.js';
5
+ import { simplifyRelationshipFields } from './simplifyRelationshipFields.js';
6
+ import { transformPointFieldsForMCP } from './transformPointFields.js';
7
+ export const convertCollectionSchemaToZod = (schema)=>{
8
+ // Clone to avoid mutating the original schema (used elsewhere for tool listing)
9
+ const schemaClone = JSON.parse(JSON.stringify(schema));
10
+ const sanitized = sanitizeJsonSchema(schemaClone);
11
+ const pointTransformed = transformPointFieldsForMCP(sanitized);
12
+ const simplifiedSchema = simplifyRelationshipFields(pointTransformed);
13
+ const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema);
14
+ // Transpile TypeScript to JavaScript
15
+ const transpileResult = ts.transpileModule(zodSchemaAsString, {
16
+ compilerOptions: {
17
+ module: ts.ModuleKind.CommonJS,
18
+ removeComments: true,
19
+ strict: false,
20
+ target: ts.ScriptTarget.ES2018
21
+ }
22
+ });
23
+ /**
24
+ * This Function evaluation is safe because:
25
+ * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer
26
+ * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code
27
+ * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic
28
+ * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing
29
+ * 5. No user input or external data is involved in the schema generation process
30
+ */ // eslint-disable-next-line @typescript-eslint/no-implied-eval
31
+ return new Function('z', `return ${transpileResult.outputText}`)(z);
32
+ };
33
+
34
+ //# sourceMappingURL=convertCollectionSchemaToZod.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/schemaConversion/convertCollectionSchemaToZod.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\nimport { jsonSchemaToZod } from 'json-schema-to-zod'\nimport * as ts from 'typescript'\nimport { z } from 'zod'\n\nimport { sanitizeJsonSchema } from './sanitizeJsonSchema.js'\nimport { simplifyRelationshipFields } from './simplifyRelationshipFields.js'\nimport { transformPointFieldsForMCP } from './transformPointFields.js'\n\nexport const convertCollectionSchemaToZod = (schema: JSONSchema4) => {\n // Clone to avoid mutating the original schema (used elsewhere for tool listing)\n const schemaClone = JSON.parse(JSON.stringify(schema)) as JSONSchema4\n\n const sanitized = sanitizeJsonSchema(schemaClone)\n const pointTransformed = transformPointFieldsForMCP(sanitized)\n const simplifiedSchema = simplifyRelationshipFields(pointTransformed)\n\n const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema)\n\n // Transpile TypeScript to JavaScript\n const transpileResult = ts.transpileModule(zodSchemaAsString, {\n compilerOptions: {\n module: ts.ModuleKind.CommonJS,\n removeComments: true,\n strict: false,\n target: ts.ScriptTarget.ES2018,\n },\n })\n\n /**\n * This Function evaluation is safe because:\n * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer\n * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code\n * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic\n * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing\n * 5. No user input or external data is involved in the schema generation process\n */\n // eslint-disable-next-line @typescript-eslint/no-implied-eval\n return new Function('z', `return ${transpileResult.outputText}`)(z)\n}\n"],"names":["jsonSchemaToZod","ts","z","sanitizeJsonSchema","simplifyRelationshipFields","transformPointFieldsForMCP","convertCollectionSchemaToZod","schema","schemaClone","JSON","parse","stringify","sanitized","pointTransformed","simplifiedSchema","zodSchemaAsString","transpileResult","transpileModule","compilerOptions","module","ModuleKind","CommonJS","removeComments","strict","target","ScriptTarget","ES2018","Function","outputText"],"mappings":"AAEA,SAASA,eAAe,QAAQ,qBAAoB;AACpD,YAAYC,QAAQ,aAAY;AAChC,SAASC,CAAC,QAAQ,MAAK;AAEvB,SAASC,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,0BAA0B,QAAQ,kCAAiC;AAC5E,SAASC,0BAA0B,QAAQ,4BAA2B;AAEtE,OAAO,MAAMC,+BAA+B,CAACC;IAC3C,gFAAgF;IAChF,MAAMC,cAAcC,KAAKC,KAAK,CAACD,KAAKE,SAAS,CAACJ;IAE9C,MAAMK,YAAYT,mBAAmBK;IACrC,MAAMK,mBAAmBR,2BAA2BO;IACpD,MAAME,mBAAmBV,2BAA2BS;IAEpD,MAAME,oBAAoBf,gBAAgBc;IAE1C,qCAAqC;IACrC,MAAME,kBAAkBf,GAAGgB,eAAe,CAACF,mBAAmB;QAC5DG,iBAAiB;YACfC,QAAQlB,GAAGmB,UAAU,CAACC,QAAQ;YAC9BC,gBAAgB;YAChBC,QAAQ;YACRC,QAAQvB,GAAGwB,YAAY,CAACC,MAAM;QAChC;IACF;IAEA;;;;;;;GAOC,GACD,8DAA8D;IAC9D,OAAO,IAAIC,SAAS,KAAK,CAAC,OAAO,EAAEX,gBAAgBY,UAAU,EAAE,EAAE1B;AACnE,EAAC"}
@@ -0,0 +1,8 @@
1
+ import type { JSONSchema4 } from 'json-schema';
2
+ /**
3
+ * Removes internal Payload properties (id, createdAt, updatedAt) from a
4
+ * JSON Schema so they don't appear in the generated Zod validation schema.
5
+ * Also strips `id` from the `required` array when present.
6
+ */
7
+ export declare function sanitizeJsonSchema(schema: JSONSchema4): JSONSchema4;
8
+ //# sourceMappingURL=sanitizeJsonSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeJsonSchema.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/sanitizeJsonSchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CAanE"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Removes internal Payload properties (id, createdAt, updatedAt) from a
3
+ * JSON Schema so they don't appear in the generated Zod validation schema.
4
+ * Also strips `id` from the `required` array when present.
5
+ */ export function sanitizeJsonSchema(schema) {
6
+ delete schema?.properties?.id;
7
+ delete schema?.properties?.createdAt;
8
+ delete schema?.properties?.updatedAt;
9
+ if (Array.isArray(schema.required)) {
10
+ schema.required = schema.required.filter((field)=>field !== 'id');
11
+ if (schema.required.length === 0) {
12
+ delete schema.required;
13
+ }
14
+ }
15
+ return schema;
16
+ }
17
+
18
+ //# sourceMappingURL=sanitizeJsonSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/schemaConversion/sanitizeJsonSchema.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\n/**\n * Removes internal Payload properties (id, createdAt, updatedAt) from a\n * JSON Schema so they don't appear in the generated Zod validation schema.\n * Also strips `id` from the `required` array when present.\n */\nexport function sanitizeJsonSchema(schema: JSONSchema4): JSONSchema4 {\n delete schema?.properties?.id\n delete schema?.properties?.createdAt\n delete schema?.properties?.updatedAt\n\n if (Array.isArray(schema.required)) {\n schema.required = schema.required.filter((field) => field !== 'id')\n if (schema.required.length === 0) {\n delete schema.required\n }\n }\n\n return schema\n}\n"],"names":["sanitizeJsonSchema","schema","properties","id","createdAt","updatedAt","Array","isArray","required","filter","field","length"],"mappings":"AAEA;;;;CAIC,GACD,OAAO,SAASA,mBAAmBC,MAAmB;IACpD,OAAOA,QAAQC,YAAYC;IAC3B,OAAOF,QAAQC,YAAYE;IAC3B,OAAOH,QAAQC,YAAYG;IAE3B,IAAIC,MAAMC,OAAO,CAACN,OAAOO,QAAQ,GAAG;QAClCP,OAAOO,QAAQ,GAAGP,OAAOO,QAAQ,CAACC,MAAM,CAAC,CAACC,QAAUA,UAAU;QAC9D,IAAIT,OAAOO,QAAQ,CAACG,MAAM,KAAK,GAAG;YAChC,OAAOV,OAAOO,QAAQ;QACxB;IACF;IAEA,OAAOP;AACT"}
@@ -0,0 +1,20 @@
1
+ import type { JSONSchema4 } from 'json-schema';
2
+ /**
3
+ * Recursively processes JSON schema properties to simplify relationship fields
4
+ * and convert `oneOf` constructs into MCP-friendly schemas.
5
+ *
6
+ * For create/update validation we only need to accept IDs (string/number),
7
+ * not populated objects. `$ref` options pointing to full entity definitions
8
+ * are removed entirely from `oneOf` unions. When a single option remains the
9
+ * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
10
+ *
11
+ * This matters because `json-schema-to-zod` converts `oneOf` into a strict
12
+ * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
13
+ * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
14
+ * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
15
+ *
16
+ * NOTE: This function must operate on a cloned schema to avoid mutating
17
+ * the original JSON schema used for tool listing.
18
+ */
19
+ export declare function simplifyRelationshipFields(schema: JSONSchema4): JSONSchema4;
20
+ //# sourceMappingURL=simplifyRelationshipFields.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simplifyRelationshipFields.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/simplifyRelationshipFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CA6C3E"}
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Recursively processes JSON schema properties to simplify relationship fields
3
+ * and convert `oneOf` constructs into MCP-friendly schemas.
4
+ *
5
+ * For create/update validation we only need to accept IDs (string/number),
6
+ * not populated objects. `$ref` options pointing to full entity definitions
7
+ * are removed entirely from `oneOf` unions. When a single option remains the
8
+ * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
9
+ *
10
+ * This matters because `json-schema-to-zod` converts `oneOf` into a strict
11
+ * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
12
+ * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
13
+ * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
14
+ *
15
+ * NOTE: This function must operate on a cloned schema to avoid mutating
16
+ * the original JSON schema used for tool listing.
17
+ */ export function simplifyRelationshipFields(schema) {
18
+ if (!schema || typeof schema !== 'object') {
19
+ return schema;
20
+ }
21
+ const processed = {
22
+ ...schema
23
+ };
24
+ if (Array.isArray(processed.oneOf)) {
25
+ const hasRef = processed.oneOf.some((option)=>option && typeof option === 'object' && '$ref' in option);
26
+ if (hasRef) {
27
+ const nonRefOptions = processed.oneOf.filter((option)=>!(option && typeof option === 'object' && '$ref' in option)).map((option)=>simplifyRelationshipFields(option));
28
+ if (nonRefOptions.length === 1) {
29
+ const single = nonRefOptions[0];
30
+ delete processed.oneOf;
31
+ Object.assign(processed, single);
32
+ } else if (nonRefOptions.length > 1) {
33
+ delete processed.oneOf;
34
+ processed.anyOf = nonRefOptions;
35
+ }
36
+ } else {
37
+ processed.anyOf = processed.oneOf.map((option)=>simplifyRelationshipFields(option));
38
+ delete processed.oneOf;
39
+ }
40
+ }
41
+ if (processed.properties && typeof processed.properties === 'object') {
42
+ processed.properties = Object.fromEntries(Object.entries(processed.properties).map(([key, value])=>[
43
+ key,
44
+ simplifyRelationshipFields(value)
45
+ ]));
46
+ }
47
+ if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
48
+ processed.items = simplifyRelationshipFields(processed.items);
49
+ }
50
+ return processed;
51
+ }
52
+
53
+ //# sourceMappingURL=simplifyRelationshipFields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/schemaConversion/simplifyRelationshipFields.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\n/**\n * Recursively processes JSON schema properties to simplify relationship fields\n * and convert `oneOf` constructs into MCP-friendly schemas.\n *\n * For create/update validation we only need to accept IDs (string/number),\n * not populated objects. `$ref` options pointing to full entity definitions\n * are removed entirely from `oneOf` unions. When a single option remains the\n * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.\n *\n * This matters because `json-schema-to-zod` converts `oneOf` into a strict\n * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing\n * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP\n * tool input schema. `anyOf` instead produces a clean `z.union([...])`.\n *\n * NOTE: This function must operate on a cloned schema to avoid mutating\n * the original JSON schema used for tool listing.\n */\nexport function simplifyRelationshipFields(schema: JSONSchema4): JSONSchema4 {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const processed = { ...schema }\n\n if (Array.isArray(processed.oneOf)) {\n const hasRef = processed.oneOf.some(\n (option) => option && typeof option === 'object' && '$ref' in option,\n )\n\n if (hasRef) {\n const nonRefOptions = processed.oneOf\n .filter((option) => !(option && typeof option === 'object' && '$ref' in option))\n .map((option) => simplifyRelationshipFields(option))\n\n if (nonRefOptions.length === 1) {\n const single = nonRefOptions[0]!\n delete processed.oneOf\n Object.assign(processed, single)\n } else if (nonRefOptions.length > 1) {\n delete processed.oneOf\n processed.anyOf = nonRefOptions\n }\n } else {\n processed.anyOf = processed.oneOf.map((option) => simplifyRelationshipFields(option))\n delete processed.oneOf\n }\n }\n\n if (processed.properties && typeof processed.properties === 'object') {\n processed.properties = Object.fromEntries(\n Object.entries(processed.properties).map(([key, value]) => [\n key,\n simplifyRelationshipFields(value),\n ]),\n )\n }\n\n if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {\n processed.items = simplifyRelationshipFields(processed.items)\n }\n\n return processed\n}\n"],"names":["simplifyRelationshipFields","schema","processed","Array","isArray","oneOf","hasRef","some","option","nonRefOptions","filter","map","length","single","Object","assign","anyOf","properties","fromEntries","entries","key","value","items"],"mappings":"AAEA;;;;;;;;;;;;;;;;CAgBC,GACD,OAAO,SAASA,2BAA2BC,MAAmB;IAC5D,IAAI,CAACA,UAAU,OAAOA,WAAW,UAAU;QACzC,OAAOA;IACT;IAEA,MAAMC,YAAY;QAAE,GAAGD,MAAM;IAAC;IAE9B,IAAIE,MAAMC,OAAO,CAACF,UAAUG,KAAK,GAAG;QAClC,MAAMC,SAASJ,UAAUG,KAAK,CAACE,IAAI,CACjC,CAACC,SAAWA,UAAU,OAAOA,WAAW,YAAY,UAAUA;QAGhE,IAAIF,QAAQ;YACV,MAAMG,gBAAgBP,UAAUG,KAAK,CAClCK,MAAM,CAAC,CAACF,SAAW,CAAEA,CAAAA,UAAU,OAAOA,WAAW,YAAY,UAAUA,MAAK,GAC5EG,GAAG,CAAC,CAACH,SAAWR,2BAA2BQ;YAE9C,IAAIC,cAAcG,MAAM,KAAK,GAAG;gBAC9B,MAAMC,SAASJ,aAAa,CAAC,EAAE;gBAC/B,OAAOP,UAAUG,KAAK;gBACtBS,OAAOC,MAAM,CAACb,WAAWW;YAC3B,OAAO,IAAIJ,cAAcG,MAAM,GAAG,GAAG;gBACnC,OAAOV,UAAUG,KAAK;gBACtBH,UAAUc,KAAK,GAAGP;YACpB;QACF,OAAO;YACLP,UAAUc,KAAK,GAAGd,UAAUG,KAAK,CAACM,GAAG,CAAC,CAACH,SAAWR,2BAA2BQ;YAC7E,OAAON,UAAUG,KAAK;QACxB;IACF;IAEA,IAAIH,UAAUe,UAAU,IAAI,OAAOf,UAAUe,UAAU,KAAK,UAAU;QACpEf,UAAUe,UAAU,GAAGH,OAAOI,WAAW,CACvCJ,OAAOK,OAAO,CAACjB,UAAUe,UAAU,EAAEN,GAAG,CAAC,CAAC,CAACS,KAAKC,MAAM,GAAK;gBACzDD;gBACApB,2BAA2BqB;aAC5B;IAEL;IAEA,IAAInB,UAAUoB,KAAK,IAAI,OAAOpB,UAAUoB,KAAK,KAAK,YAAY,CAACnB,MAAMC,OAAO,CAACF,UAAUoB,KAAK,GAAG;QAC7FpB,UAAUoB,KAAK,GAAGtB,2BAA2BE,UAAUoB,KAAK;IAC9D;IAEA,OAAOpB;AACT"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformPointFields.d.ts","sourceRoot":"","sources":["../../../src/utils/schemaConversion/transformPointFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CAoD3E"}
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../../src/utils/schemaConversion/transformPointFields.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\nexport function transformPointFieldsForMCP(schema: JSONSchema4): JSONSchema4 {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const transformed = { ...schema }\n\n if (transformed.properties && typeof transformed.properties === 'object') {\n transformed.properties = Object.fromEntries(\n Object.entries(transformed.properties).map(([key, value]) => {\n const isArrayType =\n value.type === 'array' || (Array.isArray(value.type) && value.type.includes('array'))\n\n if (\n value &&\n typeof value === 'object' &&\n isArrayType &&\n Array.isArray(value.items) &&\n value.items.length === 2 &&\n value.items.every((item: JSONSchema4) => item?.type === 'number')\n ) {\n // Transform to object format\n const isNullable = Array.isArray(value.type) && value.type.includes('null')\n\n return [\n key,\n {\n type: isNullable ? ['object', 'null'] : 'object',\n description: value.description || 'Geographic coordinates (longitude, latitude)',\n properties: {\n latitude: { type: 'number', description: 'Latitude coordinate' },\n longitude: { type: 'number', description: 'Longitude coordinate' },\n },\n required: ['longitude', 'latitude'],\n },\n ]\n }\n\n return [key, transformPointFieldsForMCP(value)]\n }),\n )\n }\n\n if (\n transformed.items &&\n typeof transformed.items === 'object' &&\n !Array.isArray(transformed.items)\n ) {\n transformed.items = transformPointFieldsForMCP(transformed.items)\n }\n\n return transformed\n}\n"],"names":["transformPointFieldsForMCP","schema","transformed","properties","Object","fromEntries","entries","map","key","value","isArrayType","type","Array","isArray","includes","items","length","every","item","isNullable","description","latitude","longitude","required"],"mappings":"AAEA,OAAO,SAASA,2BAA2BC,MAAmB;IAC5D,IAAI,CAACA,UAAU,OAAOA,WAAW,UAAU;QACzC,OAAOA;IACT;IAEA,MAAMC,cAAc;QAAE,GAAGD,MAAM;IAAC;IAEhC,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version": "3.77.0-canary.8",
4
4
  "description": "MCP (Model Context Protocol) capabilities with Payload",
5
5
  "keywords": [
6
6
  "plugin",
@@ -45,10 +45,10 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "@payloadcms/eslint-config": "3.28.0",
48
- "payload": "3.77.0-canary.7"
48
+ "payload": "3.77.0-canary.8"
49
49
  },
50
50
  "peerDependencies": {
51
- "payload": "3.77.0-canary.7"
51
+ "payload": "3.77.0-canary.8"
52
52
  },
53
53
  "homepage:": "https://payloadcms.com",
54
54
  "scripts": {
@@ -7,7 +7,7 @@ import { z } from 'zod'
7
7
  import type { PluginMCPServerConfig } from '../../../types.js'
8
8
 
9
9
  import { toCamelCase } from '../../../utils/camelCase.js'
10
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'
10
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'
11
11
  import { toolSchemas } from '../schemas.js'
12
12
 
13
13
  export const updateGlobalTool = (
@@ -7,7 +7,7 @@ import { z } from 'zod'
7
7
  import type { PluginMCPServerConfig } from '../../../types.js'
8
8
 
9
9
  import { toCamelCase } from '../../../utils/camelCase.js'
10
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'
10
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'
11
11
  import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
12
12
  import { toolSchemas } from '../schemas.js'
13
13
  export const createResourceTool = (
@@ -7,7 +7,7 @@ import { z } from 'zod'
7
7
  import type { PluginMCPServerConfig } from '../../../types.js'
8
8
 
9
9
  import { toCamelCase } from '../../../utils/camelCase.js'
10
- import { convertCollectionSchemaToZod } from '../../../utils/convertCollectionSchemaToZod.js'
10
+ import { convertCollectionSchemaToZod } from '../../../utils/schemaConversion/convertCollectionSchemaToZod.js'
11
11
  import { transformPointDataToPayload } from '../../../utils/transformPointDataToPayload.js'
12
12
  import { toolSchemas } from '../schemas.js'
13
13
  export const updateResourceTool = (
@@ -0,0 +1,41 @@
1
+ import type { JSONSchema4 } from 'json-schema'
2
+
3
+ import { jsonSchemaToZod } from 'json-schema-to-zod'
4
+ import * as ts from 'typescript'
5
+ import { z } from 'zod'
6
+
7
+ import { sanitizeJsonSchema } from './sanitizeJsonSchema.js'
8
+ import { simplifyRelationshipFields } from './simplifyRelationshipFields.js'
9
+ import { transformPointFieldsForMCP } from './transformPointFields.js'
10
+
11
+ export const convertCollectionSchemaToZod = (schema: JSONSchema4) => {
12
+ // Clone to avoid mutating the original schema (used elsewhere for tool listing)
13
+ const schemaClone = JSON.parse(JSON.stringify(schema)) as JSONSchema4
14
+
15
+ const sanitized = sanitizeJsonSchema(schemaClone)
16
+ const pointTransformed = transformPointFieldsForMCP(sanitized)
17
+ const simplifiedSchema = simplifyRelationshipFields(pointTransformed)
18
+
19
+ const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema)
20
+
21
+ // Transpile TypeScript to JavaScript
22
+ const transpileResult = ts.transpileModule(zodSchemaAsString, {
23
+ compilerOptions: {
24
+ module: ts.ModuleKind.CommonJS,
25
+ removeComments: true,
26
+ strict: false,
27
+ target: ts.ScriptTarget.ES2018,
28
+ },
29
+ })
30
+
31
+ /**
32
+ * This Function evaluation is safe because:
33
+ * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer
34
+ * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code
35
+ * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic
36
+ * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing
37
+ * 5. No user input or external data is involved in the schema generation process
38
+ */
39
+ // eslint-disable-next-line @typescript-eslint/no-implied-eval
40
+ return new Function('z', `return ${transpileResult.outputText}`)(z)
41
+ }
@@ -0,0 +1,21 @@
1
+ import type { JSONSchema4 } from 'json-schema'
2
+
3
+ /**
4
+ * Removes internal Payload properties (id, createdAt, updatedAt) from a
5
+ * JSON Schema so they don't appear in the generated Zod validation schema.
6
+ * Also strips `id` from the `required` array when present.
7
+ */
8
+ export function sanitizeJsonSchema(schema: JSONSchema4): JSONSchema4 {
9
+ delete schema?.properties?.id
10
+ delete schema?.properties?.createdAt
11
+ delete schema?.properties?.updatedAt
12
+
13
+ if (Array.isArray(schema.required)) {
14
+ schema.required = schema.required.filter((field) => field !== 'id')
15
+ if (schema.required.length === 0) {
16
+ delete schema.required
17
+ }
18
+ }
19
+
20
+ return schema
21
+ }
@@ -0,0 +1,65 @@
1
+ import type { JSONSchema4 } from 'json-schema'
2
+
3
+ /**
4
+ * Recursively processes JSON schema properties to simplify relationship fields
5
+ * and convert `oneOf` constructs into MCP-friendly schemas.
6
+ *
7
+ * For create/update validation we only need to accept IDs (string/number),
8
+ * not populated objects. `$ref` options pointing to full entity definitions
9
+ * are removed entirely from `oneOf` unions. When a single option remains the
10
+ * `oneOf` is unwrapped; otherwise it is converted to `anyOf`.
11
+ *
12
+ * This matters because `json-schema-to-zod` converts `oneOf` into a strict
13
+ * `z.any().superRefine(...)` validator whose base type is `z.any()`, causing
14
+ * `zodToJsonSchema` to emit `{}` and losing all type information in the MCP
15
+ * tool input schema. `anyOf` instead produces a clean `z.union([...])`.
16
+ *
17
+ * NOTE: This function must operate on a cloned schema to avoid mutating
18
+ * the original JSON schema used for tool listing.
19
+ */
20
+ export function simplifyRelationshipFields(schema: JSONSchema4): JSONSchema4 {
21
+ if (!schema || typeof schema !== 'object') {
22
+ return schema
23
+ }
24
+
25
+ const processed = { ...schema }
26
+
27
+ if (Array.isArray(processed.oneOf)) {
28
+ const hasRef = processed.oneOf.some(
29
+ (option) => option && typeof option === 'object' && '$ref' in option,
30
+ )
31
+
32
+ if (hasRef) {
33
+ const nonRefOptions = processed.oneOf
34
+ .filter((option) => !(option && typeof option === 'object' && '$ref' in option))
35
+ .map((option) => simplifyRelationshipFields(option))
36
+
37
+ if (nonRefOptions.length === 1) {
38
+ const single = nonRefOptions[0]!
39
+ delete processed.oneOf
40
+ Object.assign(processed, single)
41
+ } else if (nonRefOptions.length > 1) {
42
+ delete processed.oneOf
43
+ processed.anyOf = nonRefOptions
44
+ }
45
+ } else {
46
+ processed.anyOf = processed.oneOf.map((option) => simplifyRelationshipFields(option))
47
+ delete processed.oneOf
48
+ }
49
+ }
50
+
51
+ if (processed.properties && typeof processed.properties === 'object') {
52
+ processed.properties = Object.fromEntries(
53
+ Object.entries(processed.properties).map(([key, value]) => [
54
+ key,
55
+ simplifyRelationshipFields(value),
56
+ ]),
57
+ )
58
+ }
59
+
60
+ if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
61
+ processed.items = simplifyRelationshipFields(processed.items)
62
+ }
63
+
64
+ return processed
65
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"convertCollectionSchemaToZod.d.ts","sourceRoot":"","sources":["../../src/utils/convertCollectionSchemaToZod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAsD9C,eAAO,MAAM,4BAA4B,WAAY,WAAW,QAwC/D,CAAA"}
@@ -1,80 +0,0 @@
1
- import { jsonSchemaToZod } from 'json-schema-to-zod';
2
- import * as ts from 'typescript';
3
- import { z } from 'zod';
4
- import { transformPointFieldsForMCP } from './transformPointFields.js';
5
- /**
6
- * Recursively processes JSON schema properties to simplify relationship fields.
7
- * For create/update validation we only need to accept IDs (string/number),
8
- * not populated objects. This removes the $ref option from oneOf unions
9
- * that represent relationship fields, leaving only the ID shape.
10
- *
11
- * NOTE: This function must operate on a cloned schema to avoid mutating
12
- * the original JSON schema used for tool listing.
13
- */ function simplifyRelationshipFields(schema) {
14
- if (!schema || typeof schema !== 'object') {
15
- return schema;
16
- }
17
- const processed = {
18
- ...schema
19
- };
20
- if (Array.isArray(processed.oneOf)) {
21
- const hasRef = processed.oneOf.some((option)=>option && typeof option === 'object' && '$ref' in option);
22
- processed.oneOf = processed.oneOf.map((option)=>{
23
- if (option && typeof option === 'object' && '$ref' in option) {
24
- // Replace unresolved $ref with a permissive object schema to keep the union shape
25
- return {
26
- type: 'object',
27
- additionalProperties: true
28
- };
29
- }
30
- return simplifyRelationshipFields(option);
31
- });
32
- }
33
- if (processed.properties && typeof processed.properties === 'object') {
34
- processed.properties = Object.fromEntries(Object.entries(processed.properties).map(([key, value])=>[
35
- key,
36
- simplifyRelationshipFields(value)
37
- ]));
38
- }
39
- if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
40
- processed.items = simplifyRelationshipFields(processed.items);
41
- }
42
- return processed;
43
- }
44
- export const convertCollectionSchemaToZod = (schema)=>{
45
- // Clone to avoid mutating the original schema (used elsewhere for tool listing)
46
- const schemaClone = JSON.parse(JSON.stringify(schema));
47
- // Remove properties that should not be included in the Zod schema
48
- delete schemaClone?.properties?.id;
49
- delete schemaClone?.properties?.createdAt;
50
- delete schemaClone?.properties?.updatedAt;
51
- if (Array.isArray(schemaClone.required)) {
52
- schemaClone.required = schemaClone.required.filter((field)=>field !== 'id');
53
- if (schemaClone.required.length === 0) {
54
- delete schemaClone.required;
55
- }
56
- }
57
- const pointTransformed = transformPointFieldsForMCP(schemaClone);
58
- const simplifiedSchema = simplifyRelationshipFields(pointTransformed);
59
- const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema);
60
- // Transpile TypeScript to JavaScript
61
- const transpileResult = ts.transpileModule(zodSchemaAsString, {
62
- compilerOptions: {
63
- module: ts.ModuleKind.CommonJS,
64
- removeComments: true,
65
- strict: false,
66
- target: ts.ScriptTarget.ES2018
67
- }
68
- });
69
- /**
70
- * This Function evaluation is safe because:
71
- * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer
72
- * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code
73
- * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic
74
- * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing
75
- * 5. No user input or external data is involved in the schema generation process
76
- */ // eslint-disable-next-line @typescript-eslint/no-implied-eval
77
- return new Function('z', `return ${transpileResult.outputText}`)(z);
78
- };
79
-
80
- //# sourceMappingURL=convertCollectionSchemaToZod.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/convertCollectionSchemaToZod.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\nimport { jsonSchemaToZod } from 'json-schema-to-zod'\nimport * as ts from 'typescript'\nimport { z } from 'zod'\n\nimport { transformPointFieldsForMCP } from './transformPointFields.js'\n\n/**\n * Recursively processes JSON schema properties to simplify relationship fields.\n * For create/update validation we only need to accept IDs (string/number),\n * not populated objects. This removes the $ref option from oneOf unions\n * that represent relationship fields, leaving only the ID shape.\n *\n * NOTE: This function must operate on a cloned schema to avoid mutating\n * the original JSON schema used for tool listing.\n */\nfunction simplifyRelationshipFields(schema: JSONSchema4): JSONSchema4 {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const processed = { ...schema }\n\n if (Array.isArray(processed.oneOf)) {\n const hasRef = processed.oneOf.some(\n (option) => option && typeof option === 'object' && '$ref' in option,\n )\n\n processed.oneOf = processed.oneOf.map((option) => {\n if (option && typeof option === 'object' && '$ref' in option) {\n // Replace unresolved $ref with a permissive object schema to keep the union shape\n return { type: 'object', additionalProperties: true }\n }\n return simplifyRelationshipFields(option)\n })\n }\n\n if (processed.properties && typeof processed.properties === 'object') {\n processed.properties = Object.fromEntries(\n Object.entries(processed.properties).map(([key, value]) => [\n key,\n simplifyRelationshipFields(value),\n ]),\n )\n }\n\n if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {\n processed.items = simplifyRelationshipFields(processed.items)\n }\n\n return processed\n}\n\nexport const convertCollectionSchemaToZod = (schema: JSONSchema4) => {\n // Clone to avoid mutating the original schema (used elsewhere for tool listing)\n const schemaClone = JSON.parse(JSON.stringify(schema)) as JSONSchema4\n\n // Remove properties that should not be included in the Zod schema\n delete schemaClone?.properties?.id\n delete schemaClone?.properties?.createdAt\n delete schemaClone?.properties?.updatedAt\n if (Array.isArray(schemaClone.required)) {\n schemaClone.required = schemaClone.required.filter((field) => field !== 'id')\n if (schemaClone.required.length === 0) {\n delete schemaClone.required\n }\n }\n\n const pointTransformed = transformPointFieldsForMCP(schemaClone)\n const simplifiedSchema = simplifyRelationshipFields(pointTransformed)\n\n const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema)\n\n // Transpile TypeScript to JavaScript\n const transpileResult = ts.transpileModule(zodSchemaAsString, {\n compilerOptions: {\n module: ts.ModuleKind.CommonJS,\n removeComments: true,\n strict: false,\n target: ts.ScriptTarget.ES2018,\n },\n })\n\n /**\n * This Function evaluation is safe because:\n * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer\n * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code\n * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic\n * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing\n * 5. No user input or external data is involved in the schema generation process\n */\n // eslint-disable-next-line @typescript-eslint/no-implied-eval\n return new Function('z', `return ${transpileResult.outputText}`)(z)\n}\n"],"names":["jsonSchemaToZod","ts","z","transformPointFieldsForMCP","simplifyRelationshipFields","schema","processed","Array","isArray","oneOf","hasRef","some","option","map","type","additionalProperties","properties","Object","fromEntries","entries","key","value","items","convertCollectionSchemaToZod","schemaClone","JSON","parse","stringify","id","createdAt","updatedAt","required","filter","field","length","pointTransformed","simplifiedSchema","zodSchemaAsString","transpileResult","transpileModule","compilerOptions","module","ModuleKind","CommonJS","removeComments","strict","target","ScriptTarget","ES2018","Function","outputText"],"mappings":"AAEA,SAASA,eAAe,QAAQ,qBAAoB;AACpD,YAAYC,QAAQ,aAAY;AAChC,SAASC,CAAC,QAAQ,MAAK;AAEvB,SAASC,0BAA0B,QAAQ,4BAA2B;AAEtE;;;;;;;;CAQC,GACD,SAASC,2BAA2BC,MAAmB;IACrD,IAAI,CAACA,UAAU,OAAO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,UAAU;QACxE,IAAIT,YAAYO,QAAQ,CAACG,MAAM,KAAK,GAAG;YACrC,OAAOV,YAAYO,QAAQ;QAC7B;IACF;IAEA,MAAMI,mBAAmBhC,2BAA2BqB;IACpD,MAAMY,mBAAmBhC,2BAA2B+B;IAEpD,MAAME,oBAAoBrC,gBAAgBoC;IAE1C,qCAAqC;IACrC,MAAME,kBAAkBrC,GAAGsC,eAAe,CAACF,mBAAmB;QAC5DG,iBAAiB;YACfC,QAAQxC,GAAGyC,UAAU,CAACC,QAAQ;YAC9BC,gBAAgB;YAChBC,QAAQ;YACRC,QAAQ7C,GAAG8C,YAAY,CAACC,MAAM;QAChC;IACF;IAEA;;;;;;;GAOC,GACD,8DAA8D;IAC9D,OAAO,IAAIC,SAAS,KAAK,CAAC,OAAO,EAAEX,gBAAgBY,UAAU,EAAE,EAAEhD;AACnE,EAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"transformPointFields.d.ts","sourceRoot":"","sources":["../../src/utils/transformPointFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAE9C,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,WAAW,GAAG,WAAW,CAoD3E"}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/transformPointFields.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\n\nexport function transformPointFieldsForMCP(schema: JSONSchema4): JSONSchema4 {\n if (!schema || typeof schema !== 'object') {\n return schema\n }\n\n const transformed = { ...schema }\n\n if (transformed.properties && typeof transformed.properties === 'object') {\n transformed.properties = Object.fromEntries(\n Object.entries(transformed.properties).map(([key, value]) => {\n const isArrayType =\n value.type === 'array' || (Array.isArray(value.type) && value.type.includes('array'))\n\n if (\n value &&\n typeof value === 'object' &&\n isArrayType &&\n Array.isArray(value.items) &&\n value.items.length === 2 &&\n value.items.every((item: JSONSchema4) => item?.type === 'number')\n ) {\n // Transform to object format\n const isNullable = Array.isArray(value.type) && value.type.includes('null')\n\n return [\n key,\n {\n type: isNullable ? ['object', 'null'] : 'object',\n description: value.description || 'Geographic coordinates (longitude, latitude)',\n properties: {\n latitude: { type: 'number', description: 'Latitude coordinate' },\n longitude: { type: 'number', description: 'Longitude coordinate' },\n },\n required: ['longitude', 'latitude'],\n },\n ]\n }\n\n return [key, transformPointFieldsForMCP(value)]\n }),\n )\n }\n\n if (\n transformed.items &&\n typeof transformed.items === 'object' &&\n !Array.isArray(transformed.items)\n ) {\n transformed.items = transformPointFieldsForMCP(transformed.items)\n }\n\n return transformed\n}\n"],"names":["transformPointFieldsForMCP","schema","transformed","properties","Object","fromEntries","entries","map","key","value","isArrayType","type","Array","isArray","includes","items","length","every","item","isNullable","description","latitude","longitude","required"],"mappings":"AAEA,OAAO,SAASA,2BAA2BC,MAAmB;IAC5D,IAAI,CAACA,UAAU,OAAOA,WAAW,UAAU;QACzC,OAAOA;IACT;IAEA,MAAMC,cAAc;QAAE,GAAGD,MAAM;IAAC;IAEhC,IAAIC,YAAYC,UAAU,IAAI,OAAOD,YAAYC,UAAU,KAAK,UAAU;QACxED,YAAYC,UAAU,GAAGC,OAAOC,WAAW,CACzCD,OAAOE,OAAO,CAACJ,YAAYC,UAAU,EAAEI,GAAG,CAAC,CAAC,CAACC,KAAKC,MAAM;YACtD,MAAMC,cACJD,MAAME,IAAI,KAAK,WAAYC,MAAMC,OAAO,CAACJ,MAAME,IAAI,KAAKF,MAAME,IAAI,CAACG,QAAQ,CAAC;YAE9E,IACEL,SACA,OAAOA,UAAU,YACjBC,eACAE,MAAMC,OAAO,CAACJ,MAAMM,KAAK,KACzBN,MAAMM,KAAK,CAACC,MAAM,KAAK,KACvBP,MAAMM,KAAK,CAACE,KAAK,CAAC,CAACC,OAAsBA,MAAMP,SAAS,WACxD;gBACA,6BAA6B;gBAC7B,MAAMQ,aAAaP,MAAMC,OAAO,CAACJ,MAAME,IAAI,KAAKF,MAAME,IAAI,CAACG,QAAQ,CAAC;gBAEpE,OAAO;oBACLN;oBACA;wBACEG,MAAMQ,aAAa;4BAAC;4BAAU;yBAAO,GAAG;wBACxCC,aAAaX,MAAMW,WAAW,IAAI;wBAClCjB,YAAY;4BACVkB,UAAU;gCAAEV,MAAM;gCAAUS,aAAa;4BAAsB;4BAC/DE,WAAW;gCAAEX,MAAM;gCAAUS,aAAa;4BAAuB;wBACnE;wBACAG,UAAU;4BAAC;4BAAa;yBAAW;oBACrC;iBACD;YACH;YAEA,OAAO;gBAACf;gBAAKR,2BAA2BS;aAAO;QACjD;IAEJ;IAEA,IACEP,YAAYa,KAAK,IACjB,OAAOb,YAAYa,KAAK,KAAK,YAC7B,CAACH,MAAMC,OAAO,CAACX,YAAYa,KAAK,GAChC;QACAb,YAAYa,KAAK,GAAGf,2BAA2BE,YAAYa,KAAK;IAClE;IAEA,OAAOb;AACT"}
@@ -1,95 +0,0 @@
1
- import type { JSONSchema4 } from 'json-schema'
2
-
3
- import { jsonSchemaToZod } from 'json-schema-to-zod'
4
- import * as ts from 'typescript'
5
- import { z } from 'zod'
6
-
7
- import { transformPointFieldsForMCP } from './transformPointFields.js'
8
-
9
- /**
10
- * Recursively processes JSON schema properties to simplify relationship fields.
11
- * For create/update validation we only need to accept IDs (string/number),
12
- * not populated objects. This removes the $ref option from oneOf unions
13
- * that represent relationship fields, leaving only the ID shape.
14
- *
15
- * NOTE: This function must operate on a cloned schema to avoid mutating
16
- * the original JSON schema used for tool listing.
17
- */
18
- function simplifyRelationshipFields(schema: JSONSchema4): JSONSchema4 {
19
- if (!schema || typeof schema !== 'object') {
20
- return schema
21
- }
22
-
23
- const processed = { ...schema }
24
-
25
- if (Array.isArray(processed.oneOf)) {
26
- const hasRef = processed.oneOf.some(
27
- (option) => option && typeof option === 'object' && '$ref' in option,
28
- )
29
-
30
- processed.oneOf = processed.oneOf.map((option) => {
31
- if (option && typeof option === 'object' && '$ref' in option) {
32
- // Replace unresolved $ref with a permissive object schema to keep the union shape
33
- return { type: 'object', additionalProperties: true }
34
- }
35
- return simplifyRelationshipFields(option)
36
- })
37
- }
38
-
39
- if (processed.properties && typeof processed.properties === 'object') {
40
- processed.properties = Object.fromEntries(
41
- Object.entries(processed.properties).map(([key, value]) => [
42
- key,
43
- simplifyRelationshipFields(value),
44
- ]),
45
- )
46
- }
47
-
48
- if (processed.items && typeof processed.items === 'object' && !Array.isArray(processed.items)) {
49
- processed.items = simplifyRelationshipFields(processed.items)
50
- }
51
-
52
- return processed
53
- }
54
-
55
- export const convertCollectionSchemaToZod = (schema: JSONSchema4) => {
56
- // Clone to avoid mutating the original schema (used elsewhere for tool listing)
57
- const schemaClone = JSON.parse(JSON.stringify(schema)) as JSONSchema4
58
-
59
- // Remove properties that should not be included in the Zod schema
60
- delete schemaClone?.properties?.id
61
- delete schemaClone?.properties?.createdAt
62
- delete schemaClone?.properties?.updatedAt
63
- if (Array.isArray(schemaClone.required)) {
64
- schemaClone.required = schemaClone.required.filter((field) => field !== 'id')
65
- if (schemaClone.required.length === 0) {
66
- delete schemaClone.required
67
- }
68
- }
69
-
70
- const pointTransformed = transformPointFieldsForMCP(schemaClone)
71
- const simplifiedSchema = simplifyRelationshipFields(pointTransformed)
72
-
73
- const zodSchemaAsString = jsonSchemaToZod(simplifiedSchema)
74
-
75
- // Transpile TypeScript to JavaScript
76
- const transpileResult = ts.transpileModule(zodSchemaAsString, {
77
- compilerOptions: {
78
- module: ts.ModuleKind.CommonJS,
79
- removeComments: true,
80
- strict: false,
81
- target: ts.ScriptTarget.ES2018,
82
- },
83
- })
84
-
85
- /**
86
- * This Function evaluation is safe because:
87
- * 1. The input schema comes from Payload's collection configuration, which is controlled by the application developer
88
- * 2. The jsonSchemaToZod library converts JSON Schema to Zod schema definitions, producing only type validation code
89
- * 3. The transpiled output contains only Zod schema definitions (z.string(), z.number(), etc.) - no executable logic
90
- * 4. The resulting Zod schema is used only for parameter validation in MCP tools, not for data processing
91
- * 5. No user input or external data is involved in the schema generation process
92
- */
93
- // eslint-disable-next-line @typescript-eslint/no-implied-eval
94
- return new Function('z', `return ${transpileResult.outputText}`)(z)
95
- }