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

@@ -1,3 +1,4 @@
use anyhow::Context;
use codex_config::types::AppToolApproval;
use codex_config::types::McpServerAuth;
use codex_config::types::McpServerConfig;
@@ -45,7 +46,7 @@ pub(super) fn ensure_table_for_read(item: &mut TomlItem) -> Option<&mut TomlTabl
}
}
fn serialize_mcp_server_table(config: &McpServerConfig) -> TomlTable {
fn serialize_mcp_server_table(config: &McpServerConfig) -> anyhow::Result<TomlTable> {
let mut entry = TomlTable::new();
entry.set_implicit(false);
@@ -175,15 +176,15 @@ fn serialize_mcp_server_table(config: &McpServerConfig) -> TomlTable {
let mut tool_entries: Vec<_> = config.tools.iter().collect();
tool_entries.sort_by_key(|(name, _)| *name);
for (name, tool_config) in tool_entries {
tools.insert(name, serialize_mcp_server_tool(tool_config));
tools.insert(name, serialize_mcp_server_tool(tool_config)?);
}
entry.insert("tools", TomlItem::Table(tools));
}
entry
Ok(entry)
}
fn serialize_mcp_server_tool(config: &McpServerToolConfig) -> TomlItem {
fn serialize_mcp_server_tool(config: &McpServerToolConfig) -> anyhow::Result<TomlItem> {
let mut entry = TomlTable::new();
entry.set_implicit(false);
if let Some(approval_mode) = config.approval_mode {
@@ -194,15 +195,21 @@ fn serialize_mcp_server_tool(config: &McpServerToolConfig) -> TomlItem {
AppToolApproval::Approve => "approve",
});
}
TomlItem::Table(entry)
if let Some(output_token_limit) = config.output_token_limit {
entry["output_token_limit"] = value(
i64::try_from(output_token_limit.get())
.context("output_token_limit exceeds the TOML integer range")?,
);
}
Ok(TomlItem::Table(entry))
}
pub(super) fn serialize_mcp_server(config: &McpServerConfig) -> TomlItem {
TomlItem::Table(serialize_mcp_server_table(config))
pub(super) fn serialize_mcp_server(config: &McpServerConfig) -> anyhow::Result<TomlItem> {
Ok(TomlItem::Table(serialize_mcp_server_table(config)?))
}
pub(super) fn serialize_mcp_server_inline(config: &McpServerConfig) -> InlineTable {
serialize_mcp_server_table(config).into_inline_table()
pub(super) fn serialize_mcp_server_inline(config: &McpServerConfig) -> anyhow::Result<InlineTable> {
Ok(serialize_mcp_server_table(config)?.into_inline_table())
}
pub(super) fn merge_inline_table(existing: &mut InlineTable, replacement: InlineTable) {