#2692·mcp-use

Anthropic provider sends empty string tool_result content crashing with 400 Bad Request

Author: rohith500Created Sep 29, 2026Updated Sep 29, 2026
LabelsbugTypeScriptagent

Describe the Bug

When an MCP tool returns an empty result (such as { content: [] } or empty string "" from void operations like file deletion, cache flushing, or state mutations), toAnthropicMessages() emits a tool_result content block with content: "".

When sent to the Anthropic Messages API (https://api.anthropic.com/v1/messages), the API rejects the request with HTTP 400 Bad Request:

400 Bad Request: messages.X.content.Y.tool_result.content: text cannot be empty

Where did you find this issue?

In libraries/typescript/packages/agent/src/llm/providers/anthropic.ts:

typescript
function toAnthropicToolResultContent(
  content: string | ContentPart[]
): unknown {
  if (typeof content === "string") {
    return content; // <--- returns "" when content is empty string
  }
  const blocks: unknown[] = [];
  for (const p of content) {
    if (p.type === "text") {
      if (p.text) blocks.push({ type: "text", text: p.text });
    } else if (p.type === "image") {
      const img = buildImageBlock(p);
      if (img) blocks.push(img);
    }
  }
  if (blocks.length === 0) {
    return "[no content]";
  }
  return blocks;
}

Notice the asymmetry:

  1. When content is an array of parts that produces zero non-empty blocks (e.g. [] or [{ type: "text", text: "" }]), line 80 correctly falls back to "[no content]".
  2. When toAnthropicContent formats user messages (line 49), it falls back to content || "[no content]".
  3. In openai-chat-completions.ts, openai-responses.ts, and google.ts, empty tool results fall back to "[no content]".
  4. Only toAnthropicToolResultContent returns "" when typeof content === "string".

To Reproduce

Call toAnthropicMessages with a tool message carrying empty string content:

typescript
const messages: ProviderMessage[] = [
  {
    role: "tool",
    toolCallId: "call_empty",
    toolName: "delete-file",
    toolResult: { content: [] },
    content: "",
  },
];
const out = toAnthropicMessages(messages);

Observed output: out[0].content[0].content is "".

Expected output: out[0].content[0].content should be "[no content]" matching the fallback in other providers and Anthropic message formatters.

Proposed Fix

In libraries/typescript/packages/agent/src/llm/providers/anthropic.ts:

diff
 function toAnthropicToolResultContent(
   content: string | ContentPart[]
 ): unknown {
   if (typeof content === "string") {
-    return content;
+    return content || "[no content]";
   }