mcp_protocol_sdk/client/
builder.rs1use crate::client::McpClient;
6use crate::core::error::McpResult;
7use crate::protocol::types::ClientCapabilities;
8use std::time::Duration;
9
10#[derive(Debug, Clone)]
12pub struct RetryConfig {
13 pub max_attempts: Option<u32>,
15 pub initial_delay_ms: u64,
17 pub max_delay_ms: u64,
19 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#[derive(Debug, Clone)]
36pub struct ConnectionConfig {
37 pub timeout_ms: u64,
39 pub keep_alive: bool,
41 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
55pub 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 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 pub fn with_name<S: Into<String>>(mut self, name: S) -> Self {
80 self.name = Some(name.into());
81 self
82 }
83
84 pub fn with_version<S: Into<String>>(mut self, version: S) -> Self {
86 self.version = Some(version.into());
87 self
88 }
89
90 pub fn with_capabilities(mut self, capabilities: ClientCapabilities) -> Self {
92 self.capabilities = Some(capabilities);
93 self
94 }
95
96 pub fn with_timeout(mut self, timeout: Duration) -> Self {
98 self.timeout = Some(timeout);
99 self
100 }
101
102 pub fn with_retry_config(mut self, retry_config: RetryConfig) -> Self {
104 self.retry_config = Some(retry_config);
105 self
106 }
107
108 pub fn with_connection_config(mut self, connection_config: ConnectionConfig) -> Self {
110 self.connection_config = Some(connection_config);
111 self
112 }
113
114 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
133pub type ClientBuilder = McpClientBuilder;