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(), 21);
34        // Query tools (7)
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        // Mutating tools (11)
43        assert_eq!(tools[7]["name"], "chant_spec_update");
44        assert_eq!(tools[8]["name"], "chant_add");
45        assert_eq!(tools[9]["name"], "chant_finalize");
46        assert_eq!(tools[10]["name"], "chant_resume");
47        assert_eq!(tools[11]["name"], "chant_cancel");
48        assert_eq!(tools[12]["name"], "chant_archive");
49        assert_eq!(tools[13]["name"], "chant_verify");
50        assert_eq!(tools[14]["name"], "chant_work_start");
51        assert_eq!(tools[15]["name"], "chant_work_list");
52        assert_eq!(tools[16]["name"], "chant_pause");
53        assert_eq!(tools[17]["name"], "chant_takeover");
54    }
55
56    #[test]
57    fn test_json_rpc_response_success() {
58        let resp = JsonRpcResponse::success(json!(1), json!({"test": true}));
59        assert_eq!(resp.jsonrpc, "2.0");
60        assert!(resp.result.is_some());
61        assert!(resp.error.is_none());
62    }
63
64    #[test]
65    fn test_json_rpc_response_error() {
66        let resp = JsonRpcResponse::error(json!(1), -32600, "Invalid request");
67        assert_eq!(resp.jsonrpc, "2.0");
68        assert!(resp.result.is_none());
69        assert!(resp.error.is_some());
70        assert_eq!(resp.error.as_ref().unwrap().code, -32600);
71    }
72
73    #[test]
74    fn test_chant_status_schema_has_brief_and_activity() {
75        let result = handle_method("tools/list", None).unwrap();
76        let tools = result["tools"].as_array().unwrap();
77        let status_tool = tools.iter().find(|t| t["name"] == "chant_status").unwrap();
78
79        let props = &status_tool["inputSchema"]["properties"];
80        assert!(
81            props.get("brief").is_some(),
82            "chant_status should have 'brief' property"
83        );
84        assert!(
85            props.get("include_activity").is_some(),
86            "chant_status should have 'include_activity' property"
87        );
88
89        // Check descriptions
90        assert!(props["brief"]["description"]
91            .as_str()
92            .unwrap()
93            .contains("single-line"));
94        assert!(props["include_activity"]["description"]
95            .as_str()
96            .unwrap()
97            .contains("activity"));
98    }
99
100    #[test]
101    fn test_chant_ready_has_limit_param() {
102        let result = handle_method("tools/list", None).unwrap();
103        let tools = result["tools"].as_array().unwrap();
104        let ready_tool = tools.iter().find(|t| t["name"] == "chant_ready").unwrap();
105
106        let props = &ready_tool["inputSchema"]["properties"];
107        assert!(
108            props.get("limit").is_some(),
109            "chant_ready should have 'limit' property"
110        );
111        assert_eq!(props["limit"]["type"], "integer");
112        assert!(props["limit"]["description"]
113            .as_str()
114            .unwrap()
115            .contains("50"));
116    }
117
118    #[test]
119    fn test_chant_spec_list_has_limit_param() {
120        let result = handle_method("tools/list", None).unwrap();
121        let tools = result["tools"].as_array().unwrap();
122        let list_tool = tools
123            .iter()
124            .find(|t| t["name"] == "chant_spec_list")
125            .unwrap();
126
127        let props = &list_tool["inputSchema"]["properties"];
128        assert!(
129            props.get("limit").is_some(),
130            "chant_spec_list should have 'limit' property"
131        );
132        assert_eq!(props["limit"]["type"], "integer");
133        assert!(props["limit"]["description"]
134            .as_str()
135            .unwrap()
136            .contains("50"));
137    }
138
139    #[test]
140    fn test_handle_notification() {
141        // Should not panic
142        handle_notification("notifications/initialized", None);
143        handle_notification("unknown_notification", None);
144    }
145}