Skip to content

MCP Plugin Crashes with defaultDepth: 0 #15013

@jonathanelmgren

Description

@jonathanelmgren

Describe the Bug

The @payloadcms/plugin-mcp crashes when defaultDepth: 0 is configured in Payload, making it impossible to use the MCP plugin with this common performance optimization setting.

Root Cause

In packages/plugin-mcp/src/endpoints/mcp.ts (lines 45-50), the plugin queries the payload-mcp-api-keys collection without specifying a depth parameter:

const { docs } = await payload.find({
  collection: 'payload-mcp-api-keys',
  // No depth specified - uses global defaultDepth
  limit: 1,
  pagination: false,
  where,
})

When defaultDepth: 0, the user field returns as a string ID instead of the populated user object. The code then attempts to set properties on this string primitive (line 65):

const user = docs[0]?.user as TypedUser  // This is a string when depth:0
user.collection = customUserCollection ?? 'users'  // TypeError!
user._strategy = 'mcp-api-key' as const

Expected Behavior

The MCP plugin should work correctly regardless of the defaultDepth setting. The plugin should either:

  1. Override the depth for its internal queries, or
  2. Handle the case when relationships are not populated

Link to the code that reproduces this issue

https://github.com/jonathanelmgren/payload-repro

Reproduction Steps

  1. Install Payload 3.69.0 with @payloadcms/plugin-mcp
  2. Configure defaultDepth: 0 in payload.config.ts:
export default buildConfig({
  defaultDepth: 0,  // This triggers the bug
  plugins: [
    mcpPlugin({
      collections: {
        posts: { enabled: true },
      },
    }),
  ],
  // ... rest of config
})
  1. Create an API key in the admin panel at /admin/collections/payload-mcp-api-keys
  2. Add your new API key to .mcp.json
  3. Try to connect via MCP using the API key
  4. Server crashes with TypeError

Error Message

TypeError: Cannot create property 'collection' on string '69495e5d1c30e1099b58e9df'
    at getDefaultMcpAccessSettings (packages/plugin-mcp/src/endpoints/mcp.ts:65)

Which area(s) are affected?

plugin: mcp

Environment Info

Binaries:
  Node: 24.2.0
  npm: 11.3.0
  Yarn: 1.22.19
  pnpm: 10.18.3
Relevant Packages:
  payload: 3.69.0
  next: 15.4.10
  @payloadcms/db-mongodb: 3.69.0
  @payloadcms/graphql: 3.69.0
  @payloadcms/next/utilities: 3.69.0
  @payloadcms/plugin-mcp: 3.69.0
  @payloadcms/richtext-lexical: 3.69.0
  @payloadcms/translations: 3.69.0
  @payloadcms/ui/shared: 3.69.0
  react: 19.2.1
  react-dom: 19.2.1
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.6.0: Mon Jul 14 11:30:30 PDT 2025; root:xnu-11417.140.69~1/RELEASE_ARM64_T6020
  Available memory (MB): 16384
  Available CPU cores: 10

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions