#2684·mcp-use

agent: tool images are dropped when replaying externalHistory

Author: aishwary-dongreCreated Sep 28, 2026Updated Sep 28, 2026
LabelsbugTypeScriptenhancementagent

Where did you find this issue?

Found by code review while comparing the two message-conversion paths in libraries/typescript/packages/agent/src/llm/messageFormat.ts, then confirmed by running both against the same tool result. I did not find an existing issue tracking it.

Summary

convertExternalHistoryToProvider converts tool-message content with messageText, which keeps only .text:

typescript
function messageText(content: unknown): string {
  if (typeof content === "string") return content;
  if (Array.isArray(content)) {
    return content
      .map((part) =>
        part && typeof part === "object" && "text" in part &&
        typeof part.text === "string" ? part.text : ""
      )
      .join("");
  }
  return JSON.stringify(content ?? "");
}

So when a tool result replayed through RunOptions.externalHistory carries an image, the image never reaches the provider.

The Problem

  1. Array content: the image is dropped entirely. Non-text blocks map to "", so only the text survives:

    content: [ {type:"text",text:"Shot"}, {type:"image",data:"<base64>",mimeType:"image/png"} ]
    -> "Shot"

    The same happens for LangChain's own image_url block shape.

  2. Object content: the base64 is buried in text. An MCP result object hits the JSON.stringify fallback:

    -> "{\"content\":[{\"type\":\"text\",\"text\":\"Shot\"},{\"type\":\"image\",\"data\":\"iVBORw0KG...

    This is precisely what toolResultParts.ts documents as the thing to avoid:

    Image content blocks become ImageContentParts so vision-capable providers can forward the pixels; JSON.stringifying them would silently bury base64 bytes inside a text payload that the model cannot decode.

  3. The sibling path already does it correctly. convertMessagesToProvider, the inspector path, converts with toolResultToContent(result) and preserves the image as a ContentPart[]. I verified both in one run: inspector returns an array containing an ImageContentPart, the external-history path returns a string. So the two entry points disagree about the same tool result.

  4. It is reachable through documented public API. RunOptions.externalHistory is public and consumed at agents/mcp_agent.ts:377. Any caller replaying prior turns that include an image-returning tool loses the image, and a vision-capable model is left with the text alone.

Test coverage: the existing cases in src/llm/__tests__/messageFormat.test.ts only use string tool content ("Earlier result", "Tool failed"), so array content and image blocks are untested.

Proposed fix

Route tool content through the same conversion the inspector path uses, and normalise LangChain image_url blocks into the MCP image shape so one conversion handles both vocabularies:

typescript
function toolContentForProvider(content: unknown): string | ContentPart[] {
  if (!isContentBlockArray(content)) return toolResultToContent(content);
  return toolResultToContent({ content: content.map(normalizeToolBlock) });
}

Plain string content is unchanged, which keeps the common case and the existing error-replay behaviour intact.

One consequence worth stating up front: text-only array content currently joins with "" and would join with "\n" after this change, matching the inspector path. Two adjacent text blocks stop running together, which I think is an improvement, but it is a behaviour change rather than a pure fix, so say the word if you would rather preserve the existing separator.