mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
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:
@@ -1,6 +1,7 @@
|
||||
use codex_code_mode_protocol::CodeModeOutputTruncation;
|
||||
use codex_code_mode_protocol::CodeModeSessionCellExecutionLimits;
|
||||
use codex_code_mode_protocol::CodeModeToolKind;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::ExecuteRequest;
|
||||
use codex_code_mode_protocol::FunctionCallOutputContentItem;
|
||||
use codex_code_mode_protocol::ImageDetail;
|
||||
@@ -89,6 +90,17 @@ fn tool_definition(definition: proto::ToolDefinition) -> Result<ToolDefinition,
|
||||
})
|
||||
}
|
||||
|
||||
/// Prost defaults missing or unrecognized formats to plain JSON.
|
||||
pub(super) fn tool_result(result: &proto::ToolCallSucceeded) -> Result<CodeModeToolResult, Status> {
|
||||
let value = serde_json::from_slice(&result.output_json).map_err(|error| {
|
||||
Status::invalid_argument(format!("invalid code-mode tool output JSON: {error}"))
|
||||
})?;
|
||||
Ok(match result.result_format() {
|
||||
proto::ToolResultFormat::Json => CodeModeToolResult::Json(value),
|
||||
proto::ToolResultFormat::McpCallToolResult => CodeModeToolResult::McpCallToolResult(value),
|
||||
})
|
||||
}
|
||||
|
||||
fn json_field(value: Option<Vec<u8>>, field: &str) -> Result<Option<JsonValue>, Status> {
|
||||
value
|
||||
.map(|value| {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use codex_code_mode_protocol::CellId;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::FunctionCallOutputContentItem;
|
||||
use codex_code_mode_protocol::ImageDetail;
|
||||
use codex_code_mode_protocol::MissingCodeModeHostDuration;
|
||||
@@ -12,8 +13,24 @@ use tonic::Code;
|
||||
|
||||
use super::execute_request;
|
||||
use super::execution_outcome;
|
||||
use super::tool_result;
|
||||
use super::wait_response;
|
||||
|
||||
#[test]
|
||||
fn missing_and_unknown_tool_result_formats_preserve_plain_json() {
|
||||
let value = serde_json::json!({"content": [], "structuredContent": {}});
|
||||
for result_format in [0, i32::MAX] {
|
||||
let reply = proto::ToolCallSucceeded {
|
||||
output_json: serde_json::to_vec(&value).unwrap(),
|
||||
result_format,
|
||||
};
|
||||
assert_eq!(
|
||||
tool_result(&reply).unwrap(),
|
||||
CodeModeToolResult::Json(value.clone())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_names_unknown_tool_kinds_and_invalid_json_schemas() {
|
||||
let definition = proto::ToolDefinition {
|
||||
|
||||
@@ -193,11 +193,9 @@ impl GrpcCodeModeHost {
|
||||
let session = self.state.session(&request.session_id)?;
|
||||
let invocation_id = validation::uuid(&request.invocation_id, "tool invocation ID")?;
|
||||
let result = match request.outcome {
|
||||
Some(proto::complete_tool_call_request::Outcome::Succeeded(result)) => Ok(
|
||||
serde_json::from_slice(&result.output_json).map_err(|error| {
|
||||
Status::invalid_argument(format!("invalid code-mode tool output JSON: {error}"))
|
||||
})?,
|
||||
),
|
||||
Some(proto::complete_tool_call_request::Outcome::Succeeded(result)) => {
|
||||
Ok(conversions::tool_result(&result)?)
|
||||
}
|
||||
Some(proto::complete_tool_call_request::Outcome::Failed(error)) => Err(error.message),
|
||||
None => {
|
||||
return Err(Status::invalid_argument(
|
||||
|
||||
@@ -72,6 +72,7 @@ async fn rejects_oversized_identifiers_and_subscription_filters() {
|
||||
outcome: Some(proto::complete_tool_call_request::Outcome::Succeeded(
|
||||
proto::ToolCallSucceeded {
|
||||
output_json: b"null".to_vec(),
|
||||
..Default::default()
|
||||
},
|
||||
)),
|
||||
}))
|
||||
|
||||
@@ -2,13 +2,13 @@ use std::sync::Arc;
|
||||
use std::sync::PoisonError;
|
||||
|
||||
use codex_code_mode_protocol::CodeModeNestedToolCall;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::grpc as proto;
|
||||
use codex_code_mode_protocol::host::MAX_FRAME_BYTES;
|
||||
use codex_protocol::ToolName;
|
||||
use futures::StreamExt;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use prost::Message;
|
||||
use serde_json::Value as JsonValue;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio_stream::wrappers::ReceiverStream;
|
||||
@@ -92,7 +92,7 @@ impl GrpcSession {
|
||||
execution_id: String,
|
||||
invocation_id: Uuid,
|
||||
input_json: Option<Vec<u8>>,
|
||||
response: oneshot::Sender<Result<JsonValue, String>>,
|
||||
response: oneshot::Sender<Result<CodeModeToolResult, String>>,
|
||||
cancellation: &CancellationToken,
|
||||
) -> Result<(), String> {
|
||||
let cell_id = invocation.cell_id.to_string();
|
||||
@@ -215,7 +215,7 @@ impl GrpcSession {
|
||||
pub(super) fn complete_invocation(
|
||||
&self,
|
||||
invocation_id: Uuid,
|
||||
result: Result<JsonValue, String>,
|
||||
result: Result<CodeModeToolResult, String>,
|
||||
) -> Result<(), Status> {
|
||||
let response = {
|
||||
let mut state = self.state.lock().unwrap_or_else(PoisonError::into_inner);
|
||||
|
||||
@@ -361,6 +361,7 @@ async fn filtered_subscriptions_match_default_namespace_aliases() {
|
||||
outcome: Some(proto::complete_tool_call_request::Outcome::Succeeded(
|
||||
proto::ToolCallSucceeded {
|
||||
output_json: br#""done""#.to_vec(),
|
||||
..Default::default()
|
||||
},
|
||||
)),
|
||||
}))
|
||||
@@ -434,6 +435,7 @@ async fn filtered_subscriptions_receive_ordered_calls_and_unary_completions() {
|
||||
outcome: Some(proto::complete_tool_call_request::Outcome::Succeeded(
|
||||
proto::ToolCallSucceeded {
|
||||
output_json: format!(r#""result-{sequence}""#).into_bytes(),
|
||||
..Default::default()
|
||||
},
|
||||
)),
|
||||
}))
|
||||
|
||||
@@ -13,13 +13,13 @@ use codex_code_mode_protocol::CellId;
|
||||
use codex_code_mode_protocol::CodeModeOutputTruncation;
|
||||
use codex_code_mode_protocol::CodeModeSession;
|
||||
use codex_code_mode_protocol::CodeModeSessionOptions;
|
||||
use codex_code_mode_protocol::CodeModeToolResult;
|
||||
use codex_code_mode_protocol::TerminateRequest;
|
||||
use codex_code_mode_protocol::WaitOutcome;
|
||||
use codex_code_mode_protocol::grpc as proto;
|
||||
use codex_code_mode_protocol::host::MAX_PENDING_DELEGATE_CALLS;
|
||||
use codex_code_mode_runtime::InProcessCodeModeSession;
|
||||
use codex_code_mode_runtime::ToolPreload;
|
||||
use serde_json::Value as JsonValue;
|
||||
use tokio::sync::Notify;
|
||||
use tokio::sync::OwnedSemaphorePermit;
|
||||
use tokio::sync::Semaphore;
|
||||
@@ -91,7 +91,7 @@ pub(super) struct ToolSubscription {
|
||||
|
||||
pub(super) struct PendingInvocation {
|
||||
pub(super) subscription_id: Uuid,
|
||||
pub(super) response: oneshot::Sender<Result<JsonValue, String>>,
|
||||
pub(super) response: oneshot::Sender<Result<CodeModeToolResult, String>>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
|
||||
Reference in New Issue
Block a user