mcp_protocol_sdk/client/
builder.rs

1//! Client builder for MCP clients
2//!
3//! Provides a builder pattern for creating and configuring MCP clients.
4
5use crate::client::McpClient;
6use crate::core::error::McpResult;
7use crate::protocol::types::ClientCapabilities;
8use std::time::Duration;
9
10/// Configuration for retry behavior
11#[derive(Debug, Clone)]
12pub struct RetryConfig {
13    /// Maximum number of retry attempts
14    pub max_attempts: Option<u32>,
15    /// Initial delay between retry attempts (ms)
16    pub initial_delay_ms: u64,
17    /// Maximum delay between retry attempts (ms)
18    pub max_delay_ms: u64,
19    /// Backoff multiplier for retry delays
20    pub backoff_multiplier: f64,
21}
22
23impl Default for RetryConfig {
24    fn default() -> Self {
25        Self {
26            max_attempts: Some(3),
27            initial_delay_ms: 1000,
28            max_delay_ms: 30000,
29            backoff_multiplier: 2.0,
30        }
31    }
32}
33
34/// Configuration for connections
35#[derive(Debug, Clone)]
36pub struct ConnectionConfig {
37    /// Connection timeout in milliseconds
38    pub timeout_ms: u64,
39    /// Whether to enable keep-alive
40    pub keep_alive: bool,
41    /// Whether to enable compression
42    pub compression: bool,
43}
44
45impl Default for ConnectionConfig {
46    fn default() -> Self {
47        Self {
48            timeout_ms: 30000,
49            keep_alive: true,
50            compression: false,
51        }
52    }
53}
54
55/// Builder for creating MCP clients with configuration
56pub struct McpClientBuilder {
57    name: Option<String>,
58    version: Option<String>,
59    capabilities: Option<ClientCapabilities>,
60    timeout: Option<Duration>,
61    retry_config: Option<RetryConfig>,
62    connection_config: Option<ConnectionConfig>,
63}
64
65impl McpClientBuilder {
66    /// Create a new client builder
67    pub fn new() -> Self {
68        Self {
69            name: None,
70            version: None,
71            capabilities: None,
72            timeout: None,
73            retry_config: None,
74            connection_config: None,
75        }
76    }
77
78    /// Set client name
79    pub fn with_name<S: Into<String>>(mut self, name: S) -> Self {
80        self.name = Some(name.into());
81        self
82    }
83
84    /// Set client version
85    pub fn with_version<S: Into<String>>(mut self, version: S) -> Self {
86        self.version = Some(version.into());
87        self
88    }
89
90    /// Set client capabilities
91    pub fn with_capabilities(mut self, capabilities: ClientCapabilities) -> Self {
92        self.capabilities = Some(capabilities);
93        self
94    }
95
96    /// Set request timeout
97    pub fn with_timeout(mut self, timeout: Duration) -> Self {
98        self.timeout = Some(timeout);
99        self
100    }
101
102    /// Set retry configuration
103    pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
104        self.retry_config = Some(retry_config);
105        self
106    }
107
108    /// Set connection configuration
109    pub fn with_connection_config(mut self, connection_config: ConnectionConfig) -> Self {
110        self.connection_config = Some(connection_config);
111        self
112    }
113
114    /// Build the client
115    pub fn build(self) -> McpResult<McpClient> {
116        let mut client = McpClient::new(
117            self.name.unwrap_or_else(|| "mcp-client".to_string()),
118            self.version.unwrap_or_else(|| "1.0.0".to_string()),
119        );
120
121        client.set_capabilities(self.capabilities.unwrap_or_default());
122
123        Ok(client)
124    }
125}
126
127impl Default for McpClientBuilder {
128    fn default() -> Self {
129        Self::new()
130    }
131}
132
133// Legacy alias for compatibility - single definition only
134pub type ClientBuilder = McpClientBuilder;