solidmcp 0.4.0

A high-level Rust toolkit for building Model Context Protocol (MCP) servers with type safety and minimal boilerplate. Supports tools, resources, and prompts with automatic JSON schema generation.
Documentation
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
//! Comprehensive End-to-End Progress Notification Tests
//!
//! Tests comprehensive progress notification functionality across both HTTP and WebSocket transports
//! including streaming responses, chunked encoding, JSON-RPC compliance, and edge cases.

use futures_util::{SinkExt, StreamExt};
use serde_json::{json, Value};
use std::time::Duration;
use tokio::time::timeout;
use tokio_tungstenite::{connect_async, tungstenite::Message};

mod mcp_test_helpers;
use mcp_test_helpers::{with_mcp_test_server, with_mcp_connection};

// ============================================================================
// HTTP Transport Progress Notification Tests
// ============================================================================

#[tokio::test]
async fn test_http_progress_notifications_with_chunked_encoding() {
    // Test basic progress notification flow with HTTP chunked encoding
    with_mcp_test_server("http_progress_chunked", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        client.post(&server.http_url()).json(&init).send().await?;

        // Tool call with progress token
        let tool_call = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {"message": "test with progress"},
                "_meta": {
                    "progressToken": "progress-test-token-001"
                }
            }
        });

        let response = client
            .post(&server.http_url())
            .json(&tool_call)
            .send()
            .await?;

        // Verify chunked encoding is used
        assert_eq!(response.status(), 200);
        let headers = response.headers();
        assert_eq!(
            headers.get("transfer-encoding").map(|v| v.to_str().unwrap()),
            Some("chunked"),
            "Should use chunked encoding for progress tokens"
        );
        assert!(
            !headers.contains_key("content-length"),
            "Should not have content-length with chunked encoding"
        );

        // Verify response is valid
        let body: Value = response.json().await?;
        assert_eq!(body["jsonrpc"], "2.0");
        assert_eq!(body["id"], 2);
        assert!(body.get("result").is_some());

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_http_progress_notifications_with_different_token_types() {
    // Test various progress token formats (string, object, number)
    with_mcp_test_server("http_progress_token_types", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        client.post(&server.http_url()).json(&init).send().await?;

        // Test different token types
        let test_cases = vec![
            ("string", json!("string-token-123")),
            ("number", json!(42)),
            ("object", json!({"id": "obj-token", "session": "test"})),
            ("array", json!(["token", "array", 123])),
        ];

        for (test_name, token) in test_cases {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": format!("test-{}", test_name),
                "method": "tools/call",
                "params": {
                    "name": "echo",
                    "arguments": {"message": format!("test-{}", test_name)},
                    "_meta": {
                        "progressToken": token
                    }
                }
            });

            let response = client
                .post(&server.http_url())
                .json(&tool_call)
                .send()
                .await?;

            assert_eq!(response.status(), 200, "Failed for token type: {}", test_name);
            
            // Should use chunked encoding regardless of token type
            let headers = response.headers();
            assert_eq!(
                headers.get("transfer-encoding").map(|v| v.to_str().unwrap()),
                Some("chunked"),
                "Should use chunked encoding for {} token", test_name
            );

            let body: Value = response.json().await?;
            assert!(body.get("result").is_some() || body.get("error").is_some(), 
                   "Should have result or error for {}", test_name);
        }

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_http_no_chunked_encoding_without_progress_token() {
    // Verify that requests without progress tokens use Content-Length
    with_mcp_test_server("http_no_progress_content_length", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        let response = client.post(&server.http_url()).json(&init).send().await?;

        // Should use Content-Length, not chunked encoding
        let headers = response.headers();
        assert!(
            headers.contains_key("content-length"),
            "Should have content-length without progress token"
        );
        assert!(
            !headers.contains_key("transfer-encoding"),
            "Should not have transfer-encoding without progress token"
        );

        // Regular tool call without progress token
        let tool_call = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {"message": "test without progress"}
            }
        });

        let response = client
            .post(&server.http_url())
            .json(&tool_call)
            .send()
            .await?;

        let headers = response.headers();
        assert!(
            headers.contains_key("content-length"),
            "Regular requests should use content-length"
        );
        assert!(
            !headers.contains_key("transfer-encoding"),
            "Regular requests should not use transfer-encoding"
        );

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_http_progress_with_session_management() {
    // Test progress notifications work correctly with HTTP session management
    with_mcp_test_server("http_progress_sessions", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(10))
            .cookie_store(true)  // Enable cookie jar
            .build()?;

        // Initialize to establish session
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "clientInfo": {"name": "session-test", "version": "1.0"}
            }
        });
        
        let init_response = client.post(&server.http_url()).json(&init).send().await?;
        assert_eq!(init_response.status(), 200);

        // Verify session cookie was set
        let cookies = init_response.headers().get_all("set-cookie");
        let has_session_cookie = cookies.iter().any(|cookie| {
            cookie.to_str().unwrap_or("").contains("mcp_session=")
        });
        assert!(has_session_cookie, "Should set session cookie on initialize");

        // Multiple tool calls with progress in same session
        for i in 1..=3 {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": i + 1,
                "method": "tools/call",
                "params": {
                    "name": "echo",
                    "arguments": {"message": format!("session test {}", i)},
                    "_meta": {
                        "progressToken": format!("session-progress-{}", i)
                    }
                }
            });

            let response = client
                .post(&server.http_url())
                .json(&tool_call)
                .send()
                .await?;

            assert_eq!(response.status(), 200);
            // Should still use chunked encoding
            assert_eq!(
                response.headers().get("transfer-encoding").map(|v| v.to_str().unwrap()),
                Some("chunked")
            );

            let body: Value = response.json().await?;
            assert!(body.get("result").is_some());
        }

        Ok(())
    })
    .await
    .unwrap();
}

