mcptools icon indicating copy to clipboard operation
mcptools copied to clipboard

feat: support image content in tool results (ContentImageInline)

Open mrcanofcatfood opened this issue 6 months ago • 0 comments

Problem

as_tool_call_result() hardcodes all tool results as type: "text". There's no way for an R tool to return an image that renders inline in the client (e.g., Claude Desktop). This was reported in #96.

Fix

Three new code paths in as_tool_call_result():

  1. Direct image: Tool returns ContentImageInline → MCP image content block
  2. Wrapped image: Tool returns ContentToolResult(value = ContentImageInline(...)) → MCP image content block
  3. Mixed content: Tool returns ContentToolResult(value = list(ContentImageInline(...), "text")) → multiple content blocks (text + image)

Default text-only behaviour is unchanged.

Usage

After this fix, an MCP server tool can return inline images:

ellmer::tool(
  fun = function() {
    # ... generate a plot ...
    ellmer::content_image_file("map.png")
  },
  name = "generate_map",
  description = "Generate a suitability map"
)

The image will appear inline in Claude Desktop / other MCP clients, and the LLM can reason about it visually.

Testing

Verified that:

  • Tools returning plain strings → unchanged (text content)
  • Tools returning content_image_file(path) → image content block with base64 data
  • Tools returning ContentToolResult(value = content_image_plot()) → image content block
  • Tools returning mixed list → multiple content blocks

Closes #96.

mrcanofcatfood avatar May 20 '26 11:05 mrcanofcatfood