Skip to content

Fix: Change create_view elements from JSON string to typed array#66

Open
anmolxlight wants to merge 1 commit intoexcalidraw:mainfrom
anmolxlight:fix/elements-typed-array
Open

Fix: Change create_view elements from JSON string to typed array#66
anmolxlight wants to merge 1 commit intoexcalidraw:mainfrom
anmolxlight:fix/elements-typed-array

Conversation

@anmolxlight
Copy link
Copy Markdown

Problem

create_view accepts elements as z.string() — a raw JSON array string. Models frequently generate invalid JSON in this string (trailing commas, unquoted keys, comments), causing silent "Invalid JSON in elements" errors. Since the schema doesn't enforce structure, strict tool use features (Claude structured outputs, ChatGPT function calling) can't help.

Fix

Changed elements from z.string() to z.array(z.record(z.any())). This lets Zod validate the array structure directly, enabling structured output / strict tool use to generate valid input.

Before:

{
  "elements": "[{"type": "rectangle", "x": 10}]"
}

After:

{
  "elements": [{"type": "rectangle", "x": 10}]
}

The widget (mcp-app.tsx line 432) already handles both string and array inputs via typeof check, so no frontend changes needed.

Changes

  • src/server.ts: Changed schema from z.string() to z.array(z.record(z.any())), removed manual JSON.parse block, updated size check to use JSON.stringify(elements).length
  • CLAUDE.md: Updated create_view description

Related

Fixes #56

The previous z.string() schema required models to generate a JSON
string, which frequently produced invalid JSON (trailing commas,
unquoted keys, comments). This caused silent "Invalid JSON in elements"
errors with no indication of what went wrong.

Fix: change elements to z.array(z.record(z.any())). This lets Zod
validate the structure directly, enabling structured output / strict
tool use from Claude and ChatGPT to generate valid input.

The widget (mcp-app.tsx) already handles both string and array inputs
via typeof check, so no frontend changes needed.

Fixes excalidraw#56
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 5, 2026

@anmolxlight is attempting to deploy a commit to the Excalidraw Team on Vercel.

A member of the Team first needs to authorize it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Getting occasional "Invalid JSON in elements" errors

1 participant