Skip to main content

chant/mcp/
mod.rs

1//! Model Context Protocol (MCP) server implementation.
2//!
3//! # Doc Audit
4//! - audited: 2026-01-25
5//! - docs: reference/mcp.md
6//! - ignore: false
7
8pub mod handlers;
9pub mod protocol;
10pub mod server;
11pub mod tools;
12
13pub use handlers::{handle_method, handle_notification};
14pub use server::run_server;
15
16#[cfg(test)]
17mod tests {
18    use super::handlers::{handle_method, handle_notification};
19    use super::protocol::{JsonRpcResponse, PROTOCOL_VERSION, SERVER_NAME};
20    use serde_json::json;
21
22    #[test]
23    fn test_handle_initialize() {
24        let result = handle_method("initialize", None).unwrap();
25        assert_eq!(result["protocolVersion"], PROTOCOL_VERSION);
26        assert_eq!(result["serverInfo"]["name"], SERVER_NAME);
27    }
28
29    #[test]
30    fn test_handle_tools_list() {
31        let result = handle_method("tools/list", None).unwrap();
32        let tools = result["tools"].as_array().unwrap();
33        assert_eq!(tools.len(), 23);
34        // Query tools (8)
35        assert_eq!(tools[0]["name"], "chant_spec_list");
36        assert_eq!(tools[1]["name"], "chant_spec_get");
37        assert_eq!(tools[2]["name"], "chant_ready");
38        assert_eq!(tools[3]["name"], "chant_status");
39        assert_eq!(tools[4]["name"], "chant_log");
40        assert_eq!(tools[5]["name"], "chant_search");
41        assert_eq!(tools[6]["name"], "chant_diagnose");
42        assert_eq!(tools[7]["name"], "chant_lint");
43        // Mutating tools (11)
44        assert_eq!(tools[8]["name"], "chant_spec_update");
45        assert_eq!(tools[9]["name"], "chant_add");
46        assert_eq!(tools[10]["name"], "chant_finalize");
47        assert_eq!(tools[11]["name"], "chant_reset");
48        assert_eq!(tools[12]["name"], "chant_cancel");
49        assert_eq!(tools[13]["name"], "chant_archive");
50        assert_eq!(tools[14]["name"], "chant_verify");
51        assert_eq!(tools[15]["name"], "chant_work_start");
52        assert_eq!(tools[16]["name"], "chant_work_list");
53        assert_eq!(tools[17]["name"], "chant_pause");
54        assert_eq!(tools[18]["name"], "chant_takeover");
55    }
56
57    #[test]
58    fn test_json_rpc_response_success() {
59        let resp = JsonRpcResponse::success(json!(1), json!({"test": true}));
60        assert_eq!(resp.jsonrpc, "2.0");
61        assert!(resp.result.is_some());
62        assert!(resp.error.is_none());
63    }
64
65    #[test]
66    fn test_json_rpc_response_error() {
67        let resp = JsonRpcResponse::error(json!(1), -32600, "Invalid request");
68        assert_eq!(resp.jsonrpc, "2.0");
69        assert!(resp.result.is_none());
70        assert!(resp.error.is_some());
71        assert_eq!(resp.error.as_ref().unwrap().code, -32600);
72    }
73
74    #[test]
75    fn test_chant_status_schema_has_brief_and_activity() {
76        let result = handle_method("tools/list", None).unwrap();
77        let tools = result["tools"].as_array().unwrap();
78        let status_tool = tools.iter().find(|t| t["name"] == "chant_status").unwrap();
79
80        let props = &status_tool["inputSchema"]["properties"];
81        assert!(
82            props.get("brief").is_some(),
83            "chant_status should have 'brief' property"
84        );
85        assert!(
86            props.get("include_activity").is_some(),
87            "chant_status should have 'include_activity' property"
88        );
89
90        // Check descriptions
91        assert!(props["brief"]["description"]
92            .as_str()
93            .unwrap()
94            .contains("single-line"));
95        assert!(props["include_activity"]["description"]
96            .as_str()
97            .unwrap()
98            .contains("activity"));
99    }
100
101    #[test]
102    fn test_chant_ready_has_limit_param() {
103        let result = handle_method("tools/list", None).unwrap();
104        let tools = result["tools"].as_array().unwrap();
105        let ready_tool = tools.iter().find(|t| t["name"] == "chant_ready").unwrap();
106
107        let props = &ready_tool["inputSchema"]["properties"];
108        assert!(
109            props.get("limit").is_some(),
110            "chant_ready should have 'limit' property"
111        );
112        assert_eq!(props["limit"]["type"], "integer");
113        assert!(props["limit"]["description"]
114            .as_str()
115            .unwrap()
116            .contains("50"));
117    }
118
119    #[test]
120    fn test_chant_spec_list_has_limit_param() {
121        let result = handle_method("tools/list", None).unwrap();
122        let tools = result["tools"].as_array().unwrap();
123        let list_tool = tools
124            .iter()
125            .find(|t| t["name"] == "chant_spec_list")
126            .unwrap();
127
128        let props = &list_tool["inputSchema"]["properties"];
129        assert!(
130            props.get("limit").is_some(),
131            "chant_spec_list should have 'limit' property"
132        );
133        assert_eq!(props["limit"]["type"], "integer");
134        assert!(props["limit"]["description"]
135            .as_str()
136            .unwrap()
137            .contains("50"));
138    }
139
140    #[test]
141    fn test_handle_notification() {
142        // Should not panic
143        handle_notification("notifications/initialized", None);
144        handle_notification("unknown_notification", None);
145    }
146}