Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.
Rust agent-to-agentai-agentsclaude-codecodexcoding-agentscursorgrokirohmcpmcp-servermulti-agentpeer-to-peerreason
ZCode Desktop support in setup (#37)
qinghuidaqzp717b01f53b0f
diff --git a/README.md b/README.mdindex 9ffde15..eaa9aed 100644--- a/README.md+++ b/README.md@@ -53,6 +53,8 @@ That's it. Ask any agent "who's on the network?", tell it to ask someone's agent- **Messages land right away.** Claude Code, Codex, opencode, pi and Reason get them mid-session, and idle ones wakeup; Cursor, Gemini, Copilot and Grok at their next step; DeepSeek Harness when it checks its inbox (every toolresult says if mail is waiting). Claude sessions you already had open join without a restart.+- **ZCode Desktop too.** `setup` adds the MCP server to `~/.zcode/cli/config.json` (or `~/.agents/mcp.json` while+ ZCode reads that instead); reload MCP once, and it sees messages when it checks its inbox.- **Small text files go along.** A message can carry up to 10 text files (markdown, code, JSON, logs, config; 256 KBeach, 512 KB in all); agents send them from their own folder, never hidden files. The recipient's engine saves copiesunder `~/.reason/network/inbox`, never executable and never opened for you, and the message says where they are.diff --git a/src/agent.rs b/src/agent.rsindex aaf370c..3b52ce9 100644--- a/src/agent.rs+++ b/src/agent.rs@@ -59,6 +59,8 @@ pub fn from_client_name(name: &str) -> Option<&'static str> {} else if n == "dsh" || n.starts_with("dsh-") {// DeepSeek Harness: its MCP client says `dsh-mcp-client`."dsh"+ } else if n == "zcode" || n.starts_with("zcode-") || n.starts_with("zcode ") {+ "zcode"} else if has("reason") {"reason"} else if n == "amp" || n.starts_with("amp-") || n.starts_with("amp ") {@@ -255,6 +257,9 @@ mod tests {assert_eq!(from_client_name("grok-shell-reason-network"), Some("grok"));assert_eq!(from_client_name("dsh-mcp-client"), Some("dsh"));assert_eq!(from_client_name("ashdsh"), None);+ assert_eq!(from_client_name("ZCode"), Some("zcode"));+ assert_eq!(from_client_name("zcode-mcp-client"), Some("zcode"));+ assert_eq!(from_client_name("not-zcode"), None);assert_eq!(from_client_name("claude-ai"), Some("claude-desktop"));assert_eq!(from_client_name("example-client"), None);}diff --git a/src/setup.rs b/src/setup.rsindex 34dfe31..85999cf 100644--- a/src/setup.rs+++ b/src/setup.rs@@ -14,6 +14,8 @@ use std::{process::Command,};+mod zcode;+/// The service's name. A non-default `REASON_NETWORK_HOME` gets its own, so a second instance (or a test) can never/// replace the real one.fn service_name() -> String {@@ -1202,7 +1204,7 @@ fn me(exe: &Path) -> String {s[1..s.len() - 1].to_string()}-const AGENTS: [Agent; 10] = [+const AGENTS: [Agent; 11] = [Agent {id: "claude",name: "Claude Code",@@ -1250,6 +1252,14 @@ const AGENTS: [Agent; 10] = [wired: |exe| has(home().join(".cursor/mcp.json"), &me(exe)),apply: cursor,},+ Agent {+ id: "zcode",+ name: "ZCode Desktop",+ hint: "sees messages when it checks its inbox; reload MCP after setup",+ installed: zcode::installed,+ wired: zcode::wired,+ apply: zcode::apply,+ },Agent {id: "gemini",name: "Gemini CLI",@@ -1404,7 +1414,7 @@ pub async fn run(dry: bool, remove: bool, no_service: bool) -> Result<()> {println!("{verb} ({}):", crate::util::tilde(&exe.display().to_string()));if found.is_empty() && !remove {println!(- " no supported agents found (Claude Code, Codex, Grok CLI, Cursor, Gemini CLI, Copilot CLI, opencode, pi, DeepSeek Harness, Reason)"+ " no supported agents found (Claude Code, Codex, Grok CLI, Cursor, ZCode Desktop, Gemini CLI, Copilot CLI, opencode, pi, DeepSeek Harness, Reason)");}let ids: Vec<&str> = found.iter().map(|f| f.id).collect();diff --git a/src/setup/zcode.rs b/src/setup/zcode.rsnew file mode 100644index 0000000..4a1747e--- /dev/null+++ b/src/setup/zcode.rs@@ -0,0 +1,175 @@+//! ZCode Desktop (https://zcode.z.ai/en/docs/mcp-services). Its user MCP servers live in `~/.zcode/cli/config.json`+//! (`mcp.servers`); while that has none, it reads `~/.agents/mcp.json` (`mcpServers`) instead, so a server added to the+//! first would hide every server in the second. Workspace servers can't mask ours: a user server wins on a name clash.++use super::*;++pub(super) fn installed() -> bool {+ let dir = home().join(".zcode");+ dir.join("cli").is_dir() || dir.join("v2").is_dir()+}++pub(super) fn wired(exe: &Path) -> bool {+ wired_in(&home(), exe)+}++/// Whether ours is in the file ZCode reads: its own while that has any server, else the `.agents` one.+fn wired_in(home: &Path, exe: &Path) -> bool {+ let read = |(path, native): (PathBuf, bool)| -> Result<Map<String, Value>> {+ Ok(servers(&mut read_json(&path)?, native)?.clone())+ };+ let [native, fallback] = files(home);+ let servers = match read(native) {+ Ok(s) if s.is_empty() => read(fallback),+ s => s,+ };+ servers.is_ok_and(|s| s.get(AGENT_NAME).is_some_and(|e| contains(&e.to_string(), &me(exe))))+}++pub(super) fn apply(exe: &Path, remove: bool, dry: bool) -> Result<Option<Report>> {+ if !installed() {+ return Ok(None);+ }+ apply_in(&home(), exe, remove, dry).map(Some)+}++/// ZCode's own file, then the `.agents` one; `true` for ZCode's own.+fn files(home: &Path) -> [(PathBuf, bool); 2] {+ [(home.join(".zcode/cli/config.json"), true), (home.join(".agents/mcp.json"), false)]+}++fn servers(v: &mut Value, native: bool) -> Result<&mut Map<String, Value>> {+ let root = obj(v)?;+ let mcp = if native { obj(root.entry("mcp").or_insert_with(|| json!({})))? } else { root };+ obj(mcp.entry(if native { "servers" } else { "mcpServers" }).or_insert_with(|| json!({})))+}++fn apply_in(home: &Path, exe: &Path, remove: bool, dry: bool) -> Result<Report> {+ let mut r = Report { agent: "ZCode Desktop", lines: Vec::new(), notes: Vec::new() };+ let [native, fallback] = files(home);+ // Our own entry doesn't count: ZCode should go on reading `.agents` when ours is all its own file has.+ let to_fallback = !remove+ && !servers(&mut read_json(&native.0)?, true)?.keys().any(|k| k != AGENT_NAME)+ && !servers(&mut read_json(&fallback.0)?, false)?.is_empty();+ // Removing takes ours out of both: which one ZCode reads may have changed since.+ let targets = if remove || to_fallback { vec![native, fallback] } else { vec![native] };+ for (path, native) in targets {+ let mut v = read_json(&path)?;+ let servers = servers(&mut v, native)?;+ if remove || (native && to_fallback) {+ if servers.remove(AGENT_NAME).is_none() {+ continue;+ }+ } else {+ servers.insert(+ AGENT_NAME.into(),+ json!({ "type": "stdio", "command": exe.display().to_string(), "args": ["mcp"], "env": { "REASON_NETWORK_AGENT": "zcode" } }),+ );+ }+ r.lines.push(changed(&path, write_json(&path, &v, dry)?));+ }+ if r.lines.is_empty() {+ r.lines.push("no MCP server to remove".into());+ } else if !remove {+ r.notes.push("Reload MCP in ZCode Desktop (or start a new session) to load Reason Network.".into());+ }+ Ok(r)+}++#[cfg(test)]+mod tests {+ use super::*;++ fn scratch() -> PathBuf {+ let h = std::env::temp_dir().join(format!("reason-zcode-{}", ulid::Ulid::generate()));+ std::fs::create_dir_all(h.join(".zcode/cli")).unwrap();+ h+ }++ fn put(path: &Path, text: &str) {+ std::fs::create_dir_all(path.parent().unwrap()).unwrap();+ std::fs::write(path, text).unwrap();+ }++ fn read(path: &Path) -> Value {+ read_json(path).unwrap()+ }++ #[test]+ fn adds_ours_beside_other_settings_and_takes_it_out_again() {+ let h = scratch();+ let [(native, _), (fallback, _)] = files(&h);+ put(&native, r#"{"provider":"keep","mcp":{"other":7,"servers":{"memory":{"command":"npx"}}}}"#);+ let exe = Path::new("/opt/reason/reason-network");+ apply_in(&h, exe, false, false).unwrap();+ let once = std::fs::read(&native).unwrap();+ apply_in(&h, exe, false, false).unwrap();+ assert_eq!(std::fs::read(&native).unwrap(), once);+ let v = read(&native);+ assert_eq!(v["provider"], "keep");+ assert_eq!(v["mcp"]["other"], 7);+ assert_eq!(v["mcp"]["servers"][AGENT_NAME]["args"], json!(["mcp"]));+ assert!(!fallback.exists());+ // By name, wherever the binary is now.+ apply_in(&h, Path::new("/elsewhere/reason-network"), true, false).unwrap();+ assert_eq!(read(&native)["mcp"]["servers"], json!({"memory": {"command": "npx"}}));+ std::fs::remove_dir_all(h).unwrap();+ }++ #[test]+ fn goes_where_zcode_reads_its_servers() {+ let h = scratch();+ let [(native, _), (fallback, _)] = files(&h);+ put(&native, r#"{"mcp":{"servers":{}}}"#);+ put(&fallback, r#"{"mcpServers":{"memory":{"command":"npx"}}}"#);+ apply_in(&h, Path::new("/opt/reason-network"), false, false).unwrap();+ assert_eq!(read(&native), json!({"mcp": {"servers": {}}}));+ assert!(read(&fallback)["mcpServers"][AGENT_NAME].is_object());+ apply_in(&h, Path::new("/opt/reason-network"), true, false).unwrap();+ assert_eq!(read(&fallback), json!({"mcpServers": {"memory": {"command": "npx"}}}));+ std::fs::remove_dir_all(h).unwrap();+ }++ #[test]+ fn moves_to_agents_when_ours_is_all_zcode_has() {+ let h = scratch();+ let [(native, _), (fallback, _)] = files(&h);+ let exe = Path::new("/opt/reason-network");+ apply_in(&h, exe, false, false).unwrap();+ put(&fallback, r#"{"mcpServers":{"memory":{"command":"npx"}}}"#);+ apply_in(&h, exe, false, false).unwrap();+ assert_eq!(read(&native), json!({"mcp": {"servers": {}}}));+ assert!(read(&fallback)["mcpServers"][AGENT_NAME].is_object());+ std::fs::remove_dir_all(h).unwrap();+ }++ #[test]+ fn wired_only_where_zcode_reads() {+ let h = scratch();+ let [(native, _), (fallback, _)] = files(&h);+ let exe = Path::new("/opt/reason-network");+ assert!(!wired_in(&h, exe));+ apply_in(&h, exe, false, false).unwrap();+ assert!(wired_in(&h, exe));+ assert!(!wired_in(&h, Path::new("/elsewhere/reason-network")));+ // In `.agents`, ours counts only while ZCode's own file has no server.+ put(&fallback, &json!({"mcpServers": {AGENT_NAME: {"command": "/opt/reason-network"}}}).to_string());+ put(&native, r#"{"mcp":{"servers":{}}}"#);+ assert!(wired_in(&h, exe));+ put(&native, r#"{"mcp":{"servers":{"memory":{"command":"npx"}}}}"#);+ assert!(!wired_in(&h, exe));+ std::fs::remove_dir_all(h).unwrap();+ }++ #[test]+ fn leaves_a_config_it_cant_read_alone() {+ let h = scratch();+ let [(native, _), _] = files(&h);+ for bad in ["{", "[]", r#"{"mcp":[]}"#, r#"{"mcp":{"servers":false}}"#] {+ put(&native, bad);+ assert!(apply_in(&h, Path::new("/opt/reason-network"), false, false).is_err());+ assert_eq!(std::fs::read_to_string(&native).unwrap(), bad);+ }+ std::fs::remove_dir_all(h).unwrap();+ }+}diff --git a/src/wizard.rs b/src/wizard.rsindex 4a6a4d3..1cabb9a 100644--- a/src/wizard.rs+++ b/src/wizard.rs@@ -192,7 +192,7 @@ pub async fn agents_and_preferences() -> Result<()> {let found = setup::detect(&exe);let chosen: Vec<&str> = if found.is_empty() {log::warning(format!(- "No coding agents found here (Claude Code, Codex, Grok CLI, Cursor, Gemini CLI, Copilot CLI, opencode, pi, DeepSeek Harness, Reason). Install one, then run `{CLI} setup`."+ "No coding agents found here (Claude Code, Codex, Grok CLI, Cursor, ZCode Desktop, Gemini CLI, Copilot CLI, opencode, pi, DeepSeek Harness, Reason). Install one, then run `{CLI} setup`."))?;Vec::new()} else {