use std::io::Read;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use serde_json::{json, Map, Value};
use crate::cli::{Client, Format, Global, SetupArgs};
use crate::emit::Item;
use crate::exit::{Code, Failure, Run};
use crate::progress::Log;
use crate::setup;
const SERVER_NAME: &str = "spider";
const MAX_CONFIG_BYTES: u64 = 4 * 1024 * 1024;
const CLIENTS: [Client; 6] = [
Client::ClaudeCode,
Client::Codex,
Client::Cursor,
Client::Windsurf,
Client::ClaudeDesktop,
Client::Vscode,
];
#[derive(Debug, Clone, PartialEq, Eq)]
struct Outcome {
client: Client,
status: Status,
how: String,
detail: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Status {
Added,
Already,
WouldAdd,
NotFound,
Failed,
}
impl Status {
const fn label(self) -> &'static str {
match self {
Status::Added => "added",
Status::Already => "already",
Status::WouldAdd => "would_add",
Status::NotFound => "not_found",
Status::Failed => "failed",
}
}
}
enum Place {
Tool {
program: &'static str,
check: Option<Vec<String>>,
add: Vec<String>,
},
File { path: PathBuf, key: &'static str },
}
pub fn command(global: &Global, args: &SetupArgs, log: Log) -> Run<Code> {
let binary = this_binary()?;
let home = home();
let chosen: Vec<Client> = if args.client.is_empty() {
CLIENTS.to_vec()
} else {
args.client.clone()
};
let mut outcomes = Vec::new();
for client in chosen {
outcomes.push(match place(client, &binary, home.as_deref()) {
Some(place) => apply(client, place, &binary, args.dry_run),
None => Outcome {
client,
status: Status::NotFound,
how: String::new(),
detail: None,
},
});
}
let mut emitter = setup::emitter(global, Format::Text)?;
for outcome in &outcomes {
if outcome.status == Status::NotFound && args.client.is_empty() {
continue;
}
emitter.write(item(outcome))?;
}
emitter.finish()?;
let found = outcomes.iter().any(|o| o.status != Status::NotFound);
if !found {
log.say(format!(
"no agent client found on this machine. For any client that speaks the Model Context Protocol, the server is the command {binary} with the argument mcp"
));
}
if outcomes.iter().any(|o| o.status == Status::Failed) {
return Err(Failure::new(Code::Output, String::new()));
}
Ok(Code::Ok)
}
fn item(outcome: &Outcome) -> Item {
let name = outcome.client.label();
let line = match (outcome.status, &outcome.detail) {
(Status::Added, _) => format!("{name}: added ({})", outcome.how),
(Status::Already, _) => format!("{name}: already set up ({})", outcome.how),
(Status::WouldAdd, _) => format!("{name}: would add ({})", outcome.how),
(Status::NotFound, _) => format!("{name}: not found on this machine"),
(Status::Failed, Some(detail)) => format!("{name}: failed, {detail}"),
(Status::Failed, None) => format!("{name}: failed"),
};
Item::structured(json!({
"type": "setup",
"client": name,
"status": outcome.status.label(),
"how": outcome.how,
"detail": outcome.detail,
}))
.with_text(line, "txt")
}
fn this_binary() -> Run<String> {
let path = std::env::current_exe().map_err(|e| {
Failure::new(
Code::Failed,
format!("cannot tell where this binary is: {e}"),
)
})?;
path.to_str().map(str::to_string).ok_or_else(|| {
Failure::new(
Code::Failed,
"this binary's path is not text a config file can hold",
)
})
}
fn home() -> Option<PathBuf> {
spider_cloud_agent::auth::store::spider_dir()
.and_then(|dir| dir.parent().map(Path::to_path_buf))
}
fn place(client: Client, binary: &str, home: Option<&Path>) -> Option<Place> {
let words = |words: &[&str]| words.iter().map(|w| (*w).to_string()).collect::<Vec<_>>();
match client {
Client::ClaudeCode => on_path("claude").then(|| Place::Tool {
program: "claude",
check: Some(words(&["mcp", "get", SERVER_NAME])),
add: words(&[
"mcp",
"add",
"--scope",
"user",
SERVER_NAME,
"--",
binary,
"mcp",
]),
}),
Client::Codex => on_path("codex").then(|| Place::Tool {
program: "codex",
check: Some(words(&["mcp", "get", SERVER_NAME])),
add: words(&["mcp", "add", SERVER_NAME, "--", binary, "mcp"]),
}),
Client::Vscode => on_path("code").then(|| Place::Tool {
program: "code",
check: None,
add: vec![
"--add-mcp".to_string(),
json!({ "name": SERVER_NAME, "command": binary, "args": ["mcp"] }).to_string(),
],
}),
Client::Cursor => file_in(home?.join(".cursor"), "mcp.json"),
Client::Windsurf => file_in(home?.join(".codeium").join("windsurf"), "mcp_config.json"),
Client::ClaudeDesktop => file_in(desktop_dir(home?)?, "claude_desktop_config.json"),
}
}
fn file_in(dir: PathBuf, name: &str) -> Option<Place> {
dir.is_dir().then(|| Place::File {
path: dir.join(name),
key: "mcpServers",
})
}
fn desktop_dir(home: &Path) -> Option<PathBuf> {
if cfg!(target_os = "macos") {
Some(
home.join("Library")
.join("Application Support")
.join("Claude"),
)
} else if cfg!(target_os = "windows") {
std::env::var_os("APPDATA").map(|roaming| PathBuf::from(roaming).join("Claude"))
} else {
Some(home.join(".config").join("Claude"))
}
}
fn on_path(program: &str) -> bool {
let Some(paths) = std::env::var_os("PATH") else {
return false;
};
let names: &[&str] = if cfg!(target_os = "windows") {
&[".exe", ".cmd", ".bat"]
} else {
&[""]
};
std::env::split_paths(&paths).any(|dir| {
names
.iter()
.any(|ending| dir.join(format!("{program}{ending}")).is_file())
})
}
fn apply(client: Client, place: Place, binary: &str, dry_run: bool) -> Outcome {
match place {
Place::Tool {
program,
check,
add,
} => {
let how = format!("{program} {}", add.join(" "));
let done = |status, detail| Outcome {
client,
status,
how: how.clone(),
detail,
};
if let Some(check) = check {
if run(program, &check).is_ok() {
return done(Status::Already, None);
}
}
if dry_run {
return done(Status::WouldAdd, None);
}
match run(program, &add) {
Ok(()) => done(Status::Added, None),
Err(reason) => done(Status::Failed, Some(reason)),
}
}
Place::File { path, key } => {
let how = path.display().to_string();
let done = |status, detail| Outcome {
client,
status,
how: how.clone(),
detail,
};
let entry = json!({ "command": binary, "args": ["mcp"] });
let document = match read_config(&path) {
Ok(document) => document,
Err(reason) => return done(Status::Failed, Some(reason)),
};
let Some(updated) = with_entry(document, key, &entry) else {
return done(Status::Already, None);
};
if dry_run {
return done(Status::WouldAdd, None);
}
match write_config(&path, &updated) {
Ok(()) => done(Status::Added, None),
Err(reason) => done(Status::Failed, Some(reason)),
}
}
}
}
fn run(program: &str, arguments: &[String]) -> Result<(), String> {
let output = Command::new(program)
.args(arguments)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.map_err(|e| format!("{program} did not start: {e}"))?;
if output.status.success() {
return Ok(());
}
let said = String::from_utf8_lossy(&output.stderr);
let said = said.lines().find(|line| !line.trim().is_empty());
Err(match said {
Some(line) => format!("{program} said: {}", line.trim()),
None => format!("{program} left with {}", output.status),
})
}
fn read_config(path: &Path) -> Result<Map<String, Value>, String> {
let file = match std::fs::File::open(path) {
Ok(file) => file,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Map::new()),
Err(e) => return Err(format!("{} could not be read: {e}", path.display())),
};
let mut text = String::new();
file.take(MAX_CONFIG_BYTES)
.read_to_string(&mut text)
.map_err(|e| format!("{} could not be read: {e}", path.display()))?;
if text.trim().is_empty() {
return Ok(Map::new());
}
match serde_json::from_str::<Value>(&text) {
Ok(Value::Object(document)) => Ok(document),
_ => Err(format!(
"{} is not plain JSON, so it was left alone. Add the server by hand",
path.display()
)),
}
}
fn with_entry(
mut document: Map<String, Value>,
key: &str,
entry: &Value,
) -> Option<Map<String, Value>> {
let servers = document
.entry(key.to_string())
.or_insert_with(|| Value::Object(Map::new()));
if !servers.is_object() {
*servers = Value::Object(Map::new());
}
let servers = servers.as_object_mut()?;
if servers.get(SERVER_NAME) == Some(entry) {
return None;
}
servers.insert(SERVER_NAME.to_string(), entry.clone());
Some(document)
}
fn write_config(path: &Path, document: &Map<String, Value>) -> Result<(), String> {
let resolved = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
let path = resolved.as_path();
let failed = |e: std::io::Error| format!("{} could not be written: {e}", path.display());
let mut text = serde_json::to_string_pretty(document)
.map_err(|e| format!("{} could not be written: {e}", path.display()))?;
text.push('\n');
let mut beside = path.as_os_str().to_owned();
beside.push(format!(".spider-agent-{}", std::process::id()));
let beside = PathBuf::from(beside);
let written = (|| {
use std::io::Write;
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&beside)?;
if let Ok(existing) = std::fs::metadata(path) {
file.set_permissions(existing.permissions())?;
}
file.write_all(text.as_bytes())?;
file.sync_all()?;
std::fs::rename(&beside, path)
})();
written.map_err(|e| {
let _ = std::fs::remove_file(&beside);
failed(e)
})
}
#[cfg(test)]
mod tests {
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use super::*;
const BINARY: &str = "/opt/tools/bin/spider-agent";
fn entry() -> Value {
json!({ "command": BINARY, "args": ["mcp"] })
}
fn scratch(name: &str) -> PathBuf {
let dir =
std::env::temp_dir().join(format!("spider-agent-setup-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
dir
}
fn file_place(dir: &Path) -> Place {
Place::File {
path: dir.join("mcp.json"),
key: "mcpServers",
}
}
#[test]
fn a_client_with_no_config_file_gets_one_holding_the_server() {
let dir = scratch("fresh");
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, false);
assert_eq!(outcome.status, Status::Added, "{outcome:?}");
let written: Value =
serde_json::from_str(&std::fs::read_to_string(dir.join("mcp.json")).unwrap()).unwrap();
assert_eq!(written["mcpServers"]["spider"], entry());
}
#[test]
fn every_other_entry_in_the_file_is_kept() {
let dir = scratch("keep");
std::fs::write(
dir.join("mcp.json"),
r#"{"theme":"dark","mcpServers":{"other":{"command":"x","args":[]}}}"#,
)
.unwrap();
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, false);
assert_eq!(outcome.status, Status::Added);
let written: Value =
serde_json::from_str(&std::fs::read_to_string(dir.join("mcp.json")).unwrap()).unwrap();
assert_eq!(written["theme"], "dark");
assert_eq!(written["mcpServers"]["other"]["command"], "x");
assert_eq!(written["mcpServers"]["spider"], entry());
}
#[test]
fn a_second_run_changes_nothing() {
let dir = scratch("twice");
apply(Client::Cursor, file_place(&dir), BINARY, false);
let first = std::fs::read(dir.join("mcp.json")).unwrap();
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, false);
assert_eq!(outcome.status, Status::Already);
assert_eq!(std::fs::read(dir.join("mcp.json")).unwrap(), first);
}
#[test]
fn a_dry_run_writes_nothing() {
let dir = scratch("dry");
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, true);
assert_eq!(outcome.status, Status::WouldAdd);
assert!(!dir.join("mcp.json").exists());
}
#[test]
fn a_file_with_comments_is_left_exactly_as_it_was() {
let dir = scratch("comments");
let before = "{\n // my servers\n \"mcpServers\": {}\n}\n";
std::fs::write(dir.join("mcp.json"), before).unwrap();
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, false);
assert_eq!(outcome.status, Status::Failed);
assert!(outcome.detail.unwrap().contains("by hand"));
assert_eq!(
std::fs::read_to_string(dir.join("mcp.json")).unwrap(),
before
);
}
#[test]
fn a_moved_binary_updates_the_entry_rather_than_adding_a_second() {
let dir = scratch("moved");
apply(Client::Cursor, file_place(&dir), "/old/spider-agent", false);
let outcome = apply(Client::Cursor, file_place(&dir), BINARY, false);
assert_eq!(outcome.status, Status::Added);
let written: Value =
serde_json::from_str(&std::fs::read_to_string(dir.join("mcp.json")).unwrap()).unwrap();
assert_eq!(written["mcpServers"].as_object().unwrap().len(), 1);
assert_eq!(written["mcpServers"]["spider"]["command"], BINARY);
}
#[test]
fn a_client_that_is_not_installed_has_no_place() {
let home = scratch("empty-home");
for client in [Client::Cursor, Client::Windsurf] {
assert!(place(client, BINARY, Some(&home)).is_none());
}
std::fs::create_dir_all(home.join(".cursor")).unwrap();
assert!(place(Client::Cursor, BINARY, Some(&home)).is_some());
}
#[test]
fn a_record_names_the_client_and_what_happened() {
let outcome = Outcome {
client: Client::ClaudeCode,
status: Status::WouldAdd,
how: "claude mcp add".to_string(),
detail: None,
};
let item = item(&outcome);
assert_eq!(item.value["type"], "setup");
assert_eq!(item.value["client"], "claude-code");
assert_eq!(item.value["status"], "would_add");
assert!(item.text.unwrap().starts_with("claude-code: would add"));
}
}