The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): MCP image prep on the vision pool keeps the caller's runtime context (review)
Teknium775c5a91aa1e
diff --git a/tests/tools/test_mcp_image_content.py b/tests/tools/test_mcp_image_content.pyindex b4f1f6c2a2e..37afc013e54 100644--- a/tests/tools/test_mcp_image_content.py+++ b/tests/tools/test_mcp_image_content.py@@ -175,6 +175,22 @@ class TestNativeImageAttach:assert "downscaled from 3000x2000 to 1500x1000" in out["content"][0]["text"]assert "multiply any coordinates you report by 2.00" in out["content"][0]["text"]+ def test_image_prep_sees_the_callers_runtime(self, tmp_path, monkeypatch):+ """The prep pool runs in the caller's context: a managed local runtime (stb_image, no WebP) set on the+ calling session converts a small WebP instead of attaching bytes its server silently cannot decode."""+ import io+ from PIL import Image+ from agent.auxiliary_client import reset_runtime_main, set_runtime_main+ buf = io.BytesIO()+ Image.new("RGB", (64, 64), (200, 30, 30)).save(buf, "WEBP")+ token = set_runtime_main("llamacpp", "local-vlm")+ try:+ out = self._call(monkeypatch, tmp_path, {}, image=buf.getvalue(), mime="image/webp")+ finally:+ reset_runtime_main(token)+ assert isinstance(out, dict)+ assert not out["content"][1]["image_url"]["url"].startswith("data:image/webp")+def test_text_mode_an_undecodable_or_an_unshrinkable_image_keeps_the_string_result(self, tmp_path, monkeypatch):assert "MEDIA:" in self._call(monkeypatch, tmp_path, {"agent": {"image_input_mode": "text"}})# vision.max_calls_per_image counts pixels, not cache paths: a polled screenshot re-cached under a newdiff --git a/tools/mcp_tool_content.py b/tools/mcp_tool_content.pyindex 320626a5659..f75c00dab3c 100644--- a/tools/mcp_tool_content.py+++ b/tools/mcp_tool_content.py@@ -183,8 +183,12 @@ def _mcp_result_with_native_images(text: str, image_paths: List[str]) -> Any:if not _should_use_native_vision_fast_path():return text# Decode/resize on the bounded vision pool: a parallel tool batch of image-heavy MCP calls must not- # decode dozens of large images at once on tool threads.- jobs = [(p, _vision_cpu_executor.submit(_mcp_native_image_part, p)) for p in image_paths[:_MCP_NATIVE_IMAGE_MAX]]+ # decode dozens of large images at once on tool threads. Each job runs in a copy of the caller's context:+ # the active runtime (a managed local model narrows formats: no WebP) and the profile's vision settings+ # are ContextVars a bare pool thread would not see.+ import contextvars+ jobs = [(p, _vision_cpu_executor.submit(contextvars.copy_context().run, _mcp_native_image_part, p))+ for p in image_paths[:_MCP_NATIVE_IMAGE_MAX]]prepared = [(p, f.result()) for p, f in jobs]except Exception: # deliberate boundary: the MEDIA: paths already carry the images, so any failure keeps the textlogger.debug("MCP native image attach failed, keeping MEDIA: paths", exc_info=True)