The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(mcp): MCP images honour vision.max_calls_per_image and prep on the vision pool (review)
Teknium3f3df8d4d7a5
diff --git a/tests/tools/test_mcp_image_content.py b/tests/tools/test_mcp_image_content.pyindex 6cfcb4c979a..b4f1f6c2a2e 100644--- a/tests/tools/test_mcp_image_content.py+++ b/tests/tools/test_mcp_image_content.py@@ -177,6 +177,14 @@ class TestNativeImageAttach: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 new+ # name is refused once the cap is spent.+ from tools import vision_tools_history_budget as budget+ monkeypatch.setattr(budget, "_repeat_counts", {})+ capped = {"vision": {"max_calls_per_image": 1}}+ assert isinstance(self._call(monkeypatch, tmp_path, capped), dict)+ repeat = self._call(monkeypatch, tmp_path, capped)+ assert isinstance(repeat, str) and "already in context" in repeat and "MEDIA:" in repeatimport iofrom PIL import Image# A valid JPEG header over a truncated pixel stream passes the cache's and the sniff's header checks.diff --git a/tools/mcp_tool_content.py b/tools/mcp_tool_content.pyindex 279ac1dfb4a..320626a5659 100644--- a/tools/mcp_tool_content.py+++ b/tools/mcp_tool_content.py@@ -178,19 +178,34 @@ def _mcp_result_with_native_images(text: str, image_paths: List[str]) -> Any:if not image_paths:return texttry:- from tools.vision_tools import _should_use_native_vision_fast_path+ from tools.vision_tools import _should_use_native_vision_fast_path, _vision_cpu_executor+ from tools.vision_tools_history_budget import repeat_refusalif not _should_use_native_vision_fast_path():return text- attached = [(p, r) for p, r in ((p, _mcp_native_image_part(p)) for p in image_paths[:_MCP_NATIVE_IMAGE_MAX]) if r]+ # 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]]+ 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)return text+ attached, notes = [], ""+ for path, ready in prepared:+ if not ready:+ continue+ part, note = ready+ # vision.max_calls_per_image: a polled screenshot tool re-sends the same pixels under a fresh cache path+ # each call, so the reservation keys on the resized data URL (identical pixels, identical key).+ if repeat_refusal(part["image_url"]["url"]):+ notes += f"\n- MEDIA:{path}: not attached; this image is already in context (vision.max_calls_per_image)."+ continue+ attached.append(part)+ if note:+ notes += f"\n- MEDIA:{path}: {note}"if not attached:- return text- notes = "".join(f"\n- MEDIA:{p}: {note}" for p, (_part, note) in attached if note)+ return text + notesheader = "\n\nThe image(s) from this call are attached — inspect them with your native vision."- return {"_multimodal": True,- "content": [{"type": "text", "text": text + header + notes}, *(part for _p, (part, _n) in attached)],+ return {"_multimodal": True, "content": [{"type": "text", "text": text + header + notes}, *attached],"text_summary": text}diff --git a/tools/mcp_tool_handlers.py b/tools/mcp_tool_handlers.pyindex d73d5fd0c99..54f81aa7374 100644--- a/tools/mcp_tool_handlers.py+++ b/tools/mcp_tool_handlers.py@@ -601,7 +601,7 @@ def _make_tool_handler(server_name: str, tool_name: str, tool_timeout: float, *,server_name, server, op, _call, tool_timeout,(_handle_stdio_child_exited_and_retry, _handle_auth_error_and_retry, session_expired),_on_failure, record_outcome=True)- # Off the MCP loop on purpose: the image resize is CPU work that must not stall other servers' I/O.+ # Off the MCP loop on purpose: image prep is CPU work (bounded vision pool) that must not stall other servers' I/O.return _mcp_result_with_native_images(text, image_paths) if native_images and not _result_is_error(text) else textreturn _handler