Skip to content

plugin-mcp: schema eval returns "use strict" → convertedFields.partial is not a function when registering update tools #16339

Closed

Description

@bruno3du

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):

  1. json-schema-to-zod produces a small snippet of Zod code as a string.
  2. That string is passed through typescript.transpileModule with module: ts.ModuleKind.CommonJS.
  3. The transpiled output is prefixed with a prologue, e.g. "use strict";\n.
  4. The code then does new Function('z', \return ${transpileResult.outputText})(z).
  5. Because the transpiled text starts with "use strict";, the generated function is effectively:
return "use strict";
// … rest is dead code

So 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

https://github.com/payloadcms/payload/blob/main/packages/plugin-mcp/src/utils/schemaConversion/convertCollectionSchemaToZod.ts#L39

Reproduction Steps

  1. Enable @payloadcms/plugin-mcp for a collection with update: true on the MCP API key.
  2. Hit the MCP endpoint so tools register.
  3. 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 ZodObject

Which 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

  1. added
    status: needs-triage
    validate-reproduction
    on Apr 21, 2026
  2. added and removed
    validate-reproduction
    on Apr 21, 2026
  3. w41x commented on Apr 23, 2026

    @w41x

    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.

  4. self-assigned this
    on May 23, 2026
  5. removed
    status: needs-triage
    on May 23, 2026
  6. fbele commented on Jun 19, 2026

    @fbele

    Is there any update on this? I am also experiencing the same issue.

  7. ctsstc commented on Jun 25, 2026

    @ctsstc

    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] (current latest) with [email protected]:

    Impact on 3.x: any API key with update (or create) enabled on a collection 500s the entire /api/mcp endpoint on the first request — the throw becomes an unhandled APIError(500) inside getMcpHandler's per-collection try/catch, so no tools register at all. Reproduces reliably with a blocks-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 before new Function(...), and/or use z.object({}).passthrough() as the fallback so .partial()/.shape are 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.

  8. added a commit that references this issue on Sep 22, 2026
    e6efd70
  9. github-actions commented on Sep 23, 2026

    @github-actions
    Contributor

    🚀 This is included in version v3.90.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions