Skip to content

feat: built-in large tool output handling#2728

Open
yunus25jmi1 wants to merge 1 commit intodocker:mainfrom
yunus25jmi1:feat/large-tool-output-handling
Open

feat: built-in large tool output handling#2728
yunus25jmi1 wants to merge 1 commit intodocker:mainfrom
yunus25jmi1:feat/large-tool-output-handling

Conversation

@yunus25jmi1
Copy link
Copy Markdown
Contributor

@yunus25jmi1 yunus25jmi1 commented May 9, 2026

Summary

Add a new builtin hook that automatically handles large tool responses. When enabled, tool responses exceeding the threshold are saved to disk and replaced with a pointer that the agent can read back using shell tools.

This is a root-cause fix for issue #2722.


Problem

When an MCP tool returns a large response the agent breaks in one of two ways:

  1. With default max_old_tool_call_tokens budget: A single large result eats the entire budget and all earlier tool results in the session get silently replaced with [content truncated].

  2. With max_old_tool_call_tokens: -1: The full response stays in the session but overflows the model's context window. Auto-compaction can't help because the oversized result is in the most recent messages, so the agent gets stuck.

Worth noting: builtin tools (shell, openapi, api) each have a hardcoded 30,000-char per-result limit. MCP tools have no per-result limit at all.


Root Cause

MCP tools have no output limit, unlike builtin tools (shell, openapi, api) which all have a hardcoded 30,000-char limit. When MCP returns large responses, they flow into the session unmodified.


Solution

Added a new builtin hook handle_large_tool_output that:

  1. Intercepts tool responses at EventToolResponseTransform
  2. When response exceeds threshold, saves full output to disk
  3. Replaces response with a pointer showing tool name, char count, file path, preview

Configuration

agents:
  root:
    handle_large_tool_output:
      enabled: true        # default: false
      threshold: 5000      # chars - default
      output_dir: /tmp     # default: os.TempDir()
      preview_size: 3000   # chars in preview - default

Files Changed

File Change
agent-schema.json Added handle_large_tool_output config
pkg/config/latest/types.go Added HandleLargeToolOutputConfig struct
pkg/agent/agent.go Added field and accessor
pkg/agent/opts.go Added WithHandleLargeToolOutput option
pkg/teamloader/teamloader.go Wired config to agent
pkg/hooks/builtins/handle_large_tool_output.go New builtin hook
pkg/hooks/builtins/builtins.go Registered builtin
pkg/runtime/hooks.go Passed config to ApplyAgentDefaults

Testing

  • Build: ✅ Passes
  • Lint: ✅ 0 issues
  • Docker Image: ✅ Builds

Fixes #2722

@yunus25jmi1 yunus25jmi1 requested a review from a team as a code owner May 9, 2026 12:54
@yunus25jmi1 yunus25jmi1 force-pushed the feat/large-tool-output-handling branch from 19c03e4 to 1771f43 Compare May 9, 2026 12:57
@yunus25jmi1 yunus25jmi1 closed this May 9, 2026
@yunus25jmi1 yunus25jmi1 force-pushed the feat/large-tool-output-handling branch from 1771f43 to b657530 Compare May 9, 2026 13:00
Add a new builtin hook that automatically handles large tool responses.
When enabled, tool responses exceeding the threshold are saved to disk
and replaced with a pointer that the agent can read back using shell
tools. This prevents large MCP tool responses from exhausting the
model's context window while still allowing the agent to access the
full data.

The feature is configured at the agent level:
  handle_large_tool_output:
    enabled: true
    threshold: 5000      # characters (default)
    output_dir: /tmp     # default: os.TempDir()
    preview_size: 3000   # chars in preview (default)

This is a root-cause fix for docker#2722 - it addresses both the lack of
MCP output limits and context window overflow issues.
@yunus25jmi1 yunus25jmi1 reopened this May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: built-in large tool output handling

1 participant