use std::collections::BTreeSet;
use std::path::{Path, PathBuf};
use anyhow::{Context, Result, bail};
use brok_skills::Catalog;
use serde_json::{Value, json};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, clap::ValueEnum)]
pub enum Agent {
Claude,
Agents,
Cursor,
Vscode,
Codex,
}
impl Agent {
pub const ALL: [Agent; 5] = [Agent::Claude, Agent::Agents, Agent::Cursor, Agent::Vscode, Agent::Codex];
pub fn label(self) -> &'static str {
match self {
Agent::Claude => "Claude Code",
Agent::Agents => "Portable .agents/skills (Codex, Cursor, Copilot, ...)",
Agent::Cursor => "Cursor",
Agent::Vscode => "VS Code / GitHub Copilot",
Agent::Codex => "OpenAI Codex",
}
}
pub fn skills_dir(self, root: &Path, global: bool) -> PathBuf {
let base = if global { home_dir().unwrap_or_else(|| root.to_path_buf()) } else { root.to_path_buf() };
match self {
Agent::Claude => base.join(".claude").join("skills"),
Agent::Agents | Agent::Cursor | Agent::Vscode | Agent::Codex => base.join(".agents").join("skills"),
}
}
}
pub fn detect(root: &Path) -> Vec<Agent> {
let mut agents = vec![Agent::Agents];
let in_home = |dir: &str| home_dir().is_some_and(|h| h.join(dir).is_dir());
if root.join(".claude").is_dir() || root.join("CLAUDE.md").is_file() || on_path("claude") || in_home(".claude") {
agents.push(Agent::Claude);
}
if root.join(".cursor").is_dir() || on_path("cursor") || in_home(".cursor") {
agents.push(Agent::Cursor);
}
if root.join(".vscode").is_dir() || on_path("code") {
agents.push(Agent::Vscode);
}
if on_path("codex") || in_home(".codex") {
agents.push(Agent::Codex);
}
agents.sort();
agents
}
pub fn export_skills(catalog: &Catalog, dirs: &[PathBuf]) -> Result<Vec<(PathBuf, usize)>> {
let unique: BTreeSet<&PathBuf> = dirs.iter().collect();
let mut written = Vec::new();
for dir in unique {
std::fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))?;
let mut count = 0;
for skill in catalog.iter() {
skill.export_to(dir)?;
count += 1;
}
written.push((dir.clone(), count));
}
Ok(written)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum McpSetup {
Written(PathBuf),
Manual { file: &'static str, snippet: String },
}
pub fn configure_mcp(root: &Path, agent: Agent) -> Result<Option<McpSetup>> {
let server = |typed: bool| {
let mut entry = json!({ "command": "brok", "args": ["mcp"] });
if typed {
entry["type"] = json!("stdio");
}
entry
};
let written = match agent {
Agent::Agents => return Ok(None),
Agent::Codex => {
let snippet = "[mcp_servers.brokspace]\ncommand = \"brok\"\nargs = [\"mcp\"]".to_owned();
return Ok(Some(McpSetup::Manual { file: "~/.codex/config.toml", snippet }));
}
Agent::Claude => merge_server(&root.join(".mcp.json"), "mcpServers", server(true))?,
Agent::Cursor => merge_server(&root.join(".cursor").join("mcp.json"), "mcpServers", server(false))?,
Agent::Vscode => merge_server(&root.join(".vscode").join("mcp.json"), "servers", server(true))?,
};
Ok(Some(McpSetup::Written(written)))
}
fn merge_server(path: &Path, key: &str, entry: Value) -> Result<PathBuf> {
let mut document: Value = match std::fs::read_to_string(path) {
Ok(text) if !text.trim().is_empty() => serde_json::from_str(&text).with_context(|| {
format!("{} is not valid JSON; fix it or add the brokspace server by hand", path.display())
})?,
_ => json!({}),
};
let Some(object) = document.as_object_mut() else {
bail!("{} must contain a JSON object", path.display());
};
let servers = object.entry(key).or_insert_with(|| json!({}));
let Some(servers) = servers.as_object_mut() else {
bail!("`{key}` in {} must be an object", path.display());
};
servers.insert("brokspace".to_owned(), entry);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
brok_core::fsutil::write_atomic(path, format!("{}\n", serde_json::to_string_pretty(&document)?).as_bytes())?;
Ok(path.to_path_buf())
}
pub fn mcp_configured(root: &Path, agent: Agent) -> Option<bool> {
let (path, key) = match agent {
Agent::Claude => (root.join(".mcp.json"), "mcpServers"),
Agent::Cursor => (root.join(".cursor").join("mcp.json"), "mcpServers"),
Agent::Vscode => (root.join(".vscode").join("mcp.json"), "servers"),
Agent::Agents | Agent::Codex => return None,
};
let text = std::fs::read_to_string(path).ok()?;
let value: Value = serde_json::from_str(&text).ok()?;
Some(value.get(key).and_then(|s| s.get("brokspace")).is_some())
}
pub fn on_path(program: &str) -> bool {
let names: Vec<String> = if cfg!(windows) {
["exe", "cmd", "bat"].iter().map(|ext| format!("{program}.{ext}")).collect()
} else {
vec![program.to_owned()]
};
std::env::var_os("PATH")
.is_some_and(|path| std::env::split_paths(&path).any(|dir| names.iter().any(|n| dir.join(n).is_file())))
}
fn home_dir() -> Option<PathBuf> {
std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" }).map(PathBuf::from)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mcp_entries_merge_without_clobbering() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
std::fs::write(root.join(".mcp.json"), r#"{"mcpServers":{"other":{"command":"x"}},"keep":1}"#).unwrap();
configure_mcp(root, Agent::Claude).unwrap();
configure_mcp(root, Agent::Claude).unwrap(); let value: Value = serde_json::from_str(&std::fs::read_to_string(root.join(".mcp.json")).unwrap()).unwrap();
assert_eq!(value["mcpServers"]["other"]["command"], "x");
assert_eq!(value["mcpServers"]["brokspace"]["args"][0], "mcp");
assert_eq!(value["keep"], 1);
assert_eq!(mcp_configured(root, Agent::Claude), Some(true));
configure_mcp(root, Agent::Vscode).unwrap();
let vscode: Value =
serde_json::from_str(&std::fs::read_to_string(root.join(".vscode/mcp.json")).unwrap()).unwrap();
assert_eq!(vscode["servers"]["brokspace"]["type"], "stdio");
assert!(matches!(configure_mcp(root, Agent::Codex).unwrap(), Some(McpSetup::Manual { .. })));
std::fs::write(root.join(".cursor-broken.json"), "{").unwrap();
assert!(merge_server(&root.join(".cursor-broken.json"), "mcpServers", json!({})).is_err());
}
#[test]
fn exports_builtin_skills_once_per_directory() {
let dir = tempfile::tempdir().unwrap();
let catalog = Catalog::builtin();
let dirs = [Agent::Agents, Agent::Cursor, Agent::Claude].map(|a| a.skills_dir(dir.path(), false));
let written = export_skills(&catalog, &dirs).unwrap();
assert_eq!(written.len(), 2, "cursor shares .agents/skills");
assert!(dir.path().join(".claude/skills/tdd/SKILL.md").is_file());
assert!(dir.path().join(".agents/skills/brok-context/agents/openai.yaml").is_file());
}
}