Support per-tool MCP output limits (#41421)

## What changed

- Add a positive `output_token_limit` setting to each entry under an MCP server's `tools` configuration.
- Apply the most restrictive limit when plugin and user policies overlap, while keeping approval policy independent.
- Carry the effective MCP output budget in conversation history so tool output, post-tool hook responses, and resumed sessions use the same truncation limit.

## Testing

- Cover configuration parsing, serialization, schema validation, and plugin policy merging.
- Cover MCP output below and above the configured limit, post-tool hook responses, and session resume.

GitOrigin-RevId: d0beb4fca9ba6055d9e1d31c137373b465d50d61
This commit is contained in:
pakrym-oai authored and copyberry committed 2026-08-28 21:40:23 +00:00
1 parent 03147407e3
commit f742dabc6f
38 files changed
+487 -224

No files matched your search

+13 -8
View File
@@ -1,4 +1,3 @@
use crate::context_manager::truncate_function_output_payload;
use crate::original_image_detail::sanitize_original_image_detail;
use crate::session::session::Session;
use crate::session::step_context::StepContext;
@@ -16,9 +15,11 @@ use codex_protocol::models::ResponseItem;
use codex_protocol::models::function_call_output_content_items_to_text;
use codex_tools::LoadableToolSpec;
use codex_tools::ToolName;
use codex_utils_audio::estimate_audio_token_count;
use codex_utils_output_truncation::TruncationPolicy;
use codex_utils_output_truncation::approx_token_count;
use codex_utils_output_truncation::formatted_truncate_text;
use codex_utils_output_truncation::truncate_function_output_payload;
use codex_utils_output_truncation::truncate_text;
use serde::Serialize;
use serde_json::Value as JsonValue;
@@ -122,6 +123,10 @@ impl ToolOutput for McpToolOutput {
self.result.success()
}
fn fallback_token_limit_override(&self) -> Option<usize> {
Some((self.truncation_policy * 1.2).token_budget())
}
fn to_response_item(&self, call_id: &str, _payload: &ToolPayload) -> ResponseInputItem {
ResponseInputItem::FunctionCallOutput {
call_id: call_id.to_string(),
@@ -165,13 +170,13 @@ impl McpToolOutput {
}
}
// This is the context-injection form, so keep it aligned with the
// function-call output truncation that conversation history already
// applies. Code-mode consumers still get the raw `CallToolResult`.
//
// The text is serialized again inside the Responses payload, so allow
// a small buffer for JSON escaping and wrapper overhead.
truncate_function_output_payload(&payload, self.truncation_policy * 1.2)
// History receives this budget in tokens. Code Mode keeps the raw result.
truncate_function_output_payload(
&mut payload,
self.truncation_policy * 1.2,
estimate_audio_token_count,
);
payload
}
}