Skip to main content

oxicode_agent/mcp/
content.rs

1//! MCP content transformation.
2//!
3//! Converts MCP server content types into plain text suitable for
4//! inclusion in agent tool results.
5
6use super::types::McpContent;
7
8/// Transform MCP content blocks into a single text string.
9///
10/// Text content is passed through, images and resources are formatted
11/// as descriptive text markers.
12pub fn transform_mcp_content(content: &[McpContent]) -> String {
13    let mut parts = Vec::new();
14
15    for item in content {
16        match item {
17            McpContent::Text { text } => {
18                parts.push(text.clone());
19            }
20            McpContent::Image { mime_type, .. } => {
21                parts.push(format!(
22                    "[Image content: {}]",
23                    mime_type.as_deref().unwrap_or("image/*")
24                ));
25            }
26            McpContent::Resource { resource } => {
27                let uri = &resource.uri;
28                if let Some(text) = &resource.text {
29                    parts.push(format!("[Resource: {uri}]\n{text}"));
30                } else if resource.blob.is_some() {
31                    parts.push(format!("[Resource: {uri}] (binary)"));
32                } else {
33                    parts.push(format!("[Resource: {uri}] (empty)"));
34                }
35            }
36        }
37    }
38
39    if parts.is_empty() {
40        "(empty result)".to_string()
41    } else {
42        parts.join("\n")
43    }
44}