claude_cli+codemode: MCP prompt points at codemode; MCP tools listed first in codemode

This commit is contained in:
hermes committed 2026-11-13 12:38:25 +00:00
1 parent 93bd2693c5
commit fa0ff339f6
4 files changed
+26 -4

No files matched your search

+6 -2
View File
@@ -474,6 +474,10 @@ fn pricing(model: &str) -> Option<hx_ai::Pricing> {
static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new(); static COMPAT: std::sync::OnceLock<hx_ai::openai_compat::Config> = std::sync::OnceLock::new();
/// `codemode` from config: also offer the codemode tool to claude. /// `codemode` from config: also offer the codemode tool to claude.
static CODEMODE: std::sync::OnceLock<bool> = std::sync::OnceLock::new(); static CODEMODE: std::sync::OnceLock<bool> = 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. /// `claude_cli` from config: extra args for the `claude` binary.
static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new(); static CLAUDE_CLI: std::sync::OnceLock<Vec<String>> = std::sync::OnceLock::new();
@@ -917,7 +921,7 @@ async fn main() -> Result<()> {
mcp.wait_all(std::time::Duration::from_secs(1)).await; mcp.wait_all(std::time::Duration::from_secs(1)).await;
trace("mcp"); trace("mcp");
mcp.register(&mut agent.tools); mcp.register(&mut agent.tools);
let idx = mcp.system_prompt(); let idx = mcp.system_prompt_for(via_codemode());
if !idx.is_empty() { if !idx.is_empty() {
agent.append_system(&idx); agent.append_system(&idx);
} }
@@ -1151,7 +1155,7 @@ async fn main() -> Result<()> {
.unwrap_or(context::DEFAULT_PROMPT), .unwrap_or(context::DEFAULT_PROMPT),
context::prompt_tail(&context::hx_home(), &cwd) context::prompt_tail(&context::hx_home(), &cwd)
); );
let idx = mcp.system_prompt(); let idx = mcp.system_prompt_for(via_codemode());
if !idx.is_empty() { if !idx.is_empty() {
sys.push_str("\n\n"); sys.push_str("\n\n");
sys.push_str(&idx); sys.push_str(&idx);
+10 -1
View File
@@ -395,10 +395,19 @@ impl Hub {
} }
pub fn system_prompt(&self) -> String { 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 idx = self.index();
let mut s = String::new(); let mut s = String::new();
if !idx.is_empty() { 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 { for (_, b) in idx {
s.push_str(&b); s.push_str(&b);
} }
+6
View File
@@ -36,6 +36,12 @@ async fn stdio_lazy_index_describe_call() {
let sp = h.system_prompt(); let sp = h.system_prompt();
assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}"); assert!(sp.contains("- f.echo: Echo a message back\n"), "{sp}");
assert!(!sp.contains("second line")); 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.")); assert!(sp.contains("mem://notes") && sp.contains("Use echo for tests."));
let st = h.status(); let st = h.status();
assert!( assert!(
+4 -1
View File
@@ -86,7 +86,10 @@ impl Codemode {
let mut names = BTreeMap::new(); let mut names = BTreeMap::new();
let mut list = String::new(); let mut list = String::new();
let mut hidden = 0; 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 id = ident(&s.name);
let first = s.description.split(". ").next().unwrap_or("").trim(); let first = s.description.split(". ").next().unwrap_or("").trim();
let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema)); let line = format!("- tools.{id}({}): {first}\n", sig(&s.input_schema));