Skip to content

The agent that grows with you

fix(tools): an image-bearing MCP result keeps its incomplete-data notice (review)

Tekniuma3ab152cd534
diff --git a/agent/tool_dispatch_helpers.py b/agent/tool_dispatch_helpers.py
index 11a86bc947d..b5d0f5fa86b 100644
--- a/agent/tool_dispatch_helpers.py
+++ b/agent/tool_dispatch_helpers.py
@@ -509,8 +509,17 @@ def _detect_upstream_elision(content: Any) -> bool:
def _maybe_append_elision_notice(name: str, content: Any) -> Any:
- """Append the incompleteness notice to untrusted string results with elision markers."""
- if _is_untrusted_tool(name) and _detect_upstream_elision(content):
+ """Append the incompleteness notice to untrusted results with elision markers. A multimodal part list (an MCP
+ result that also carries images) is judged by its first text part, which is where the notice goes."""
+ if not _is_untrusted_tool(name):
+ return content
+ if isinstance(content, list):
+ idx = next((i for i, item in enumerate(content) if _is_text_item(item)), None)
+ if idx is not None and _detect_upstream_elision(content[idx]["text"]):
+ return [*content[:idx], {**content[idx], "text": content[idx]["text"] + _UPSTREAM_ELISION_NOTICE},
+ *content[idx + 1:]]
+ return content
+ if _detect_upstream_elision(content):
return content + _UPSTREAM_ELISION_NOTICE
return content
diff --git a/tests/agent/test_tool_dispatch_helpers.py b/tests/agent/test_tool_dispatch_helpers.py
index dfab302720c..095f342871b 100644
--- a/tests/agent/test_tool_dispatch_helpers.py
+++ b/tests/agent/test_tool_dispatch_helpers.py
@@ -289,3 +289,14 @@ class TestElisionNoticeWiring:
assert content.index(notice) < content.index("</untrusted_tool_result>")
# Exactly one notice.
assert content.count(notice) == 1
+
+ def test_notice_reaches_an_image_bearing_mcp_result(self):
+ """An MCP result that also carries a screenshot is a part list; its elided text still gets the notice,
+ inside the wrapped text part, and the image part is untouched."""
+ from agent.tool_dispatch_helpers import _UPSTREAM_ELISION_NOTICE, make_tool_result_message
+ img = {"type": "image_url", "image_url": {"url": "data:image/jpeg;base64,AAAA"}}
+ msg = make_tool_result_message(
+ "mcp_srv_snap", [{"type": "text", "text": self._elided()}, {"type": "text", "text": "notes"}, img], "call_2")
+ text = msg["content"][0]["text"]
+ assert text.index(_UPSTREAM_ELISION_NOTICE.strip()) < text.index("</untrusted_tool_result>")
+ assert _UPSTREAM_ELISION_NOTICE.strip() not in msg["content"][1]["text"] and msg["content"][2] == img