Preserve encrypted content in MCP tool outputs (#33509)

## What changed

- Convert MCP text content marked with `_meta["codex/encryptedContent"] = true`
  into `encrypted_content` function call output items.
- Prefer the content-item payload whenever encrypted content is present so that
  `structured_content` does not replace the encrypted output.

## Testing

- Add a stdio MCP integration test that round-trips mixed plaintext and
  encrypted tool output.

GitOrigin-RevId: c3997a16b06ad38ff22b21016ede35b97974d9ec
This commit is contained in:
rka-oai
2026-07-16 05:38:36 +00:00
committed by copyberry
parent 1d941253e9
commit cbc83d961e
3 changed files with 157 additions and 7 deletions
+36 -7
View File
@@ -1901,6 +1901,18 @@ impl CallToolResult {
}
pub fn as_function_call_output_payload(&self) -> FunctionCallOutputPayload {
let content_items = convert_mcp_content_to_items(&self.content);
if content_items.as_ref().is_some_and(|items| {
items
.iter()
.any(|item| matches!(item, FunctionCallOutputContentItem::EncryptedContent { .. }))
}) {
return FunctionCallOutputPayload {
body: FunctionCallOutputBody::ContentItems(content_items.unwrap_or_default()),
success: Some(self.success()),
};
}
if let Some(structured_content) = &self.structured_content
&& !structured_content.is_null()
{
@@ -1930,8 +1942,6 @@ impl CallToolResult {
}
};
let content_items = convert_mcp_content_to_items(&self.content);
let body = match content_items {
Some(content_items) => FunctionCallOutputBody::ContentItems(content_items),
None => FunctionCallOutputBody::Text(serialized_content),
@@ -1951,13 +1961,18 @@ impl CallToolResult {
fn convert_mcp_content_to_items(
contents: &[serde_json::Value],
) -> Option<Vec<FunctionCallOutputContentItem>> {
const CODEX_ENCRYPTED_CONTENT_META_KEY: &str = "codex/encryptedContent";
const CODEX_IMAGE_DETAIL_META_KEY: &str = "codex/imageDetail";
#[derive(serde::Deserialize)]
#[serde(tag = "type")]
enum McpContent {
#[serde(rename = "text")]
Text { text: String },
Text {
text: String,
#[serde(rename = "_meta", default)]
meta: Option<serde_json::Value>,
},
#[serde(rename = "image")]
Image {
data: String,
@@ -1970,18 +1985,32 @@ fn convert_mcp_content_to_items(
Unknown,
}
let mut saw_image = false;
let mut saw_content_item = false;
let mut items = Vec::with_capacity(contents.len());
for content in contents {
let item = match serde_json::from_value::<McpContent>(content.clone()) {
Ok(McpContent::Text { text }) => FunctionCallOutputContentItem::InputText { text },
Ok(McpContent::Text { text, meta }) => {
if meta
.as_ref()
.and_then(|meta| meta.get(CODEX_ENCRYPTED_CONTENT_META_KEY))
.and_then(serde_json::Value::as_bool)
== Some(true)
{
saw_content_item = true;
FunctionCallOutputContentItem::EncryptedContent {
encrypted_content: text,
}
} else {
FunctionCallOutputContentItem::InputText { text }
}
}
Ok(McpContent::Image {
data,
mime_type,
meta,
}) => {
saw_image = true;
saw_content_item = true;
let image_url = if data.starts_with("data:") {
data
} else {
@@ -2012,7 +2041,7 @@ fn convert_mcp_content_to_items(
items.push(item);
}
if saw_image { Some(items) } else { None }
if saw_content_item { Some(items) } else { None }
}
// Implement Display so callers can treat the payload like a plain string when logging or doing