| 1 | mod deeplink; |
| 2 | mod entry; |
| 3 | mod error; |
| 4 | mod file; |
| 5 | mod json_file; |
| 6 | mod toml_file; |
| 7 | mod wsl; |
| 8 | |
| 9 | #[cfg(test)] |
| 10 | mod tests; |
| 11 | |
| 12 | use std::path::{Path, PathBuf}; |
| 13 | |
| 14 | use crate::McpEndpoint; |
| 15 | use entry::ServerEntry; |
| 16 | pub use error::InstallError; |
| 17 | |
| 18 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 19 | pub enum Target { |
| 20 | ClaudeCode, |
| 21 | Codex, |
| 22 | CopilotCli, |
| 23 | VsCode, |
| 24 | Cursor, |
| 25 | } |
| 26 | |
| 27 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 28 | pub enum Location { |
| 29 | Windows, |
| 30 | Wsl { distro: String }, |
| 31 | Native, |
| 32 | } |
| 33 | |
| 34 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 35 | pub struct InstallState { |
| 36 | pub target: Target, |
| 37 | pub available: bool, |
| 38 | pub installed_at: Vec<Location>, |
| 39 | pub stale: bool, |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 43 | pub enum InstallReport { |
| 44 | Written(Vec<Location>), |
| 45 | OpenUrl(String), |
| 46 | } |
| 47 | |
| 48 | #[derive(Debug, Clone, Default)] |
| 49 | pub struct Homes { |
| 50 | pub entries: Vec<(Location, PathBuf)>, |
| 51 | } |
| 52 | |
| 53 | impl Homes { |
| 54 | pub fn detect() -> Homes { |
| 55 | let mut entries = Vec::new(); |
| 56 | #[cfg(windows)] |
| 57 | { |
| 58 | if let Some(profile) = std::env::var_os("USERPROFILE") { |
| 59 | entries.push((Location::Windows, PathBuf::from(profile))); |
| 60 | } |
| 61 | if let Some((distro, home)) = wsl::default_home() { |
| 62 | entries.push((Location::Wsl { distro }, home)); |
| 63 | } |
| 64 | } |
| 65 | #[cfg(not(windows))] |
| 66 | if let Some(home) = std::env::var_os("HOME") { |
| 67 | entries.push((Location::Native, PathBuf::from(home))); |
| 68 | } |
| 69 | Homes { entries } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | #[cfg(windows)] |
| 74 | pub fn wsl_reachable(port: u16) -> Option<bool> { |
| 75 | wsl::reachable(port) |
| 76 | } |
| 77 | |
| 78 | #[cfg(not(windows))] |
| 79 | pub fn wsl_reachable(_port: u16) -> Option<bool> { |
| 80 | None |
| 81 | } |
| 82 | |
| 83 | enum Format { |
| 84 | Json { container: &'static str }, |
| 85 | Toml, |
| 86 | } |
| 87 | |
| 88 | impl Target { |
| 89 | pub const ALL: [Target; 5] = [ |
| 90 | Target::ClaudeCode, |
| 91 | Target::Codex, |
| 92 | Target::CopilotCli, |
| 93 | Target::VsCode, |
| 94 | Target::Cursor, |
| 95 | ]; |
| 96 | |
| 97 | pub fn label(self) -> &'static str { |
| 98 | match self { |
| 99 | Target::ClaudeCode => "Claude Code", |
| 100 | Target::Codex => "Codex CLI", |
| 101 | Target::CopilotCli => "Copilot CLI", |
| 102 | Target::VsCode => "VS Code", |
| 103 | Target::Cursor => "Cursor", |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | pub fn config_hint(self) -> &'static str { |
| 108 | match self { |
| 109 | Target::ClaudeCode => "~/.claude.json", |
| 110 | Target::Codex => "~/.codex/config.toml", |
| 111 | Target::CopilotCli => "~/.copilot/mcp-config.json", |
| 112 | Target::VsCode => "Code/User/mcp.json", |
| 113 | Target::Cursor => "~/.cursor/mcp.json", |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | fn format(self) -> Format { |
| 118 | match self { |
| 119 | Target::Codex => Format::Toml, |
| 120 | Target::VsCode => Format::Json { |
| 121 | container: "servers", |
| 122 | }, |
| 123 | _ => Format::Json { |
| 124 | container: "mcpServers", |
| 125 | }, |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | fn config_path(self, location: &Location, home: &Path) -> PathBuf { |
| 130 | match self { |
| 131 | Target::ClaudeCode => home.join(".claude.json"), |
| 132 | Target::Codex => home.join(".codex").join("config.toml"), |
| 133 | Target::CopilotCli => home.join(".copilot").join("mcp-config.json"), |
| 134 | Target::Cursor => home.join(".cursor").join("mcp.json"), |
| 135 | Target::VsCode => { |
| 136 | let base = match location { |
| 137 | Location::Windows => home.join("AppData").join("Roaming"), |
| 138 | _ => home.join(".config"), |
| 139 | }; |
| 140 | base.join("Code").join("User").join("mcp.json") |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn marker_paths(self, location: &Location, home: &Path) -> Vec<PathBuf> { |
| 146 | let config = self.config_path(location, home); |
| 147 | let mut markers = vec![config.clone()]; |
| 148 | match self { |
| 149 | Target::ClaudeCode => markers.push(home.join(".claude")), |
| 150 | _ => markers.extend(config.parent().map(Path::to_path_buf)), |
| 151 | } |
| 152 | markers |
| 153 | } |
| 154 | |
| 155 | fn binary_names(self) -> &'static [&'static str] { |
| 156 | match self { |
| 157 | Target::ClaudeCode => &["claude"], |
| 158 | Target::Codex => &["codex"], |
| 159 | Target::CopilotCli => &["copilot"], |
| 160 | Target::VsCode => &["code"], |
| 161 | Target::Cursor => &["cursor"], |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | fn is_available_in(self, location: &Location, home: &Path) -> bool { |
| 166 | self.marker_paths(location, home) |
| 167 | .iter() |
| 168 | .any(|path| path.exists()) |
| 169 | || matches!(location, Location::Windows | Location::Native) |
| 170 | && self.binary_names().iter().any(|name| binary_on_path(name)) |
| 171 | } |
| 172 | |
| 173 | fn json_entry(self, entry: &ServerEntry) -> serde_json::Value { |
| 174 | match self { |
| 175 | Target::ClaudeCode | Target::VsCode => entry.to_json(true, false), |
| 176 | Target::CopilotCli => entry.to_json(true, true), |
| 177 | _ => entry.to_json(false, false), |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | fn read_entry(self, path: &Path) -> Result<Option<ServerEntry>, InstallError> { |
| 182 | match self.format() { |
| 183 | Format::Json { container } => json_file::read_entry(path, container), |
| 184 | Format::Toml => toml_file::read_entry(path), |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | fn write_entry(self, path: &Path, entry: &ServerEntry) -> Result<(), InstallError> { |
| 189 | match self.format() { |
| 190 | Format::Json { container } => { |
| 191 | json_file::upsert(path, container, &self.json_entry(entry)) |
| 192 | } |
| 193 | Format::Toml => toml_file::upsert(path, entry), |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | fn binary_on_path(name: &str) -> bool { |
| 199 | let Some(paths) = std::env::var_os("PATH") else { |
| 200 | return false; |
| 201 | }; |
| 202 | std::env::split_paths(&paths).any(|directory| { |
| 203 | ["", ".exe", ".cmd"] |
| 204 | .iter() |
| 205 | .any(|extension| directory.join(format!("{name}{extension}")).is_file()) |
| 206 | }) |
| 207 | } |
| 208 | |
| 209 | pub fn status(target: Target, endpoint: &McpEndpoint, homes: &Homes) -> InstallState { |
| 210 | let expected = ServerEntry::expected(endpoint); |
| 211 | let mut state = InstallState { |
| 212 | target, |
| 213 | available: false, |
| 214 | installed_at: Vec::new(), |
| 215 | stale: false, |
| 216 | }; |
| 217 | for (location, home) in &homes.entries { |
| 218 | state.available |= target.is_available_in(location, home); |
| 219 | let path = target.config_path(location, home); |
| 220 | if let Ok(Some(found)) = target.read_entry(&path) { |
| 221 | state.stale |= found != expected; |
| 222 | state.installed_at.push(location.clone()); |
| 223 | } |
| 224 | } |
| 225 | state |
| 226 | } |
| 227 | |
| 228 | pub fn install( |
| 229 | target: Target, |
| 230 | endpoint: &McpEndpoint, |
| 231 | homes: &Homes, |
| 232 | ) -> Result<InstallReport, InstallError> { |
| 233 | let entry = ServerEntry::expected(endpoint); |
| 234 | if target == Target::VsCode { |
| 235 | return Ok(InstallReport::OpenUrl(deeplink::vscode(&entry))); |
| 236 | } |
| 237 | let mut written = Vec::new(); |
| 238 | for (location, home) in &homes.entries { |
| 239 | if !target.is_available_in(location, home) { |
| 240 | continue; |
| 241 | } |
| 242 | target.write_entry(&target.config_path(location, home), &entry)?; |
| 243 | written.push(location.clone()); |
| 244 | } |
| 245 | if written.is_empty() { |
| 246 | return Err(InstallError::NotAvailable(target)); |
| 247 | } |
| 248 | Ok(InstallReport::Written(written)) |
| 249 | } |
| 250 | |
| 251 | pub fn uninstall(target: Target, homes: &Homes) -> Result<(), InstallError> { |
| 252 | for (location, home) in &homes.entries { |
| 253 | let path = target.config_path(location, home); |
| 254 | match target.format() { |
| 255 | Format::Json { container } => json_file::remove(&path, container)?, |
| 256 | Format::Toml => toml_file::remove(&path)?, |
| 257 | } |
| 258 | } |
| 259 | Ok(()) |
| 260 | } |
| 261 | |
| 262 | pub fn reinstall_all( |
| 263 | endpoint: &McpEndpoint, |
| 264 | homes: &Homes, |
| 265 | ) -> Vec<(Target, Result<(), InstallError>)> { |
| 266 | let entry = ServerEntry::expected(endpoint); |
| 267 | let mut results = Vec::new(); |
| 268 | for target in Target::ALL { |
| 269 | for (location, home) in &homes.entries { |
| 270 | let path = target.config_path(location, home); |
| 271 | if !matches!(target.read_entry(&path), Ok(Some(_))) { |
| 272 | continue; |
| 273 | } |
| 274 | results.push((target, target.write_entry(&path, &entry))); |
| 275 | } |
| 276 | } |
| 277 | results |
| 278 | } |