Skip to content

U+2028 LINE SEPARATOR in MCP tool results causes JSON parsing failures. #882

@bmaurer

Description

@bmaurer

A U+2028 LINE SEPARATOR in MCP tool results causes the claude SDK to fail with

Error: CLI output was not valid JSON. This may indicate an error during startup. Output: {"type":"user","message":{"role":"user","content":[{"tool_use_id":"toolu_01F1kfdYcsZrBCXmE8y7bn6k","type":"tool_result","content":[{"type":"text","text":"before

Minimal repro below:

#!/usr/bin/env npx tsx
/**
 * U+2028 LINE SEPARATOR in MCP tool results causes JSON parsing failures.
 *
 * - JSON.stringify does NOT escape U+2028
 * - Node's readline treats U+2028 as a line terminator
 * - The JSON line gets split, causing JSON.parse to fail
 */

import { query, createSdkMcpServer, tool } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";

const mcpServer = createSdkMcpServer({
  name: "test",
  version: "1.0.0",
  tools: [
    tool("get_text", "Get text", { id: z.string() }, async () => ({
      content: [{ type: "text", text: "before\u2028after" }],
    })),
  ],
});

(async () => {
  const q = query({
    prompt: "Call get_text with id 'x'",
    options: {
      model: "haiku",
      mcpServers: { test: mcpServer },
      allowedTools: ["mcp__test__get_text"],
      tools: [],
    },
  });

  for await (const msg of q) {
    if (msg.type === "result") break;
  }
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions