mcp-stdio-proxy 0.2.8

MCP (Model Context Protocol) proxy server and CLI tool for protocol conversion and remote service access
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
// MCP 客户端模块测试 - 集成测试

// ============== 共享测试工具 ==============

#[cfg(test)]
mod test_helpers {
    use serde_json::json;
    use std::process::Stdio;
    use std::time::Duration;
    use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
    use tokio::process::{Child, Command};
    use tokio::time::timeout;

    /// 测试端口分配
    pub const TEST_PORT_INTEGRATION: u16 = 19880; // integration_tests 使用
    pub const TEST_PORT_PROTOCOL: u16 = 19881; // protocol detection 使用
    pub const TEST_PORT_RECONNECT: u16 = 19876; // reconnection_tests 使用

    /// 获取预编译的 test_mcp_server 二进制路径
    ///
    /// 注意:test_mcp_server 现在是 mcp-sse-proxy 的 example,
    /// 编译输出在 target/debug/examples/test_mcp_server
    pub fn get_test_mcp_server_path() -> String {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        let workspace_root = std::path::Path::new(manifest_dir)
            .parent()
            .and_then(|p| p.parent())
            .expect(
                "无法定位 workspace 根目录(mcp-proxy 应在 crates/ 下,target/ 在 workspace 根)",
            );
        workspace_root
            .join("target/debug/examples/test_mcp_server")
            .to_string_lossy()
            .to_string()
    }

    /// 获取预编译的 mcp-proxy 二进制路径
    pub fn get_mcp_proxy_path() -> String {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        let workspace_root = std::path::Path::new(manifest_dir)
            .parent()
            .and_then(|p| p.parent())
            .expect(
                "无法定位 workspace 根目录(mcp-proxy 应在 crates/ 下,target/ 在 workspace 根)",
            );
        workspace_root
            .join("target/debug/mcp-proxy")
            .to_string_lossy()
            .to_string()
    }

    /// 创建 MCP 服务配置 JSON(使用预编译二进制)
    pub fn create_test_config() -> String {
        let binary_path = get_test_mcp_server_path();
        json!({
            "mcpServers": {
                "test-server": {
                    "command": binary_path,
                    "args": []
                }
            }
        })
        .to_string()
    }

    /// 启动 proxy 服务器
    pub async fn spawn_proxy_server(port: u16) -> anyhow::Result<Child> {
        let config = create_test_config();
        let mcp_proxy_path = get_mcp_proxy_path();

        let child = Command::new(&mcp_proxy_path)
            .args([
                "proxy",
                "--port",
                &port.to_string(),
                "--host",
                "127.0.0.1",
                "--config",
                &config,
            ])
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true)
            .spawn()?;

