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

+16 -2
View File
@@ -30,6 +30,8 @@ use crate::tools::tool_dispatch_trace::ToolDispatchTrace;
use crate::util::error_or_panic;
use codex_analytics::ControlToolCallStatus;
use codex_extension_api::ToolCallOutcome;
use codex_history::CodexHarnessMetadata;
use codex_history::ResponseItemEnvelope;
use codex_protocol::models::FunctionCallOutputPayload;
use codex_protocol::models::ResponseInputItem;
use codex_protocol::parse_command::ParsedCommand;
@@ -191,14 +193,22 @@ pub(crate) struct AnyToolResult {
}
impl AnyToolResult {
pub(crate) fn into_response(self) -> ResponseInputItem {
pub(crate) fn into_response(self) -> ResponseItemEnvelope {
let Self {
call_id,
payload,
result,
..
} = self;
result.to_response_item(&call_id, &payload)
ResponseItemEnvelope {
item: result.to_response_item(&call_id, &payload).into(),
metadata: result
.fallback_token_limit_override()
.map(|limit| CodexHarnessMetadata {
fallback_token_limit_override: Some(limit),
..Default::default()
}),
}
}
pub(crate) fn code_mode_result(self) -> serde_json::Value {
@@ -223,6 +233,10 @@ impl ToolOutput for PostToolUseFeedbackOutput {
self.original.success_for_logging()
}
fn fallback_token_limit_override(&self) -> Option<usize> {
self.original.fallback_token_limit_override()
}
fn to_response_item(&self, call_id: &str, payload: &ToolPayload) -> ResponseInputItem {
self.model_visible.to_response_item(call_id, payload)
}