use super::{claude_cli::Scope, jsonedit, read};
use crate::clients::{
context::Context,
files,
naming::{NEW, OLD},
targets,
};
use serde_json::{Value, json};
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Kind {
Claude(Scope),
Json(&'static str),
Toml,
Named,
}
#[derive(Clone, Debug)]
pub struct Container {
pub client: &'static str,
pub file: PathBuf,
pub kind: Kind,
pub project: Option<PathBuf>,
}
pub struct Entries {
pub old: Option<Value>,
pub new: Option<Value>,
}
impl Container {
pub fn pointer(&self, key: &str) -> String {
match &self.kind {
Kind::Claude(scope) => scope.pointer(key),
Kind::Json(member) => jsonedit::join(&[member, key]),
Kind::Toml | Kind::Named => jsonedit::join(&["mcp_servers", key]),
}
}
pub fn shared(&self) -> bool {
self.file.file_name().is_some_and(|n| n == ".mcp.json")
}
pub fn entries(&self) -> Result<Entries, ()> {
let document = match targets::document(self.client, &self.file) {
targets::Document::Absent => json!({}),
targets::Document::Read(value) => value,
targets::Document::Unreadable => return Err(()),
};
let at = |key: &str| document.pointer(&self.pointer(key)).cloned();
Ok(Entries {
old: at(OLD),
new: at(NEW),
})
}
}
fn json(path: &Path) -> Value {
read::file(path)
.and_then(|r| r.json())
.unwrap_or(Value::Null)
}
fn keys(value: &Value) -> Vec<PathBuf> {
value
.as_object()
.into_iter()
.flatten()
.map(|(k, _)| PathBuf::from(k))
.collect()
}
pub fn projects(client: &str, context: &Context) -> Vec<PathBuf> {
let base = context.directory(client);
let mut found = match client {
"claude-code" => keys(&json(&targets::mcp(client, context, "user").path)["projects"]),
"codex" => keys(&files::read(&base.join("config.toml")).unwrap_or_default()["projects"]),
"pi" => json(&base.join("trust.json"))
.as_object()
.into_iter()
.flatten()
.filter(|(_, trusted)| trusted.as_bool() == Some(true))
.map(|(k, _)| PathBuf::from(k))
.collect(),
"muse-code" => keys(&json(&base.join("trust.json"))),
"vibe" => files::read(&base.join("trusted_folders.toml")).unwrap_or_default()["trusted"]
.as_array()
.into_iter()
.flatten()
.filter_map(|p| p.as_str().map(PathBuf::from))
.collect(),
"agy" => crate::clients::receipt::load(client, context).unwrap_or_default()["entries"]
.as_array()
.into_iter()
.flatten()
.filter_map(|e| e["file"].as_str().map(PathBuf::from))
.filter_map(|f| {
f.parent()
.filter(|d| d.ends_with(".agents"))?
.parent()
.map(Path::to_path_buf)
})
.collect(),
_ => vec![],
};
found.push(context.cwd.clone());
let mut seen = std::collections::BTreeSet::new();
found
.into_iter()
.filter_map(|p| p.canonicalize().ok())
.filter(|p| p.is_dir() && seen.insert(p.clone()))
.collect()
}
pub fn containers(client: &'static str, context: &Context) -> Vec<Container> {
let user = targets::mcp(client, context, "user").path;
let at = |file: PathBuf, kind: Kind, project: Option<PathBuf>| Container {
client,
file,
kind,
project,
};
let mut found = vec![];
let projects = projects(client, context);
match client {
"claude-code" => {
found.push(at(user.clone(), Kind::Claude(Scope::User), None));
for (key, entry) in json(&user)["projects"].as_object().into_iter().flatten() {
if entry["mcpServers"]
.as_object()
.is_some_and(|m| !m.is_empty())
{
let scope = Scope::Local(key.clone());
found.push(at(
user.clone(),
Kind::Claude(scope),
Some(PathBuf::from(key)),
));
}
}
for p in &projects {
found.push(at(
p.join(".mcp.json"),
Kind::Json("mcpServers"),
Some(p.clone()),
));
}
}
"codex" => {
found.push(at(user, Kind::Toml, None));
for p in &projects {
found.push(at(
p.join(".codex/config.toml"),
Kind::Toml,
Some(p.clone()),
));
}
}
"opencode" => {
let base = context.directory(client);
for (dir, project) in [
(base, None),
(context.cwd.clone(), Some(context.cwd.clone())),
] {
for name in ["opencode.json", "opencode.jsonc"] {
let file = dir.join(name);
if file.exists() || (name == "opencode.json" && project.is_none()) {
found.push(at(file, Kind::Json("mcp"), project.clone()));
}
}
}
let _ = user;
}
"agy" | "vibe" => {
let kind = if client == "vibe" {
Kind::Named
} else {
Kind::Json("mcpServers")
};
found.push(at(user, kind.clone(), None));
for p in &projects {
let file = p.join(crate::clients::owned::relative(client, true, false));
found.push(at(file, kind.clone(), Some(p.clone())));
}
}
"muse-code" | "pi" => {
found.push(at(user, Kind::Json("mcpServers"), None));
for p in &projects {
let file = if client == "pi" {
p.join(".pi/mcp.json")
} else {
p.join(".mcp.json")
};
found.push(at(file, Kind::Json("mcpServers"), Some(p.clone())));
}
}
_ => {
found.push(at(user, Kind::Json("mcpServers"), None));
if client == "cursor" {
let project = context.cwd.join(".cursor/mcp.json");
found.push(at(
project,
Kind::Json("mcpServers"),
Some(context.cwd.clone()),
));
}
}
}
let mut seen: Vec<(PathBuf, String)> = vec![];
found.retain(|c| {
let key = (
c.file.canonicalize().unwrap_or_else(|_| c.file.clone()),
c.pointer(OLD),
);
let fresh = !seen.contains(&key);
seen.push(key);
fresh
});
found
}
pub fn contains_old(path: &Path) -> bool {
std::fs::read(path).is_ok_and(|bytes| {
String::from_utf8_lossy(&bytes)
.to_lowercase()
.contains("neosian-memory")
})
}