        Ok(child)
    }

    /// 等待服务器就绪(TCP 轮询)
    pub async fn wait_for_server_ready(addr: &str, max_retries: u32) -> anyhow::Result<()> {
        for i in 0..max_retries {
            match tokio::net::TcpStream::connect(addr).await {
                Ok(_) => {
                    println!("✅ Server ready (try #{})", i + 1);
                    return Ok(());
                }
                Err(_) => {
                    if i < max_retries - 1 {
                        tokio::time::sleep(Duration::from_millis(500)).await;
                    }
                }
            }
        }
        anyhow::bail!("服务器在 {} 次尝试后未就绪", max_retries)
    }

    /// 启动 convert 客户端进程
    pub async fn spawn_convert_client(
        url: &str,
        ping_interval: u64,
        ping_timeout: u64,
    ) -> anyhow::Result<Child> {
        let mcp_proxy_path = get_mcp_proxy_path();

        let child = Command::new(&mcp_proxy_path)
            .args([
                "convert",
                url,
                "--ping-interval",
                &ping_interval.to_string(),
                "--ping-timeout",
                &ping_timeout.to_string(),
            ])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true)
            .spawn()?;

        Ok(child)
    }

    /// 监控 stderr 输出,查找任一日志模式
    pub async fn wait_for_stderr_patterns(
        stderr: &mut BufReader<tokio::process::ChildStderr>,
        patterns: &[&str],
        timeout_duration: Duration,
    ) -> anyhow::Result<bool> {
        let result = timeout(timeout_duration, async {
            let mut line = String::new();
            loop {
                line.clear();
                match stderr.read_line(&mut line).await {
                    Ok(0) => return false, // EOF
                    Ok(_) => {
                        print!("[stderr] {}", line);
                        if patterns.iter().any(|pattern| line.contains(pattern)) {
                            return true;
                        }
                    }
                    Err(_) => return false,
                }
            }
        })
        .await;

        match result {
            Ok(found) => Ok(found),
            Err(_) => Ok(false), // timeout
        }
    }

    /// 发送 JSON-RPC 请求并获取响应
    pub async fn send_jsonrpc_and_receive(
        stdin: &mut tokio::process::ChildStdin,
        stdout: &mut BufReader<tokio::process::ChildStdout>,
        request: serde_json::Value,
    ) -> anyhow::Result<serde_json::Value> {
        let msg = format!("{}\n", serde_json::to_string(&request)?);
        stdin.write_all(msg.as_bytes()).await?;
        stdin.flush().await?;

        let mut response = String::new();
        stdout.read_line(&mut response).await?;
        let parsed: serde_json::Value = serde_json::from_str(&response)?;
        Ok(parsed)
    }

    /// 初始化 MCP 客户端(发送 initialize + initialized)
    pub async fn initialize_mcp_client(
        stdin: &mut tokio::process::ChildStdin,
        stdout: &mut BufReader<tokio::process::ChildStdout>,
    ) -> anyhow::Result<()> {
        // 发送 initialize 请求
        let init_request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2024-11-05",
                "capabilities": {
                    "roots": {"listChanged": true},
                    "sampling": {}
                },
                "clientInfo": {
                    "name": "test-client",
                    "version": "1.0.0"
                }
            }
        });

        let init_response = send_jsonrpc_and_receive(stdin, stdout, init_request).await?;
        if init_response["error"].is_object() {
            anyhow::bail!("Initialize failed: {:?}", init_response["error"]);
        }

        // 发送 initialized 通知
        let initialized_notification = json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        });
        let msg = format!("{}\n", serde_json::to_string(&initialized_notification)?);
        stdin.write_all(msg.as_bytes()).await?;
        stdin.flush().await?;

        // 等待一下让服务器处理
        tokio::time::sleep(Duration::from_millis(100)).await;
        Ok(())
    }
}

// ============== 集成测试 ==============

#[cfg(test)]
mod integration_tests {
    use super::test_helpers::*;
    use serde_json::json;
    use std::time::Duration;
    use tokio::io::BufReader;

