mcp_common/
client_config.rs1use std::collections::HashMap;
7use std::time::Duration;
8
9#[derive(Clone, Debug, Default)]
25pub struct McpClientConfig {
26 pub url: String,
28 pub headers: HashMap<String, String>,
30 pub connect_timeout: Option<Duration>,
32 pub read_timeout: Option<Duration>,
34}
35
36impl McpClientConfig {
37 pub fn new(url: impl Into<String>) -> Self {
42 Self {
43 url: url.into(),
44 headers: HashMap::new(),
45 connect_timeout: None,
46 read_timeout: None,
47 }
48 }
49
50 pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
56 self.headers.insert(key.into(), value.into());
57 self
58 }
59
60 pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
62 self.headers.extend(headers);
63 self
64 }
65
66 pub fn with_bearer_auth(self, token: impl Into<String>) -> Self {
71 self.with_header("Authorization", format!("Bearer {}", token.into()))
72 }
73
74 pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
79 self.connect_timeout = Some(timeout);
80 self
81 }
82
83 pub fn with_read_timeout(mut self, timeout: Duration) -> Self {
88 self.read_timeout = Some(timeout);
89 self
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn test_new_config() {
99 let config = McpClientConfig::new("http://localhost:8080");
100 assert_eq!(config.url, "http://localhost:8080");
101 assert!(config.headers.is_empty());
102 }
103
104 #[test]
105 fn test_with_header() {
106 let config = McpClientConfig::new("http://localhost:8080").with_header("X-Custom", "value");
107 assert_eq!(config.headers.get("X-Custom"), Some(&"value".to_string()));
108 }
109
110 #[test]
111 fn test_with_bearer_auth() {
112 let config = McpClientConfig::new("http://localhost:8080").with_bearer_auth("mytoken");
113 assert_eq!(
114 config.headers.get("Authorization"),
115 Some(&"Bearer mytoken".to_string())
116 );
117 }
118
119 #[test]
120 fn test_builder_chain() {
121 let config = McpClientConfig::new("http://localhost:8080")
122 .with_header("X-Api-Key", "key123")
123 .with_connect_timeout(Duration::from_secs(30))
124 .with_read_timeout(Duration::from_secs(60));
125
126 assert_eq!(config.url, "http://localhost:8080");
127 assert_eq!(config.headers.get("X-Api-Key"), Some(&"key123".to_string()));
128 assert_eq!(config.connect_timeout, Some(Duration::from_secs(30)));
129 assert_eq!(config.read_timeout, Some(Duration::from_secs(60)));
130 }
131}