// ============================================================================
// WebSocket Transport Progress Notification Tests
// ============================================================================

#[tokio::test]
async fn test_websocket_progress_notifications_basic_flow() {
    // Test basic progress notification flow over WebSocket
    with_mcp_connection("ws_progress_basic", |server, mut write, mut read| async move {
        // Tool call with progress token
        let tool_call = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {"message": "websocket progress test"},
                "_meta": {
                    "progressToken": "ws-progress-token-001"
                }
            }
        });

        write.send(Message::Text(tool_call.to_string().into())).await?;

        // Read response - should get the final result
        let response_text = timeout(Duration::from_secs(5), async {
            while let Some(Ok(Message::Text(text))) = read.next().await {
                let msg: Value = serde_json::from_str(&text.to_string())?;
                if msg.get("id") == Some(&json!(2)) {
                    return Ok::<String, anyhow::Error>(text.to_string());
                }
            }
            Err(anyhow::anyhow!("No response received"))
        }).await??;

        let response: Value = serde_json::from_str(&response_text)?;
        assert_eq!(response["jsonrpc"], "2.0");
        assert_eq!(response["id"], 2);
        assert!(response.get("result").is_some(), "Should have result");

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_websocket_concurrent_progress_notifications() {
    // Test multiple concurrent progress notifications over WebSocket
    with_mcp_connection("ws_concurrent_progress", |server, mut write, mut read| async move {
        // Send multiple tool calls with different progress tokens concurrently
        let progress_tokens = vec![
            "concurrent-token-1",
            "concurrent-token-2", 
            "concurrent-token-3",
            "concurrent-token-4",
            "concurrent-token-5"
        ];

        // Send all requests
        for (i, token) in progress_tokens.iter().enumerate() {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": i + 2,
                "method": "tools/call",
                "params": {
                    "name": "echo",
                    "arguments": {"message": format!("concurrent test {}", i)},
                    "_meta": {
                        "progressToken": token
                    }
                }
            });

            write.send(Message::Text(tool_call.to_string().into())).await?;
        }

        // Collect all responses
        let mut received_ids = std::collections::HashSet::new();
        let timeout_duration = Duration::from_secs(10);
        
        let responses = timeout(timeout_duration, async {
            let mut responses = Vec::new();
            while received_ids.len() < progress_tokens.len() {
                if let Some(Ok(Message::Text(text))) = read.next().await {
                    let msg: Value = serde_json::from_str(&text.to_string())?;
                    if let Some(id) = msg.get("id").and_then(|id| id.as_u64()) {
                        if id >= 2 && id < 2 + progress_tokens.len() as u64 {
                            received_ids.insert(id);
                            responses.push(msg);
                        }
                    }
                }
            }
            Ok::<Vec<Value>, anyhow::Error>(responses)
        }).await??;

        // Verify all responses received
        assert_eq!(responses.len(), progress_tokens.len());
        for response in responses {
            assert_eq!(response["jsonrpc"], "2.0");
            assert!(response.get("result").is_some());
        }

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_websocket_progress_with_large_payloads() {
    // Test progress notifications with large message payloads
    with_mcp_connection("ws_progress_large", |server, mut write, mut read| async move {
        // Create large payload
        let large_data = (0..10000).map(|i| format!("data-item-{}", i)).collect::<Vec<_>>();
        
        let tool_call = json!({
            "jsonrpc": "2.0",
            "id": 2,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {
                    "message": "large payload test",
                    "large_data": large_data
                },
                "_meta": {
                    "progressToken": "large-payload-token"
                }
            }
        });

        write.send(Message::Text(tool_call.to_string().into())).await?;

        // Should handle large payloads without issues
        let response_text = timeout(Duration::from_secs(10), async {
            while let Some(Ok(Message::Text(text))) = read.next().await {
                let msg: Value = serde_json::from_str(&text.to_string())?;
                if msg.get("id") == Some(&json!(2)) {
                    return Ok::<String, anyhow::Error>(text.to_string());
                }
            }
            Err(anyhow::anyhow!("No response received"))
        }).await??;

        let response: Value = serde_json::from_str(&response_text)?;
        assert_eq!(response["jsonrpc"], "2.0");
        assert!(response.get("result").is_some());

        Ok(())
    })
    .await
    .unwrap();
}