    /// 测试本地 MCP 服务连接和通信
    ///
    /// 使用本地 test-mcp-server + mcp-proxy proxy 进行测试
    /// 验证完整的 MCP 通信流程:initialize -> tools/list -> tools/call
    #[ignore = "集成测试:需启动真实 mcp-proxy/test-mcp-server 子进程与本地端口,仅在开发机手动 --ignored 运行(CI 无该环境,main 线 CI 自 8 月起因此失败)"]
    #[tokio::test]
    async fn test_real_mcp_service_communication() {
        println!("\\n========== Test: MCP service connection and communication ==========");

        // 1. 启动本地 proxy 服务器
        println!("🚀 Start local proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_INTEGRATION)
            .await
            .expect("启动 proxy 失败");

        let addr = format!("127.0.0.1:{}", TEST_PORT_INTEGRATION);
        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器启动超时");

        // 等待 proxy 完全初始化后端连接
        tokio::time::sleep(Duration::from_secs(3)).await;

        // 2. 启动 convert 客户端连接到本地 proxy
        println!("🔗 Start convert client...");
        let url = format!("http://{}", addr);
        let mut client = spawn_convert_client(&url, 30, 10)
            .await
            .expect("启动 convert 失败");

        let mut stdin = client.stdin.take().expect("获取 stdin 失败");
        let stdout = client.stdout.take().expect("获取 stdout 失败");
        let stderr = client.stderr.take().expect("获取 stderr 失败");
        let mut stdout_reader = BufReader::new(stdout);
        let mut stderr_reader = BufReader::new(stderr);

        // 等待客户端连接成功
        println!("⏳ Waiting for client to connect...");
        let connected = wait_for_stderr_patterns(
            &mut stderr_reader,
            &["开始代理转换", "proxying traffic", "Stdio server started"],
            Duration::from_secs(15),
        )
        .await
        .expect("监控 stderr 失败");

        if !connected {
            println!("⚠️ No connection success log detected, trying to communicate directly...");
            // 额外等待以确保连接建立
            tokio::time::sleep(Duration::from_secs(2)).await;
        }

        // 3. 初始化 MCP 客户端
        println!("🤝 Initialize MCP client...");
        initialize_mcp_client(&mut stdin, &mut stdout_reader)
            .await
            .expect("初始化失败");

        // 4. 发送 tools/list 请求
        println!("📋 Get tool list...");
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list"
        });
        let tools_response =
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, tools_request)
                .await
                .expect("tools/list 请求失败");

        assert_eq!(tools_response["jsonrpc"], "2.0");
        assert_eq!(tools_response["id"], 2);
        assert!(tools_response["result"]["tools"].is_array());

        let tools = tools_response["result"]["tools"].as_array().unwrap();
        println!("✅ Obtained {} tools", tools.len());

        // 验证本地测试工具存在
        let tool_names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
        println!("Tool list: {:?}", tool_names);
        assert!(tool_names.contains(&"echo"), "应该包含 echo 工具");
        assert!(tool_names.contains(&"increment"), "应该包含 increment 工具");

        // 5. 测试调用 echo 工具
        println!("🔧 Call the echo tool...");
        let call_tool_request = json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {
                    "message": "Hello from integration test!"
                }
            }
        });

        let call_response =
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, call_tool_request)
                .await
                .expect("tools/call 请求失败");

        assert_eq!(call_response["jsonrpc"], "2.0");
        assert_eq!(call_response["id"], 3);

        // 验证返回结果
        if call_response["error"].is_object() {
            panic!("Tool call failed with error: {:?}", call_response["error"]);
        }

        let result = &call_response["result"];
        assert!(
            !result["isError"].as_bool().unwrap_or(true),
            "echo 调用不应该出错"
        );
        assert!(result["content"].is_array(), "Content should be an array");

        let content = result["content"].as_array().unwrap();
        assert!(!content.is_empty(), "Content should not be empty");

        let first_content = &content[0];
        assert_eq!(first_content["type"], "text");
        let text = first_content["text"]
            .as_str()
            .expect("Should have text field");
        assert!(
            text.contains("Hello from integration test!"),
            "Should echo our message"
        );

        println!("✅ Tool call successful! Response: {}", text);

        // 6. 测试调用 increment 工具
        println!("🔧 Call the increment tool...");
        let increment_request = json!({
            "jsonrpc": "2.0",
            "id": 4,
            "method": "tools/call",
            "params": {
                "name": "increment",
                "arguments": {}
            }
        });

        let increment_response =
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, increment_request)
                .await
                .expect("increment 请求失败");

        assert!(
            !increment_response["result"]["isError"]
                .as_bool()
                .unwrap_or(true),
            "increment 调用不应该出错"
        );
        println!("✅ increment call successful");

        // 清理:关闭进程
        println!("🧹 Cleaning process...");
        drop(stdin);
        let _ = client.kill().await;
        let _ = proxy.kill().await;

        println!("========== Test completed ==========\\n");
    }

    /// 测试协议检测功能
    ///
    /// 使用本地 mcp-proxy proxy 服务测试协议检测
    /// 本地 proxy 默认使用 Streamable HTTP 协议
    #[ignore = "集成测试:需启动真实 mcp-proxy/test-mcp-server 子进程与本地端口,仅在开发机手动 --ignored 运行(CI 无该环境,main 线 CI 自 8 月起因此失败)"]
    #[tokio::test]
    async fn test_protocol_detection() {
        println!("\\n========== Test: Protocol Detection ==========");

        // 1. 启动本地 proxy 服务器(Streamable HTTP 模式)
        println!("🚀 Start local proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_PROTOCOL)
            .await
            .expect("启动 proxy 失败");

        let addr = format!("127.0.0.1:{}", TEST_PORT_PROTOCOL);
        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器启动超时");

        // 2. 测试协议检测
        println!("🔍Detect protocol type...");
        let url = format!("http://{}", addr);
        let protocol = crate::client::protocol::detect_mcp_protocol_with_headers(&url, None).await;

        assert!(protocol.is_ok(), "协议检测应该成功");

        let protocol = protocol.unwrap();
        use crate::client::protocol::McpProtocol;
        // 本地 proxy 默认使用 Streamable HTTP
        assert_eq!(
            protocol,
            McpProtocol::Stream,
            "应该检测到 Streamable HTTP 协议"
        );

        println!("✅ Protocol detected: {:?}", protocol);

        // 清理
        println!("🧹 Cleaning process...");
        let _ = proxy.kill().await;

        println!("========== Test completed ==========\\n");
    }
}

