Commit 6184b0c
authored
fix(plugin-mcp): add error handling to convertCollectionSchemaToZod (#16114)
## Summary
Wraps `convertCollectionSchemaToZod` in a try/catch so that a single
collection's schema conversion failure doesn't crash the entire
`tools/list` MCP response.
## Problem
The schema conversion pipeline (`JSON Schema → json-schema-to-zod →
TypeScript transpile → eval`) can fail for collections with complex
nested schemas. When it does, the error propagates up to the MCP SDK's
`tools/list` handler, crashing the entire response and making **all**
MCP tools inaccessible — not just the one with the problematic schema.
The most common trigger is a Zod v4 bug where `toJSONSchema` crashes
with `TypeError: Cannot read properties of undefined (reading '_zod')`
on record schemas with undefined `valueType` (see
[colinhacks/zod#5821](colinhacks/zod#5821), PR
[colinhacks/zod#5822](colinhacks/zod#5822)).
## Fix
```typescript
try {
// existing conversion pipeline...
return new Function('z', `return ${transpileResult.outputText}`)(z)
} catch (error) {
console.warn(`[plugin-mcp] Schema conversion failed, using permissive fallback:`, error.message)
return z.record(z.any())
}
```
When conversion fails:
- The tool is still listed in `tools/list` (with a permissive
`z.record(z.any())` schema)
- Other tools with valid schemas are completely unaffected
- A warning is logged to help diagnose which collections have
problematic schemas
## Impact
Without this fix, any project with 20+ collections (where at least one
has a complex nested block schema) will have a completely non-functional
MCP server — `tools/list` returns an error instead of the tool list.
With this fix, only the affected collection loses strict parameter
validation; everything else works normally.
## Testing
Verified in production with a 22-collection Payload CMS deployment on
Vercel. Before the fix, `tools/list` crashed entirely. After, all tools
are listed and functional.1 parent 0981cdc commit 6184b0c
1 file changed
Lines changed: 37 additions & 24 deletions
Lines changed: 37 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
28 | 29 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
39 | 52 | | |
0 commit comments