// ============================================================================
// Edge Cases and Error Handling Tests
// ============================================================================

#[tokio::test]
async fn test_malformed_progress_tokens() {
    // Test handling of malformed or edge-case progress tokens
    with_mcp_test_server("malformed_progress_tokens", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        client.post(&server.http_url()).json(&init).send().await?;

        // Test edge cases
        let test_cases = vec![
            ("null_token", json!(null)),
            ("empty_string", json!("")),
            ("very_long_string", json!("x".repeat(10000))),
            ("special_chars", json!("token-with-!@#$%^&*()_+-={}[]|\\:;\"'<>?,./~`")),
            ("unicode", json!("🚀💻📡🎯✅❌⚡🔄📊")),
            ("nested_object", json!({"level1": {"level2": {"token": "deep"}}})),
        ];

        for (test_name, token) in test_cases {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": format!("test-{}", test_name),
                "method": "tools/call",
                "params": {
                    "name": "echo",
                    "arguments": {"message": format!("test-{}", test_name)},
                    "_meta": {
                        "progressToken": token
                    }
                }
            });

            let response = client
                .post(&server.http_url())
                .json(&tool_call)
                .send()
                .await?;

            // Should handle gracefully (not crash)
            assert_eq!(response.status(), 200, "Failed for test case: {}", test_name);
            
            let body: Value = response.json().await?;
            assert!(
                body.get("result").is_some() || body.get("error").is_some(),
                "Should have result or error for {}", test_name
            );
        }

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_missing_progress_token_fields() {
    // Test requests with malformed _meta fields
    with_mcp_test_server("missing_progress_fields", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        client.post(&server.http_url()).json(&init).send().await?;

        // Test various malformed _meta scenarios
        let test_cases = vec![
            ("empty_meta", json!({})),
            ("null_meta", json!(null)),
            ("missing_progress_token", json!({"other": "field"})),
            ("progress_token_typo", json!({"progresstoken": "typo"})),
        ];

        for (test_name, meta) in test_cases {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": format!("test-{}", test_name),
                "method": "tools/call",
                "params": {
                    "name": "echo",  
                    "arguments": {"message": format!("test-{}", test_name)},
                    "_meta": meta
                }
            });

            let response = client
                .post(&server.http_url())
                .json(&tool_call)
                .send()
                .await?;

            // Should not use chunked encoding without valid progress token
            let headers = response.headers();
            assert!(
                headers.contains_key("content-length"),
                "Should use content-length for {}", test_name
            );
            assert!(
                !headers.contains_key("transfer-encoding"),
                "Should not use chunked encoding for {}", test_name
            );

            let body: Value = response.json().await?;
            assert!(body.get("result").is_some(), "Should have result for {}", test_name);
        }

        Ok(())
    })
    .await
    .unwrap();
}

// ============================================================================
// JSON-RPC Compliance Tests
// ============================================================================

#[tokio::test]
async fn test_progress_notification_jsonrpc_compliance() {
    // Test that all progress-related responses follow JSON-RPC 2.0 spec
    with_mcp_test_server("jsonrpc_compliance", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(5))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        let init_response = client.post(&server.http_url()).json(&init).send().await?;
        let init_body: Value = init_response.json().await?;
        
        // Verify initialize response compliance
        assert_eq!(init_body["jsonrpc"], "2.0");
        assert_eq!(init_body["id"], 1);
        assert!(init_body.get("result").is_some());

        // Tool call with progress token
        let tool_call = json!({
            "jsonrpc": "2.0",
            "id": 42,
            "method": "tools/call",
            "params": {
                "name": "echo",
                "arguments": {"message": "compliance test"},
                "_meta": {
                    "progressToken": "compliance-token"
                }
            }
        });

        let response = client
            .post(&server.http_url())
            .json(&tool_call)
            .send()
            .await?;

        let body: Value = response.json().await?;
        
        // Verify JSON-RPC 2.0 compliance
        assert_eq!(body["jsonrpc"], "2.0", "Must have jsonrpc field with value '2.0'");
        assert_eq!(body["id"], 42, "Must echo the request ID");
        
        // Must have either result or error, but not both
        let has_result = body.get("result").is_some();
        let has_error = body.get("error").is_some();
        assert!(
            has_result || has_error,
            "Response must have either 'result' or 'error' field"
        );
        assert!(
            !(has_result && has_error),
            "Response must not have both 'result' and 'error' fields"
        );

        if has_error {
            let error = &body["error"];
            assert!(error.get("code").is_some(), "Error must have 'code' field");
            assert!(error.get("message").is_some(), "Error must have 'message' field");
            
            // Verify error code is valid JSON-RPC error code
            if let Some(code) = error["code"].as_i64() {
                assert!(
                    code >= -32768 && code <= -32000 || code >= -1 && code <= 1000,
                    "Error code should be in valid JSON-RPC range"
                );
            }
        }

        Ok(())
    })
    .await
    .unwrap();
}

