From fa0ff339f63609c736f72819d7f190c0087ed4d7 Mon Sep 17 00:00:00 2001 From: hermes Date: Fri, 13 Nov 2026 12:38:25 +0000 Subject: [PATCH 1/2] claude_cli+codemode: MCP prompt points at codemode; MCP tools listed first in codemode --- crates/hx-cli/src/main.rs | 8 ++++++-- crates/hx-mcp/src/lib.rs | 11 ++++++++++- crates/hx-mcp/src/tests.rs | 6 ++++++ crates/hx-tools/src/codemode.rs | 5 ++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 6e8c82d..493b7d0 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -474,6 +474,10 @@ fn pricing(model: &str) -> Option { static COMPAT: std::sync::OnceLock = std::sync::OnceLock::new(); /// `codemode` from config: also offer the codemode tool to claude. static CODEMODE: std::sync::OnceLock = std::sync::OnceLock::new(); +/// claude_cli sees only `codemode` (via `hx mcp-serve`), so MCP tools are script-only. +fn via_codemode() -> bool { + CLAUDE_CLI.get().is_some() && CODEMODE.get().copied().unwrap_or(false) +} /// `claude_cli` from config: extra args for the `claude` binary. static CLAUDE_CLI: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -917,7 +921,7 @@ async fn main() -> Result<()> { mcp.wait_all(std::time::Duration::from_secs(1)).await; trace("mcp"); mcp.register(&mut agent.tools); - let idx = mcp.system_prompt(); + let idx = mcp.system_prompt_for(via_codemode()); if !idx.is_empty() { agent.append_system(&idx); } @@ -1151,7 +1155,7 @@ async fn main() -> Result<()> { .unwrap_or(context::DEFAULT_PROMPT), context::prompt_tail(&context::hx_home(), &cwd) ); - let idx = mcp.system_prompt(); + let idx = mcp.system_prompt_for(via_codemode()); if !idx.is_empty() { sys.push_str("\n\n"); sys.push_str(&idx); diff --git a/crates/hx-mcp/src/lib.rs b/crates/hx-mcp/src/lib.rs index 734752c..356f5ba 100644 --- a/crates/hx-mcp/src/lib.rs +++ b/crates/hx-mcp/src/lib.rs @@ -395,10 +395,19 @@ impl Hub { } pub fn system_prompt(&self) -> String { + self.system_prompt_for(false) + } + + /// `via_codemode`: the model cannot call `mcp_*` directly (claude_cli exposes only `codemode`). + pub fn system_prompt_for(&self, via_codemode: bool) -> String { let idx = self.index(); let mut s = String::new(); if !idx.is_empty() { - s.push_str("# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n"); + s.push_str(if via_codemode { + "# MCP tools\nNot top-level tools: reach them only through `codemode`. Inside a script: `await tools.mcp_call({server, tool, arguments})`; `await tools.mcp_describe({server, tool})` gives the argument schema (also `tools.mcp_read({server, uri})`).\n" + } else { + "# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n" + }); for (_, b) in idx { s.push_str(&b); } diff --git a/crates/hx-mcp/src/tests.rs b/crates/hx-mcp/src/tests.rs index 734bbbc..2cfa237 100644 --- a/crates/hx-mcp/src/tests.rs +++ b/crates/hx-mcp/src/tests.rs @@ -36,6 +36,12 @@ async fn stdio_lazy_index_describe_call() { let sp = h.system_prompt(); assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}"); assert!(!sp.contains("second line")); + let cm = h.system_prompt_for(true); + assert!( + cm.contains("only through `codemode`") && cm.contains("- f.echo:"), + "{cm}" + ); + assert!(!sp.contains("codemode")); assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests.")); let st = h.status(); assert!( diff --git a/crates/hx-tools/src/codemode.rs b/crates/hx-tools/src/codemode.rs index 71661a0..0057f65 100644 --- a/crates/hx-tools/src/codemode.rs +++ b/crates/hx-tools/src/codemode.rs @@ -86,7 +86,10 @@ impl Codemode { let mut names = BTreeMap::new(); let mut list = String::new(); let mut hidden = 0; - for s in reg.specs() { + // MCP tools first so builtins cannot push them out of the listing budget + let mut specs = reg.specs(); + specs.sort_by_key(|s| !(s.name.contains("__") || s.name.starts_with("mcp_"))); + for s in specs { let id = ident(&s.name); let first = s.description.split(". ").next().unwrap_or("").trim(); let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema)); -- 2.54.1 From 7218d1433b82af6d795d3a77bae60ebcd0b1429e Mon Sep 17 00:00:00 2001 From: hermes Date: Fri, 13 Nov 2026 13:12:20 +0000 Subject: [PATCH 2/2] claude_cli without codemode: serve MCP tools via mcp-serve (mcp__hx__*), prompt names them; skip self-referencing servers --- crates/hx-cli/src/main.rs | 35 +++++++++++++++++++++-------------- crates/hx-mcp/src/lib.rs | 23 ++++++++++++++++------- crates/hx-mcp/src/tests.rs | 6 +++++- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/crates/hx-cli/src/main.rs b/crates/hx-cli/src/main.rs index 493b7d0..749cfa0 100644 --- a/crates/hx-cli/src/main.rs +++ b/crates/hx-cli/src/main.rs @@ -474,9 +474,16 @@ fn pricing(model: &str) -> Option { static COMPAT: std::sync::OnceLock = std::sync::OnceLock::new(); /// `codemode` from config: also offer the codemode tool to claude. static CODEMODE: std::sync::OnceLock = std::sync::OnceLock::new(); -/// claude_cli sees only `codemode` (via `hx mcp-serve`), so MCP tools are script-only. -fn via_codemode() -> bool { - CLAUDE_CLI.get().is_some() && CODEMODE.get().copied().unwrap_or(false) +/// claude_cli sees only the `hx mcp-serve` tools: MCP is script-only with codemode, else `mcp__hx__*`. +fn via() -> hx_mcp::Via { + match ( + CLAUDE_CLI.get().is_some(), + CODEMODE.get().copied().unwrap_or(false), + ) { + (true, true) => hx_mcp::Via::Codemode, + (true, false) => hx_mcp::Via::ClaudeCli, + _ => hx_mcp::Via::Direct, + } } /// `claude_cli` from config: extra args for the `claude` binary. static CLAUDE_CLI: std::sync::OnceLock> = std::sync::OnceLock::new(); @@ -753,21 +760,21 @@ async fn main() -> Result<()> { .permit .set(hx_core::approve::remote_permit(p.into())); } + // claude keeps its own tools; hx's MCP servers are added directly, or only through `codemode` + let cfg = Config::load(&cwd)?; + let mut servers = + hx_mcp::config::load(&context::hx_home(), &cwd).map_err(anyhow::Error::msg)?; + // a server entry that is hx itself would spawn mcp-serve recursively + servers.retain(|_, s| !s.args.iter().any(|a| a == "mcp-serve")); + let hub = Arc::new(hx_mcp::Hub::start(servers).await); if std::env::var_os("HX_CODEMODE").is_some() { - // claude keeps its own tools; only `codemode` is added, and scripts may call the hx tools - let cfg = Config::load(&cwd)?; - let hub = Arc::new( - hx_mcp::Hub::start( - hx_mcp::config::load(&context::hx_home(), &cwd) - .map_err(anyhow::Error::msg)?, - ) - .await, - ); let mut all = hx_tools::registry(); hx_tools::web::register(&mut all, web_search(&cfg)); hub.register(&mut all); all.add(hx_lens::LspTool::new(mgr.clone())); reg.add(hx_tools::codemode::Codemode::new(&all)); + } else { + hub.register(&mut reg); } hx_mcp::serve::serve( reg, @@ -921,7 +928,7 @@ async fn main() -> Result<()> { mcp.wait_all(std::time::Duration::from_secs(1)).await; trace("mcp"); mcp.register(&mut agent.tools); - let idx = mcp.system_prompt_for(via_codemode()); + let idx = mcp.system_prompt_for(via()); if !idx.is_empty() { agent.append_system(&idx); } @@ -1155,7 +1162,7 @@ async fn main() -> Result<()> { .unwrap_or(context::DEFAULT_PROMPT), context::prompt_tail(&context::hx_home(), &cwd) ); - let idx = mcp.system_prompt_for(via_codemode()); + let idx = mcp.system_prompt_for(via()); if !idx.is_empty() { sys.push_str("\n\n"); sys.push_str(&idx); diff --git a/crates/hx-mcp/src/lib.rs b/crates/hx-mcp/src/lib.rs index 356f5ba..528072d 100644 --- a/crates/hx-mcp/src/lib.rs +++ b/crates/hx-mcp/src/lib.rs @@ -135,6 +135,16 @@ async fn load(cfg: &Server) -> (State, Option>) { } } +/// How the model reaches the lazy MCP tools. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Via { + Direct, + /// claude_cli + codemode: only inside `codemode` scripts + Codemode, + /// claude_cli without codemode: `mcp__hx__*` tools served by `hx mcp-serve` + ClaudeCli, +} + impl Hub { /// Connect to all servers in parallel and wait for every one (CLI commands, tests). pub async fn start(cfgs: BTreeMap) -> Self { @@ -395,18 +405,17 @@ impl Hub { } pub fn system_prompt(&self) -> String { - self.system_prompt_for(false) + self.system_prompt_for(Via::Direct) } - /// `via_codemode`: the model cannot call `mcp_*` directly (claude_cli exposes only `codemode`). - pub fn system_prompt_for(&self, via_codemode: bool) -> String { + pub fn system_prompt_for(&self, via: Via) -> String { let idx = self.index(); let mut s = String::new(); if !idx.is_empty() { - s.push_str(if via_codemode { - "# MCP tools\nNot top-level tools: reach them only through `codemode`. Inside a script: `await tools.mcp_call({server, tool, arguments})`; `await tools.mcp_describe({server, tool})` gives the argument schema (also `tools.mcp_read({server, uri})`).\n" - } else { - "# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n" + s.push_str(match via { + Via::Codemode => "# MCP tools\nNot top-level tools: reach them only through `codemode`. Inside a script: `await tools.mcp_call({server, tool, arguments})`; `await tools.mcp_describe({server, tool})` gives the argument schema (also `tools.mcp_read({server, uri})`).\n", + Via::ClaudeCli => "# MCP tools\nCall with the `mcp__hx__mcp_call` tool (server, tool, arguments). Use `mcp__hx__mcp_describe` first to get the argument schema (`mcp__hx__mcp_read` reads resources).\n", + Via::Direct => "# MCP tools\nCall with `mcp_call` (server, tool, arguments). Use `mcp_describe` first to get the argument schema.\n", }); for (_, b) in idx { s.push_str(&b); diff --git a/crates/hx-mcp/src/tests.rs b/crates/hx-mcp/src/tests.rs index 2cfa237..49843c5 100644 --- a/crates/hx-mcp/src/tests.rs +++ b/crates/hx-mcp/src/tests.rs @@ -36,12 +36,16 @@ async fn stdio_lazy_index_describe_call() { let sp = h.system_prompt(); assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}"); assert!(!sp.contains("second line")); - let cm = h.system_prompt_for(true); + let cm = h.system_prompt_for(crate::Via::Codemode); assert!( cm.contains("only through `codemode`") && cm.contains("- f.echo:"), "{cm}" ); assert!(!sp.contains("codemode")); + assert!( + h.system_prompt_for(crate::Via::ClaudeCli) + .contains("mcp__hx__mcp_call") + ); assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests.")); let st = h.status(); assert!( -- 2.54.1