Preserve MCP tool output as content items (#40737)

## What changed

- Convert unstructured MCP results into typed function-call output items instead of serializing the entire content array as a text string.
- Keep structured MCP results as serialized text and preserve media, encrypted content, and unknown content through their existing item conversions.
- Drop empty text items during output truncation so they do not consume API array slots.

## Testing

- Cover text-only, mixed unstructured, structured, and image-sanitized MCP results.
- Verify empty text items are discarded under byte- and token-based truncation policies.

GitOrigin-RevId: fa1c5b7dee6f003a094265379dcabdd060386a48
This commit is contained in:
pakrym-oai authored and copyberry committed 2026-08-25 23:42:50 +00:00
1 parent 62aacbb2c9
commit 75cb7c903d
12 files changed
+288 -104

No files matched your search

+16 -22
View File
@@ -108,29 +108,23 @@ fn mcp_tool_output_response_item_includes_wall_time() {
},
);
match response {
ResponseInputItem::FunctionCallOutput { call_id, output } => {
assert_eq!(call_id, "mcp-call-1");
assert_eq!(output.success, Some(true));
let Some(text) = output.body.to_text() else {
panic!("MCP output should serialize as text");
};
let Some(payload) = text.strip_prefix("Wall time: 1.2500 seconds\nOutput:\n") else {
panic!("MCP output should include wall-time header: {text}");
};
let parsed: serde_json::Value = serde_json::from_str(payload).unwrap_or_else(|err| {
panic!("MCP output should serialize JSON content: {err}");
});
assert_eq!(
parsed,
json!([{
"type": "text",
"text": "done",
}])
);
assert_eq!(
response,
ResponseInputItem::FunctionCallOutput {
call_id: "mcp-call-1".to_string(),
output: FunctionCallOutputPayload {
body: FunctionCallOutputBody::ContentItems(vec![
FunctionCallOutputContentItem::InputText {
text: "Wall time: 1.2500 seconds\nOutput:".to_string(),
},
FunctionCallOutputContentItem::InputText {
text: "done".to_string(),
},
]),
success: Some(true),
},
}
other => panic!("expected FunctionCallOutput, got {other:?}"),
}
);
}
#[test]