Skip to content

Commit 44440cc

Browse files
author
viethx
committed
fix(anthropic): only advertise OAuth curated builtins backed by registry tools
The OAuth format_tools path unconditionally advertised the curated Claude-Code builtins (Agent, Glob, ...) even when their backing registry tools no longer exist. After 'remove deprecated subagent tool' and the native glob removal, models on the subscription route kept calling Agent/Glob and hit 'Unknown tool: subagent/glob' errors, silently losing subagent fan-out. Each curated builtin now declares which local registry tools it fronts and is only advertised when one of them is present, so the advertised toolset always matches what the registry can execute.
1 parent 28e476a commit 44440cc

1 file changed

Lines changed: 137 additions & 56 deletions

File tree

  • crates/jcode-provider-anthropic/src

‎crates/jcode-provider-anthropic/src/lib.rs‎

Lines changed: 137 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -355,67 +355,113 @@ fn anthropic_input_schema(schema: &Value) -> Value {
355355

356356
pub fn format_tools(tools: &[ToolDefinition], is_oauth: bool, cache_ttl_1h: bool) -> Vec<ApiTool> {
357357
if is_oauth {
358+
let registry_names: std::collections::HashSet<&str> =
359+
tools.iter().map(|tool| tool.name.as_str()).collect();
360+
358361
// Curated Claude-Code builtin tool definitions. These remain hand-tuned
359362
// because the Anthropic OAuth (subscription) endpoint expects the
360363
// builtin names with compatible schemas. Anything not represented here
361364
// is appended from the real registry below so OAuth users keep the full
362365
// toolset (websearch, webfetch, browser, codesearch, memory, ...).
363-
let mut out = vec![
364-
ApiTool {
365-
name: "Agent".to_string(),
366-
description: "Launch a new agent to handle complex, multi-step tasks.".to_string(),
367-
input_schema: json!({"type":"object","properties":{"description":{"type":"string"},"prompt":{"type":"string"},"subagent_type":{"type":"string"},"run_in_background":{"type":"boolean"}},"required":["description","prompt"],"additionalProperties":false}),
368-
cache_control: None,
369-
},
370-
ApiTool {
371-
name: "Bash".to_string(),
372-
description: "Executes a given bash command and returns its output.".to_string(),
373-
input_schema: json!({"type":"object","properties":{"command":{"type":"string"},"timeout":{"type":"integer"},"run_in_background":{"type":"boolean"}},"required":["command"],"additionalProperties":false}),
374-
cache_control: None,
375-
},
376-
ApiTool {
377-
name: "Edit".to_string(),
378-
description: "Performs exact string replacements in files.".to_string(),
379-
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"old_string":{"type":"string"},"new_string":{"type":"string"},"replace_all":{"type":"boolean","default":false}},"required":["file_path","old_string","new_string"],"additionalProperties":false}),
380-
cache_control: None,
381-
},
382-
ApiTool {
383-
name: "Glob".to_string(),
384-
description: "Fast file pattern matching tool.".to_string(),
385-
input_schema: json!({"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"}},"required":["pattern"],"additionalProperties":false}),
386-
cache_control: None,
387-
},
388-
ApiTool {
389-
name: "Grep".to_string(),
390-
description: "A powerful search tool built on ripgrep.".to_string(),
391-
input_schema: json!({"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"},"glob":{"type":"string"},"output_mode":{"type":"string","enum":["content","files_with_matches","count"]},"-B":{"type":"number"},"-A":{"type":"number"},"-C":{"type":"number"},"context":{"type":"number"},"-n":{"type":"boolean"},"-i":{"type":"boolean"},"type":{"type":"string"},"head_limit":{"type":"number"},"offset":{"type":"number"},"multiline":{"type":"boolean"}},"required":["pattern"],"additionalProperties":false}),
392-
cache_control: None,
393-
},
394-
ApiTool {
395-
name: "Read".to_string(),
396-
description: "Reads a file from the local filesystem.".to_string(),
397-
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"offset":{"type":"integer","minimum":0},"limit":{"type":"integer","exclusiveMinimum":0},"pages":{"type":"string"}},"required":["file_path"],"additionalProperties":false}),
398-
cache_control: None,
399-
},
400-
ApiTool {
401-
name: "ScheduleWakeup".to_string(),
402-
description: "Schedule when to resume work in /loop dynamic mode.".to_string(),
403-
input_schema: json!({"type":"object","properties":{"delaySeconds":{"type":"number"},"reason":{"type":"string"},"prompt":{"type":"string"}},"required":["delaySeconds","reason","prompt"],"additionalProperties":false}),
404-
cache_control: None,
405-
},
406-
ApiTool {
407-
name: "Skill".to_string(),
408-
description: "Execute a skill within the main conversation".to_string(),
409-
input_schema: json!({"type":"object","properties":{"skill":{"type":"string"},"args":{"type":"string"}},"required":["skill"],"additionalProperties":false}),
410-
cache_control: None,
411-
},
412-
ApiTool {
413-
name: "Write".to_string(),
414-
description: "Writes a file to the local filesystem.".to_string(),
415-
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"content":{"type":"string"}},"required":["file_path","content"],"additionalProperties":false}),
416-
cache_control: None,
417-
},
366+
//
367+
// Each entry lists the local registry tools it fronts. A builtin is
368+
// only advertised when one of those tools actually exists in the
369+
// registry, so removed tools (e.g. the retired `subagent` and native
370+
// `glob`) are not advertised as ghosts the model can call but the
371+
// registry cannot execute ("Unknown tool: subagent/glob").
372+
let curated: Vec<(&[&str], ApiTool)> = vec![
373+
(
374+
&["subagent"],
375+
ApiTool {
376+
name: "Agent".to_string(),
377+
description: "Launch a new agent to handle complex, multi-step tasks."
378+
.to_string(),
379+
input_schema: json!({"type":"object","properties":{"description":{"type":"string"},"prompt":{"type":"string"},"subagent_type":{"type":"string"},"run_in_background":{"type":"boolean"}},"required":["description","prompt"],"additionalProperties":false}),
380+
cache_control: None,
381+
},
382+
),
383+
(
384+
&["bash"],
385+
ApiTool {
386+
name: "Bash".to_string(),
387+
description: "Executes a given bash command and returns its output."
388+
.to_string(),
389+
input_schema: json!({"type":"object","properties":{"command":{"type":"string"},"timeout":{"type":"integer"},"run_in_background":{"type":"boolean"}},"required":["command"],"additionalProperties":false}),
390+
cache_control: None,
391+
},
392+
),
393+
(
394+
&["edit"],
395+
ApiTool {
396+
name: "Edit".to_string(),
397+
description: "Performs exact string replacements in files.".to_string(),
398+
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"old_string":{"type":"string"},"new_string":{"type":"string"},"replace_all":{"type":"boolean","default":false}},"required":["file_path","old_string","new_string"],"additionalProperties":false}),
399+
cache_control: None,
400+
},
401+
),
402+
(
403+
&["glob"],
404+
ApiTool {
405+
name: "Glob".to_string(),
406+
description: "Fast file pattern matching tool.".to_string(),
407+
input_schema: json!({"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"}},"required":["pattern"],"additionalProperties":false}),
408+
cache_control: None,
409+
},
410+
),
411+
(
412+
// `Grep` resolves to the native grep replacement: the OAuth
413+
// reverse map yields `grep`, and `resolve_tool_name` aliases
414+
// that to `agentgrep`.
415+
&["grep", "agentgrep"],
416+
ApiTool {
417+
name: "Grep".to_string(),
418+
description: "A powerful search tool built on ripgrep.".to_string(),
419+
input_schema: json!({"type":"object","properties":{"pattern":{"type":"string"},"path":{"type":"string"},"glob":{"type":"string"},"output_mode":{"type":"string","enum":["content","files_with_matches","count"]},"-B":{"type":"number"},"-A":{"type":"number"},"-C":{"type":"number"},"context":{"type":"number"},"-n":{"type":"boolean"},"-i":{"type":"boolean"},"type":{"type":"string"},"head_limit":{"type":"number"},"offset":{"type":"number"},"multiline":{"type":"boolean"}},"required":["pattern"],"additionalProperties":false}),
420+
cache_control: None,
421+
},
422+
),
423+
(
424+
&["read"],
425+
ApiTool {
426+
name: "Read".to_string(),
427+
description: "Reads a file from the local filesystem.".to_string(),
428+
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"offset":{"type":"integer","minimum":0},"limit":{"type":"integer","exclusiveMinimum":0},"pages":{"type":"string"}},"required":["file_path"],"additionalProperties":false}),
429+
cache_control: None,
430+
},
431+
),
432+
(
433+
&["schedule"],
434+
ApiTool {
435+
name: "ScheduleWakeup".to_string(),
436+
description: "Schedule when to resume work in /loop dynamic mode.".to_string(),
437+
input_schema: json!({"type":"object","properties":{"delaySeconds":{"type":"number"},"reason":{"type":"string"},"prompt":{"type":"string"}},"required":["delaySeconds","reason","prompt"],"additionalProperties":false}),
438+
cache_control: None,
439+
},
440+
),
441+
(
442+
&["skill_manage"],
443+
ApiTool {
444+
name: "Skill".to_string(),
445+
description: "Execute a skill within the main conversation".to_string(),
446+
input_schema: json!({"type":"object","properties":{"skill":{"type":"string"},"args":{"type":"string"}},"required":["skill"],"additionalProperties":false}),
447+
cache_control: None,
448+
},
449+
),
450+
(
451+
&["write"],
452+
ApiTool {
453+
name: "Write".to_string(),
454+
description: "Writes a file to the local filesystem.".to_string(),
455+
input_schema: json!({"type":"object","properties":{"file_path":{"type":"string"},"content":{"type":"string"}},"required":["file_path","content"],"additionalProperties":false}),
456+
cache_control: None,
457+
},
458+
),
418459
];
460+
let mut out: Vec<ApiTool> = curated
461+
.into_iter()
462+
.filter(|(serves, _)| serves.iter().any(|name| registry_names.contains(name)))
463+
.map(|(_, tool)| tool)
464+
.collect();
419465

