The response-size guard truncated data-query and info-tool responses by
measuring the bare unwrapped payload dict, then re-wrapped it into a
ToolResult afterward. The ToolResult envelope (content/type/text/meta,
plus JSON-escaping of the embedded payload string) adds real overhead —
empirically ~2% for large payloads and ~40 tokens fixed for small ones —
so a candidate that measured under the limit could still end up oversized
once wrapped, causing an unnecessary hard block.
Thread an optional size_fn through the truncation helpers (bisection and
phase checks) so the middleware can measure candidates against the actual
wrapped object during the search, not just re-validate the wrong
measurement afterward.
Co-Authored-By: Claude <noreply@anthropic.com>