use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use anyhow::Context;
use clap::ValueEnum;
use serde_json::{json, Map, Value as Json};
use crate::identity::{self, DB_ENV, HARNESS_ENV, IDENTITY_ENV};
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum Harness {
ClaudeCode,
Codex,
Copilot,
CopilotCli,
Gemini,
#[value(name = "opencode")]
OpenCode,
}
enum Shape {
Json { container: &'static str },
Toml,
}
impl Harness {
pub fn harness_name(self) -> &'static str {
match self {
Harness::ClaudeCode => "claude-code",
Harness::Codex => "codex",
Harness::Copilot | Harness::CopilotCli => "copilot",
Harness::Gemini => "gemini",
Harness::OpenCode => "opencode",
}
}
pub fn label(self) -> &'static str {
match self {
Harness::ClaudeCode => "Claude Code",
Harness::Codex => "the Codex CLI",
Harness::Copilot => "Copilot in VS Code",
Harness::CopilotCli => "the Copilot CLI",
Harness::Gemini => "the Gemini CLI",
Harness::OpenCode => "OpenCode",
}
}
pub fn config_path(self, cwd: &Path) -> PathBuf {
match self {
Harness::ClaudeCode => cwd.join(".mcp.json"),
Harness::Codex => identity::home().join(".codex").join("config.toml"),
Harness::Copilot => cwd.join(".vscode").join("mcp.json"),
Harness::CopilotCli => identity::home().join(".copilot").join("mcp-config.json"),
Harness::Gemini => cwd.join(".gemini").join("settings.json"),
Harness::OpenCode => {
let dir = identity::config_dir().join("opencode");
let json = dir.join("opencode.json");
let jsonc = dir.join("opencode.jsonc");
if json.exists() || !jsonc.exists() {
json
} else {
jsonc
}
}
}
}
fn shape(self) -> Shape {
match self {
Harness::ClaudeCode | Harness::CopilotCli | Harness::Gemini => Shape::Json {
container: "mcpServers",
},
Harness::Copilot => Shape::Json {
container: "servers",
},
Harness::OpenCode => Shape::Json { container: "mcp" },
Harness::Codex => Shape::Toml,
}
}
pub fn next_step(self) -> &'static str {
match self {
Harness::ClaudeCode => {
"start a session in this directory; `claude mcp list` confirms it"
}
Harness::Codex => "restart the Codex CLI; `codex mcp list` confirms it",
Harness::Copilot => {
"in VS Code: MCP: List Servers → hird → Start Server, then tick hird in the \
agent-mode tools picker"
}
Harness::CopilotCli => "restart the Copilot CLI; `/mcp` confirms it",
Harness::Gemini => "restart the Gemini CLI; `gemini mcp list` confirms it",
Harness::OpenCode => "restart OpenCode; `opencode mcp list` confirms it",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Registration {
pub name: String,
pub command: String,
pub args: Vec<String>,
pub env: BTreeMap<String, String>,
}
impl Registration {
pub fn new(harness: Harness, name: &str, db: Option<&Path>) -> Registration {
let mut env = BTreeMap::new();
env.insert(HARNESS_ENV.to_string(), harness.harness_name().to_string());
if let Some(db) = db {
env.insert(DB_ENV.to_string(), db.display().to_string());
}
Registration {
name: name.to_string(),
command: this_binary(),
args: vec!["mcp".to_string()],
env,
}
}
pub fn with_capabilities(mut self, capabilities: &[String]) -> crate::Result<Registration> {
let capabilities = crate::capability::normalize_all(capabilities)?;
if !capabilities.is_empty() {
self.env.insert(
crate::capability::CAPABILITIES_ENV.to_string(),
capabilities.join(","),
);
}
Ok(self)
}
pub fn acting_for(mut self, principal: &str) -> Registration {
if let Some(principal) = identity::principal_name(principal) {
self.env.insert(IDENTITY_ENV.to_string(), principal);
}
self
}
fn as_json(&self, harness: Harness) -> Json {
if harness == Harness::OpenCode {
let command: Vec<&str> = std::iter::once(self.command.as_str())
.chain(self.args.iter().map(String::as_str))
.collect();
return json!({
"type": "local",
"command": command,
"environment": self.env,
});
}
let mut entry = Map::new();
match harness {
Harness::Copilot => {
entry.insert("type".into(), json!("stdio"));
}
Harness::CopilotCli => {
entry.insert("type".into(), json!("local"));
}
_ => {}
}
entry.insert("command".into(), json!(self.command));
entry.insert("args".into(), json!(self.args));
entry.insert("env".into(), json!(self.env));
if harness == Harness::CopilotCli {
entry.insert("tools".into(), json!(["*"]));
}
Json::Object(entry)
}
fn as_toml_block(&self) -> String {
let env: Vec<String> = self
.env
.iter()
.map(|(k, v)| format!("{k} = {}", toml_str(v)))
.collect();
let args: Vec<String> = self.args.iter().map(|a| toml_str(a)).collect();
format!(
"[mcp_servers.{}]\ncommand = {}\nargs = [{}]\nenv = {{ {} }}\n",
self.name,
toml_str(&self.command),
args.join(", "),
env.join(", ")
)
}
fn as_toml_value(&self) -> toml::Value {
let mut table = toml::map::Map::new();
table.insert("command".into(), toml::Value::String(self.command.clone()));
table.insert(
"args".into(),
toml::Value::Array(
self.args
.iter()
.map(|a| toml::Value::String(a.clone()))
.collect(),
),
);
let mut env = toml::map::Map::new();
for (k, v) in &self.env {
env.insert(k.clone(), toml::Value::String(v.clone()));
}
table.insert("env".into(), toml::Value::Table(env));
toml::Value::Table(table)
}
pub fn render(&self, harness: Harness) -> String {
match harness.shape() {
Shape::Json { container } => {
let mut servers = Map::new();
servers.insert(self.name.clone(), self.as_json(harness));
let mut doc = Map::new();
doc.insert(container.to_string(), Json::Object(servers));
let mut text = serde_json::to_string_pretty(&Json::Object(doc))
.unwrap_or_else(|_| "{}".to_string());
text.push('\n');
text
}
Shape::Toml => self.as_toml_block(),
}
}
}
fn toml_str(s: &str) -> String {
toml::Value::String(s.to_string()).to_string()
}
fn this_binary() -> String {
std::env::current_exe()
.map(|path| std::fs::canonicalize(&path).unwrap_or(path))
.map(|path| path.display().to_string())
.unwrap_or_else(|_| "hird".to_string())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Outcome {
Created,
Added,
Replaced,
Unchanged,
}
impl Outcome {
pub fn describe(self, name: &str, path: &Path) -> String {
let path = path.display();
match self {
Outcome::Created => format!("registered {name} in {path}"),
Outcome::Added => format!("added {name} to {path}"),
Outcome::Replaced => format!("replaced {name} in {path}"),
Outcome::Unchanged => format!("{name} was already registered in {path}"),
}
}
}
pub fn apply(
harness: Harness,
reg: &Registration,
cwd: &Path,
force: bool,
) -> anyhow::Result<(PathBuf, Outcome)> {
let path = harness.config_path(cwd);
let outcome = match harness.shape() {
Shape::Json { container } => write_json(&path, container, reg, harness, force)?,
Shape::Toml => write_toml(&path, reg, force)?,
};
Ok((path, outcome))
}
fn write_json(
path: &Path,
container: &str,
reg: &Registration,
harness: Harness,
force: bool,
) -> anyhow::Result<Outcome> {
let entry = reg.as_json(harness);
let (mut doc, existed) = match std::fs::read_to_string(path) {
Ok(raw) if raw.trim().is_empty() => (Map::new(), true),
Ok(raw) => {
let parsed: Json = serde_json::from_str(&raw).map_err(|e| {
anyhow::anyhow!(
"{} is not JSON this can edit ({e}). Comments and trailing commas are \
legal in some of these files and not in any JSON parser — add the \
block by hand, or see `hird register {} --print`",
path.display(),
harness
.to_possible_value()
.expect("named variant")
.get_name()
)
})?;
match parsed {
Json::Object(map) => (map, true),
_ => anyhow::bail!("{} is JSON, but not an object", path.display()),
}
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (Map::new(), false),
Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
};
let servers = match doc
.entry(container.to_string())
.or_insert_with(|| json!({}))
{
Json::Object(map) => map,
_ => anyhow::bail!(
"{} has a `{container}` that is not an object; leaving it alone",
path.display()
),
};
let outcome = match servers.get(®.name) {
Some(current) if *current == entry => return Ok(Outcome::Unchanged),
Some(current) if !force => {
anyhow::bail!(
"{} already registers {:?}, differently:\n\n{}\n\nre-run with --force to \
replace it",
path.display(),
reg.name,
serde_json::to_string_pretty(current).unwrap_or_default()
)
}
Some(_) => Outcome::Replaced,
None if existed => Outcome::Added,
None => Outcome::Created,
};
servers.insert(reg.name.clone(), entry);
let mut text = serde_json::to_string_pretty(&Json::Object(doc))?;
text.push('\n');
write_file(path, &text)?;
Ok(outcome)
}
fn write_toml(path: &Path, reg: &Registration, force: bool) -> anyhow::Result<Outcome> {
let (raw, existed) = match std::fs::read_to_string(path) {
Ok(raw) => (raw, true),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => (String::new(), false),
Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())),
};
let parsed: toml::Table = raw
.parse()
.with_context(|| format!("{} is not valid TOML; leaving it alone", path.display()))?;
let current = parsed
.get("mcp_servers")
.and_then(toml::Value::as_table)
.and_then(|servers| servers.get(®.name));
let outcome = match current {
Some(value) if *value == reg.as_toml_value() => return Ok(Outcome::Unchanged),
Some(value) if !force => anyhow::bail!(
"{} already registers {:?}, differently:\n\n{}\nre-run with --force to replace it",
path.display(),
reg.name,
toml::to_string(value).unwrap_or_default()
),
Some(_) => Outcome::Replaced,
None if existed => Outcome::Added,
None => Outcome::Created,
};
let block = reg.as_toml_block();
let text = match span_of(&raw, ®.name) {
Some((from, to)) => {
let mut out = String::with_capacity(raw.len() + block.len());
out.push_str(&raw[..from]);
out.push_str(&block);
out.push_str(&raw[to..]);
out
}
None => {
let mut out = raw;
if !out.is_empty() && !out.ends_with('\n') {
out.push('\n');
}
if !out.is_empty() {
out.push('\n');
}
out.push_str(&block);
out
}
};
write_file(path, &text)?;
Ok(outcome)
}
fn span_of(raw: &str, name: &str) -> Option<(usize, usize)> {
let quoted = format!("[mcp_servers.\"{name}\"]");
let bare = format!("[mcp_servers.{name}]");
let mut offset = 0usize;
let mut start = None;
for line in raw.split_inclusive('\n') {
let trimmed = line.trim();
match start {
None => {
if trimmed == bare || trimmed == quoted {
start = Some(offset);
}
}
Some(from) => {
if trimmed.starts_with('[') {
return Some((from, offset));
}
}
}
offset += line.len();
}
start.map(|from| (from, raw.len()))
}
fn write_file(path: &Path, text: &str) -> anyhow::Result<()> {
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
std::fs::create_dir_all(parent)
.with_context(|| format!("creating {}", parent.display()))?;
}
std::fs::write(path, text).with_context(|| format!("writing {}", path.display()))
}
#[cfg(test)]
mod tests {
use super::*;
fn reg(name: &str) -> Registration {
Registration {
name: name.to_string(),
command: "/opt/bin/hird".to_string(),
args: vec!["mcp".to_string()],
env: BTreeMap::from([(HARNESS_ENV.to_string(), "codex".to_string())]),
}
}
#[test]
fn every_harness_names_itself_for_the_board() {
assert_eq!(Harness::ClaudeCode.harness_name(), "claude-code");
assert_eq!(Harness::Codex.harness_name(), "codex");
assert_eq!(Harness::Copilot.harness_name(), "copilot");
assert_eq!(Harness::CopilotCli.harness_name(), "copilot");
assert_eq!(Harness::Gemini.harness_name(), "gemini");
assert_eq!(Harness::OpenCode.harness_name(), "opencode");
}
#[test]
fn the_registration_names_an_absolute_command() {
let reg = Registration::new(Harness::Codex, "hird", None);
assert!(reg.command.starts_with('/'), "{}", reg.command);
assert_eq!(reg.args, vec!["mcp".to_string()]);
assert_eq!(reg.env[HARNESS_ENV], "codex");
assert!(!reg.env.contains_key(DB_ENV));
}
#[test]
fn a_scratch_database_rides_along_in_the_environment() {
let reg = Registration::new(Harness::Codex, "hird-scratch", Some(Path::new("/tmp/s.db")));
assert_eq!(reg.env[DB_ENV], "/tmp/s.db");
}
#[test]
fn capabilities_are_normalized_into_the_registration_environment() {
let reg = Registration::new(Harness::Codex, "hird", None)
.with_capabilities(&[" Browser ".into(), "network".into()])
.unwrap();
assert_eq!(
reg.env[crate::capability::CAPABILITIES_ENV],
"browser,network"
);
}
#[test]
fn each_json_harness_gets_its_own_dialect() {
let vscode = reg("hird").as_json(Harness::Copilot);
assert_eq!(vscode["type"], json!("stdio"));
assert!(vscode.get("tools").is_none());
let cli = reg("hird").as_json(Harness::CopilotCli);
assert_eq!(cli["type"], json!("local"));
assert_eq!(cli["tools"], json!(["*"]));
let claude = reg("hird").as_json(Harness::ClaudeCode);
assert!(claude.get("type").is_none());
let gemini = reg("hird").as_json(Harness::Gemini);
assert!(gemini.get("type").is_none());
assert!(gemini.get("tools").is_none());
assert_eq!(gemini["command"], json!("/opt/bin/hird"));
assert_eq!(gemini["args"], json!(["mcp"]));
assert_eq!(gemini["env"], json!({ "HIRD_HARNESS": "codex" }));
let opencode = reg("hird").as_json(Harness::OpenCode);
assert_eq!(opencode["type"], json!("local"));
assert_eq!(opencode["command"], json!(["/opt/bin/hird", "mcp"]));
assert_eq!(opencode["environment"], json!({ "HIRD_HARNESS": "codex" }));
assert!(opencode.get("args").is_none());
assert!(opencode.get("env").is_none());
}
#[test]
fn the_toml_block_keeps_env_inline() {
let block = reg("hird").as_toml_block();
assert_eq!(
block,
"[mcp_servers.hird]\ncommand = \"/opt/bin/hird\"\nargs = [\"mcp\"]\n\
env = { HIRD_HARNESS = \"codex\" }\n"
);
let parsed: toml::Table = block.parse().unwrap();
assert_eq!(parsed["mcp_servers"]["hird"], reg("hird").as_toml_value());
}
#[test]
fn a_missing_file_is_created_with_only_our_entry() {
let dir = tempfile::tempdir().unwrap();
let (path, outcome) = apply(Harness::Copilot, ®("hird"), dir.path(), false).unwrap();
assert_eq!(outcome, Outcome::Created);
assert_eq!(path, dir.path().join(".vscode").join("mcp.json"));
let written: Json = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(
written["servers"]["hird"]["command"],
json!("/opt/bin/hird")
);
}
#[test]
fn the_gemini_registration_stays_inside_the_checkout() {
let dir = tempfile::tempdir().unwrap();
let (path, outcome) = apply(Harness::Gemini, ®("hird"), dir.path(), false).unwrap();
assert_eq!(outcome, Outcome::Created);
assert_eq!(path, dir.path().join(".gemini").join("settings.json"));
let written: Json = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(
written["mcpServers"]["hird"]["command"],
json!("/opt/bin/hird")
);
}
#[test]
fn registering_twice_changes_nothing_and_is_not_an_error() {
let dir = tempfile::tempdir().unwrap();
apply(Harness::ClaudeCode, ®("hird"), dir.path(), false).unwrap();
let (_, again) = apply(Harness::ClaudeCode, ®("hird"), dir.path(), false).unwrap();
assert_eq!(again, Outcome::Unchanged);
}
#[test]
fn another_server_in_the_file_survives_registration() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".mcp.json");
std::fs::write(
&path,
r#"{"mcpServers":{"other":{"command":"thing"}},"unrelated":true}"#,
)
.unwrap();
let (_, outcome) = apply(Harness::ClaudeCode, ®("hird"), dir.path(), false).unwrap();
assert_eq!(outcome, Outcome::Added);
let written: Json = serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
assert_eq!(written["mcpServers"]["other"]["command"], json!("thing"));
assert_eq!(written["unrelated"], json!(true));
assert!(written["mcpServers"]["hird"].is_object());
}
#[test]
fn a_hand_tuned_entry_is_refused_until_force() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".mcp.json");
std::fs::write(
&path,
r#"{"mcpServers":{"hird":{"command":"/somewhere/else/hird"}}}"#,
)
.unwrap();
let err = apply(Harness::ClaudeCode, ®("hird"), dir.path(), false).unwrap_err();
let message = format!("{err:#}");
assert!(message.contains("--force"), "{message}");
assert!(std::fs::read_to_string(&path)
.unwrap()
.contains("/somewhere/else/hird"));
let (_, forced) = apply(Harness::ClaudeCode, ®("hird"), dir.path(), true).unwrap();
assert_eq!(forced, Outcome::Replaced);
}
#[test]
fn json_that_cannot_be_parsed_is_never_overwritten() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".mcp.json");
std::fs::write(&path, "{ // a comment VS Code would accept\n}").unwrap();
let err = apply(Harness::ClaudeCode, ®("hird"), dir.path(), false).unwrap_err();
assert!(format!("{err:#}").contains("--print"), "{err:#}");
assert!(std::fs::read_to_string(&path).unwrap().contains("comment"));
}
#[test]
fn a_toml_block_is_appended_and_the_comments_around_it_survive() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(&path, "# my settings\nmodel = \"o3\"\n").unwrap();
let outcome = write_toml(&path, ®("hird"), false).unwrap();
assert_eq!(outcome, Outcome::Added);
let written = std::fs::read_to_string(&path).unwrap();
assert!(
written.starts_with("# my settings\nmodel = \"o3\"\n"),
"{written}"
);
assert!(written.contains("[mcp_servers.hird]"), "{written}");
assert_eq!(
write_toml(&path, ®("hird"), false).unwrap(),
Outcome::Unchanged
);
}
#[test]
fn forcing_a_toml_entry_replaces_only_that_block() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(
&path,
"[mcp_servers.hird]\ncommand = \"hird\"\n\n[mcp_servers.other]\ncommand = \"x\"\n",
)
.unwrap();
assert_eq!(
write_toml(&path, ®("hird"), true).unwrap(),
Outcome::Replaced
);
let written = std::fs::read_to_string(&path).unwrap();
let parsed: toml::Table = written.parse().unwrap();
assert_eq!(
parsed["mcp_servers"]["hird"]["command"],
toml::Value::String("/opt/bin/hird".into())
);
assert_eq!(
parsed["mcp_servers"]["other"]["command"],
toml::Value::String("x".into())
);
}
#[test]
fn broken_toml_is_left_exactly_as_it_was() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("config.toml");
std::fs::write(&path, "this is not = = toml\n").unwrap();
assert!(write_toml(&path, ®("hird"), false).is_err());
assert_eq!(
std::fs::read_to_string(&path).unwrap(),
"this is not = = toml\n"
);
}
#[test]
fn the_printed_form_is_what_would_be_written() {
let printed = reg("hird").render(Harness::Copilot);
let parsed: Json = serde_json::from_str(&printed).unwrap();
assert_eq!(
parsed["servers"]["hird"],
reg("hird").as_json(Harness::Copilot)
);
assert!(printed.ends_with("\n"));
}
}