Skip to main content

do_memory_mcp/protocol/
mod.rs

1//! MCP Protocol handlers
2//!
3//! This module contains core MCP protocol handlers:
4//! - handle_initialize: Initialize request handler
5//! - handle_list_tools: List available tools
6//! - handle_shutdown: Shutdown the server
7//!
8//! These handlers are used by both the library and binary crate.
9
10mod handlers;
11mod types;
12
13pub use handlers::*;
14pub use types::*;
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use crate::jsonrpc::JsonRpcRequest;
20    use serde_json::json;
21
22    #[test]
23    fn test_supported_versions() {
24        assert_eq!(SUPPORTED_VERSIONS, &["2025-11-25", "2024-11-05"]);
25    }
26
27    #[test]
28    fn test_oauth_config_default() {
29        let config = OAuthConfig::default();
30        assert!(!config.enabled);
31        assert!(config.audience.is_none());
32        assert!(config.issuer.is_none());
33        assert_eq!(config.scopes.len(), 2);
34    }
35
36    #[tokio::test]
37    async fn test_initialize_protocol_version_latest() {
38        let req = JsonRpcRequest {
39            jsonrpc: Some("2.0".to_string()),
40            id: Some(json!(1)),
41            method: "initialize".into(),
42            params: Some(json!({
43                "protocolVersion": "2025-11-25",
44                "capabilities": {},
45                "clientInfo": {"name": "test", "version": "1.0"}
46            })),
47        };
48
49        let resp = handle_initialize(req, &OAuthConfig::default()).await;
50        assert!(resp.is_some());
51        let resp = resp.unwrap();
52        assert!(resp.result.is_some());
53
54        let result = resp.result.unwrap();
55        let protocol_version = result.get("protocolVersion").and_then(|v| v.as_str());
56        assert_eq!(protocol_version, Some("2025-11-25"));
57    }
58
59    #[tokio::test]
60    async fn test_initialize_protocol_version_older() {
61        let req = JsonRpcRequest {
62            jsonrpc: Some("2.0".to_string()),
63            id: Some(json!(2)),
64            method: "initialize".into(),
65            params: Some(json!({
66                "protocolVersion": "2024-11-05",
67                "capabilities": {},
68                "clientInfo": {"name": "test", "version": "1.0"}
69            })),
70        };
71
72        let resp = handle_initialize(req, &OAuthConfig::default()).await;
73        assert!(resp.is_some());
74        let resp = resp.unwrap();
75        assert!(resp.result.is_some());
76
77        let result = resp.result.unwrap();
78        let protocol_version = result.get("protocolVersion").and_then(|v| v.as_str());
79        assert_eq!(protocol_version, Some("2024-11-05"));
80    }
81
82    #[tokio::test]
83    async fn test_initialize_protocol_version_unsupported() {
84        let req = JsonRpcRequest {
85            jsonrpc: Some("2.0".to_string()),
86            id: Some(json!(3)),
87            method: "initialize".into(),
88            params: Some(json!({
89                "protocolVersion": "2020-01-01",
90                "capabilities": {},
91                "clientInfo": {"name": "test", "version": "1.0"}
92            })),
93        };
94
95        let resp = handle_initialize(req, &OAuthConfig::default()).await;
96        assert!(resp.is_some());
97        let resp = resp.unwrap();
98        assert!(resp.result.is_some());
99
100        let result = resp.result.unwrap();
101        let protocol_version = result.get("protocolVersion").and_then(|v| v.as_str());
102        // Should return latest supported version
103        assert_eq!(protocol_version, Some("2025-11-25"));
104    }
105
106    #[tokio::test]
107    async fn test_initialize_no_version() {
108        let req = JsonRpcRequest {
109            jsonrpc: Some("2.0".to_string()),
110            id: Some(json!(4)),
111            method: "initialize".into(),
112            params: Some(json!({
113                "capabilities": {},
114                "clientInfo": {"name": "test", "version": "1.0"}
115            })),
116        };
117
118        let resp = handle_initialize(req, &OAuthConfig::default()).await;
119        assert!(resp.is_some());
120        let resp = resp.unwrap();
121        assert!(resp.result.is_some());
122
123        let result = resp.result.unwrap();
124        let protocol_version = result.get("protocolVersion").and_then(|v| v.as_str());
125        assert_eq!(protocol_version, Some("2025-11-25"));
126    }
127}