Repository navigation
Description
Describe the Bug
Package / area
@payloadcms/plugin-mcp (packages/plugin-mcp in this monorepo)
Summary
When MCP registers collection tools (e.g. Update {collection}), updateResourceTool calls convertCollectionSchemaToZod(schema) and then convertedFields.partial(). In practice convertedFields is sometimes not a ZodObject (e.g. the string "use strict"), so registration throws:
TypeError: convertedFields.partial is not a function
Wrapped as: Error registering tools for collection posts: ...
Root cause
In convertCollectionSchemaToZod (src/utils/schemaConversion/convertCollectionSchemaToZod.ts):
- json-schema-to-zod produces a small snippet of Zod code as a string.
- That string is passed through typescript.transpileModule with module: ts.ModuleKind.CommonJS.
- The transpiled output is prefixed with a prologue, e.g. "use strict";\n.
- The code then does new Function('z', \return ${transpileResult.outputText}
)(z). - Because the transpiled text starts with "use strict";, the generated function is effectively:
return "use strict";
// … rest is dead codeSo the “schema” returned is the string "use strict", not a Zod schema. Strings have no .partial(), hence the error when building updateResourceSchema.
This is easy to reproduce with any minimal object schema and the current transpileModule + return concatenation pattern.
Secondary issue
On failure, the catch path returns z.record(z.any()). ZodRecord does not implement .partial() like ZodObject, so even the intentional fallback would still break updateResourceTool if the try path threw.
Suggested fix
Strip the "use strict" prologue from transpileResult.outputText (and normalize to a single expression) before new Function('z', 'return …').
Optionally change the fallback to something that supports .partial() / .shape as used by callers (e.g. z.object({}).passthrough()), or branch in updateResourceTool / createResourceTool when the converted value is not a ZodObject.
Link to the code that reproduces this issue
Reproduction Steps
- Enable @payloadcms/plugin-mcp for a collection with update: true on the MCP API key.
- Hit the MCP endpoint so tools register.
- Observe failure when registering the update tool for that collection.
Minimal isolated repro (same pipeline as the plugin):
import { jsonSchemaToZod } from 'json-schema-to-zod'
import * as ts from 'typescript'
import { z } from 'zod'
const zodSchemaAsString = jsonSchemaToZod({
type: 'object',
properties: { title: { type: 'string' } },
})
const tr = ts.transpileModule(zodSchemaAsString, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2018,
},
})
const fn = new Function('z', 'return ' + tr.outputText)
const r = fn(z)
// r is the string "use strict", not a ZodObjectWhich area(s) are affected?
plugin: mcp
Environment Info
Environment
@payloadcms/plugin-mcp: 3.82.x (and likely same code path on current main)
typescript: 6.x (behavior may match older TS as well for this prologue)
json-schema-to-zod: as declared by the plugin
Workaround used downstream
Strip "use strict"; from transpiled output before eval, and use a ZodObject-compatible fallback instead of z.record for callers that call .partial().
Activity
w41x commentedon Apr 23, 2026
I have noticed those errors since updating to TypeScript 6.x. I did not have time to debug and just reversed back to TypeScript 5.9.3. They seem to be ignored without the default stronger Type Checking in 6.x. Thank you! I hope it gets fixed soon.
fbele commentedon Jun 19, 2026
Is there any update on this? I am also experiencing the same issue.
ctsstc commentedon Jun 25, 2026
This still reproduces on the current stable 3.x release — the fix in #16726 ("feat(plugin-mcp)!: mcp plugin refactor, add stdio mcp") only landed on
main(the 4.0 line) and wasn't backported to 3.x.Confirmed on
@payloadcms/[email protected](currentlatest) with[email protected]:convertCollectionSchemaToZodstill builds the schema viats.transpileModule+new Function('z', \return ${transpileResult.outputText}`)(z), so TS 6's"use strict";prologue makes it return the string"use strict"instead of aZodObject`. — convertCollectionSchemaToZod.ts (v3.85.1)updateResourceToolstill calls.partial()on that value, unguarded. — resource/update.ts#L300 (v3.85.1)catchfallback still returnsz.record(z.any()), which also has no.partial(). — convertCollectionSchemaToZod.ts#L50 (v3.85.1)Impact on 3.x: any API key with
update(orcreate) enabled on a collection 500s the entire/api/mcpendpoint on the first request — the throw becomes an unhandledAPIError(500)insidegetMcpHandler's per-collectiontry/catch, so no tools register at all. Reproduces reliably with ablocks-based collection. Downgrading to[email protected]avoids it, but that isn't always viable.Would the maintainers consider a 3.x backport, or a minimal guard on the 3.x branch — strip the
"use strict";prologue beforenew Function(...), and/or usez.object({}).passthrough()as the fallback so.partial()/.shapeare always safe? Happy to open a PR against the 3.x branch if that's welcome.Environment:
@payloadcms/[email protected]&3.85.1,[email protected],@payloadcms/db-sqlite,[email protected], Next 16, Node 24.fix(plugin-mcp): create/update tools crash the MCP endpoint under Typ…
github-actions commentedon Sep 23, 2026
🚀 This is included in version v3.90.2