claude-sdk-rs 1.0.0

Rust SDK for Claude AI with CLI integration - type-safe async API for Claude Code and direct SDK usage
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
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
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tokio::time::{interval, sleep, timeout};
use uuid::Uuid;

use crate::mcp::clients::{MCPClient, StdioMCPClient, WebSocketMCPClient};
use crate::mcp::core::error::{
    circuit_breaker::{CircuitBreakerConfig, CircuitBreakerRegistry},
    WorkflowError,
};
use crate::mcp::health::{ConnectionHealthMonitor, HealthConfig, HealthStatus};
use crate::mcp::load_balancer::{ConnectionInfo, MCPLoadBalancer};
use crate::mcp::transport::TransportType;

/// A borrowed connection that automatically returns to the pool when dropped
pub struct BorrowedConnection {
    client: Arc<RwLock<Box<dyn MCPClient>>>,
    connection_id: String,
    pool: std::sync::Weak<MCPConnectionPool>,
    server_id: String,
}

impl BorrowedConnection {
    pub async fn list_tools(
        &self,
    ) -> Result<Vec<crate::mcp::protocol::ToolDefinition>, WorkflowError> {
        let mut client = self.client.write().await;
        client.list_tools().await
    }

    pub async fn call_tool(
        &self,
        name: &str,
        args: serde_json::Value,
    ) -> Result<crate::mcp::protocol::MCPResponse, WorkflowError> {
        let mut client = self.client.write().await;

        // Convert args to expected format
        let args_map = if args.is_null() {
            None
        } else if let serde_json::Value::Object(map) = args {
            Some(map.into_iter().collect())
        } else {
            Some(HashMap::from([("value".to_string(), args)]))
        };

        let result = client.call_tool(name, args_map).await?;

        // Convert CallToolResult to MCPResponse
        Ok(crate::mcp::protocol::MCPResponse::Result {
            id: uuid::Uuid::new_v4().to_string(),
            result: crate::mcp::protocol::ResponseResult::CallTool(result),
        })
    }

    pub async fn is_connected(&self) -> bool {
        let client = self.client.read().await;
        client.is_connected()
    }

    pub fn connection_id(&self) -> &str {
        &self.connection_id
    }
}

