1use graphrt_core::tool::{ToolOutput, ToolSpec};
4use graphrt_core::value::Value;
5use rmcp::model::{CallToolResult, ContentBlock, ResourceContents, Tool};
6
7pub fn tool_spec(tool: &Tool) -> ToolSpec {
9 let description = tool.description.as_deref().or(tool.title.as_deref()).unwrap_or_default().to_owned();
10 ToolSpec {
11 name: tool.name.to_string(),
12 description,
13 parameters: serde_json::Value::Object((*tool.input_schema).clone()),
14 }
15}
16
17pub fn tool_output(result: CallToolResult) -> ToolOutput {
25 let mut texts: Vec<String> = Vec::with_capacity(result.content.len());
26 let mut has_binary = false;
27 for block in &result.content {
28 match block {
29 ContentBlock::Text(t) => texts.push(t.text.clone()),
30 ContentBlock::Image(img) => {
31 has_binary = true;
32 texts.push(format!("[image: {}]", img.mime_type));
33 }
34 ContentBlock::Audio(audio) => {
35 has_binary = true;
36 texts.push(format!("[audio: {}]", audio.mime_type));
37 }
38 ContentBlock::Resource(res) => match &res.resource {
39 ResourceContents::TextResourceContents { text, .. } => texts.push(text.clone()),
40 ResourceContents::BlobResourceContents { uri, .. } => {
41 has_binary = true;
42 texts.push(format!("[resource: {uri}]"));
43 }
44 _ => has_binary = true,
45 },
46 ContentBlock::ResourceLink(link) => {
47 has_binary = true;
48 texts.push(format!("[resource: {}]", link.uri));
49 }
50 _ => has_binary = true,
51 }
52 }
53 let data = match result.structured_content {
54 Some(structured) => Value::from_json(structured),
55 None if has_binary => serde_json::to_value(&result.content).map(Value::from_json).unwrap_or(Value::Null),
56 None => Value::Null,
57 };
58 ToolOutput { text: texts.join("\n"), data, is_error: result.is_error.unwrap_or(false) }
59}
60
61#[cfg(test)]
62mod tests {
63 use std::sync::Arc;
64
65 use super::*;
66
67 #[test]
68 fn maps_text_blocks() {
69 let r = CallToolResult::success(vec![ContentBlock::text("a"), ContentBlock::text("b")]);
70 let out = tool_output(r);
71 assert_eq!(out.text, "a\nb");
72 assert!(out.data.is_null());
73 assert!(!out.is_error);
74 }
75
76 #[test]
77 fn maps_images_and_structured_content() {
78 let r = CallToolResult::success(vec![ContentBlock::image("AAAA", "image/png")]);
79 let out = tool_output(r);
80 assert_eq!(out.text, "[image: image/png]");
81 let first = out.data.get_path(&["0", "mimeType"]);
82 assert_eq!(first, Some(Value::from("image/png")));
83
84 let mut r = CallToolResult::success(vec![ContentBlock::text("{}")]);
85 r.structured_content = Some(serde_json::json!({"temp": 21}));
86 let out = tool_output(r);
87 assert_eq!(out.data.get_path(&["temp"]), Some(Value::Int(21)));
88 }
89
90 #[test]
91 fn maps_error_flag() {
92 let out = tool_output(CallToolResult::error(vec![ContentBlock::text("bad")]));
93 assert!(out.is_error);
94 assert_eq!(out.text, "bad");
95 }
96
97 #[test]
98 fn maps_tool_spec() {
99 let schema = serde_json::json!({"type": "object", "properties": {"q": {"type": "string"}}});
100 let serde_json::Value::Object(map) = schema.clone() else { panic!("object") };
101 let tool = Tool::new("search", "Search the web", Arc::new(map));
102 let spec = tool_spec(&tool);
103 assert_eq!(spec.name, "search");
104 assert_eq!(spec.description, "Search the web");
105 assert_eq!(spec.parameters, schema);
106 }
107}