Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions rust/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ pub fn convert_mcp_call_tool_result(value: &serde_json::Value) -> Option<ToolRes
let mime_type = resource
.get("mimeType")
.and_then(serde_json::Value::as_str)
.filter(|s| !s.is_empty())
.unwrap_or("application/octet-stream");
let description = resource
.get("uri")
Expand Down Expand Up @@ -523,12 +524,130 @@ mod tests {
.expect("binary results should be captured");
assert_eq!(binary_results.len(), 2);
assert_eq!(binary_results[0].r#type, "image");
assert_eq!(binary_results[0].data, "aW1n");
assert_eq!(binary_results[0].mime_type, "image/png");
assert_eq!(
binary_results[1].description.as_deref(),
Some("file:///tmp/data.bin")
);
}

#[test]
fn convert_mcp_call_tool_result_converts_image_content() {
let result = convert_mcp_call_tool_result(&serde_json::json!({
"content": [
{ "type": "image", "data": "aW1hZ2U=", "mimeType": "image/jpeg" }
]
}))
.expect("valid CallToolResult should convert");

let ToolResult::Expanded(expanded) = result else {
panic!("expected expanded tool result");
};

assert_eq!(expanded.text_result_for_llm, "");
assert_eq!(expanded.result_type, "success");
let binary_results = expanded
.binary_results_for_llm
.expect("image result should be captured");
assert_eq!(binary_results.len(), 1);
assert_eq!(binary_results[0].data, "aW1hZ2U=");
assert_eq!(binary_results[0].mime_type, "image/jpeg");
assert_eq!(binary_results[0].r#type, "image");
assert!(binary_results[0].description.is_none());
}

#[test]
fn convert_mcp_call_tool_result_converts_resource_blob_content() {
let result = convert_mcp_call_tool_result(&serde_json::json!({
"content": [
{
"type": "resource",
"resource": {
"uri": "file:///tmp/report.pdf",
"blob": "cGRm",
"mimeType": "application/pdf"
}
}
]
}))
.expect("valid CallToolResult should convert");

let ToolResult::Expanded(expanded) = result else {
panic!("expected expanded tool result");
};

let binary_results = expanded
.binary_results_for_llm
.expect("resource result should be captured");
assert_eq!(binary_results.len(), 1);
assert_eq!(binary_results[0].data, "cGRm");
assert_eq!(binary_results[0].mime_type, "application/pdf");
assert_eq!(binary_results[0].r#type, "resource");
assert_eq!(
binary_results[0].description.as_deref(),
Some("file:///tmp/report.pdf")
);
}

#[test]
fn convert_mcp_call_tool_result_defaults_resource_blob_mime_type() {
let result = convert_mcp_call_tool_result(&serde_json::json!({
"content": [
{
"type": "resource",
"resource": {
"uri": "file:///tmp/data.bin",
"blob": "Ymlu"
}
},
{
"type": "resource",
"resource": {
"blob": "YmluMg==",
"mimeType": ""
}
}
]
}))
.expect("valid CallToolResult should convert");

let ToolResult::Expanded(expanded) = result else {
panic!("expected expanded tool result");
};

let binary_results = expanded
.binary_results_for_llm
.expect("resource blobs should be captured");
assert_eq!(binary_results.len(), 2);
assert_eq!(binary_results[0].mime_type, "application/octet-stream");
assert_eq!(binary_results[1].mime_type, "application/octet-stream");
}

#[test]
fn convert_mcp_call_tool_result_omits_binary_results_without_binary_content() {
let result = convert_mcp_call_tool_result(&serde_json::json!({
"content": [
{ "type": "text", "text": "hello" },
{
"type": "resource",
"resource": {
"uri": "file:///tmp/readme.md",
"text": "resource text"
}
}
]
}))
.expect("valid CallToolResult should convert");

let ToolResult::Expanded(expanded) = result else {
panic!("expected expanded tool result");
};

assert_eq!(expanded.text_result_for_llm, "hello\nresource text");
assert!(expanded.binary_results_for_llm.is_none());
}

#[tokio::test]
async fn tool_handler_call_returns_result() {
let tool = EchoTool;
Expand Down
61 changes: 60 additions & 1 deletion rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,8 @@ mod tests {
Attachment, AttachmentLineRange, AttachmentSelectionPosition, AttachmentSelectionRange,
ConnectionState, CustomAgentConfig, DeliveryMode, GitHubReferenceType,
InfiniteSessionConfig, ProviderConfig, ResumeSessionConfig, SessionConfig, SessionEvent,
SessionId, SystemMessageConfig, Tool, ensure_attachment_display_names,
SessionId, SystemMessageConfig, Tool, ToolBinaryResult, ToolResult, ToolResultExpanded,
ToolResultResponse, ensure_attachment_display_names,
};
use crate::generated::session_events::TypedSessionEvent;

Expand Down Expand Up @@ -3035,6 +3036,64 @@ mod tests {
assert!(tool.parameters.is_empty());
}

#[test]
fn tool_result_expanded_serializes_binary_results_for_llm() {
let response = ToolResultResponse {
result: ToolResult::Expanded(ToolResultExpanded {
text_result_for_llm: "rendered chart".to_string(),
result_type: "success".to_string(),
binary_results_for_llm: Some(vec![ToolBinaryResult {
data: "aW1n".to_string(),
mime_type: "image/png".to_string(),
r#type: "image".to_string(),
description: Some("chart preview".to_string()),
}]),
session_log: None,
error: None,
tool_telemetry: None,
}),
};

let wire = serde_json::to_value(&response).unwrap();

assert_eq!(
wire,
json!({
"result": {
"textResultForLlm": "rendered chart",
"resultType": "success",
"binaryResultsForLlm": [
{
"data": "aW1n",
"mimeType": "image/png",
"type": "image",
"description": "chart preview"
}
]
}
})
);
}

#[test]
fn tool_result_expanded_omits_binary_results_for_llm_when_none() {
let response = ToolResultResponse {
result: ToolResult::Expanded(ToolResultExpanded {
text_result_for_llm: "ok".to_string(),
result_type: "success".to_string(),
binary_results_for_llm: None,
session_log: None,
error: None,
tool_telemetry: None,
}),
};

let wire = serde_json::to_value(&response).unwrap();

assert_eq!(wire["result"]["textResultForLlm"], "ok");
assert!(wire["result"].get("binaryResultsForLlm").is_none());
}

#[test]
fn session_config_default_enables_permission_flow_flags() {
let cfg = SessionConfig::default();
Expand Down
Loading