agpm_cli/mcp/
settings.rs

1use crate::mcp::models::{ClaudeSettings, McpServerConfig};
2use anyhow::{Context, Result};
3use std::collections::HashMap;
4use std::path::Path;
5
6impl ClaudeSettings {
7    /// Load an existing `.claude/settings.local.json` file or create a new configuration.
8    ///
9    /// This method preserves all existing configurations.
10    pub fn load_or_default(path: &Path) -> Result<Self> {
11        if path.exists() {
12            crate::utils::read_json_file(path).with_context(|| {
13                format!(
14                    "Failed to parse settings file: {}\n\
15                     The file may be malformed or contain invalid JSON.",
16                    path.display()
17                )
18            })
19        } else {
20            Ok(Self::default())
21        }
22    }
23
24    /// Save the settings to `.claude/settings.local.json` file.
25    ///
26    /// The file is written atomically to prevent corruption.
27    pub fn save(&self, path: &Path) -> Result<()> {
28        // Create a backup if the file exists
29        if path.exists() {
30            // Generate backup path at project root: .agpm/backups/claude-code/settings.local.json
31            let backup_path = crate::utils::generate_backup_path(path, "claude-code")?;
32
33            // Ensure backup directory exists
34            if let Some(backup_dir) = backup_path.parent() {
35                if !backup_dir.exists() {
36                    std::fs::create_dir_all(backup_dir).with_context(|| {
37                        format!("Failed to create directory: {}", backup_dir.display())
38                    })?;
39                }
40            }
41
42            std::fs::copy(path, &backup_path).with_context(|| {
43                format!("Failed to create backup of settings at: {}", backup_path.display())
44            })?;
45        }
46
47        // Write with pretty formatting for readability
48        crate::utils::write_json_file(path, self, true)
49            .with_context(|| format!("Failed to write settings to: {}", path.display()))?;
50
51        Ok(())
52    }
53
54    /// Update MCP servers from stored configurations.
55    ///
56    /// This method loads all MCP server configurations from the specified directory
57    /// and merges them into the settings, preserving user-managed servers.
58    pub fn update_mcp_servers(&mut self, mcp_servers_dir: &Path) -> Result<()> {
59        if !mcp_servers_dir.exists() {
60            return Ok(());
61        }
62
63        let mut agpm_servers = HashMap::new();
64
65        // Read all .json files from the mcp-servers directory
66        for entry in std::fs::read_dir(mcp_servers_dir).with_context(|| {
67            format!("Failed to read MCP servers directory: {}", mcp_servers_dir.display())
68        })? {
69            let entry = entry?;
70            let path = entry.path();
71
72            if path.extension().is_some_and(|ext| ext == "json") {
73                let server_config: McpServerConfig = crate::utils::read_json_file(&path)
74                    .with_context(|| {
75                        format!("Failed to parse MCP server file: {}", path.display())
76                    })?;
77
78                // Use the filename without extension as the server name
79                if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
80                    agpm_servers.insert(name.to_string(), server_config);
81                }
82            }
83        }
84
85        // Initialize mcp_servers if None
86        if self.mcp_servers.is_none() {
87            self.mcp_servers = Some(HashMap::new());
88        }
89
90        // Update MCP servers, preserving user-managed ones
91        if let Some(servers) = &mut self.mcp_servers {
92            // Remove old AGPM-managed servers
93            servers
94                .retain(|_, config| config.agpm_metadata.as_ref().is_none_or(|meta| !meta.managed));
95
96            // Add all AGPM-managed servers
97            servers.extend(agpm_servers);
98        }
99
100        Ok(())
101    }
102}