Support non-text content types (images, other) in tool results
Problem
as_tool_call_result() currently hardcodes all tool results as type: "text" content blocks:
jsonrpc_response(data$id, list(content = list(list(type = "text",
text = format_result(result))), isError = is_error))
The MCP specification supports multiple content types in tool results, including image (base64-encoded) and audio. There's currently no way for an R tool to return an image that renders inline in the client (e.g., Claude Desktop).
Use Case
We're building an MCP server for data analysis. Our tool produces plots, such as Kaplan-Meier survival curves and other plots. We'd like to return these plots as inline images so the LLM client can see and reason about them — but today everything gets serialized to text.
Proposed Behavior
When a tool function returns an ellmer::ContentImageInline object (or a list containing one), as_tool_call_result() should produce the appropriate MCP image content block:
{
"content": [
{ "type": "image", "data": "<base64>", "mimeType": "image/png" }
]
}
Ideally this would also support mixed content (text + image) in a single tool result, which the MCP spec allows.
ellmer already has the building blocks: content_image_file(), content_image_plot(), and ContentImageInline — it's just the mcptools serialization layer that doesn't handle them.
Environment
- mcptools 0.2.1
- ellmer 0.3.2
- R 4.4.x
I hit this same issue using btw::btw_tool_run_r() via Kiro (stdio transport, mcptools 0.2.1, btw 1.2.1, R 4.5.3, Windows). Any code that produces a plot fails with:
Could not convert tool result from a list to JSON.
No method asJSON S3 class: S7_object
The root cause is that btw_tool_run_r() returns an ellmer::ContentToolResult whose @value slot contains ellmer::ContentImageInline S7 objects. When ellmer:::tool_string() tries jsonlite::toJSON() on this list, it fails because jsonlite has no S3 method for S7 objects.
Workaround
Monkey-patching as_tool_call_result in the mcptools namespace to handle S7 Content objects:
fix_plot_serialization <- function() {
patched_as_tool_call_result <- function(data, result) {
is_error <- FALSE
if (inherits(result, "ellmer::ContentToolResult")) {
is_error <- !is.null(result@error)
content_list <- lapply(result@value, function(item) {
if (inherits(item, "ellmer::ContentImageInline")) {
list(type = "image", mimeType = item@type, data = item@data)
} else if (inherits(item, "ellmer::ContentText")) {
list(type = "text", text = item@text)
} else if (is.character(item)) {
list(type = "text", text = paste(item, collapse = "\n"))
} else {
list(type = "text", text = format(item))
}
})
mcptools:::jsonrpc_response(data$id, list(content = content_list, isError = is_error))
} else {
mcptools:::jsonrpc_response(data$id, list(
content = list(list(type = "text", text = paste(result, collapse = "\n"))),
isError = is_error
))
}
}
assignInNamespace("as_tool_call_result", patched_as_tool_call_result, ns = "mcptools")
}
fix_plot_serialization()
After applying this patch, plots render correctly and are returned as MCP image content blocks. The patch needs to be re-applied each session (e.g., via source() after mcptools::mcp_session()).
This confirms the fix is straightforward — as_tool_call_result() just needs to inspect the result for ellmer::Content subclasses and emit the appropriate MCP content type (image, text, etc.) instead of blindly calling toJSON().
Ran into this building an SDM MCP server. Here's a concrete fix proposal:
The issue is in as_tool_call_result() in R/session.R. It hardcodes type: "text" for all results. When a tool returns a ContentImageInline, it should produce an MCP image content block instead.
Proposed fix — check for ContentImageInline before falling through to text:
as_tool_call_result <- function(data, result) {
is_error <- FALSE
# Handle ContentImageInline — return MCP image content block
if (S7_inherits(result, ContentImageInline)) {
return(jsonrpc_response(data$id, list(
content = list(list(
type = "image",
data = result@data,
mimeType = result@type
)),
isError = FALSE
)))
}
# Handle ContentToolResult wrapping an image
if (S7_inherits(result, ContentToolResult)) {
is_error <- !is.null(result@error)
value <- result@value
if (S7_inherits(value, ContentImageInline)) {
return(jsonrpc_response(data$id, list(
content = list(list(
type = "image",
data = value@data,
mimeType = value@type
)),
isError = is_error
)))
}
}
# Default: text (existing behaviour)
format_result <- function(x) paste(x, collapse = "\n")
if (S7_inherits(result, ContentToolResult)) {
format_result <- asNamespace("ellmer")[["tool_string"]] %||% format_result
}
jsonrpc_response(data$id, list(
content = list(list(type = "text", text = format_result(result))),
isError = is_error
))
}
This covers the two main cases:
- Tool directly returns
ContentImageInline(e.g.,content_image_file("map.png")) - Tool returns
ContentToolResult(value = ContentImageInline(...))
ellmer already has content_image_file(), content_image_plot(), and ContentImageInline — it's just the mcptools serialization layer that needs to handle them.
For mixed content (text + image in one result), we'd need to handle lists of Content objects too, but that could be a follow-up.
Happy to open a PR for this.