use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use crate::lsp::manifest::{self, ExtensionSummary};
fn catalog_summaries() -> Vec<ExtensionSummary> {
manifest::summaries(manifest::CATALOG_MANIFESTS)
}
fn find_source(id: &str) -> Option<&'static str> {
manifest::CATALOG_MANIFESTS
.iter()
.copied()
.find(|s| manifest::parse(s).map(|m| m.id == id).unwrap_or(false))
}
pub fn bundled_ids() -> BTreeSet<String> {
manifest::summaries(manifest::BUNDLED_MANIFESTS)
.into_iter()
.map(|s| s.id)
.collect()
}
pub fn installed_ids(user_dir: &Path) -> BTreeSet<String> {
let mut ids = bundled_ids();
for src in manifest::read_extension_sources(user_dir) {
if let Ok(m) = manifest::parse(&src) {
ids.insert(m.id);
}
}
ids
}
fn available_from(installed: &BTreeSet<String>) -> Vec<ExtensionSummary> {
catalog_summaries()
.into_iter()
.filter(|s| !installed.contains(&s.id))
.collect()
}
pub fn available() -> Vec<ExtensionSummary> {
let installed = installed_ids(&manifest::user_extensions_dir());
let mut out = available_from(&installed);
let bundled_catalog_ids: BTreeSet<String> =
catalog_summaries().into_iter().map(|s| s.id).collect();
out.extend(crate::mcp::registry_index::available_summaries(
&bundled_catalog_ids,
&installed,
));
out
}
fn write_into(dir: &Path, id: &str) -> Result<PathBuf> {
let source = find_source(id).with_context(|| format!("no catalog entry '{id}'"))?;
let ext_dir = dir.join(id);
std::fs::create_dir_all(&ext_dir).with_context(|| format!("creating {ext_dir:?}"))?;
let path = ext_dir.join("extension.toml");
std::fs::write(&path, source).with_context(|| format!("writing {path:?}"))?;
Ok(path)
}
pub fn install_in(extensions_dir: &Path, id: &str) -> Result<PathBuf> {
if find_source(id).is_some() {
write_into(extensions_dir, id)
} else {
crate::mcp::registry_index::install_remote(id)
}
}
pub fn is_catalog_entry(id: &str) -> bool {
find_source(id).is_some()
}
pub fn is_removable(id: &str) -> bool {
is_catalog_entry(id) || crate::mcp::registry_index::is_index_entry(id)
}
fn remove_from(dir: &Path, id: &str) -> Result<()> {
if !is_removable(id) {
anyhow::bail!(
"'{id}' is not from the catalog or the extension index; croft only uninstalls what it added"
);
}
let ext_dir = dir.join(id);
let manifest = ext_dir.join("extension.toml");
if manifest.exists() {
std::fs::remove_file(&manifest).with_context(|| format!("removing {manifest:?}"))?;
}
let _ = std::fs::remove_dir(&ext_dir);
Ok(())
}
pub fn uninstall_in(extensions_dir: &Path, id: &str) -> Result<()> {
remove_from(extensions_dir, id)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn csvlens_is_in_the_catalog_until_installed() {
let none = BTreeSet::new();
assert!(
available_from(&none).iter().any(|s| s.id == "csvlens"),
"csvlens is a catalog entry"
);
let installed: BTreeSet<String> = ["csvlens".to_string()].into_iter().collect();
assert!(!available_from(&installed).iter().any(|s| s.id == "csvlens"));
}
#[test]
fn codeql_comes_from_the_index_and_uninstalls() {
use crate::mcp::registry_index::{CODEQL_MANIFEST, available_summaries};
assert!(!bundled_ids().contains("codeql"));
assert!(!is_catalog_entry("codeql"));
let cache = tempfile::tempdir().unwrap();
let _cache = crate::session_state::CacheDirForThisTestThread::set(cache.path());
crate::mcp::registry_index::cache_index_for_test(&[CODEQL_MANIFEST]);
let dir = tempfile::tempdir().unwrap();
let offered = |dir: &Path| {
available_summaries(&BTreeSet::new(), &installed_ids(dir))
.iter()
.any(|s| s.id == "codeql" && !s.builtin)
};
assert!(offered(dir.path()), "under AVAILABLE");
let added = dir.path().join("codeql");
std::fs::create_dir_all(&added).unwrap();
std::fs::write(added.join("extension.toml"), CODEQL_MANIFEST).unwrap();
assert!(!offered(dir.path()), "added, it leaves AVAILABLE");
uninstall_in(dir.path(), "codeql").expect("an index entry uninstalls");
assert!(!added.exists());
assert!(offered(dir.path()), "and is back under AVAILABLE");
}
#[test]
fn catalog_lists_the_seeded_servers() {
let ids: Vec<String> = catalog_summaries().into_iter().map(|s| s.id).collect();
for id in ["mcp-fetch", "mcp-time", "mcp-markitdown"] {
assert!(
ids.contains(&id.to_string()),
"catalog missing {id}: {ids:?}"
);
}
}
#[test]
fn available_excludes_installed_ids() {
let mut installed = BTreeSet::new();
installed.insert("mcp-fetch".to_string());
let avail: Vec<String> = available_from(&installed)
.into_iter()
.map(|s| s.id)
.collect();
assert!(
!avail.contains(&"mcp-fetch".to_string()),
"added entry must drop out"
);
assert!(
avail.contains(&"mcp-time".to_string()),
"others stay available"
);
}
#[test]
fn install_writes_a_loadable_manifest_and_rejects_unknown() {
let tmp = std::env::temp_dir().join(format!("croft-catalog-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&tmp);
let path = write_into(&tmp, "mcp-time").expect("writes the manifest");
assert!(path.is_file());
let m = manifest::parse(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(m.id, "mcp-time");
assert_eq!(m.mcp_servers.len(), 1);
assert_eq!(m.commands.len(), 1);
assert!(write_into(&tmp, "no-such-entry").is_err());
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
fn uninstall_removes_a_catalog_entry_and_refuses_non_catalog_ids() {
let tmp = std::env::temp_dir().join(format!("croft-catalog-rm-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&tmp);
let path = write_into(&tmp, "mcp-time").expect("writes the manifest");
assert!(path.is_file());
remove_from(&tmp, "mcp-time").expect("uninstalls the catalog entry");
assert!(!tmp.join("mcp-time").exists(), "manifest dir must be gone");
write_into(&tmp, "mcp-time").unwrap();
std::fs::write(tmp.join("mcp-time/notes.md"), "mine").unwrap();
remove_from(&tmp, "mcp-time").expect("uninstalls the catalog entry");
assert!(!tmp.join("mcp-time/extension.toml").exists());
assert!(
tmp.join("mcp-time/notes.md").is_file(),
"the user's file stays"
);
let user_dir = tmp.join("my-own-ext");
std::fs::create_dir_all(&user_dir).unwrap();
assert!(remove_from(&tmp, "my-own-ext").is_err());
assert!(user_dir.exists(), "a non-catalog dir is never deleted");
remove_from(&tmp, "mcp-fetch").expect("absent catalog entry is a no-op");
assert!(is_catalog_entry("mcp-fetch"));
assert!(!is_catalog_entry("my-own-ext"));
let _ = std::fs::remove_dir_all(&tmp);
}
#[test]
#[ignore = "needs uv and network"]
fn catalog_uv_servers_answer_initialize_at_their_pins() {
use std::io::Write;
let mut checked = 0;
for src in manifest::CATALOG_MANIFESTS {
let doc: toml::Value = toml::from_str(src).unwrap();
for server in doc
.get("mcp_servers")
.and_then(toml::Value::as_array)
.into_iter()
.flatten()
{
let Some(p) = server.get("provision") else {
continue;
};
if p.get("kind").and_then(toml::Value::as_str) != Some("uv") {
continue;
}
let field = |k: &str| p.get(k).and_then(toml::Value::as_str).unwrap().to_string();
let spec = format!("{}=={}", field("package"), field("version"));
let mut child = std::process::Command::new("uvx")
.args(["--from", &spec, &field("bin")])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.unwrap();
let init = r#"{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-11-25","capabilities":{},"clientInfo":{"name":"croft","version":"test"}}}"#;
writeln!(child.stdin.take().unwrap(), "{init}").unwrap();
let out = child.wait_with_output().unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
assert!(
stdout.contains(r#""id":1,"result""#),
"{spec} did not answer initialize:\n{}",
String::from_utf8_lossy(&out.stderr)
);
checked += 1;
}
}
assert!(checked >= 3, "found {checked} uv catalog servers");
}
}