Commit 66a3b01b0 for llama.cpp
commit 66a3b01b0a243f119b144abfa7122b1677ea9114
Author: Hamish M. Blair <hmblair@stanford.edu>
Date: Wed Nov 25 02:23:30 2026 +1100
server : include MCP image and audio data in tool results (#32064)
diff --git a/tools/server/README-dev.md b/tools/server/README-dev.md
index 997b8137d..55925bb99 100644
--- a/tools/server/README-dev.md
+++ b/tools/server/README-dev.md
@@ -203,7 +203,7 @@ Headers:
- `x-tool-cwd`: optional; if set, use as the CWD for tool; this is not part of tool's params because it's meant to be set by the runtime, not the LLM itself
- `x-tool-runtime`: optional; if set, run the tool inside this isolate instead of on the host. Either `docker-container:<id>` or `podman-container:<id>`, using an already-running container, or `ssh:<target>`, running the tool on a remote host. Refused with 403 when the server is started with `--tools-runtime`, so clients cannot move tools out of that sandbox
-Returns JSON object. There are two response formats (MCP tools use the same two formats: their result content is concatenated into `plain_text_response`, and RPC or tool errors are surfaced as the `error` string):
+Returns JSON object. There are two response formats (MCP tools use the same two formats: their text parts are concatenated into `plain_text_response`, image and audio parts are rendered there as `data:<mime>;base64,...` lines, and RPC or tool errors are surfaced as the `error` string):
Format 1: Plain text. The text will be placed into a field called `plain_text_response`, example:
diff --git a/tools/server/server-mcp.cpp b/tools/server/server-mcp.cpp
index 08929ba54..09dc870f0 100644
--- a/tools/server/server-mcp.cpp
+++ b/tools/server/server-mcp.cpp
@@ -3,7 +3,9 @@
#include "common.h"
#include "subproc.h"
+#include <algorithm>
#include <atomic>
+#include <cctype>
#include <chrono>
#include <cstdio>
#include <fstream>
@@ -192,18 +194,46 @@ static std::string rpc_error_message(const json & resp) {
return "unknown error";
}
+// renders an MCP image or audio part as a single-line base64 data URI (wrapped base64 is joined)
+static std::string mcp_media_part_to_data_uri(const json & part) {
+ const std::string mime = part.value("mimeType", "");
+ std::string data = part.value("data", "");
+ data.erase(std::remove_if(data.begin(), data.end(), [](unsigned char c) { return std::isspace(c); }), data.end());
+ if (mime.empty() || data.empty()) {
+ return "";
+ }
+ return "data:" + mime + ";base64," + data;
+}
+
+// renders one MCP content part as text; unsupported part types render as an empty string
+static std::string mcp_content_part_to_text(const json & part) {
+ if (!part.is_object()) {
+ return "";
+ }
+ const std::string type = part.value("type", "");
+ if (type == "text") {
+ return part.value("text", "");
+ }
+ if (type == "image" || type == "audio") {
+ return mcp_media_part_to_data_uri(part);
+ }
+ return "";
+}
+
// normalize an MCP tools/call result to the /tools contract (see README-dev.md):
-// concat text parts of result.content[], and surface an isError result
+// concat text parts of result.content[], render media parts as data URI lines, and surface an isError result
static json mcp_result_to_response(const json & result) {
std::string text;
if (result.contains("content") && result.at("content").is_array()) {
for (const auto & part : result.at("content")) {
- if (part.is_object() && part.value("type", "") == "text") {
- if (!text.empty()) {
- text += "\n";
- }
- text += part.value("text", "");
+ const std::string piece = mcp_content_part_to_text(part);
+ if (piece.empty()) {
+ continue;
+ }
+ if (!text.empty()) {
+ text += "\n";
}
+ text += piece;
}
}
if (result.is_object() && result.value("isError", false)) {