use std::collections::HashMap;
use std::time::Duration;
#[derive(Clone, Debug, Default)]
pub struct McpClientConfig {
pub url: String,
pub headers: HashMap<String, String>,
pub connect_timeout: Option<Duration>,
pub read_timeout: Option<Duration>,
}
impl McpClientConfig {
pub fn new(url: impl Into<String>) -> Self {
Self {
url: url.into(),
headers: HashMap::new(),
connect_timeout: None,
read_timeout: None,
}
}
pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.headers.insert(key.into(), value.into());
self
}
pub fn with_headers(mut self, headers: HashMap<String, String>) -> Self {
self.headers.extend(headers);
self
}
pub fn with_bearer_auth(self, token: impl Into<String>) -> Self {
self.with_header("Authorization", format!("Bearer {}", token.into()))
}
pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
self.connect_timeout = Some(timeout);
self
}
pub fn with_read_timeout(mut self, timeout: Duration) -> Self {
self.read_timeout = Some(timeout);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_config() {
let config = McpClientConfig::new("http://localhost:8080");
assert_eq!(config.url, "http://localhost:8080");
assert!(config.headers.is_empty());
}
#[test]
fn test_with_header() {
let config = McpClientConfig::new("http://localhost:8080").with_header("X-Custom", "value");
assert_eq!(config.headers.get("X-Custom"), Some(&"value".to_string()));
}
#[test]
fn test_with_bearer_auth() {
let config = McpClientConfig::new("http://localhost:8080").with_bearer_auth("mytoken");
assert_eq!(
config.headers.get("Authorization"),
Some(&"Bearer mytoken".to_string())
);
}
#[test]
fn test_builder_chain() {
let config = McpClientConfig::new("http://localhost:8080")
.with_header("X-Api-Key", "key123")
.with_connect_timeout(Duration::from_secs(30))
.with_read_timeout(Duration::from_secs(60));
assert_eq!(config.url, "http://localhost:8080");
assert_eq!(config.headers.get("X-Api-Key"), Some(&"key123".to_string()));
assert_eq!(config.connect_timeout, Some(Duration::from_secs(30)));
assert_eq!(config.read_timeout, Some(Duration::from_secs(60)));
}
}