Deduplicate MCP text fallbacks in Code Mode output (#63540)

## Why

MCP tools can return structured data alongside a JSON text fallback for
clients that need it. Printing the whole result in Code Mode can show
the same data twice.

## What changed

Enable `features.code_mode_mcp_result_dedup` to let `text()` omit
`content` when it prints an original, successful MCP result whose only
content block is a plain text fallback that parses to the same JSON as
`structuredContent`. The JavaScript result retains both fields.

Carry the result format through gRPC so the host can distinguish MCP
results from ordinary JSON. The feature is off by default.

## Testing

Add coverage for both feature flag settings, for printing original MCP
results and copies through gRPC, and for comparing equivalent JSON.

GitOrigin-RevId: a8b0b42f5375d9744c235a0e663090dd5982e37e
This commit is contained in:
Charlie Marsh
2026-11-24 04:49:27 +00:00
committed by copyberry
parent 714bc44f0d
commit 96681d86f0
81 changed files with 955 additions and 143 deletions
@@ -151,6 +151,13 @@ message CompleteToolCallRequest {
message ToolCallSucceeded {
bytes output_json = 1;
ToolResultFormat result_format = 2;
}
enum ToolResultFormat {
// The default preserves the full JSON value when the format is omitted or unknown.
TOOL_RESULT_FORMAT_JSON = 0;
TOOL_RESULT_FORMAT_MCP_CALL_TOOL_RESULT = 1;
}
message ToolCallFailed {
+1
View File
@@ -55,6 +55,7 @@ pub use session::CodeModeSessionOptions;
pub use session::CodeModeSessionProvider;
pub use session::CodeModeSessionProviderFuture;
pub use session::CodeModeSessionResultFuture;
pub use session::CodeModeToolResult;
pub use session::NoopCodeModeSessionDelegate;
pub use session::NotificationFuture;
pub use session::StartedCell;
+23 -2
View File
@@ -20,10 +20,31 @@ pub type CodeModeSessionResultFuture<'a, T> =
Pin<Box<dyn Future<Output = Result<T, String>> + Send + 'a>>;
pub type CodeModeSessionProviderFuture<'a> =
CodeModeSessionResultFuture<'a, Arc<dyn CodeModeSession>>;
pub type ToolInvocationFuture<'a> =
Pin<Box<dyn Future<Output = Result<JsonValue, String>> + Send + 'a>>;
pub type ToolInvocationFuture<'a> = CodeModeSessionResultFuture<'a, CodeModeToolResult>;
pub type NotificationFuture<'a> = Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'a>>;
/// A nested call's JSON value and how the runtime should treat it when printed.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum CodeModeToolResult {
Json(JsonValue),
McpCallToolResult(JsonValue),
}
impl CodeModeToolResult {
/// Returns the full script-visible value without its format tag.
pub fn into_json(self) -> JsonValue {
match self {
Self::Json(value) | Self::McpCallToolResult(value) => value,
}
}
}
impl From<JsonValue> for CodeModeToolResult {
fn from(value: JsonValue) -> Self {
Self::Json(value)
}
}
/// Optional resource limits shared by every cell in one code-mode session.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct CodeModeSessionCellExecutionLimits {