// ============================================================================
// Performance and Load Tests
// ============================================================================

#[tokio::test]
async fn test_progress_notifications_performance() {
    // Test performance with rapid progress notifications
    with_mcp_test_server("progress_performance", |server| async move {
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(30))
            .build()?;

        // Initialize
        let init = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {
                "protocolVersion": "2025-06-18",
                "capabilities": {}
            }
        });
        client.post(&server.http_url()).json(&init).send().await?;

        let start_time = std::time::Instant::now();
        let num_requests = 50;

        // Send multiple requests with progress tokens rapidly
        let mut handles = Vec::new();
        for i in 0..num_requests {
            let client = client.clone();
            let url = server.http_url();
            
            let handle = tokio::spawn(async move {
                let tool_call = json!({
                    "jsonrpc": "2.0",
                    "id": i + 2,
                    "method": "tools/call",
                    "params": {
                        "name": "echo",
                        "arguments": {"message": format!("perf test {}", i)},
                        "_meta": {
                            "progressToken": format!("perf-token-{}", i)
                        }
                    }
                });

                let response = client.post(&url).json(&tool_call).send().await?;
                let body: Value = response.json().await?;
                
                Ok::<Value, anyhow::Error>(body)
            });
            
            handles.push(handle);
        }

        // Wait for all requests to complete
        let mut success_count = 0;
        for handle in handles {
            match handle.await? {
                Ok(body) => {
                    if body.get("result").is_some() {
                        success_count += 1;
                    }
                }
                Err(e) => println!("Request failed: {}", e),
            }
        }

        let elapsed = start_time.elapsed();
        println!(
            "Performance test: {}/{} requests succeeded in {:?} ({:.2} req/sec)",
            success_count, num_requests, elapsed, 
            num_requests as f64 / elapsed.as_secs_f64()
        );

        // Should handle at least 80% of requests successfully
        assert!(
            success_count >= (num_requests * 4 / 5),
            "Should handle most requests successfully under load"
        );

        Ok(())
    })
    .await
    .unwrap();
}

#[tokio::test]
async fn test_websocket_progress_notifications_stress() {
    // Stress test WebSocket progress notifications
    with_mcp_connection("ws_progress_stress", |server, mut write, mut read| async move {
        let num_requests = 20;
        let start_time = std::time::Instant::now();

        // Send rapid progress requests
        for i in 0..num_requests {
            let tool_call = json!({
                "jsonrpc": "2.0",
                "id": i + 2,
                "method": "tools/call",
                "params": {
                    "name": "echo",
                    "arguments": {"message": format!("stress test {}", i)},
                    "_meta": {
                        "progressToken": format!("stress-token-{}", i)
                    }
                }
            });

            write.send(Message::Text(tool_call.to_string().into())).await?;
        }

        // Collect responses
        let mut received_responses = 0;
        let timeout_duration = Duration::from_secs(20);
        
        timeout(timeout_duration, async {
            while received_responses < num_requests {
                if let Some(Ok(Message::Text(text))) = read.next().await {
                    let msg: Value = serde_json::from_str(&text.to_string())?;
                    if let Some(id) = msg.get("id").and_then(|id| id.as_u64()) {
                        if id >= 2 && id < 2 + num_requests as u64 {
                            received_responses += 1;
                        }
                    }
                }
            }
            Ok::<(), anyhow::Error>(())
        }).await??;

        let elapsed = start_time.elapsed();
        println!(
            "WebSocket stress test: {}/{} responses in {:?} ({:.2} req/sec)",
            received_responses, num_requests, elapsed,
            num_requests as f64 / elapsed.as_secs_f64()
        );

        assert_eq!(received_responses, num_requests, "Should receive all responses");

        Ok(())
    })
    .await
    .unwrap();
}