/// 本地重连测试模块
///
/// 使用 `mcp-proxy proxy` 启动本地服务,`mcp-proxy convert` 连接测试
/// 验证通道检查和自动重连逻辑
#[cfg(test)]
mod reconnection_tests {
    use super::test_helpers::*;
    use serde_json::json;
    use std::time::Duration;
    use tokio::io::{AsyncBufReadExt, BufReader};
    use tokio::time::timeout;

    /// 测试配置
    const CLIENT_CONNECT_TIMEOUT: Duration = Duration::from_secs(15);
    const RECONNECT_DETECT_TIMEOUT: Duration = Duration::from_secs(30);

    /// 测试 1: 正常连接和通信
    ///
    /// 验证:
    /// - proxy 服务启动正常
    /// - convert 客户端连接成功
    /// - tools/list 请求正常响应
    #[ignore = "集成测试:需启动真实 mcp-proxy/test-mcp-server 子进程与本地端口,仅在开发机手动 --ignored 运行(CI 无该环境,main 线 CI 自 8 月起因此失败)"]
    #[tokio::test]
    async fn test_reconnection_normal_connection() {
        println!("\\n========== Test 1: Normal connection and communication ==========");

        // 1. 启动 proxy 服务器
        println!("🚀 Start proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_RECONNECT)
            .await
            .expect("启动 proxy 失败");

        // 等待服务器就绪
        let addr = format!("127.0.0.1:{}", TEST_PORT_RECONNECT);
        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器启动超时");

        // 2. 启动 convert 客户端
        println!("🔗 Start convert client...");
        let url = format!("http://{}", addr);
        let mut client = spawn_convert_client(&url, 5, 3)
            .await
            .expect("启动 convert 失败");

        let mut stdin = client.stdin.take().expect("获取 stdin 失败");
        let stdout = client.stdout.take().expect("获取 stdout 失败");
        let stderr = client.stderr.take().expect("获取 stderr 失败");
        let mut stdout_reader = BufReader::new(stdout);
        let mut stderr_reader = BufReader::new(stderr);

        // 等待客户端连接成功(监控 stderr)
        println!("⏳ Waiting for client to connect...");
        let connected = wait_for_stderr_patterns(
            &mut stderr_reader,
            &["连接成功", "Backend connected successfully"],
            CLIENT_CONNECT_TIMEOUT,
        )
        .await
        .expect("监控 stderr 失败");

        if !connected {
            // 可能连接很快,直接尝试初始化
            println!("⚠️ No connection success log detected, trying to communicate directly...");
        }

        // 3. 初始化 MCP 客户端
        println!("🤝 Initialize MCP client...");
        initialize_mcp_client(&mut stdin, &mut stdout_reader)
            .await
            .expect("初始化失败");

        // 4. 发送 tools/list 请求
        println!("📋 Get tool list...");
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list"
        });
        let tools_response =
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, tools_request)
                .await
                .expect("tools/list 请求失败");

        assert!(
            tools_response["result"]["tools"].is_array(),
            "应该返回工具列表"
        );
        let tools = tools_response["result"]["tools"].as_array().unwrap();
        println!("✅ Obtained {} tools", tools.len());

        // 验证我们的测试工具存在
        let tool_names: Vec<&str> = tools.iter().filter_map(|t| t["name"].as_str()).collect();
        assert!(tool_names.contains(&"echo"), "应该包含 echo 工具");
        assert!(tool_names.contains(&"increment"), "应该包含 increment 工具");

        // 5. 测试调用 increment 工具
        println!("🔧 Call the increment tool...");
        let call_request = json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/call",
            "params": {
                "name": "increment",
                "arguments": {}
            }
        });
        let call_response = send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, call_request)
            .await
            .expect("tools/call 请求失败");

        assert!(
            !call_response["result"]["isError"].as_bool().unwrap_or(true),
            "increment 调用不应该出错"
        );
        println!("✅ increment call successful");

        // 清理
        println!("🧹 Cleaning process...");
        drop(stdin);
        let _ = client.kill().await;
        let _ = proxy.kill().await;

        println!("========== Test 1 Complete ==========\\n");
    }

    /// 测试 2: 服务器重启后自动重连
    ///
    /// 验证:
    /// - 杀死 proxy 服务后,convert 检测到断开
    /// - 重启 proxy 后,convert 自动重连
    /// - 重连后功能正常
    #[ignore = "集成测试:需启动真实 mcp-proxy 子进程与本地端口,仅开发机手动 --ignored 运行"]
    #[tokio::test]
    async fn test_reconnection_on_server_restart() {
        println!("\\n========== Test 2: Automatically reconnect after server restart ==========");

        // 1. 启动 proxy 服务器
        println!("🚀 Start proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_RECONNECT + 1)
            .await
            .expect("启动 proxy 失败");

        let addr = format!("127.0.0.1:{}", TEST_PORT_RECONNECT + 1);
        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器启动超时");

        // 2. 启动 convert 客户端(短 ping 间隔以快速检测断开)
        println!("🔗 Start convert client...");
        let url = format!("http://{}", addr);
        let mut client = spawn_convert_client(&url, 2, 2)
            .await
            .expect("启动 convert 失败");

        let stderr = client.stderr.take().expect("获取 stderr 失败");
        let mut stderr_reader = BufReader::new(stderr);

        // 等待客户端连接成功
        println!("⏳ Waiting for client to connect...");
        tokio::time::sleep(Duration::from_secs(5)).await;

        // 3. 杀死 proxy 服务器
        println!("💀 Kill proxy server...");
        let _ = proxy.kill().await;

        // 4. 等待客户端检测到断开
        println!("⏳ Waiting for the client to detect the disconnect...");
        let disconnected = wait_for_stderr_patterns(
            &mut stderr_reader,
            &["连接断开", "EVENT_DISCONNECTED", "Connection disconnected"],
            RECONNECT_DETECT_TIMEOUT,
        )
        .await
        .expect("监控 stderr 失败");

        // 也可能是 "Ping 检测" 或 "后端连接已关闭"
        if !disconnected {
            println!(
                "⚠️ No clear disconnection log detected, check if there are any reconnection attempts..."
            );
        }

        // 5. 重启 proxy 服务器
        println!("🔄 Restart proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_RECONNECT + 1)
            .await
            .expect("重启 proxy 失败");

        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器重启超时");

        // 6. 等待客户端重连成功
        println!("⏳ Waiting for the client to reconnect...");
        let reconnected = wait_for_stderr_patterns(
            &mut stderr_reader,
            &[
                "重连成功",
                "EVENT_RECONNECTED",
                "Reconnected, proxy service resumed",
            ],
            RECONNECT_DETECT_TIMEOUT,
        )
        .await
        .expect("监控 stderr 失败");

        if reconnected {
            println!("✅ The client has been reconnected successfully");
        } else {
            println!(
                "⚠️ No reconnection success log detected (may have reconnected before timeout)"
            );
        }

        // 清理
        println!("🧹 Cleaning process...");
        let _ = client.kill().await;
        let _ = proxy.kill().await;

        println!("========== Test 2 Complete ==========\\n");
    }

    /// 测试 3: 指数退避验证
    ///
    /// 验证退避时间递增(1s, 2s, 4s...)
    #[ignore = "集成测试:需启动真实 mcp-proxy/test-mcp-server 子进程与本地端口,仅在开发机手动 --ignored 运行(CI 无该环境,main 线 CI 自 8 月起因此失败)"]
    #[tokio::test]
    async fn test_reconnection_exponential_backoff() {
        println!("\\n========== Test 3: Exponential Backoff Verification ==========");

        // 不启动服务器,直接启动客户端
        // 客户端应该不断尝试连接并显示退避时间
        println!("🔗 Start convert client (serverless)...");
        let url = format!("http://127.0.0.1:{}", TEST_PORT_RECONNECT + 2);
        let mut client = spawn_convert_client(&url, 30, 10)
            .await
            .expect("启动 convert 失败");

        let stderr = client.stderr.take().expect("获取 stderr 失败");
        let mut stderr_reader = BufReader::new(stderr);

        // 监控退避日志
        println!("⏳ Monitor the backoff log (wait about 15 seconds)...");
        let mut backoff_times: Vec<String> = Vec::new();

        let result = timeout(Duration::from_secs(15), async {
            let mut line = String::new();
            loop {
                line.clear();
                match stderr_reader.read_line(&mut line).await {
                    Ok(0) => break,
                    Ok(_) => {
                        print!("[stderr] {}", line);
                        // 查找退避时间日志(兼容中英文/事件标记)
                        if line.contains("秒后重连")
                            || line.contains("retrying in")
                            || line.contains("EVENT_RETRY_BACKOFF")
                        {
                            backoff_times.push(line.clone());
                            if backoff_times.len() >= 3 {
                                break;
                            }
                        }
                    }
                    Err(_) => break,
                }
            }
        })
        .await;

        if result.is_err() {
            println!("⚠️ Monitoring timeout");
        }

        println!("📊 Detected backoff log: {:?}", backoff_times);

        // 验证退避时间递增
        if backoff_times.len() >= 2 {
            println!("✅ It has been detected that the backoff mechanism is working");
        } else {
            println!("⚠️ Not enough backoff logs detected");
        }

        // 清理
        println!("🧹 Cleaning process...");
        let _ = client.kill().await;

        println!("========== Test 3 Complete ==========\\n");
    }

    /// 测试 4: 通道断开后请求是否立即返回错误
    ///
    /// 验证:
    /// - 服务器停止后,客户端发送请求能否立即返回错误
    /// - 而不是空等超时
    #[ignore = "集成测试:需启动真实 mcp-proxy 子进程与本地端口,仅开发机手动 --ignored 运行"]
    #[tokio::test]
    async fn test_request_returns_error_when_connection_closed() {
        println!(
            "\\n========== Test 4: The request returns an error immediately after the channel is disconnected =========="
        );

        // 1. 启动 proxy 服务器
        println!("🚀 Start proxy server...");
        let mut proxy = spawn_proxy_server(TEST_PORT_RECONNECT + 3)
            .await
            .expect("启动 proxy 失败");

        let addr = format!("127.0.0.1:{}", TEST_PORT_RECONNECT + 3);
        wait_for_server_ready(&addr, 20)
            .await
            .expect("服务器启动超时");

        // 2. 启动 convert 客户端(短 ping 间隔以快速检测断开)
        println!("🔗 Start convert client...");
        let url = format!("http://{}", addr);
        let mut client = spawn_convert_client(&url, 2, 2)
            .await
            .expect("启动 convert 失败");

        let mut stdin = client.stdin.take().expect("获取 stdin 失败");
        let stdout = client.stdout.take().expect("获取 stdout 失败");
        let stderr = client.stderr.take().expect("获取 stderr 失败");
        let mut stdout_reader = BufReader::new(stdout);
        let mut stderr_reader = BufReader::new(stderr);

        // 后台监控 stderr
        let stderr_monitor = tokio::spawn(async move {
            let mut line = String::new();
            loop {
                line.clear();
                match stderr_reader.read_line(&mut line).await {
                    Ok(0) => break,
                    Ok(_) => {
                        print!("[stderr] {}", line);
                    }
                    Err(_) => break,
                }
            }
        });

        // 等待连接建立
        tokio::time::sleep(Duration::from_secs(3)).await;

        // 3. 初始化 MCP 客户端
        println!("🤝 Initialize MCP client...");
        initialize_mcp_client(&mut stdin, &mut stdout_reader)
            .await
            .expect("初始化失败");

        // 4. 发送第一个 tools/list 请求确认通信正常
        println!("📋 Send first tools/list request (should succeed)...");
        let tools_request = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/list"
        });

        let start = std::time::Instant::now();
        let tools_response =
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, tools_request)
                .await
                .expect("tools/list 请求失败");
        let elapsed = start.elapsed();

        assert!(
            tools_response["result"]["tools"].is_array(),
            "第一个请求应该成功返回工具列表"
        );
        println!(
            "✅ The first request was successful and took time: {:?}",
            elapsed
        );

        // 5. 杀死 proxy 服务器
        println!("💀 Kill proxy server...");
        let _ = proxy.kill().await;

        // 等待 ping 检测发现连接断开(ping 间隔是 2s,超时也是 2s)
        println!("⏳ Wait for the ping to detect the disconnection (about 5 seconds)...");
        tokio::time::sleep(Duration::from_secs(5)).await;

        // 6. 发送第二个 tools/list 请求
        println!("📋 Send a second tools/list request (should return an error quickly)...");
        let tools_request2 = json!({
            "jsonrpc": "2.0",
            "id": 3,
            "method": "tools/list"
        });

        let start = std::time::Instant::now();
        let result = timeout(
            Duration::from_secs(5),
            send_jsonrpc_and_receive(&mut stdin, &mut stdout_reader, tools_request2),
        )
        .await;
        let elapsed = start.elapsed();

        match result {
            Ok(Ok(response)) => {
                // 检查是否是错误响应
                if response["error"].is_object() {
                    println!("✅ Error response received, time taken: {:?}", elapsed);
                    println!("Error message: {:?}", response["error"]);
                    assert!(
                        elapsed < Duration::from_secs(3),
                        "错误响应应该在 3 秒内返回,实际耗时: {:?}",
                        elapsed
                    );
                } else {
                    // 如果返回了成功响应,说明可能重连了
                    println!(
                        "⚠️ Successful response received (may have been reconnected), time taken: {:?}",
                        elapsed
                    );
                }
            }
            Ok(Err(e)) => {
                println!(
                    "✅ The request failed (as expected), time taken: {:?}",
                    elapsed
                );
                println!("Error: {}", e);
                assert!(
                    elapsed < Duration::from_secs(3),
                    "错误应该在 3 秒内返回,实际耗时: {:?}",
                    elapsed
                );
            }
            Err(_) => {
                println!(
                    "❌ The request times out (5 seconds), indicating that the client is waiting!"
                );
                panic!("请求应该快速返回错误,而不是超时空等");
            }
        }

        // 清理
        println!("🧹 Cleaning process...");
        drop(stdin);
        stderr_monitor.abort();
        let _ = client.kill().await;

        println!("========== Test 4 Complete ==========\\n");
    }
}