mcprotocol_rs/protocol/
message.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::fmt;
4
5use super::{ClientCapabilities, ImplementationInfo, RequestId, ServerCapabilities};
6use crate::Result;
7
8/// Base JSON-RPC message
9/// 基础 JSON-RPC 消息
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(untagged)]
12pub enum Message {
13    Request(Request),
14    Response(Response),
15    Notification(Notification),
16}
17
18/// JSON-RPC request message
19/// JSON-RPC 请求消息
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct Request {
22    /// Protocol version (must be "2.0")
23    /// 协议版本(必须为 "2.0")
24    pub jsonrpc: String,
25    /// Request method
26    /// 请求方法
27    pub method: String,
28    /// Optional parameters
29    /// 可选参数
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub params: Option<Value>,
32    /// Request ID
33    /// 请求 ID
34    pub id: RequestId,
35}
36
37/// JSON-RPC response message
38/// JSON-RPC 响应消息
39#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct Response {
41    /// Protocol version (must be "2.0")
42    /// 协议版本(必须为 "2.0")
43    pub jsonrpc: String,
44    /// Request ID
45    /// 请求 ID
46    pub id: RequestId,
47    /// Response result
48    /// 响应结果
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub result: Option<Value>,
51    /// Error if any
52    /// 如果有错误
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub error: Option<ResponseError>,
55}
56
57/// JSON-RPC notification message (request without ID)
58/// JSON-RPC 通知消息(没有 ID 的请求)
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct Notification {
61    /// Protocol version (must be "2.0")
62    /// 协议版本(必须为 "2.0")
63    pub jsonrpc: String,
64    /// Notification method
65    /// 通知方法
66    pub method: String,
67    /// Optional parameters
68    /// 可选参数
69    #[serde(skip_serializing_if = "Option::is_none")]
70    pub params: Option<Value>,
71}
72
73/// Error response
74/// 错误响应
75#[derive(Debug, Clone, Serialize, Deserialize)]
76pub struct ResponseError {
77    /// Error code
78    /// 错误代码
79    pub code: i32,
80    /// Error message
81    /// 错误消息
82    pub message: String,
83    /// Additional error data
84    /// 附加错误数据
85    #[serde(skip_serializing_if = "Option::is_none")]
86    pub data: Option<Value>,
87}
88
89/// Standard error codes
90/// 标准错误代码
91pub mod error_codes {
92    pub const PARSE_ERROR: i32 = -32700;
93    pub const INVALID_REQUEST: i32 = -32600;
94    pub const METHOD_NOT_FOUND: i32 = -32601;
95    pub const INVALID_PARAMS: i32 = -32602;
96    pub const INTERNAL_ERROR: i32 = -32603;
97    pub const SERVER_NOT_INITIALIZED: i32 = -32002;
98    pub const UNKNOWN_ERROR_CODE: i32 = -32001;
99    pub const REQUEST_CANCELLED: i32 = -32800;
100}
101
102/// MCP method types
103/// MCP 方法类型
104#[derive(Debug, Clone, Serialize, Deserialize)]
105#[serde(rename_all = "camelCase")]
106pub enum Method {
107    // Lifecycle methods
108    // 生命周期方法
109    Initialize,
110    Initialized,
111    Shutdown,
112    Exit,
113
114    // Utility methods
115    // 实用方法
116    #[serde(rename = "notifications/cancelled")]
117    Cancel,
118    #[serde(rename = "ping")]
119    Ping,
120    #[serde(rename = "$/progress")]
121    Progress,
122
123    // Server feature methods
124    // 服务器功能方法
125    #[serde(rename = "prompts/list")]
126    ListPrompts,
127    #[serde(rename = "prompts/get")]
128    GetPrompt,
129    #[serde(rename = "prompts/execute")]
130    ExecutePrompt,
131
132    #[serde(rename = "resources/list")]
133    ListResources,
134    #[serde(rename = "resources/get")]
135    GetResource,
136    #[serde(rename = "resources/create")]
137    CreateResource,
138    #[serde(rename = "resources/update")]
139    UpdateResource,
140    #[serde(rename = "resources/delete")]
141    DeleteResource,
142    #[serde(rename = "resources/subscribe")]
143    SubscribeResource,
144    #[serde(rename = "resources/unsubscribe")]
145    UnsubscribeResource,
146
147    #[serde(rename = "tools/list")]
148    ListTools,
149    #[serde(rename = "tools/get")]
150    GetTool,
151    #[serde(rename = "tools/execute")]
152    ExecuteTool,
153    #[serde(rename = "tools/cancel")]
154    CancelTool,
155
156    // Client feature methods
157    // 客户端功能方法
158    #[serde(rename = "roots/list")]
159    ListRoots,
160    #[serde(rename = "roots/get")]
161    GetRoot,
162
163    #[serde(rename = "sampling/request")]
164    SamplingRequest,
165}
166
167impl Request {
168    /// Creates a new request
169    /// 创建一个新的请求
170    pub fn new(method: Method, params: Option<Value>, id: RequestId) -> Self {
171        // ID is guaranteed to be string or number by type system
172        // ID 已经通过类型系统保证是字符串或整数
173        // ID uniqueness should be checked at session level
174        // 在实际使用时,应该在会话级别检查 ID 的唯一性
175        Self {
176            jsonrpc: super::JSONRPC_VERSION.to_string(),
177            method: method.to_string(),
178            params,
179            id,
180        }
181    }
182
183    /// Validates that the request ID is unique within the given session
184    /// 验证请求 ID 在给定的会话中是唯一的
185    pub fn validate_id_uniqueness(&self, used_ids: &mut std::collections::HashSet<String>) -> bool {
186        let id_str = match &self.id {
187            RequestId::String(s) => s.clone(),
188            RequestId::Number(n) => n.to_string(),
189        };
190        used_ids.insert(id_str)
191    }
192}
193
194impl Response {
195    /// Creates a new successful response
196    /// 创建一个新的成功响应
197    pub fn success(result: Value, id: RequestId) -> Self {
198        Self {
199            jsonrpc: super::JSONRPC_VERSION.to_string(),
200            id,
201            result: Some(result),
202            error: None,
203        }
204    }
205
206    /// Creates a new error response
207    /// 创建一个新的错误响应
208    pub fn error(error: ResponseError, id: RequestId) -> Self {
209        Self {
210            jsonrpc: super::JSONRPC_VERSION.to_string(),
211            id,
212            result: None,
213            error: Some(error),
214        }
215    }
216}
217
218impl Notification {
219    /// Creates a new notification
220    /// 创建一个新的通知
221    pub fn new(method: Method, params: Option<Value>) -> Self {
222        Self {
223            jsonrpc: super::JSONRPC_VERSION.to_string(),
224            method: method.to_string(),
225            params,
226        }
227    }
228}
229
230impl fmt::Display for Method {
231    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232        match self {
233            Method::Initialize => write!(f, "initialize"),
234            Method::Initialized => write!(f, "initialized"),
235            Method::Shutdown => write!(f, "shutdown"),
236            Method::Exit => write!(f, "exit"),
237            Method::Cancel => write!(f, "notifications/cancelled"),
238            Method::Ping => write!(f, "ping"),
239            Method::Progress => write!(f, "$/progress"),
240            Method::ListPrompts => write!(f, "prompts/list"),
241            Method::GetPrompt => write!(f, "prompts/get"),
242            Method::ExecutePrompt => write!(f, "prompts/execute"),
243            Method::ListResources => write!(f, "resources/list"),
244            Method::GetResource => write!(f, "resources/get"),
245            Method::CreateResource => write!(f, "resources/create"),
246            Method::UpdateResource => write!(f, "resources/update"),
247            Method::DeleteResource => write!(f, "resources/delete"),
248            Method::SubscribeResource => write!(f, "resources/subscribe"),
249            Method::UnsubscribeResource => write!(f, "resources/unsubscribe"),
250            Method::ListTools => write!(f, "tools/list"),
251            Method::GetTool => write!(f, "tools/get"),
252            Method::ExecuteTool => write!(f, "tools/execute"),
253            Method::CancelTool => write!(f, "tools/cancel"),
254            Method::ListRoots => write!(f, "roots/list"),
255            Method::GetRoot => write!(f, "roots/get"),
256            Method::SamplingRequest => write!(f, "sampling/request"),
257        }
258    }
259}
260
261#[cfg(test)]
262mod tests {
263    use super::*;
264    use serde_json::json;
265    use std::collections::HashSet;
266
267    #[test]
268    fn test_request_id_must_be_string_or_integer() {
269        // test string id
270        // 测试字符串 ID
271        let string_id = RequestId::String("test-id".to_string());
272        let request = Request::new(Method::Initialize, None, string_id.clone());
273        assert!(matches!(request.id, RequestId::String(_)));
274
275        // test integer id
276        // 测试整数 ID
277        let integer_id = RequestId::Number(42);
278        let request = Request::new(Method::Initialize, None, integer_id.clone());
279        assert!(matches!(request.id, RequestId::Number(_)));
280    }
281
282    #[test]
283    fn test_request_id_uniqueness() {
284        let mut used_ids = HashSet::new();
285
286        // test string id uniqueness
287        // 测试字符串 ID 的唯一性
288        let id1 = RequestId::String("test-1".to_string());
289        let id2 = RequestId::String("test-1".to_string());
290
291        assert!(is_unique_id(&id1, &mut used_ids)); // First use should return true
292                                                    // 第一次使用应该返回 true
293        assert!(!is_unique_id(&id2, &mut used_ids)); // Repeated use should return false
294                                                     // 重复使用应该返回 false
295
296        // test integer id uniqueness
297        // 测试整数 ID 的唯一性
298        let id3 = RequestId::Number(1);
299        let id4 = RequestId::Number(1);
300
301        assert!(is_unique_id(&id3, &mut used_ids)); // First use should return true
302                                                    // 第一次使用应该返回 true
303        assert!(!is_unique_id(&id4, &mut used_ids)); // Repeated use should return false
304                                                     // 重复使用应该返回 false
305    }
306
307    // Helper function: Check if ID is unique
308    // 辅助函数:检查 ID 是否唯一
309    fn is_unique_id(id: &RequestId, used_ids: &mut HashSet<String>) -> bool {
310        let id_str = match id {
311            RequestId::String(s) => s.clone(),
312            RequestId::Number(n) => n.to_string(),
313        };
314        used_ids.insert(id_str)
315    }
316
317    #[test]
318    fn test_request_id_serialization() {
319        // test string id serialization
320        // 测试字符串 ID 的序列化
321        let string_id = RequestId::String("test-id".to_string());
322        let json = serde_json::to_string(&string_id).unwrap();
323        assert_eq!(json, r#""test-id""#);
324
325        // test integer id serialization
326        // 测试整数 ID 的序列化
327        let integer_id = RequestId::Number(42);
328        let json = serde_json::to_string(&integer_id).unwrap();
329        assert_eq!(json, "42");
330    }
331
332    #[test]
333    fn test_request_id_deserialization() {
334        // test string id deserialization
335        // 测试字符串 ID 的反序列化
336        let json = r#""test-id""#;
337        let id: RequestId = serde_json::from_str(json).unwrap();
338        assert!(matches!(id, RequestId::String(s) if s == "test-id"));
339
340        // test integer id deserialization
341        // 测试整数 ID 的反序列化
342        let json = "42";
343        let id: RequestId = serde_json::from_str(json).unwrap();
344        assert!(matches!(id, RequestId::Number(n) if n == 42));
345
346        // test null value should fail
347        // 测试 null 值应该失败
348        let json = "null";
349        let result: std::result::Result<RequestId, serde_json::Error> = serde_json::from_str(json);
350        assert!(result.is_err());
351    }
352
353    #[test]
354    fn test_request_with_same_id_in_different_sessions() {
355        // test same id in different sessions
356        // 测试不同会话中使用相同的 ID
357        let id = RequestId::Number(1);
358
359        // first session
360        // 第一个会话
361        let mut session1_ids = HashSet::new();
362        assert!(is_unique_id(&id, &mut session1_ids));
363
364        // second session
365        // 第二个会话(新的 HashSet 代表新的会话)
366        let mut session2_ids = HashSet::new();
367        assert!(is_unique_id(&id, &mut session2_ids));
368    }
369
370    #[test]
371    fn test_response_must_match_request_id() {
372        // Create a request
373        // 创建一个请求
374        let request_id = RequestId::Number(42);
375        let request = Request::new(Method::Initialize, None, request_id.clone());
376
377        // Create success response
378        // 创建成功响应
379        let success_response = Response::success(json!({"result": "success"}), request_id.clone());
380        assert!(matches!(success_response.id, RequestId::Number(42)));
381
382        // Create error response
383        // 创建错误响应
384        let error_response = Response::error(
385            ResponseError {
386                code: error_codes::INTERNAL_ERROR,
387                message: "error".to_string(),
388                data: None,
389            },
390            request_id.clone(),
391        );
392        assert!(matches!(error_response.id, RequestId::Number(42)));
393
394        // Verify response with different ID
395        // 验证不同 ID 的响应
396        let different_id = RequestId::Number(43);
397        let different_response = Response::success(json!({"result": "success"}), different_id);
398        assert!(matches!(different_response.id, RequestId::Number(43)));
399    }
400
401    #[test]
402    fn test_response_must_set_result_or_error_not_both() {
403        let id = RequestId::Number(1);
404
405        // Test success response only sets result
406        // 测试成功响应只设置 result
407        let success_response = Response::success(json!({"data": "success"}), id.clone());
408        assert!(success_response.result.is_some());
409        assert!(success_response.error.is_none());
410
411        // Test error response only sets error
412        // 测试错误响应只设置 error
413        let error_response = Response::error(
414            ResponseError {
415                code: error_codes::INTERNAL_ERROR,
416                message: "error".to_string(),
417                data: None,
418            },
419            id.clone(),
420        );
421        assert!(error_response.result.is_none());
422        assert!(error_response.error.is_some());
423
424        // Test serialization ensures both are not included
425        // 测试序列化确保不会同时包含两者
426        let success_json = serde_json::to_string(&success_response).unwrap();
427        assert!(!success_json.contains(r#""error""#));
428
429        let error_json = serde_json::to_string(&error_response).unwrap();
430        assert!(!error_json.contains(r#""result""#));
431    }
432
433    #[test]
434    fn test_error_code_must_be_integer() {
435        let id = RequestId::Number(1);
436
437        // Test standard error codes
438        // 测试标准错误代码
439        let standard_errors = [
440            error_codes::PARSE_ERROR,
441            error_codes::INVALID_REQUEST,
442            error_codes::METHOD_NOT_FOUND,
443            error_codes::INVALID_PARAMS,
444            error_codes::INTERNAL_ERROR,
445            error_codes::SERVER_NOT_INITIALIZED,
446            error_codes::UNKNOWN_ERROR_CODE,
447            error_codes::REQUEST_CANCELLED,
448        ];
449
450        for &code in &standard_errors {
451            let error_response = Response::error(
452                ResponseError {
453                    code,
454                    message: "test error".to_string(),
455                    data: None,
456                },
457                id.clone(),
458            );
459
460            if let Some(error) = error_response.error {
461                // Verify error code is integer type
462                // 验证错误代码是整数类型
463                assert_eq!(
464                    std::mem::size_of_val(&error.code),
465                    std::mem::size_of::<i32>()
466                );
467
468                // Verify error code serialization
469                // 验证错误代码的序列化
470                let json = serde_json::to_string(&error).unwrap();
471                assert!(json.contains(&format!(r#"code":{}"#, error.code)));
472            } else {
473                panic!("Error field should be set");
474            }
475        }
476
477        // Test custom error codes
478        // 测试自定义错误代码
479        let custom_codes = [-1, 0, 1, 1000, -1000];
480        for code in custom_codes {
481            let error_response = Response::error(
482                ResponseError {
483                    code,
484                    message: "custom error".to_string(),
485                    data: None,
486                },
487                id.clone(),
488            );
489
490            if let Some(error) = error_response.error {
491                assert_eq!(error.code, code);
492                // Verify error code is integer type
493                // 验证错误代码是整数类型
494                assert_eq!(
495                    std::mem::size_of_val(&error.code),
496                    std::mem::size_of::<i32>()
497                );
498            } else {
499                panic!("Error field should be set");
500            }
501        }
502    }
503
504    #[test]
505    fn test_notification_must_not_contain_id() {
506        // Create a notification
507        // 创建一个通知
508        let notification = Notification::new(Method::Initialized, Some(json!({"status": "ready"})));
509
510        // Serialize notification to JSON
511        // 将通知序列化为 JSON
512        let json_str = serde_json::to_string(&notification).unwrap();
513
514        // Verify JSON does not contain "id" field
515        // 验证 JSON 不包含 "id" 字段
516        assert!(!json_str.contains(r#""id""#));
517
518        // Test deserialization of notification without ID
519        // 测试不带 ID 的通知的反序列化
520        let json_without_id = r#"{
521            "jsonrpc": "2.0",
522            "method": "initialized",
523            "params": {"status": "ready"}
524        }"#;
525        let parsed: Message = serde_json::from_str(json_without_id).unwrap();
526        assert!(matches!(parsed, Message::Notification(_)));
527
528        // Test message with ID should not be parsed as notification
529        // 测试带有 ID 的消息不应该被解析为通知
530        let json_with_id = r#"{
531            "jsonrpc": "2.0",
532            "method": "initialized",
533            "params": {"status": "ready"},
534            "id": 1
535        }"#;
536        let parsed: Message = serde_json::from_str(json_with_id).unwrap();
537        assert!(matches!(parsed, Message::Request(_)));
538        assert!(!matches!(parsed, Message::Notification(_)));
539    }
540
541    #[test]
542    fn test_initialization_protocol_compliance() {
543        // Test initialize request format
544        // 测试初始化请求格式
545        let request = Request::new(
546            Method::Initialize,
547            Some(json!({
548                "protocolVersion": super::super::PROTOCOL_VERSION,
549                "capabilities": {
550                    "roots": {
551                        "listChanged": true
552                    },
553                    "sampling": {}
554                },
555                "clientInfo": {
556                    "name": "TestClient",
557                    "version": "1.0.0"
558                }
559            })),
560            RequestId::Number(1),
561        );
562
563        let request_json = serde_json::to_string(&request).unwrap();
564
565        // Verify request format
566        // 验证请求格式
567        assert!(request_json.contains(r#""method":"initialize""#));
568        assert!(request_json.contains(super::super::PROTOCOL_VERSION));
569        assert!(request_json.contains(r#""capabilities""#));
570        assert!(request_json.contains(r#""clientInfo""#));
571
572        // Test initialize response format
573        // 测试初始化响应格式
574        let response = Response::success(
575            json!({
576                "protocolVersion": super::super::PROTOCOL_VERSION,
577                "capabilities": {
578                    "prompts": {
579                        "listChanged": true
580                    },
581                    "resources": {
582                        "subscribe": true,
583                        "listChanged": true
584                    },
585                    "tools": {
586                        "listChanged": true
587                    },
588                    "logging": {}
589                },
590                "serverInfo": {
591                    "name": "TestServer",
592                    "version": "1.0.0"
593                }
594            }),
595            RequestId::Number(1),
596        );
597
598        let response_json = serde_json::to_string(&response).unwrap();
599
600        // Verify response format
601        // 验证响应格式
602        assert!(response_json.contains(super::super::PROTOCOL_VERSION));
603        assert!(response_json.contains(r#""capabilities""#));
604        assert!(response_json.contains(r#""serverInfo""#));
605
606        // Test initialized notification format
607        // 测试初始化完成通知格式
608        let notification = Notification::new(Method::Initialized, None);
609        let notification_json = serde_json::to_string(&notification).unwrap();
610
611        // Verify notification format
612        // 验证通知格式
613        assert!(notification_json.contains(r#""method":"initialized""#));
614        assert!(!notification_json.contains(r#""id""#));
615    }
616
617    #[test]
618    fn test_initialization_version_negotiation() {
619        // Test server accepting client version
620        // 测试服务器接受客户端版本
621        let client_request = Request::new(
622            Method::Initialize,
623            Some(json!({
624                "protocolVersion": super::super::PROTOCOL_VERSION
625            })),
626            RequestId::Number(1),
627        );
628
629        let server_response = Response::success(
630            json!({
631                "protocolVersion": super::super::PROTOCOL_VERSION
632            }),
633            RequestId::Number(1),
634        );
635
636        let client_version: String = serde_json::from_value(
637            client_request
638                .params
639                .unwrap()
640                .get("protocolVersion")
641                .unwrap()
642                .clone(),
643        )
644        .unwrap();
645
646        let server_version: String = serde_json::from_value(
647            server_response
648                .result
649                .unwrap()
650                .get("protocolVersion")
651                .unwrap()
652                .clone(),
653        )
654        .unwrap();
655
656        // Verify version match
657        // 验证版本匹配
658        assert_eq!(client_version, server_version);
659        assert_eq!(client_version, super::super::PROTOCOL_VERSION);
660
661        // Test server rejecting unsupported version
662        // 测试服务器拒绝不支持的版本
663        let unsupported_version = "1.0.0";
664        let client_request = Request::new(
665            Method::Initialize,
666            Some(json!({
667                "protocolVersion": unsupported_version
668            })),
669            RequestId::Number(2),
670        );
671
672        let server_error = Response::error(
673            ResponseError {
674                code: error_codes::INVALID_REQUEST,
675                message: "Unsupported protocol version".to_string(),
676                data: Some(json!({
677                    "supported": [super::super::PROTOCOL_VERSION],
678                    "requested": unsupported_version
679                })),
680            },
681            RequestId::Number(2),
682        );
683
684        // Verify error response format
685        // 验证错误响应格式
686        let error_json = serde_json::to_string(&server_error).unwrap();
687        assert!(error_json.contains("Unsupported protocol version"));
688        assert!(error_json.contains(super::super::PROTOCOL_VERSION));
689        assert!(error_json.contains(unsupported_version));
690    }
691
692    #[test]
693    fn test_ping_mechanism() {
694        // 测试 ping 请求格式
695        // Test ping request format
696        let ping_request =
697            Request::new(Method::Ping, None, RequestId::String("ping-1".to_string()));
698
699        // 验证请求格式
700        // Verify request format
701        let request_json = serde_json::to_string(&ping_request).unwrap();
702        assert!(request_json.contains(r#""method":"ping""#));
703        assert!(request_json.contains(r#""id":"ping-1""#));
704        assert!(!request_json.contains("params"));
705
706        // 测试 ping 响应格式
707        // Test ping response format
708        let ping_response = Response::success(json!({}), RequestId::String("ping-1".to_string()));
709
710        // 验证响应格式
711        // Verify response format
712        let response_json = serde_json::to_string(&ping_response).unwrap();
713        assert!(response_json.contains(r#""result":{}"#));
714        assert!(response_json.contains(r#""id":"ping-1""#));
715        assert!(!response_json.contains("error"));
716
717        // 测试 ping 请求的 ID 唯一性
718        // Test ping request ID uniqueness
719        let mut session_ids = HashSet::new();
720        assert!(ping_request.validate_id_uniqueness(&mut session_ids));
721        assert!(!ping_request.validate_id_uniqueness(&mut session_ids));
722
723        // 测试 ping 响应必须匹配请求 ID
724        // Test ping response must match request ID
725        let mismatched_response =
726            Response::success(json!({}), RequestId::String("wrong-id".to_string()));
727        assert_ne!(ping_request.id, mismatched_response.id);
728
729        // 测试 ping 超时错误响应
730        // Test ping timeout error response
731        let timeout_error = Response::error(
732            ResponseError {
733                code: error_codes::REQUEST_CANCELLED,
734                message: "Ping timeout".to_string(),
735                data: None,
736            },
737            RequestId::String("ping-1".to_string()),
738        );
739
740        // 验证错误响应格式
741        // Verify error response format
742        let error_json = serde_json::to_string(&timeout_error).unwrap();
743        assert!(error_json.contains("Ping timeout"));
744        assert!(error_json.contains(&error_codes::REQUEST_CANCELLED.to_string()));
745    }
746
747    #[test]
748    fn test_ping_pong_sequence() {
749        // 测试完整的 ping-pong 序列
750        // Test complete ping-pong sequence
751        let mut session_ids = HashSet::new();
752
753        // 1. 发送 ping 请求
754        // 1. Send ping request
755        let ping_request = Request::new(
756            Method::Ping,
757            None,
758            RequestId::String("ping-seq-1".to_string()),
759        );
760        assert!(ping_request.validate_id_uniqueness(&mut session_ids));
761
762        // 2. 接收 pong 响应
763        // 2. Receive pong response
764        let pong_response =
765            Response::success(json!({}), RequestId::String("ping-seq-1".to_string()));
766
767        // 验证响应匹配请求
768        // Verify response matches request
769        assert_eq!(ping_request.id, pong_response.id);
770        assert!(pong_response.result.is_some());
771        assert!(pong_response.error.is_none());
772
773        // 3. 测试多个 ping 请求
774        // 3. Test multiple ping requests
775        let ping_request_2 = Request::new(
776            Method::Ping,
777            None,
778            RequestId::String("ping-seq-2".to_string()),
779        );
780        assert!(ping_request_2.validate_id_uniqueness(&mut session_ids));
781
782        // 验证不同 ping 请求的 ID 不同
783        // Verify different ping requests have different IDs
784        assert_ne!(ping_request.id, ping_request_2.id);
785    }
786}