420466
// Forward every other registered tool, remapping its name to the
421467
// OAuth-accepted form. This restores websearch/webfetch/browser/
@@ -990,6 +1036,9 @@ mod cache_prefix_invariant_tests {
9901036
let registry = vec![
9911037
tool_def("bash"),
9921038
tool_def("read"),
1039+
tool_def("write"),
1040+
tool_def("edit"),
1041+
tool_def("agentgrep"),
9931042
tool_def("subagent"),
9941043
tool_def("websearch"),
9951044
tool_def("webfetch"),
@@ -1002,7 +1051,7 @@ mod cache_prefix_invariant_tests {
10021051
let names: Vec<&str> = formatted.iter().map(|t| t.name.as_str()).collect();
10031052

10041053
// Curated builtins are present under their OAuth names.
1005-
for builtin in ["Bash", "Read", "Agent", "Write", "Edit", "Glob", "Grep"] {
1054+
for builtin in ["Bash", "Read", "Agent", "Write", "Edit", "Grep"] {
10061055
assert!(
10071056
names.contains(&builtin),
10081057
"missing builtin {builtin} in {names:?}"
@@ -1021,6 +1070,38 @@ mod cache_prefix_invariant_tests {
10211070
assert_eq!(names.iter().filter(|n| **n == "Read").count(), 1);
10221071
}
10231072

1073+
#[test]
1074+
fn oauth_format_tools_drops_builtins_missing_from_registry() {
1075+
// A registry without subagent/glob (both removed from the default
1076+
// toolset) must not advertise the Agent/Glob builtins: the model would
1077+
// call them and hit "Unknown tool: subagent/glob" (ghost tools).
1078+
let registry = vec![
1079+
tool_def("bash"),
1080+
tool_def("read"),
1081+
tool_def("write"),
1082+
tool_def("edit"),
1083+
tool_def("agentgrep"),
1084+
tool_def("websearch"),
1085+
];
1086+
1087+
let formatted = format_tools(&registry, true, false);
1088+
let names: Vec<&str> = formatted.iter().map(|t| t.name.as_str()).collect();
1089+
1090+
for ghost in ["Agent", "Glob"] {
1091+
assert!(
1092+
!names.contains(&ghost),
1093+
"{ghost} advertised without a backing registry tool: {names:?}"
1094+
);
1095+
}
1096+
// Backed builtins stay advertised; `agentgrep` backs `Grep`.
1097+
for builtin in ["Bash", "Read", "Write", "Edit", "Grep"] {
1098+
assert!(
1099+
names.contains(&builtin),
1100+
"missing builtin {builtin} in {names:?}"
1101+
);
1102+
}
1103+
}
1104+
10241105
#[test]
10251106
fn oauth_format_tools_places_single_cache_breakpoint_on_last_tool() {
10261107
let registry = vec![tool_def("bash"), tool_def("websearch")];

0 commit comments

Comments
 (0)