Anthropic provider sends empty string tool_result content crashing with 400 Bad Request
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 emptyWhere did you find this issue?
In libraries/typescript/packages/agent/src/llm/providers/anthropic.ts:
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:
- When
contentis an array of parts that produces zero non-empty blocks (e.g.[]or[{ type: "text", text: "" }]), line 80 correctly falls back to"[no content]". - When
toAnthropicContentformats user messages (line 49), it falls back tocontent || "[no content]". - In
openai-chat-completions.ts,openai-responses.ts, andgoogle.ts, empty tool results fall back to"[no content]". - Only
toAnthropicToolResultContentreturns""whentypeof content === "string".
To Reproduce
Call toAnthropicMessages with a tool message carrying empty string content:
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:
function toAnthropicToolResultContent(
content: string | ContentPart[]
): unknown {
if (typeof content === "string") {
- return content;
+ return content || "[no content]";
}Source: mcp-use/mcp-use