1use 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#[derive(Debug, Clone, Default, Serialize, Deserialize)]
67#[serde(rename_all = "camelCase")]
68pub struct McpConfig {
69 #[serde(default)]
71 pub mcp_servers: HashMap<String, ServerConfig>,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub struct ServerConfig {
78 pub command: String,
80
81 #[serde(default)]
83 pub args: Vec<String>,
84
85 #[serde(default)]
87 pub env: HashMap<String, String>,
88
89 #[serde(default)]
91 pub cwd: Option<String>,
92
93 #[serde(default)]
95 pub disabled: bool,
96}
97
98impl ServerConfig {
99 #[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 #[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 #[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 #[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 #[must_use]
134 pub fn disabled(mut self) -> Self {
135 self.disabled = true;
136 self
137 }
138}
139
140#[derive(Debug)]
146pub enum ConfigError {
147 NotFound(String),
149 ReadError(std::io::Error),
151 ParseError(String),
153 ServerNotFound(String),
155 ServerDisabled(String),
157 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
189impl McpConfig {
194 #[must_use]
196 pub fn new() -> Self {
197 Self::default()
198 }
199
200 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 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 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 #[must_use]
249 pub fn to_json(&self) -> String {
250 serde_json::to_string_pretty(self).unwrap_or_else(|_| "{}".to_string())
251 }
252
253 #[must_use]
255 pub fn to_toml(&self) -> String {
256 toml::to_string_pretty(self).unwrap_or_else(|_| String::new())
257 }
258
259 pub fn add_server(&mut self, name: impl Into<String>, config: ServerConfig) {
261 self.mcp_servers.insert(name.into(), config);
262 }
263
264 #[must_use]
266 pub fn get_server(&self, name: &str) -> Option<&ServerConfig> {
267 self.mcp_servers.get(name)
268 }
269
270 #[must_use]
272 pub fn server_names(&self) -> Vec<&str> {
273 self.mcp_servers.keys().map(String::as_str).collect()
274 }
275
276 #[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 pub fn client(&self, name: &str) -> Result<Client, ConfigError> {
292 self.client_with_cx(name, Cx::for_testing())
293 }
294
295 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 pub fn merge(&mut self, other: McpConfig) {
317 self.mcp_servers.extend(other.mcp_servers);
318 }
319}
320
321fn spawn_client_from_config(
323 name: &str,
324 config: &ServerConfig,
325 cx: Cx,
326) -> Result<Client, ConfigError> {
327 let mut cmd = Command::new(&config.command);
329 cmd.args(&config.args);
330
331 for (key, value) in &config.env {
333 cmd.env(key, value);
334 }
335
336 if let Some(ref cwd) = config.cwd {
338 cmd.current_dir(cwd);
339 }
340
341 cmd.stdin(Stdio::piped());
343 cmd.stdout(Stdio::piped());
344 cmd.stderr(Stdio::inherit());
345
346 let mut child = cmd.spawn().map_err(|e| {
348 ConfigError::SpawnError(format!("Failed to spawn {}: {}", config.command, e))
349 })?;
350
351 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 let transport = StdioTransport::new(stdout, stdin);
361
362 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_and_initialize_client(child, transport, cx, client_info, client_capabilities)
371 .map_err(|e| ConfigError::SpawnError(format!("Initialization failed: {e}")))
372}
373
374fn 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 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(¶ms)
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 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 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 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 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 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 Ok(Client::from_parts(child, transport, cx, session, 30_000))
450}
451
452#[derive(Debug, Clone)]
461pub struct ConfigLoader {
462 search_paths: Vec<PathBuf>,
464}
465
466impl Default for ConfigLoader {
467 fn default() -> Self {
468 Self::new()
469 }
470}
471
472impl ConfigLoader {
473 #[must_use]
475 pub fn new() -> Self {
476 Self {
477 search_paths: default_config_paths(),
478 }
479 }
480
481 #[must_use]
483 pub fn from_path(path: impl Into<PathBuf>) -> Self {
484 Self {
485 search_paths: vec![path.into()],
486 }
487 }
488
489 #[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 #[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 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 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 #[must_use]
539 pub fn search_paths(&self) -> &[PathBuf] {
540 &self.search_paths
541 }
542
543 #[must_use]
545 pub fn existing_paths(&self) -> Vec<&PathBuf> {
546 self.search_paths.iter().filter(|p| p.exists()).collect()
547 }
548}
549
550#[must_use]
556pub fn default_config_paths() -> Vec<PathBuf> {
557 let mut paths = Vec::new();
558
559 paths.push(PathBuf::from(".mcp/config.json"));
561 paths.push(PathBuf::from(".vscode/mcp.json"));
562
563 if let Some(home) = dirs::home_dir() {
565 #[cfg(target_os = "macos")]
566 {
567 paths.push(home.join("Library/Application Support/Claude/claude_desktop_config.json"));
569 paths.push(home.join(".config/mcp/config.json"));
571 }
572
573 #[cfg(target_os = "windows")]
574 {
575 if let Some(appdata) = dirs::data_dir() {
577 paths.push(appdata.join("Claude/claude_desktop_config.json"));
578 }
579 paths.push(home.join(".mcp/config.json"));
581 }
582
583 #[cfg(target_os = "linux")]
584 {
585 if let Ok(xdg_config) = env::var("XDG_CONFIG_HOME") {
587 let xdg_path = PathBuf::from(xdg_config);
588 paths.push(xdg_path.join("mcp/config.json"));
589 paths.push(xdg_path.join("claude/config.json"));
590 } else {
591 paths.push(home.join(".config/mcp/config.json"));
592 paths.push(home.join(".config/claude/config.json"));
593 }
594 }
595 }
596
597 paths
598}
599
600#[must_use]
602pub fn claude_desktop_config_path() -> Option<PathBuf> {
603 #[cfg(target_os = "macos")]
604 {
605 dirs::home_dir()
606 .map(|h| h.join("Library/Application Support/Claude/claude_desktop_config.json"))
607 }
608
609 #[cfg(target_os = "windows")]
610 {
611 dirs::data_dir().map(|d| d.join("Claude/claude_desktop_config.json"))
612 }
613
614 #[cfg(target_os = "linux")]
615 {
616 if let Ok(xdg_config) = env::var("XDG_CONFIG_HOME") {
617 Some(PathBuf::from(xdg_config).join("claude/config.json"))
618 } else {
619 dirs::home_dir().map(|h| h.join(".config/claude/config.json"))
620 }
621 }
622}
623
624#[cfg(test)]
629mod tests {
630 use super::*;
631
632 #[test]
633 fn test_empty_config() {
634 let config = McpConfig::new();
635 assert!(config.mcp_servers.is_empty());
636 assert!(config.server_names().is_empty());
637 }
638
639 #[test]
640 fn test_parse_json_config() {
641 let json = r#"{
642 "mcpServers": {
643 "filesystem": {
644 "command": "npx",
645 "args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"],
646 "env": {
647 "DEBUG": "true"
648 }
649 },
650 "other": {
651 "command": "python",
652 "args": ["-m", "my_server"],
653 "disabled": true
654 }
655 }
656 }"#;
657
658 let config = McpConfig::from_json(json).unwrap();
659
660 assert_eq!(config.mcp_servers.len(), 2);
661
662 let fs = config.get_server("filesystem").unwrap();
663 assert_eq!(fs.command, "npx");
664 assert_eq!(fs.args.len(), 3);
665 assert_eq!(fs.env.get("DEBUG"), Some(&"true".to_string()));
666 assert!(!fs.disabled);
667
668 let other = config.get_server("other").unwrap();
669 assert!(other.disabled);
670
671 let enabled = config.enabled_servers();
673 assert_eq!(enabled.len(), 1);
674 assert!(enabled.contains(&"filesystem"));
675 }
676
677 #[test]
678 fn test_parse_toml_config() {
679 let toml = r#"
681 [mcpServers.filesystem]
682 command = "npx"
683 args = ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
684
685 [mcpServers.filesystem.env]
686 DEBUG = "true"
687 "#;
688
689 let config = McpConfig::from_toml(toml).unwrap();
690
691 let fs = config.get_server("filesystem").unwrap();
692 assert_eq!(fs.command, "npx");
693 assert_eq!(fs.args.len(), 3);
694 assert_eq!(fs.env.get("DEBUG"), Some(&"true".to_string()));
695 }
696
697 #[test]
698 fn test_server_config_builder() {
699 let config = ServerConfig::new("python")
700 .with_args(["-m", "my_server"])
701 .with_env("API_KEY", "secret")
702 .with_cwd("/opt/server");
703
704 assert_eq!(config.command, "python");
705 assert_eq!(config.args, vec!["-m", "my_server"]);
706 assert_eq!(config.env.get("API_KEY"), Some(&"secret".to_string()));
707 assert_eq!(config.cwd, Some("/opt/server".to_string()));
708 assert!(!config.disabled);
709 }
710
711 #[test]
712 fn test_config_add_and_get_server() {
713 let mut config = McpConfig::new();
714
715 config.add_server("test", ServerConfig::new("echo").with_args(["hello"]));
716
717 assert_eq!(config.server_names().len(), 1);
718 assert!(config.get_server("test").is_some());
719 assert!(config.get_server("nonexistent").is_none());
720 }
721
722 #[test]
723 fn test_config_merge() {
724 let mut base = McpConfig::new();
725 base.add_server("server1", ServerConfig::new("cmd1"));
726 base.add_server("server2", ServerConfig::new("cmd2"));
727
728 let mut overlay = McpConfig::new();
729 overlay.add_server("server2", ServerConfig::new("cmd2-override"));
730 overlay.add_server("server3", ServerConfig::new("cmd3"));
731
732 base.merge(overlay);
733
734 assert_eq!(base.mcp_servers.len(), 3);
735 assert_eq!(base.get_server("server1").unwrap().command, "cmd1");
736 assert_eq!(base.get_server("server2").unwrap().command, "cmd2-override");
737 assert_eq!(base.get_server("server3").unwrap().command, "cmd3");
738 }
739
740 #[test]
741 fn test_config_serialization() {
742 let mut config = McpConfig::new();
743 config.add_server(
744 "test",
745 ServerConfig::new("npx")
746 .with_args(["-y", "server"])
747 .with_env("KEY", "value"),
748 );
749
750 let json = config.to_json();
751 assert!(json.contains("mcpServers"));
752 assert!(json.contains("npx"));
753
754 let toml = config.to_toml();
755 assert!(toml.contains("mcpServers"));
756 assert!(toml.contains("npx"));
757 }
758
759 #[test]
760 fn test_config_loader() {
761 let loader = ConfigLoader::new()
762 .with_path("/custom/path/config.json")
763 .with_priority_path("/priority/config.json");
764
765 let paths = loader.search_paths();
766 assert!(
767 paths
768 .first()
769 .unwrap()
770 .to_str()
771 .unwrap()
772 .contains("priority")
773 );
774 assert!(paths.last().unwrap().to_str().unwrap().contains("custom"));
775 }
776
777 #[test]
778 fn test_error_server_not_found() {
779 let config = McpConfig::new();
780 let result = config.client("nonexistent");
781 assert!(matches!(result, Err(ConfigError::ServerNotFound(_))));
782 }
783
784 #[test]
785 fn test_error_server_disabled() {
786 let mut config = McpConfig::new();
787 config.add_server("disabled", ServerConfig::new("echo").disabled());
788
789 let result = config.client("disabled");
790 assert!(matches!(result, Err(ConfigError::ServerDisabled(_))));
791 }
792
793 #[test]
794 fn test_default_config_paths_not_empty() {
795 let paths = default_config_paths();
796 assert!(!paths.is_empty());
797 }
798
799 #[test]
800 fn test_config_error_display() {
801 let errors = vec![
802 (ConfigError::NotFound("path".into()), "not found"),
803 (
804 ConfigError::ServerNotFound("name".into()),
805 "server not found",
806 ),
807 (ConfigError::ServerDisabled("name".into()), "disabled"),
808 (ConfigError::ParseError("msg".into()), "parse"),
809 ];
810
811 for (error, expected) in errors {
812 assert!(
813 error.to_string().to_lowercase().contains(expected),
814 "Expected '{}' to contain '{}'",
815 error,
816 expected
817 );
818 }
819 }
820}