impl Drop for BorrowedConnection {
    fn drop(&mut self) {
        if let Some(pool) = self.pool.upgrade() {
            let server_id = self.server_id.clone();
            let connection_id = self.connection_id.clone();

            // Return connection to pool asynchronously
            tokio::spawn(async move {
                let _ = pool
                    .return_connection_internal(&server_id, &connection_id)
                    .await;
            });
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfig {
    pub max_connections_per_server: usize,
    #[serde(with = "serde_duration")]
    pub connection_timeout: Duration,
    #[serde(with = "serde_duration")]
    pub idle_timeout: Duration,
    pub retry_attempts: usize,
    #[serde(with = "serde_duration")]
    pub retry_delay: Duration,
    #[serde(with = "serde_duration")]
    pub health_check_interval: Duration,
    /// Enable load balancing across multiple connections
    pub enable_load_balancing: bool,
    /// Load balancing strategy
    pub load_balancing_strategy: LoadBalancingStrategy,
    /// Circuit breaker configuration
    pub circuit_breaker: CircuitBreakerConfig,
    /// Health monitoring configuration
    pub health_monitoring: HealthConfig,
    /// Enable automatic reconnection
    pub enable_auto_reconnect: bool,
    /// Exponential backoff configuration
    pub backoff_config: BackoffConfig,
}

// Helper module for serializing Duration as seconds
mod serde_duration {
    use serde::{Deserialize, Deserializer, Serialize, Serializer};
    use std::time::Duration;

    pub fn serialize<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        duration.as_secs().serialize(serializer)
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Duration, D::Error>
    where
        D: Deserializer<'de>,
    {
        let secs = u64::deserialize(deserializer)?;
        Ok(Duration::from_secs(secs))
    }
}

/// Load balancing strategies for connection selection
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum LoadBalancingStrategy {
    /// Round-robin selection
    RoundRobin,
    /// Random selection
    Random,
    /// Least connections first
    LeastConnections,
    /// Health-based selection (prefer healthy connections)
    HealthBased,
}

/// Exponential backoff configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackoffConfig {
    /// Initial delay
    pub initial_delay: Duration,
    /// Maximum delay
    pub max_delay: Duration,
    /// Multiplier for each retry
    pub multiplier: f64,
    /// Add jitter to prevent thundering herd
    pub jitter: bool,
}

impl Default for BackoffConfig {
    fn default() -> Self {
        Self {
            initial_delay: Duration::from_millis(100),
            max_delay: Duration::from_secs(30),
            multiplier: 2.0,
            jitter: true,
        }
    }
}

impl Default for ConnectionConfig {
    fn default() -> Self {
        Self {
            max_connections_per_server: 5,
            connection_timeout: Duration::from_secs(30),
            idle_timeout: Duration::from_secs(300), // 5 minutes
            retry_attempts: 3,
            retry_delay: Duration::from_millis(1000),
            health_check_interval: Duration::from_secs(60),
            enable_load_balancing: true,
            load_balancing_strategy: LoadBalancingStrategy::HealthBased,
            circuit_breaker: CircuitBreakerConfig::default(),
            health_monitoring: HealthConfig::default(),
            enable_auto_reconnect: true,
            backoff_config: BackoffConfig::default(),
        }
    }
}

#[derive(Debug)]
struct PooledConnection {
    client: Arc<RwLock<Box<dyn MCPClient>>>,
    last_used: Arc<RwLock<Instant>>,
    is_healthy: Arc<RwLock<bool>>,
    connection_id: String,
    created_at: Instant,
    use_count: Arc<RwLock<u64>>,
    transport_type: TransportType,
    in_use: Arc<RwLock<bool>>,
}

impl PooledConnection {
    fn new(
        client: Box<dyn MCPClient>,
        connection_id: String,
        transport_type: TransportType,
    ) -> Self {
        let now = Instant::now();
        Self {
            client: Arc::new(RwLock::new(client)),
            last_used: Arc::new(RwLock::new(now)),
            is_healthy: Arc::new(RwLock::new(true)),
            connection_id,
            created_at: now,
            use_count: Arc::new(RwLock::new(0)),
            transport_type,
            in_use: Arc::new(RwLock::new(false)),
        }
    }

    async fn is_expired(&self, idle_timeout: Duration) -> bool {
        let last_used = *self.last_used.read().await;
        last_used.elapsed() > idle_timeout
    }

    async fn touch(&self) {
        let mut last_used = self.last_used.write().await;
        *last_used = Instant::now();
        let mut use_count = self.use_count.write().await;
        *use_count += 1;
    }

    fn age(&self) -> Duration {
        self.created_at.elapsed()
    }

    async fn is_busy(&self) -> bool {
        let in_use = *self.in_use.read().await;
        let last_used = *self.last_used.read().await;
        // Connection is busy if in use OR used very recently
        in_use || last_used.elapsed() < Duration::from_millis(100)
    }

    async fn is_healthy(&self) -> bool {
        *self.is_healthy.read().await
    }

    async fn set_healthy(&self, healthy: bool) {
        let mut is_healthy = self.is_healthy.write().await;
        *is_healthy = healthy;
    }

    async fn set_in_use(&self, in_use: bool) {
        let mut in_use_guard = self.in_use.write().await;
        *in_use_guard = in_use;
    }

    async fn is_available(&self) -> bool {
        let is_healthy = self.is_healthy().await;
        let is_busy = self.is_busy().await;
        let client_guard = self.client.read().await;
        let is_connected = client_guard.is_connected();

        is_healthy && !is_busy && is_connected
    }

    async fn get_use_count(&self) -> u64 {
        *self.use_count.read().await
    }
}

pub struct MCPConnectionPool {
    connections: Arc<RwLock<HashMap<String, Vec<PooledConnection>>>>,
    config: ConnectionConfig,
    server_configs: Arc<RwLock<HashMap<String, (TransportType, String, String)>>>, // server_id -> (transport, client_name, client_version)
    circuit_breakers: Arc<CircuitBreakerRegistry>,
    health_monitor: Arc<ConnectionHealthMonitor>,
    load_balancer: Arc<RwLock<MCPLoadBalancer>>,
    // metrics_collector: Option<Arc<MCPMetricsCollector>>,
    background_tasks: Arc<RwLock<Vec<tokio::task::JoinHandle<()>>>>,
}

impl MCPConnectionPool {
    pub fn new(config: ConnectionConfig) -> Arc<Self> {
        let circuit_breaker_registry = Arc::new(CircuitBreakerRegistry::new());
        let health_monitor = Arc::new(ConnectionHealthMonitor::new(
            config.health_monitoring.clone(),
        ));
        let load_balancer = Arc::new(RwLock::new(MCPLoadBalancer::new(
            config.load_balancing_strategy,
        )));

        Arc::new(Self {
            connections: Arc::new(RwLock::new(HashMap::new())),
            config,
            server_configs: Arc::new(RwLock::new(HashMap::new())),
            circuit_breakers: circuit_breaker_registry,
            health_monitor,
            load_balancer,
            // metrics_collector: None,
            background_tasks: Arc::new(RwLock::new(Vec::new())),
        })
    }

    // Set metrics collector for monitoring
    // pub fn set_metrics_collector(&mut self, collector: Arc<MCPMetricsCollector>) {
    //     self.metrics_collector = Some(collector);
    // }

    /// Record connection request metrics
    fn record_connection_request(&self, _success: bool, _latency: Duration) {
        // if let Some(collector) = &self.metrics_collector {
        //     collector.record_connection_request(success, latency);
        // }
    }

    /// Record health check metrics
    fn record_health_check(&self, _success: bool) {
        // if let Some(collector) = &self.metrics_collector {
        //     collector.record_health_check(success);
        // }
    }

    /// Start background tasks for health monitoring and cleanup
    pub async fn start_background_tasks(self: &Arc<Self>) {
        let mut tasks = self.background_tasks.write().await;

        // Health monitoring task
        let health_task = self.health_monitor.start_background_monitoring().await;
        tasks.push(health_task);

        // Connection cleanup task
        let cleanup_task = self.start_cleanup_task().await;
        tasks.push(cleanup_task);

        // Reconnection task
        if self.config.enable_auto_reconnect {
            let reconnect_task = self.start_reconnection_task().await;
            tasks.push(reconnect_task);
        }

        // Health check task
        let health_check_task = self.start_health_check_task().await;
        tasks.push(health_check_task);
    }

    /// Stop all background tasks
    pub async fn stop_background_tasks(&self) {
        let mut tasks = self.background_tasks.write().await;
        for task in tasks.drain(..) {
            task.abort();
        }
        self.health_monitor.stop_background_monitoring().await;
    }

    pub async fn register_server(
        &self,
        server_id: String,
        transport: TransportType,
        client_name: String,
        client_version: String,
    ) {
        let mut configs = self.server_configs.write().await;
        configs.insert(server_id, (transport, client_name, client_version));
    }

    pub async fn get_connection(
        self: &Arc<Self>,
        server_id: &str,
    ) -> Result<BorrowedConnection, WorkflowError> {
        // Try to get an existing connection first
        if let Some(borrowed_conn) = self.try_get_existing_connection(server_id).await? {
            return Ok(borrowed_conn);
        }

        // Create a new connection with retry logic
        self.create_connection_with_retry(server_id).await
    }

    async fn try_get_existing_connection(
        self: &Arc<Self>,
        server_id: &str,
    ) -> Result<Option<BorrowedConnection>, WorkflowError> {
        let connections = self.connections.read().await;

        if let Some(pool) = connections.get(server_id) {
            // Collect connection info for load balancer
            let mut connection_infos = Vec::new();

            for conn in pool.iter() {
                if !conn.is_expired(self.config.idle_timeout).await {
                    let health_status = match conn.is_healthy().await {
                        true => HealthStatus::Healthy,
                        false => HealthStatus::Unhealthy,
                    };

                    connection_infos.push(ConnectionInfo {
                        connection_id: conn.connection_id.clone(),
                        server_id: server_id.to_string(),
                        health_status,
                        response_time: None, // Would need to track this
                        use_count: conn.get_use_count().await,
                        is_available: conn.is_available().await,
                    });
                }
            }

            // Use load balancer to select the best connection
            let load_balancer = self.load_balancer.read().await;
            let Some(selected_id) = load_balancer
                .select_connection(server_id, &connection_infos)
                .await?
            else {
                return Ok(None);
            };

            // Find the selected connection and mark it as in use
            for conn in pool.iter() {
                if conn.connection_id != selected_id || !conn.is_available().await {
                    continue;
                }

                conn.set_in_use(true).await;
                conn.touch().await;

                return Ok(Some(BorrowedConnection {
                    client: Arc::clone(&conn.client),
                    connection_id: conn.connection_id.clone(),
                    pool: Arc::downgrade(self),
                    server_id: server_id.to_string(),
                }));
            }
        }

        Ok(None)
    }

    async fn create_connection_with_retry(
        self: &Arc<Self>,
        server_id: &str,
    ) -> Result<BorrowedConnection, WorkflowError> {
        let configs = self.server_configs.read().await;
        let (transport, client_name, client_version) = configs
            .get(server_id)
            .ok_or_else(|| WorkflowError::MCPError {
                message: format!("Server {} not registered", server_id),
            })?
            .clone();
        drop(configs);

        let mut last_error = None;

        for attempt in 0..self.config.retry_attempts {
            let start_time = Instant::now();

            match self
                .create_single_connection(&transport, &client_name, &client_version)
                .await
            {
                Ok(client) => {
                    let latency = start_time.elapsed();
                    self.record_connection_request(true, latency);

                    // Create a new connection for the pool
                    let pool_client = self
                        .create_single_connection(&transport, &client_name, &client_version)
                        .await?;
                    let connection_id =
                        self.add_to_pool(server_id, pool_client, &transport).await?;

                    // Start monitoring the new connection
                    self.health_monitor
                        .start_monitoring(connection_id.clone(), server_id.to_string())
                        .await;

                    // Return a borrowed connection for the new client
                    return self.create_borrowed_connection(server_id, client).await;
                }
                Err(error) => {
                    let latency = start_time.elapsed();
                    self.record_connection_request(false, latency);

                    last_error = Some(error);
                    if attempt < self.config.retry_attempts - 1 {
                        tracing::warn!(
                            "Connection attempt {} failed for server {}, retrying in {:?}",
                            attempt + 1,
                            server_id,
                            self.config.retry_delay
                        );
                        sleep(self.config.retry_delay).await;
                    }
                }
            }
        }

        Err(
            last_error.unwrap_or_else(|| WorkflowError::MCPConnectionError {
                message: "Failed to create connection after retries".to_string(),
            }),
        )
    }

    async fn create_single_connection(
        &self,
        transport: &TransportType,
        client_name: &str,
        client_version: &str,
    ) -> Result<Box<dyn MCPClient>, WorkflowError> {
        let mut client: Box<dyn MCPClient> = match transport {
            TransportType::Stdio { command, args, .. } => {
                Box::new(StdioMCPClient::new(command.clone(), args.clone()))
            }
            TransportType::WebSocket { url, .. } => Box::new(WebSocketMCPClient::new(url.clone())),
            TransportType::Http { .. } => {
                return Err(WorkflowError::MCPError {
                    message: "HTTP transport not yet supported for connection pooling".to_string(),
                });
            }
        };

        // Connect with timeout
        timeout(self.config.connection_timeout, client.connect())
            .await
            .map_err(|_| WorkflowError::MCPConnectionError {
                message: "Connection timeout".to_string(),
            })??;

        // Initialize with timeout
        timeout(
            self.config.connection_timeout,
            client.initialize(client_name, client_version),
        )
        .await
        .map_err(|_| WorkflowError::MCPConnectionError {
            message: "Initialization timeout".to_string(),
        })??;

        Ok(client)
    }

    async fn add_to_pool(
        &self,
        server_id: &str,
        client: Box<dyn MCPClient>,
        transport_type: &TransportType,
    ) -> Result<String, WorkflowError> {
        let mut connections = self.connections.write().await;
        let pool = connections
            .entry(server_id.to_string())
            .or_insert_with(Vec::new);

        // Check if we're at the connection limit
        if pool.len() >= self.config.max_connections_per_server {
            // Remove the oldest connection (LRU)
            let mut oldest_index = None;
            let mut oldest_time = Instant::now();

            for (index, conn) in pool.iter().enumerate() {
                let last_used = *conn.last_used.read().await;
                if last_used < oldest_time {
                    oldest_time = last_used;
                    oldest_index = Some(index);
                }
            }

            if let Some(index) = oldest_index {
                let old_conn = pool.remove(index);
                let _ = old_conn.client.write().await.disconnect().await;
                self.health_monitor
                    .stop_monitoring(&old_conn.connection_id)
                    .await;
            }
        }

        let connection_id = format!("{}_{}", server_id, Uuid::new_v4());
        let pooled_conn =
            PooledConnection::new(client, connection_id.clone(), transport_type.clone());
        pool.push(pooled_conn);

        tracing::debug!(
            "Added connection {} to pool for server {}",
            connection_id,
            server_id
        );
        Ok(connection_id)
    }

    /// Internal method to return a connection to the pool
    pub async fn return_connection_internal(
        &self,
        server_id: &str,
        connection_id: &str,
    ) -> Result<(), WorkflowError> {
        let connections = self.connections.read().await;
        if let Some(pool) = connections.get(server_id) {
            for conn in pool.iter() {
                if conn.connection_id == connection_id {
                    conn.set_in_use(false).await;
                    tracing::debug!(
                        "Returned connection {} to pool for server {}",
                        connection_id,
                        server_id
                    );
                    break;
                }
            }
        }
        Ok(())
    }

    /// Create a borrowed connection wrapper
    async fn create_borrowed_connection(
        self: &Arc<Self>,
        server_id: &str,
        _client: Box<dyn MCPClient>,
    ) -> Result<BorrowedConnection, WorkflowError> {
        // Create a new connection for the pool
        let pool_client = self.create_single_connection_from_server(server_id).await?;
        let connection_id = self
            .add_to_pool(
                server_id,
                pool_client,
                &self.get_transport_for_server(server_id).await?,
            )
            .await?;

        // Create the borrowed connection
        let connections = self.connections.read().await;
        if let Some(pool) = connections.get(server_id) {
            for conn in pool.iter() {
                if conn.connection_id == connection_id {
                    conn.set_in_use(true).await;
                    return Ok(BorrowedConnection {
                        client: Arc::clone(&conn.client),
                        connection_id: conn.connection_id.clone(),
                        pool: Arc::downgrade(self),
                        server_id: server_id.to_string(),
                    });
                }
            }
        }

        Err(WorkflowError::MCPError {
            message: "Failed to create borrowed connection".to_string(),
        })
    }

    /// Get transport type for a server
    async fn get_transport_for_server(
        &self,
        server_id: &str,
    ) -> Result<TransportType, WorkflowError> {
        let configs = self.server_configs.read().await;
        let (transport, _, _) = configs
            .get(server_id)
            .ok_or_else(|| WorkflowError::MCPError {
                message: format!("Server {} not registered", server_id),
            })?
            .clone();
        Ok(transport)
    }

    /// Create a single connection for a registered server
    async fn create_single_connection_from_server(
        &self,
        server_id: &str,
    ) -> Result<Box<dyn MCPClient>, WorkflowError> {
        let configs = self.server_configs.read().await;
        let (transport, client_name, client_version) = configs
            .get(server_id)
            .ok_or_else(|| WorkflowError::MCPError {
                message: format!("Server {} not registered", server_id),
            })?
            .clone();
        drop(configs);

        self.create_single_connection(&transport, &client_name, &client_version)
            .await
    }

    pub async fn health_check(&self) -> Result<HashMap<String, bool>, WorkflowError> {
        let mut results = HashMap::new();
        let connections = self.connections.read().await;

        for (server_id, pool) in connections.iter() {
            // Check both pool health and circuit breaker state
            let mut healthy_count = 0;
            for conn in pool.iter() {
                if conn.is_healthy().await {
                    healthy_count += 1;
                }
            }

            let circuit_breaker = self.circuit_breakers.get(server_id).await;
            let circuit_state = if let Some(breaker) = circuit_breaker {
                breaker.read().await.state()
            } else {
                crate::mcp::core::error::circuit_breaker::CircuitState::Closed
            };

            let is_healthy = healthy_count > 0
                && circuit_state != crate::mcp::core::error::circuit_breaker::CircuitState::Open;

            results.insert(server_id.clone(), is_healthy);
        }

        Ok(results)
    }

    pub async fn cleanup_expired_connections(&self) -> Result<usize, WorkflowError> {
        let mut connections = self.connections.write().await;
        let mut cleaned_count = 0;

        for pool in connections.values_mut() {
            let original_len = pool.len();
            let mut to_remove = Vec::new();

            for (index, conn) in pool.iter().enumerate() {
                if conn.is_expired(self.config.idle_timeout).await {
                    to_remove.push(index);
                }
            }

            // Remove connections in reverse order to maintain indices
            for &index in to_remove.iter().rev() {
                pool.remove(index);
            }

            cleaned_count += original_len - pool.len();
        }

        Ok(cleaned_count)
    }

    pub async fn disconnect_all(&self) -> Result<(), WorkflowError> {
        let mut connections = self.connections.write().await;

        for pool in connections.values_mut() {
            for conn in pool.drain(..) {
                let mut client = conn.client.write().await;
                let _ = client.disconnect().await;
            }
        }

        Ok(())
    }

    pub async fn get_pool_stats(&self) -> HashMap<String, PoolStats> {
        let connections = self.connections.read().await;
        let mut stats = HashMap::new();

        for (server_id, pool) in connections.iter() {
            let mut healthy_count = 0;
            let mut connected_count = 0;
            let mut busy_count = 0;
            let mut total_use_count = 0;

            for conn in pool.iter() {
                if conn.is_healthy().await {
                    healthy_count += 1;
                }

                let client = conn.client.read().await;
                if client.is_connected() {
                    connected_count += 1;
                }

                if conn.is_busy().await {
                    busy_count += 1;
                }

                total_use_count += conn.get_use_count().await;
            }

            stats.insert(
                server_id.clone(),
                PoolStats {
                    total_connections: pool.len(),
                    healthy_connections: healthy_count,
                    connected_connections: connected_count,
                    busy_connections: busy_count,
                    average_age_seconds: if !pool.is_empty() {
                        pool.iter().map(|conn| conn.age().as_secs()).sum::<u64>()
                            / pool.len() as u64
                    } else {
                        0
                    },
                    total_use_count,
                },
            );
        }

        stats
    }

    /// Get detailed health information including circuit breaker metrics
    pub async fn get_detailed_health(&self) -> DetailedHealthInfo {
        let connections = self.connections.read().await;
        let mut server_health = HashMap::new();

        for (server_id, pool) in connections.iter() {
            let circuit_breaker = self.circuit_breakers.get(server_id).await;
            // let circuit_metrics = circuit_breaker.metrics();
            let circuit_state = if let Some(breaker) = circuit_breaker {
                breaker.read().await.state()
            } else {
                crate::mcp::core::error::circuit_breaker::CircuitState::Closed
            };

            let mut healthy_count = 0;
            let mut connected_count = 0;
            let mut busy_count = 0;
            let mut total_use_count = 0u64;

            for conn in pool.iter() {
                if *conn.is_healthy.read().await {
                    healthy_count += 1;
                }
                if conn.client.read().await.is_connected() {
                    connected_count += 1;
                }
                if conn.is_busy().await {
                    busy_count += 1;
                }
                total_use_count += *conn.use_count.read().await;
            }

            let pool_stats = PoolStats {
                total_connections: pool.len(),
                healthy_connections: healthy_count,
                connected_connections: connected_count,
                busy_connections: busy_count,
                average_age_seconds: if !pool.is_empty() {
                    pool.iter().map(|conn| conn.age().as_secs()).sum::<u64>() / pool.len() as u64
                } else {
                    0
                },
                total_use_count,
            };

            server_health.insert(
                server_id.clone(),
                ServerHealthInfo {
                    pool_stats,
                    circuit_state,
                    // circuit_metrics,
                },
            );
        }

        let health_summary = self.health_monitor.get_health_summary().await;

        DetailedHealthInfo {
            server_health,
            overall_summary: health_summary,
        }
    }

    /// Start the cleanup task for removing expired connections
    #[allow(clippy::excessive_nesting)]
    async fn start_cleanup_task(&self) -> tokio::task::JoinHandle<()> {
        let connections = Arc::clone(&self.connections);
        let health_monitor = Arc::clone(&self.health_monitor);
        let config = self.config.clone();

        tokio::spawn(async move {
            let mut interval = interval(config.health_check_interval);

            loop {
                interval.tick().await;

                let mut connections_guard = connections.write().await;
                for (server_id, pool) in connections_guard.iter_mut() {
                    let original_len = pool.len();
                    let mut removed_connections = Vec::new();

                    // We need to handle async calls, so we'll collect expired connections first
                    let mut expired_indices = Vec::new();
                    for (index, conn) in pool.iter().enumerate() {
                        if conn.is_expired(config.idle_timeout).await {
                            expired_indices.push(index);
                            removed_connections.push(conn.connection_id.clone());
                        }
                    }

                    // Remove expired connections in reverse order
                    for &index in expired_indices.iter().rev() {
                        pool.remove(index);
                    }

                    let removed_count = original_len - pool.len();
                    if removed_count > 0 {
                        tracing::info!(
                            "Cleaned up {} expired connections for server {}",
                            removed_count,
                            server_id
                        );

                        // Stop monitoring removed connections
                        for connection_id in removed_connections {
                            health_monitor.stop_monitoring(&connection_id).await;
                        }
                    }
                }
            }
        })
    }

    /// Start the reconnection task for automatically reconnecting failed connections
    async fn start_reconnection_task(&self) -> tokio::task::JoinHandle<()> {
        let connections = Arc::clone(&self.connections);
        let server_configs = Arc::clone(&self.server_configs);
        let circuit_breakers = Arc::clone(&self.circuit_breakers);
        let config = self.config.clone();

        tokio::spawn(async move {
            let mut interval = interval(config.health_check_interval * 2); // Less frequent than cleanup

            loop {
                interval.tick().await;

                let server_configs_guard = server_configs.read().await;
                for server_id in server_configs_guard.keys() {
                    // Check circuit breaker state
                    let circuit_breaker = circuit_breakers.get(server_id).await;
                    let circuit_state = if let Some(breaker) = circuit_breaker {
                        breaker.read().await.state()
                    } else {
                        crate::mcp::core::error::circuit_breaker::CircuitState::Closed
                    };

                    // Only attempt reconnection if circuit is not open
                    if circuit_state != crate::mcp::core::error::circuit_breaker::CircuitState::Open
                    {
                        let connections_guard = connections.read().await;
                        if let Some(pool) = connections_guard.get(server_id) {
                            let mut healthy_count = 0;
                            for conn in pool.iter() {
                                if *conn.is_healthy.read().await
                                    && conn.client.read().await.is_connected()
                                {
                                    healthy_count += 1;
                                }
                            }

                            // If we have fewer healthy connections than desired, try to add more
                            if healthy_count < config.max_connections_per_server / 2 {
                                tracing::debug!(
                                    "Server {} has only {} healthy connections, attempting to add more",
                                    server_id,
                                    healthy_count
                                );
                                // Note: In a real implementation, we'd need access to the pool methods
                                // This would require restructuring to avoid circular dependencies
                            }
                        }
                    }
                }
                drop(server_configs_guard);
            }
        })
    }

    /// Force reconnection for a specific server
    pub async fn force_reconnect(&self, server_id: &str) -> Result<(), WorkflowError> {
        // Reset circuit breaker
        let circuit_breaker = self.circuit_breakers.get(server_id).await;
        if let Some(breaker) = circuit_breaker {
            breaker.write().await.reset();
        }

        // Clear existing connections
        let mut connections = self.connections.write().await;
        if let Some(pool) = connections.get_mut(server_id) {
            let mut removed_connections = Vec::new();
            for conn in pool.drain(..) {
                removed_connections.push(conn.connection_id.clone());
                let _ = conn.client.write().await.disconnect().await;
            }

            // Stop monitoring removed connections
            for connection_id in removed_connections {
                self.health_monitor.stop_monitoring(&connection_id).await;
            }
        }

        tracing::info!("Forced reconnection for server {}", server_id);
        Ok(())
    }

    // /// Get circuit breaker metrics for all servers
    // pub async fn get_circuit_breaker_metrics(&self) -> HashMap<String, crate::core::error::circuit_breaker::CircuitBreakerMetrics> {
    //     let mut metrics = HashMap::new();
    //
    //     let breakers = self.circuit_breakers.all().await;
    //     for (service, breaker) in breakers {
    //         metrics.insert(service, breaker.metrics());
    //     }
    //
    //     metrics
    // }

    /// Perform health checks on all connections and update their health status
    pub async fn perform_health_checks(&self) -> Result<(), WorkflowError> {
        let connections = self.connections.read().await;

        for (server_id, pool) in connections.iter() {
            for conn in pool.iter() {
                // Skip connections that are currently in use
                if conn.is_busy().await {
                    continue;
                }

                // Perform health check
                let mut client_guard = conn.client.write().await;
                match self
                    .health_monitor
                    .check_connection_health(&conn.connection_id, &mut client_guard)
                    .await
                {
                    Ok(health_status) => {
                        let is_healthy = matches!(
                            health_status,
                            HealthStatus::Healthy | HealthStatus::Degraded
                        );
                        conn.set_healthy(is_healthy).await;
                        self.record_health_check(is_healthy);

                        if !is_healthy {
                            tracing::warn!(
                                "Connection {} for server {} is unhealthy: {:?}",
                                conn.connection_id,
                                server_id,
                                health_status
                            );
                        }
                    }
                    Err(e) => {
                        conn.set_healthy(false).await;
                        self.record_health_check(false);
                        tracing::error!(
                            "Health check failed for connection {} on server {}: {}",
                            conn.connection_id,
                            server_id,
                            e
                        );
                    }
                }
            }
        }

        Ok(())
    }

    /// Attempt to recover failed connections by creating new ones
    pub async fn recover_failed_connections(self: &Arc<Self>) -> Result<(), WorkflowError> {
        let servers_to_recover = {
            let connections = self.connections.read().await;
            let mut servers = Vec::new();

            for (server_id, pool) in connections.iter() {
                let healthy_count = pool
                    .iter()
                    .filter(|_conn| {
                        // We need to handle async operations here
                        true // Placeholder - we'll need to refactor this
                    })
                    .count();

                // If we have fewer than half the max connections healthy, attempt recovery
                if healthy_count < self.config.max_connections_per_server / 2 {
                    servers.push(server_id.clone());
                }
            }

            servers
        };

        for server_id in servers_to_recover {
            tracing::info!("Attempting to recover connections for server {}", server_id);

            // Try to create a new connection to replace failed ones
            match self.create_connection_with_retry(&server_id).await {
                Ok(_) => {
                    tracing::info!("Successfully recovered connection for server {}", server_id);
                }
                Err(e) => {
                    tracing::error!(
                        "Failed to recover connection for server {}: {}",
                        server_id,
                        e
                    );
                }
            }
        }

        Ok(())
    }

    /// Update load balancer weights based on current pool health
    pub async fn update_load_balancer_weights(self: &Arc<Self>) -> Result<(), WorkflowError> {
        let pool_stats = self.get_pool_stats().await;
        let load_balancer = self.load_balancer.write().await;
        load_balancer.update_server_weights(pool_stats).await;
        Ok(())
    }

    /// Start the health check task for monitoring and recovery
    async fn start_health_check_task(self: &Arc<Self>) -> tokio::task::JoinHandle<()> {
        let pool = Arc::clone(self);
        let config = self.config.clone();

        tokio::spawn(async move {
            let mut interval = interval(config.health_check_interval);

            loop {
                interval.tick().await;

                // Perform health checks
                if let Err(e) = pool.perform_health_checks().await {
                    tracing::error!("Health check task failed: {}", e);
                }

                // Attempt to recover failed connections
                if config.enable_auto_reconnect {
                    if let Err(e) = pool.recover_failed_connections().await {
                        tracing::error!("Connection recovery task failed: {}", e);
                    }
                }

                // Update load balancer weights
                if let Err(e) = pool.update_load_balancer_weights().await {
                    tracing::error!("Load balancer weight update failed: {}", e);
                }

                tracing::debug!("Health check task completed");
            }
        })
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PoolStats {
    pub total_connections: usize,
    pub healthy_connections: usize,
    pub connected_connections: usize,
    pub busy_connections: usize,
    pub average_age_seconds: u64,
    pub total_use_count: u64,
}

/// Server-specific health information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerHealthInfo {
    pub pool_stats: PoolStats,
    pub circuit_state: crate::mcp::core::error::circuit_breaker::CircuitState,
    // pub circuit_metrics: crate::core::error::circuit_breaker::CircuitBreakerMetrics,
}

/// Detailed health information for the entire pool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetailedHealthInfo {
    pub server_health: HashMap<String, ServerHealthInfo>,
    pub overall_summary: crate::mcp::health::HealthSummary,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mcp::transport::TransportType;

    #[tokio::test]
    async fn test_connection_pool_creation() {
        let config = ConnectionConfig::default();
        let pool = MCPConnectionPool::new(config);

        let stats = pool.get_pool_stats().await;
        assert!(stats.is_empty());
    }

    #[tokio::test]
    async fn test_server_registration() {
        let pool = MCPConnectionPool::new(ConnectionConfig::default());

        pool.register_server(
            "test-server".to_string(),
            TransportType::Stdio {
                command: "echo".to_string(),
                args: vec!["hello".to_string()],
                auto_restart: true,
                max_restarts: 3,
            },
            "test-client".to_string(),
            "1.0.0".to_string(),
        )
        .await;

        let configs = pool.server_configs.read().await;
        assert!(configs.contains_key("test-server"));
    }

    #[tokio::test]
    async fn test_pool_stats() {
        let pool = MCPConnectionPool::new(ConnectionConfig::default());

        pool.register_server(
            "test-server".to_string(),
            TransportType::WebSocket {
                url: "ws://localhost:8080".to_string(),
                heartbeat_interval: None,
                reconnect_config: crate::mcp::transport::ReconnectConfig::default(),
            },
            "test-client".to_string(),
            "1.0.0".to_string(),
        )
        .await;

        let stats = pool.get_pool_stats().await;
        assert_eq!(stats.len(), 0); // No connections created yet
    }

    #[tokio::test]
    async fn test_cleanup_expired_connections() {
        let pool = MCPConnectionPool::new(ConnectionConfig::default());

        let cleaned = pool.cleanup_expired_connections().await.unwrap();
        assert_eq!(cleaned, 0); // No connections to clean
    }
}