Skip to main content

mcp_stdio_client/
lib.rs

1//! MCP Stdio Client Library
2//!
3//! Tier: T3 (Domain-Specific Logic)
4
5#![forbid(unsafe_code)]
6#![warn(missing_docs)]
7#![cfg_attr(
8    not(test),
9    deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)
10)]
11
12use serde_json::Value;
13
14pub fn summarize(value: &Value) -> String {
15    if let Some(result) = value.get("result") {
16        if let Some(info) = result.get("serverInfo") {
17            return info.to_string();
18        }
19
20        return result.to_string();
21    }
22
23    value.to_string()
24}
25
26pub fn format_output(value: &Value, raw: bool) -> String {
27    if raw {
28        return value.to_string();
29    }
30
31    if let Some(result) = value.get("result") {
32        if let Some(content) = result.get("content") {
33            return content.to_string();
34        }
35
36        return result.to_string();
37    }
38
39    value.to_string()
40}
41
42#[cfg(test)]
43
44mod tests {
45
46    use super::*;
47
48    use serde_json::json;
49
50    #[test]
51
52    fn test_summarize_server_info() {
53        let val = json!({"result": {"serverInfo": {"name": "test", "version": "1"}}});
54
55        assert!(summarize(&val).contains("test"));
56    }
57
58    #[test]
59
60    fn test_summarize_no_result() {
61        let val = json!({"error": "oops"});
62
63        assert_eq!(summarize(&val), val.to_string());
64    }
65
66    #[test]
67
68    fn test_format_output_raw() {
69        let val = json!({"a": 1});
70
71        assert_eq!(format_output(&val, true), val.to_string());
72    }
73
74    #[test]
75
76    fn test_format_output_content() {
77        let val = json!({"result": {"content": "hello"}});
78
79        assert_eq!(format_output(&val, false), "\"hello\"");
80    }
81}