Skip to main content

fastmcp_client/
mcp_config.rs

1//! MCP Configuration file support for server registry.
2//!
3//! This module provides configuration file parsing and client creation from config.
4//! It supports the standard MCP configuration format used by Claude Desktop and other clients.
5//!
6//! # Configuration Format
7//!
8//! The standard format is JSON with the following structure:
9//!
10//! ```json
11//! {
12//!     "mcpServers": {
13//!         "server-name": {
14//!             "command": "npx",
15//!             "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path"],
16//!             "env": {
17//!                 "EXAMPLE_ENV": "example-value"
18//!             }
19//!         }
20//!     }
21//! }
22//! ```
23//!
24//! # Usage
25//!
26//! ```ignore
27//! use fastmcp_rust::mcp_config::{McpConfig, ConfigLoader};
28//!
29//! // Load from default location
30//! let config = ConfigLoader::default()?.load()?;
31//!
32//! // Create a client for a specific server
33//! let client = config.client("filesystem")?;
34//!
35//! // Or load from a specific path
36//! let config = McpConfig::from_file("/path/to/config.json")?;
37//! ```
38//!
39//! # Default Locations
40//!
41//! Config files are searched in order:
42//! - macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`
43//! - Windows: `%APPDATA%\Claude\claude_desktop_config.json`
44//! - Linux: `~/.config/claude/config.json`
45//!
46//! Project-specific configs can be in `.vscode/mcp.json` or `.mcp/config.json`.
47
48use std::collections::HashMap;
49use std::env;
50use std::path::{Path, PathBuf};
51use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio};
52
53use asupersync::Cx;
54use fastmcp_core::{McpError, McpResult};
55use fastmcp_transport::StdioTransport;
56use serde::{Deserialize, Serialize};
57
58use crate::{Client, ClientSession};
59use fastmcp_protocol::{ClientCapabilities, ClientInfo};
60
61// ============================================================================
62// Configuration Types
63// ============================================================================
64
65/// MCP configuration file containing server definitions.
66#[derive(Debug, Clone, Default, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68pub struct McpConfig {
69    /// Server configurations keyed by name.
70    #[serde(default)]
71    pub mcp_servers: HashMap<String, ServerConfig>,
72}
73
74/// Configuration for a single MCP server.
75#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub struct ServerConfig {
78    /// Command to execute (e.g., "npx", "uvx", "python").
79    pub command: String,
80
81    /// Arguments to pass to the command.
82    #[serde(default)]
83    pub args: Vec<String>,
84
85    /// Environment variables to set.
86    #[serde(default)]
87    pub env: HashMap<String, String>,
88
89    /// Working directory for the server process.
90    #[serde(default)]
91    pub cwd: Option<String>,
92
93    /// Whether the server is disabled.
94    #[serde(default)]
95    pub disabled: bool,
96}
97
98impl ServerConfig {
99    /// Creates a new server configuration.
100    #[must_use]
101    pub fn new(command: impl Into<String>) -> Self {
102        Self {
103            command: command.into(),
104            args: Vec::new(),
105            env: HashMap::new(),
106            cwd: None,
107            disabled: false,
108        }
109    }
110
111    /// Adds arguments.
112    #[must_use]
113    pub fn with_args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
114        self.args = args.into_iter().map(Into::into).collect();
115        self
116    }
117
118    /// Adds an environment variable.
119    #[must_use]
120    pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
121        self.env.insert(key.into(), value.into());
122        self
123    }
124
125    /// Sets the working directory.
126    #[must_use]
127    pub fn with_cwd(mut self, cwd: impl Into<String>) -> Self {
128        self.cwd = Some(cwd.into());
129        self
130    }
131
132    /// Sets the disabled flag.
133    #[must_use]
134    pub fn disabled(mut self) -> Self {
135        self.disabled = true;
136        self
137    }
138}
139
140// ============================================================================
141// Configuration Errors
142// ============================================================================
143
144/// Errors that can occur during configuration operations.
145#[derive(Debug)]
146pub enum ConfigError {
147    /// Configuration file not found.
148    NotFound(String),
149    /// Failed to read configuration file.
150    ReadError(std::io::Error),
151    /// Failed to parse configuration.
152    ParseError(String),
153    /// Server not found in configuration.
154    ServerNotFound(String),
155    /// Server is disabled.
156    ServerDisabled(String),
157    /// Failed to spawn server process.
158    SpawnError(String),
159}
160
161impl std::fmt::Display for ConfigError {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        match self {
164            ConfigError::NotFound(path) => write!(f, "Configuration file not found: {path}"),
165            ConfigError::ReadError(e) => write!(f, "Failed to read configuration: {e}"),
166            ConfigError::ParseError(e) => write!(f, "Failed to parse configuration: {e}"),
167            ConfigError::ServerNotFound(name) => write!(f, "Server not found: {name}"),
168            ConfigError::ServerDisabled(name) => write!(f, "Server is disabled: {name}"),
169            ConfigError::SpawnError(e) => write!(f, "Failed to spawn server: {e}"),
170        }
171    }
172}
173
174impl std::error::Error for ConfigError {
175    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
176        match self {
177            ConfigError::ReadError(e) => Some(e),
178            _ => None,
179        }
180    }
181}
182
183impl From<ConfigError> for McpError {
184    fn from(err: ConfigError) -> Self {
185        McpError::internal_error(err.to_string())
186    }
187}
188
189// ============================================================================
190// Configuration Loading
191// ============================================================================
192
193impl McpConfig {
194    /// Creates an empty configuration.
195    #[must_use]
196    pub fn new() -> Self {
197        Self::default()
198    }
199
200    /// Loads configuration from a JSON file.
201    ///
202    /// # Errors
203    ///
204    /// Returns an error if the file cannot be read or parsed.
205    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, ConfigError> {
206        let path = path.as_ref();
207        let content = std::fs::read_to_string(path).map_err(|e| {
208            if e.kind() == std::io::ErrorKind::NotFound {
209                ConfigError::NotFound(path.display().to_string())
210            } else {
211                ConfigError::ReadError(e)
212            }
213        })?;
214
215        Self::from_json(&content)
216    }
217
218    /// Parses configuration from a JSON string.
219    ///
220    /// # Errors
221    ///
222    /// Returns an error if parsing fails.
223    pub fn from_json(json: &str) -> Result<Self, ConfigError> {
224        serde_json::from_str(json).map_err(|e| ConfigError::ParseError(e.to_string()))
225    }
226
227    /// Parses configuration from a TOML string.
228    ///
229    /// TOML format is an alternative supported by some MCP clients:
230    ///
231    /// ```toml
232    /// [mcp_servers.filesystem]
233    /// command = "npx"
234    /// args = ["-y", "@modelcontextprotocol/server-filesystem", "/path"]
235    ///
236    /// [mcp_servers.filesystem.env]
237    /// EXAMPLE_ENV = "example-value"
238    /// ```
239    ///
240    /// # Errors
241    ///
242    /// Returns an error if parsing fails.
243    pub fn from_toml(toml: &str) -> Result<Self, ConfigError> {
244        toml::from_str(toml).map_err(|e| ConfigError::ParseError(e.to_string()))
245    }
246
247    /// Serializes configuration to JSON.
248    #[must_use]
249    pub fn to_json(&self) -> String {
250        serde_json::to_string_pretty(self).unwrap_or_else(|_| "{}".to_string())
251    }
252
253    /// Serializes configuration to TOML.
254    #[must_use]
255    pub fn to_toml(&self) -> String {
256        toml::to_string_pretty(self).unwrap_or_else(|_| String::new())
257    }
258
259    /// Adds a server configuration.
260    pub fn add_server(&mut self, name: impl Into<String>, config: ServerConfig) {
261        self.mcp_servers.insert(name.into(), config);
262    }
263
264    /// Gets a server configuration by name.
265    #[must_use]
266    pub fn get_server(&self, name: &str) -> Option<&ServerConfig> {
267        self.mcp_servers.get(name)
268    }
269
270    /// Returns all server names.
271    #[must_use]
272    pub fn server_names(&self) -> Vec<&str> {
273        self.mcp_servers.keys().map(String::as_str).collect()
274    }
275
276    /// Returns enabled server names.
277    #[must_use]
278    pub fn enabled_servers(&self) -> Vec<&str> {
279        self.mcp_servers
280            .iter()
281            .filter(|(_, c)| !c.disabled)
282            .map(|(n, _)| n.as_str())
283            .collect()
284    }
285
286    /// Creates a client for a server by name.
287    ///
288    /// # Errors
289    ///
290    /// Returns an error if the server is not found, disabled, or fails to start.
291    pub fn client(&self, name: &str) -> Result<Client, ConfigError> {
292        self.client_with_cx(name, Cx::for_request())
293    }
294
295    /// Creates a client with a provided Cx for cancellation support.
296    ///
297    /// # Errors
298    ///
299    /// Returns an error if the server is not found, disabled, or fails to start.
300    pub fn client_with_cx(&self, name: &str, cx: Cx) -> Result<Client, ConfigError> {
301        let config = self
302            .mcp_servers
303            .get(name)
304            .ok_or_else(|| ConfigError::ServerNotFound(name.to_string()))?;
305
306        if config.disabled {
307            return Err(ConfigError::ServerDisabled(name.to_string()));
308        }
309
310        spawn_client_from_config(name, config, cx)
311    }
312
313    /// Merges another configuration into this one.
314    ///
315    /// Servers from `other` override servers with the same name.
316    pub fn merge(&mut self, other: McpConfig) {
317        self.mcp_servers.extend(other.mcp_servers);
318    }
319}
320
321/// Spawns a client from a server configuration.
322fn spawn_client_from_config(
323    name: &str,
324    config: &ServerConfig,
325    cx: Cx,
326) -> Result<Client, ConfigError> {
327    // Build the command
328    let mut cmd = Command::new(&config.command);
329    cmd.args(&config.args);
330
331    // Set environment variables
332    for (key, value) in &config.env {
333        cmd.env(key, value);
334    }
335
336    // Set working directory if specified
337    if let Some(ref cwd) = config.cwd {
338        cmd.current_dir(cwd);
339    }
340
341    // Configure stdio
342    cmd.stdin(Stdio::piped());
343    cmd.stdout(Stdio::piped());
344    cmd.stderr(Stdio::inherit());
345
346    // Spawn the process
347    let mut child = cmd.spawn().map_err(|e| {
348        ConfigError::SpawnError(format!("Failed to spawn {}: {}", config.command, e))
349    })?;
350
351    // Get stdin/stdout handles
352    let stdin = child.stdin.take().ok_or_else(|| {
353        ConfigError::SpawnError(format!("Failed to get stdin for server '{name}'"))
354    })?;
355    let stdout = child.stdout.take().ok_or_else(|| {
356        ConfigError::SpawnError(format!("Failed to get stdout for server '{name}'"))
357    })?;
358
359    // Create transport
360    let transport = StdioTransport::new(stdout, stdin);
361
362    // Create client info
363    let client_info = ClientInfo {
364        name: format!("fastmcp-client:{name}"),
365        version: env!("CARGO_PKG_VERSION").to_owned(),
366    };
367    let client_capabilities = ClientCapabilities::default();
368
369    // Create client and initialize
370    create_and_initialize_client(child, transport, cx, client_info, client_capabilities)
371        .map_err(|e| ConfigError::SpawnError(format!("Initialization failed: {e}")))
372}
373
374/// Creates a client and performs initialization handshake.
375fn create_and_initialize_client(
376    child: Child,
377    mut transport: StdioTransport<ChildStdout, ChildStdin>,
378    cx: Cx,
379    client_info: ClientInfo,
380    client_capabilities: ClientCapabilities,
381) -> McpResult<Client> {
382    use fastmcp_protocol::{
383        InitializeParams, InitializeResult, JsonRpcMessage, JsonRpcRequest, PROTOCOL_VERSION,
384    };
385    use fastmcp_transport::Transport;
386
387    // Send initialize request
388    let params = InitializeParams {
389        protocol_version: PROTOCOL_VERSION.to_string(),
390        capabilities: client_capabilities.clone(),
391        client_info: client_info.clone(),
392    };
393
394    let params_value = serde_json::to_value(&params)
395        .map_err(|e| McpError::internal_error(format!("Failed to serialize params: {e}")))?;
396
397    let request = JsonRpcRequest::new("initialize", Some(params_value), 1);
398
399    transport
400        .send(&cx, &JsonRpcMessage::Request(request))
401        .map_err(crate::transport_error_to_mcp)?;
402
403    // Receive response
404    let response = loop {
405        let message = transport.recv(&cx).map_err(crate::transport_error_to_mcp)?;
406        if let JsonRpcMessage::Response(resp) = message {
407            break resp;
408        }
409    };
410
411    // Check for error
412    if let Some(error) = response.error {
413        return Err(McpError::new(
414            fastmcp_core::McpErrorCode::from(error.code),
415            error.message,
416        ));
417    }
418
419    // Parse result
420    let result_value = response
421        .result
422        .ok_or_else(|| McpError::internal_error("No result in initialize response"))?;
423
424    let init_result: InitializeResult = serde_json::from_value(result_value)
425        .map_err(|e| McpError::internal_error(format!("Failed to parse initialize result: {e}")))?;
426
427    // Send initialized notification
428    let notification = JsonRpcRequest {
429        jsonrpc: std::borrow::Cow::Borrowed(fastmcp_protocol::JSONRPC_VERSION),
430        method: "initialized".to_string(),
431        params: Some(serde_json::json!({})),
432        id: None,
433    };
434
435    transport
436        .send(&cx, &JsonRpcMessage::Request(notification))
437        .map_err(crate::transport_error_to_mcp)?;
438
439    // Create session
440    let session = ClientSession::new(
441        client_info,
442        client_capabilities,
443        init_result.server_info,
444        init_result.capabilities,
445        init_result.protocol_version,
446    );
447
448    // Return client
449    Ok(Client::from_parts(child, transport, cx, session, 30_000))
450}
451
452// ============================================================================
453// Configuration Loader
454// ============================================================================
455
456/// Loader for finding and loading MCP configurations.
457///
458/// This handles platform-specific default locations and searching
459/// multiple potential config file paths.
460#[derive(Debug, Clone)]
461pub struct ConfigLoader {
462    /// Paths to search for configuration files.
463    search_paths: Vec<PathBuf>,
464}
465
466impl Default for ConfigLoader {
467    fn default() -> Self {
468        Self::new()
469    }
470}
471
472impl ConfigLoader {
473    /// Creates a new loader with default search paths.
474    #[must_use]
475    pub fn new() -> Self {
476        Self {
477            search_paths: default_config_paths(),
478        }
479    }
480
481    /// Creates a loader with a single specific path.
482    #[must_use]
483    pub fn from_path(path: impl Into<PathBuf>) -> Self {
484        Self {
485            search_paths: vec![path.into()],
486        }
487    }
488
489    /// Adds a search path.
490    #[must_use]
491    pub fn with_path(mut self, path: impl Into<PathBuf>) -> Self {
492        self.search_paths.push(path.into());
493        self
494    }
495
496    /// Prepends a search path (searched first).
497    #[must_use]
498    pub fn with_priority_path(mut self, path: impl Into<PathBuf>) -> Self {
499        self.search_paths.insert(0, path.into());
500        self
501    }
502
503    /// Loads configuration from the first existing file.
504    ///
505    /// # Errors
506    ///
507    /// Returns an error if no configuration file is found or parsing fails.
508    pub fn load(&self) -> Result<McpConfig, ConfigError> {
509        for path in &self.search_paths {
510            if path.exists() {
511                return McpConfig::from_file(path);
512            }
513        }
514
515        Err(ConfigError::NotFound(
516            "No MCP configuration file found".to_string(),
517        ))
518    }
519
520    /// Loads and merges all existing configuration files.
521    ///
522    /// Later files override earlier ones.
523    pub fn load_all(&self) -> McpConfig {
524        let mut config = McpConfig::new();
525
526        for path in &self.search_paths {
527            if path.exists() {
528                if let Ok(loaded) = McpConfig::from_file(path) {
529                    config.merge(loaded);
530                }
531            }
532        }
533
534        config
535    }
536
537    /// Returns all search paths.
538    #[must_use]
539    pub fn search_paths(&self) -> &[PathBuf] {
540        &self.search_paths
541    }
542
543    /// Returns paths that exist.
544    #[must_use]
545    pub fn existing_paths(&self) -> Vec<&PathBuf> {
546        self.search_paths.iter().filter(|p| p.exists()).collect()
547    }
548}
549
550// ============================================================================
551// Default Config Paths
552// ============================================================================
553
554/// Returns platform-specific default configuration paths.
555#[must_use]
556pub fn default_config_paths() -> Vec<PathBuf> {
557    let mut paths = Vec::new();
558
559    // Project-specific configs (current directory)
560    paths.push(PathBuf::from(".mcp/config.json"));
561    paths.push(PathBuf::from(".vscode/mcp.json"));
562
563    // User-level configs
564    if let Some(home) = dirs::home_dir() {
565        #[cfg(target_os = "macos")]
566        {
567            // Claude Desktop on macOS
568            paths.push(home.join("Library/Application Support/Claude/claude_desktop_config.json"));
569            // Generic MCP config
570            paths.push(home.join(".config/mcp/config.json"));
571        }
572
573        #[cfg(target_os = "windows")]
574        {
575            // Claude Desktop on Windows
576            if let Some(appdata) = dirs::data_dir() {
577                paths.push(appdata.join("Claude/claude_desktop_config.json"));
578            }
579            // Generic MCP config
580            paths.push(home.join(".mcp/config.json"));
581        }
582
583        #[cfg(all(unix, not(target_os = "macos")))]
584        {
585            // XDG config directory — applies to Linux and every other
586            // non-macOS Unix (FreeBSD, NetBSD, OpenBSD, Illumos, etc.).
587            // Mirrors the same broadening done in
588            // `claude_desktop_config_path` so callers iterating both
589            // surfaces don't see a Linux/BSD asymmetry.
590            //
591            // Per XDG Base Directory Specification: empty XDG_CONFIG_HOME
592            // is equivalent to unset (`env::var` returns Ok("") for an
593            // empty value, so check `is_empty` explicitly to avoid
594            // emitting relative `mcp/config.json` / `claude/config.json`
595            // paths against the caller's CWD).
596            if let Some(xdg_config) = env::var("XDG_CONFIG_HOME").ok().filter(|s| !s.is_empty()) {
597                let xdg_path = PathBuf::from(xdg_config);
598                paths.push(xdg_path.join("mcp/config.json"));
599                paths.push(xdg_path.join("claude/config.json"));
600            } else {
601                paths.push(home.join(".config/mcp/config.json"));
602                paths.push(home.join(".config/claude/config.json"));
603            }
604        }
605    }
606
607    paths
608}
609
610/// Returns the Claude Desktop configuration path for the current platform.
611#[must_use]
612pub fn claude_desktop_config_path() -> Option<PathBuf> {
613    #[cfg(target_os = "macos")]
614    {
615        dirs::home_dir()
616            .map(|h| h.join("Library/Application Support/Claude/claude_desktop_config.json"))
617    }
618
619    #[cfg(target_os = "windows")]
620    {
621        dirs::data_dir().map(|d| d.join("Claude/claude_desktop_config.json"))
622    }
623
624    #[cfg(all(unix, not(target_os = "macos")))]
625    {
626        // Per the XDG Base Directory Specification: "If $XDG_CONFIG_HOME
627        // is either not set or empty, a default equal to $HOME/.config
628        // should be used." `env::var` returns Ok("") for an empty value,
629        // so check `is_empty` explicitly to avoid emitting a relative
630        // `claude/config.json` path.
631        if let Some(xdg_config) = env::var("XDG_CONFIG_HOME").ok().filter(|s| !s.is_empty()) {
632            Some(PathBuf::from(xdg_config).join("claude/config.json"))
633        } else {
634            dirs::home_dir().map(|h| h.join(".config/claude/config.json"))
635        }
636    }
637
638    #[cfg(not(any(target_os = "macos", target_os = "windows", unix)))]
639    {
640        None
641    }
642}
643
644// ============================================================================
645// Tests
646// ============================================================================
647
648#[cfg(test)]
649mod tests {
650    use super::*;
651
652    #[test]
653    fn test_empty_config() {
654        let config = McpConfig::new();
655        assert!(config.mcp_servers.is_empty());
656        assert!(config.server_names().is_empty());
657    }
658
659    #[test]
660    fn test_parse_json_config() {
661        let json = r#"{
662            "mcpServers": {
663                "filesystem": {
664                    "command": "npx",
665                    "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
666                    "env": {
667                        "DEBUG": "true"
668                    }
669                },
670                "other": {
671                    "command": "python",
672                    "args": ["-m", "my_server"],
673                    "disabled": true
674                }
675            }
676        }"#;
677
678        let config = McpConfig::from_json(json).unwrap();
679
680        assert_eq!(config.mcp_servers.len(), 2);
681
682        let fs = config.get_server("filesystem").unwrap();
683        assert_eq!(fs.command, "npx");
684        assert_eq!(fs.args.len(), 3);
685        assert_eq!(fs.env.get("DEBUG"), Some(&"true".to_string()));
686        assert!(!fs.disabled);
687
688        let other = config.get_server("other").unwrap();
689        assert!(other.disabled);
690
691        // enabled_servers should only return non-disabled servers
692        let enabled = config.enabled_servers();
693        assert_eq!(enabled.len(), 1);
694        assert!(enabled.contains(&"filesystem"));
695    }
696
697    #[test]
698    fn test_parse_toml_config() {
699        // Note: serde rename_all="camelCase" applies to TOML too
700        let toml = r#"
701            [mcpServers.filesystem]
702            command = "npx"
703            args = ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
704
705            [mcpServers.filesystem.env]
706            DEBUG = "true"
707        "#;
708
709        let config = McpConfig::from_toml(toml).unwrap();
710
711        let fs = config.get_server("filesystem").unwrap();
712        assert_eq!(fs.command, "npx");
713        assert_eq!(fs.args.len(), 3);
714        assert_eq!(fs.env.get("DEBUG"), Some(&"true".to_string()));
715    }
716
717    #[test]
718    fn test_server_config_builder() {
719        let config = ServerConfig::new("python")
720            .with_args(["-m", "my_server"])
721            .with_env("EXAMPLE_ENV", "example-value")
722            .with_cwd("/opt/server");
723
724        assert_eq!(config.command, "python");
725        assert_eq!(config.args, vec!["-m", "my_server"]);
726        assert_eq!(
727            config.env.get("EXAMPLE_ENV"),
728            Some(&"example-value".to_string())
729        );
730        assert_eq!(config.cwd, Some("/opt/server".to_string()));
731        assert!(!config.disabled);
732    }
733
734    #[test]
735    fn test_config_add_and_get_server() {
736        let mut config = McpConfig::new();
737
738        config.add_server("test", ServerConfig::new("echo").with_args(["hello"]));
739
740        assert_eq!(config.server_names().len(), 1);
741        assert!(config.get_server("test").is_some());
742        assert!(config.get_server("nonexistent").is_none());
743    }
744
745    #[test]
746    fn test_config_merge() {
747        let mut base = McpConfig::new();
748        base.add_server("server1", ServerConfig::new("cmd1"));
749        base.add_server("server2", ServerConfig::new("cmd2"));
750
751        let mut overlay = McpConfig::new();
752        overlay.add_server("server2", ServerConfig::new("cmd2-override"));
753        overlay.add_server("server3", ServerConfig::new("cmd3"));
754
755        base.merge(overlay);
756
757        assert_eq!(base.mcp_servers.len(), 3);
758        assert_eq!(base.get_server("server1").unwrap().command, "cmd1");
759        assert_eq!(base.get_server("server2").unwrap().command, "cmd2-override");
760        assert_eq!(base.get_server("server3").unwrap().command, "cmd3");
761    }
762
763    #[test]
764    fn test_config_serialization() {
765        let mut config = McpConfig::new();
766        config.add_server(
767            "test",
768            ServerConfig::new("npx")
769                .with_args(["-y", "server"])
770                .with_env("KEY", "value"),
771        );
772
773        let json = config.to_json();
774        assert!(json.contains("mcpServers"));
775        assert!(json.contains("npx"));
776
777        let toml = config.to_toml();
778        assert!(toml.contains("mcpServers"));
779        assert!(toml.contains("npx"));
780    }
781
782    #[test]
783    fn test_config_loader() {
784        let loader = ConfigLoader::new()
785            .with_path("/custom/path/config.json")
786            .with_priority_path("/priority/config.json");
787
788        let paths = loader.search_paths();
789        assert!(
790            paths
791                .first()
792                .unwrap()
793                .to_str()
794                .unwrap()
795                .contains("priority")
796        );
797        assert!(paths.last().unwrap().to_str().unwrap().contains("custom"));
798    }
799
800    #[test]
801    fn test_error_server_not_found() {
802        let config = McpConfig::new();
803        let result = config.client("nonexistent");
804        assert!(matches!(result, Err(ConfigError::ServerNotFound(_))));
805    }
806
807    #[test]
808    fn test_error_server_disabled() {
809        let mut config = McpConfig::new();
810        config.add_server("disabled", ServerConfig::new("echo").disabled());
811
812        let result = config.client("disabled");
813        assert!(matches!(result, Err(ConfigError::ServerDisabled(_))));
814    }
815
816    #[test]
817    fn test_default_config_paths_not_empty() {
818        let paths = default_config_paths();
819        assert!(!paths.is_empty());
820    }
821
822    #[test]
823    fn test_config_error_display() {
824        let errors = vec![
825            (ConfigError::NotFound("path".into()), "not found"),
826            (
827                ConfigError::ServerNotFound("name".into()),
828                "server not found",
829            ),
830            (ConfigError::ServerDisabled("name".into()), "disabled"),
831            (ConfigError::ParseError("msg".into()), "parse"),
832        ];
833
834        for (error, expected) in errors {
835            assert!(
836                error.to_string().to_lowercase().contains(expected),
837                "Expected '{}' to contain '{}'",
838                error,
839                expected
840            );
841        }
842    }
843
844    #[test]
845    fn test_config_error_source() {
846        let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "no access");
847        let config_err = ConfigError::ReadError(io_err);
848        assert!(std::error::Error::source(&config_err).is_some());
849
850        let not_found = ConfigError::NotFound("path".into());
851        assert!(std::error::Error::source(&not_found).is_none());
852
853        let parse_err = ConfigError::ParseError("bad".into());
854        assert!(std::error::Error::source(&parse_err).is_none());
855    }
856
857    #[test]
858    fn test_config_error_into_mcp_error() {
859        let err = ConfigError::ServerNotFound("test-srv".into());
860        let mcp_err: McpError = err.into();
861        assert_eq!(mcp_err.code, fastmcp_core::McpErrorCode::InternalError);
862        assert!(mcp_err.message.contains("test-srv"));
863    }
864
865    #[test]
866    fn test_server_config_disabled_builder() {
867        let config = ServerConfig::new("echo").disabled();
868        assert!(config.disabled);
869    }
870
871    #[test]
872    fn test_config_json_round_trip() {
873        let mut config = McpConfig::new();
874        config.add_server(
875            "srv",
876            ServerConfig::new("cmd")
877                .with_args(["a1", "a2"])
878                .with_env("K", "V")
879                .with_cwd("/tmp"),
880        );
881
882        let json = config.to_json();
883        let restored = McpConfig::from_json(&json).expect("round-trip parse");
884        let srv = restored.get_server("srv").expect("server present");
885        assert_eq!(srv.command, "cmd");
886        assert_eq!(srv.args, vec!["a1", "a2"]);
887        assert_eq!(srv.env.get("K"), Some(&"V".to_string()));
888        assert_eq!(srv.cwd.as_deref(), Some("/tmp"));
889    }
890
891    #[test]
892    fn test_config_toml_round_trip() {
893        let mut config = McpConfig::new();
894        config.add_server(
895            "srv",
896            ServerConfig::new("python").with_args(["-m", "server"]),
897        );
898
899        let toml_str = config.to_toml();
900        let restored = McpConfig::from_toml(&toml_str).expect("round-trip parse");
901        let srv = restored.get_server("srv").expect("server present");
902        assert_eq!(srv.command, "python");
903        assert_eq!(srv.args, vec!["-m", "server"]);
904    }
905
906    #[test]
907    fn test_parse_invalid_json() {
908        let result = McpConfig::from_json("not json {{{");
909        assert!(matches!(result, Err(ConfigError::ParseError(_))));
910    }
911
912    #[test]
913    fn test_parse_invalid_toml() {
914        let result = McpConfig::from_toml("[invalid toml = = =");
915        assert!(matches!(result, Err(ConfigError::ParseError(_))));
916    }
917
918    #[test]
919    fn test_from_file_not_found() {
920        let result = McpConfig::from_file("/nonexistent/path/to/config.json");
921        assert!(matches!(result, Err(ConfigError::NotFound(_))));
922    }
923
924    #[test]
925    fn test_config_merge_empty() {
926        let mut base = McpConfig::new();
927        base.add_server("a", ServerConfig::new("cmd_a"));
928        base.merge(McpConfig::new());
929        assert_eq!(base.mcp_servers.len(), 1);
930        assert!(base.get_server("a").is_some());
931    }
932
933    #[test]
934    fn test_config_loader_from_path() {
935        let loader = ConfigLoader::from_path("/specific/path.json");
936        assert_eq!(loader.search_paths().len(), 1);
937        assert_eq!(
938            loader.search_paths()[0],
939            PathBuf::from("/specific/path.json")
940        );
941    }
942
943    #[test]
944    fn test_config_loader_load_no_files_exist() {
945        let loader =
946            ConfigLoader::from_path("/nonexistent/a.json").with_path("/nonexistent/b.json");
947        let result = loader.load();
948        assert!(matches!(result, Err(ConfigError::NotFound(_))));
949    }
950
951    #[test]
952    fn test_config_loader_load_all_no_files() {
953        let loader = ConfigLoader::from_path("/nonexistent/a.json");
954        let config = loader.load_all();
955        assert!(config.mcp_servers.is_empty());
956    }
957
958    #[test]
959    fn test_config_loader_existing_paths_empty() {
960        let loader = ConfigLoader::from_path("/nonexistent/file.json");
961        assert!(loader.existing_paths().is_empty());
962    }
963
964    #[test]
965    fn test_config_loader_default() {
966        let loader = ConfigLoader::default();
967        assert!(!loader.search_paths().is_empty());
968    }
969
970    #[test]
971    fn test_enabled_servers_all_disabled() {
972        let mut config = McpConfig::new();
973        config.add_server("a", ServerConfig::new("cmd").disabled());
974        config.add_server("b", ServerConfig::new("cmd").disabled());
975        assert!(config.enabled_servers().is_empty());
976    }
977
978    #[test]
979    fn test_claude_desktop_config_path_is_some() {
980        // On all supported platforms, this should return Some when home dir is available
981        let path = claude_desktop_config_path();
982        // Home dir is usually available in CI and dev environments
983        if dirs::home_dir().is_some() {
984            assert!(path.is_some());
985        }
986    }
987
988    #[test]
989    fn test_server_config_with_multiple_env_vars() {
990        let config = ServerConfig::new("cmd")
991            .with_env("A", "1")
992            .with_env("B", "2")
993            .with_env("C", "3");
994        assert_eq!(config.env.len(), 3);
995        assert_eq!(config.env.get("A"), Some(&"1".to_string()));
996        assert_eq!(config.env.get("B"), Some(&"2".to_string()));
997        assert_eq!(config.env.get("C"), Some(&"3".to_string()));
998    }
999
1000    #[test]
1001    fn test_config_spawn_error_display() {
1002        let err = ConfigError::SpawnError("process died".into());
1003        let msg = err.to_string().to_lowercase();
1004        assert!(msg.contains("spawn"));
1005        assert!(msg.contains("process died"));
1006    }
1007
1008    #[test]
1009    fn test_config_empty_json_object() {
1010        let config = McpConfig::from_json("{}").expect("parse empty object");
1011        assert!(config.mcp_servers.is_empty());
1012    }
1013
1014    #[test]
1015    fn test_config_json_with_defaults() {
1016        let json = r#"{"mcpServers": {"srv": {"command": "echo"}}}"#;
1017        let config = McpConfig::from_json(json).expect("parse");
1018        let srv = config.get_server("srv").unwrap();
1019        assert!(srv.args.is_empty());
1020        assert!(srv.env.is_empty());
1021        assert!(srv.cwd.is_none());
1022        assert!(!srv.disabled);
1023    }
1024}