MCP: a model that reads pictures gets the pictures its tools return, by default (#2343)

mcp.image_results (7c9b6f3a2, PR #2473) passes an MCP tool's image
blocks to the picture reader, but it was off unless the run config said
true, so a tool that takes a screenshot or reads an image handed the
model only "[image (...): not shown to the model]". The owner's rule is
that every picture-capable model reads pictures by default (coordinator
11-16).

Not set in the config, it now follows the loaded model: on when it has a
picture reader, off when it has none (serve.mcp.resolve_image_results,
called in serve/server.py's start-up right after the Service and its
reader exist). "image_results": false keeps it off and true keeps it on,
as before. The limits are unchanged: at most 4 images per result, 24 MB
of base64 each, PNG / JPEG / WebP with the header checked, cached beside
the config. docs/MCP_IMAGES.md says the new default in plain words.

From PR #2343's request (ds): its other parts are not taken (lifting
pictures out of a tool's text; the reader's working-folder repair).

Test: serve/test_mcp_image_default.py (the module does not import on the
base: no resolve_image_results), 5 pass: on with a reader and no
setting (3 images reach the model), off without a reader, an explicit
false respected, the start-up order; test_mcp_media's omitted-switch case
now expects "not set" (it failed on the base's DEFAULTS); serve.test_mcp
passes (55 in all).
This commit is contained in:
Niko1221 committed 2026-11-16 18:56:53 +01:00
1 parent 0f09a2eff4
commit 6339bb0947
5 files changed
+81 -8

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
# MCP raster images in the web chat
Enable this optional feature with `"mcp": {"image_results": true}` in the run configuration (alongside existing MCP settings). The value must be a JSON boolean (`true` or `false`); strings and numbers are rejected. It is disabled by default; text-only tool output and stream event fields remain unchanged when disabled.
This is on by default when the loaded model can read pictures, and off when it cannot. To turn it off, add `"mcp": {"image_results": false}` to the run configuration (beside the other MCP settings); `true` turns it on. The value must be a JSON boolean (`true` or `false`); strings and numbers are rejected. When it is off, the model gets the tool's text only, and the stream events are the same as before.
MCP image results are cached by SHA-256 next to the run configuration, in `<config-stem>.tool-media/`. The web chat receives small same-origin references instead of embedding base64 in chat history. PNG, JPEG and WebP are supported; SVG and arbitrary file/resource URIs are not rendered. Each call displays at most four images, each with at most 24 million base64 characters (about 18 MB decoded). If the cache cannot be written, the chat retains the text result and logs the cache error.
+10 -1
View File
@@ -37,7 +37,16 @@ import urllib.request
from serve import lifeline
PROTOCOL = "2025-06-18" # the MCP revision Strata asks for; the server's answer is used as given
DEFAULTS = {"timeout_s": 60.0, "max_result_chars": 20000, "max_rounds": 8, "start_timeout_s": 120.0, "image_results": False}
# image_results: None = not set in the config, which means on when the loaded model has a picture reader (#2343, the owner's rule
# that every picture-capable model reads pictures by default); true / false in the config decide it (resolve_image_results)
DEFAULTS = {"timeout_s": 60.0, "max_result_chars": 20000, "max_rounds": 8, "start_timeout_s": 120.0, "image_results": None}
def resolve_image_results(settings: dict, has_picture_reader: bool) -> bool:
"""Whether MCP tool images reach the model: the config's mcp.image_results when it says true or false, else on exactly
when the loaded model has a picture reader (#2343)."""
value = settings.get("image_results")
return value if isinstance(value, bool) else bool(has_picture_reader)
class McpError(RuntimeError):
+3 -1
View File
@@ -64,7 +64,7 @@ sys.path.insert(0, str(ROOT)) # run as a script (run-<model>.bat) as well as a
from serve.frontend import (THINK_END, ChatTemplate, Event, OutputParser, parser_for_template, anthropic_to_messages, anthropic_tools, # noqa: E402
forced_call, images_of, literal_tags, mark_think_literals, openai_to_messages, require_messages,
tool_choice_of, unmark_think_literals)
from serve.mcp import McpCancelled, hub_from_config # noqa: E402
from serve.mcp import McpCancelled, hub_from_config, resolve_image_results # noqa: E402
from serve import runconfig # noqa: E402
from serve.webfiles import subtree_file as web_subtree_file # noqa: E402
from strata_control import engine_component # noqa: E402
@@ -10465,6 +10465,8 @@ def _main(argv=None, *, lifecycle_event=None, interrupt_exit_code: int = 0) -> i
if hub is not None:
import atexit
svc.mcp = hub
# #2343: tool images reach a model that reads pictures unless the config says "image_results": false
hub.settings["image_results"] = resolve_image_results(hub.settings, svc.vision is not None)
if hub.settings["image_results"]:
hub.media_dir = Path(a.config).with_suffix(".tool-media") if a.config else Path.cwd() / ".tool-media"
print(f"[strata] starting {len(hub.servers)} MCP server{'s' * (len(hub.servers) != 1)} for the web app's "
+61
View File
@@ -0,0 +1,61 @@
"""#2343: MCP tool images reach the model by default when the loaded model has a picture reader (the owner's rule: every
picture-capable model reads pictures by default). Without a reader they stay off; "image_results": false in the run config
keeps them off; true keeps them on. The limits stay as they were (4 per result, 24 MB of base64, PNG / JPEG / WebP)."""
from __future__ import annotations
import base64
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from serve.mcp import hub_from_config, resolve_image_results
ROOT = Path(__file__).resolve().parents[1]
PNG = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jB0kAAAAASUVORK5CYII=")
class Default(unittest.TestCase):
def test_not_set_follows_the_picture_reader(self):
self.assertTrue(resolve_image_results({"image_results": None}, True))
self.assertFalse(resolve_image_results({"image_results": None}, False))
self.assertTrue(resolve_image_results({}, True))
def test_an_explicit_value_is_respected(self):
self.assertFalse(resolve_image_results({"image_results": False}, True))
self.assertTrue(resolve_image_results({"image_results": True}, False))
def tool_images(self, config: dict, has_reader: bool):
hub = hub_from_config({"mcp_servers": {"fake": {"command": "unused"}}, "mcp": config})
hub.settings["image_results"] = resolve_image_results(hub.settings, has_reader)
server = hub.servers["fake"]
server.status = "ready"
server.tools = [{"name": "shot", "inputSchema": {}}]
block = {"type": "image", "mimeType": "image/png", "data": base64.b64encode(PNG).decode()}
with tempfile.TemporaryDirectory() as tmp:
hub.media_dir = Path(tmp)
with patch.object(server, "call", return_value={"content": [{"type": "text", "text": "a shot"}, block] * 3}):
return hub.call("fake__shot", {})
def test_a_model_with_a_reader_gets_the_pictures_without_any_setting(self):
result = self.tool_images({}, has_reader=True)
self.assertEqual(len(result["images"]), 3)
self.assertEqual([p["type"] for p in result["model_content"]], ["text", "image", "image", "image"])
def test_without_a_reader_or_with_false_the_model_reads_text_only(self):
for config, reader in (({}, False), ({"image_results": False}, True)):
with self.subTest(config=config, reader=reader):
result = self.tool_images(config, has_reader=reader)
self.assertNotIn("images", result)
self.assertNotIn("model_content", result)
self.assertIn("not shown to the model", result["text"])
def test_the_server_decides_it_after_the_picture_reader_is_known(self):
src = (ROOT / "serve" / "server.py").read_text(encoding="utf-8")
at = src.index('hub.settings["image_results"] = resolve_image_results(hub.settings, svc.vision is not None)')
self.assertLess(at, src.index('if hub.settings["image_results"]:\n hub.media_dir'))
self.assertLess(src.index("svc = Service(engine, tok"), at)
if __name__ == "__main__":
unittest.main()
+6 -5
View File
@@ -32,11 +32,12 @@ class Configuration(unittest.TestCase):
self.assertEqual(len(result['images']), 1)
self.assertEqual(result['model_content'][1]['type'], 'image')
def test_disabled_and_omitted_switch_stay_off(self):
for settings in ({}, {'image_results': False}):
with self.subTest(settings=settings):
hub = hub_from_config({'mcp_servers': {'fake': {'command': 'unused'}}, 'mcp': settings})
self.assertFalse(hub.settings['image_results'])
def test_disabled_switch_stays_off_and_an_omitted_one_is_left_to_the_picture_reader(self):
hub = hub_from_config({'mcp_servers': {'fake': {'command': 'unused'}}, 'mcp': {'image_results': False}})
self.assertIs(hub.settings['image_results'], False)
# #2343: not set = decided at start by whether the model reads pictures (serve.mcp.resolve_image_results)
hub = hub_from_config({'mcp_servers': {'fake': {'command': 'unused'}}, 'mcp': {}})
self.assertIsNone(hub.settings['image_results'])
def test_switch_rejects_non_booleans(self):
for value in ('true', 'false', 0, 1, None, [], {}):