sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

claude 工具调用出现问题

Open heishen6 opened this issue 7 months ago • 1 comments

Image

API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"Tool reference 'WebFetch' not found in available tools"},"request_id":"xxxx"}

heishen6 avatar Apr 25 '26 13:04 heishen6

我这里也复现到了同类问题,补充一份脱敏后的定位信息,供参考。

环境/现象(已脱敏):

  • Sub2API 版本:基于 v0.1.118
  • 账号类型:anthropic / oauth
  • 入口:/v1/messages
  • stream: true
  • 上游返回:400 invalid_request_error
  • 错误信息:Tool reference 'WebFetch' not found in available tools

脱敏后的请求结构里,顶层 tools 数量为 8,包含:

["Agent", "Bash", "Edit", "Read", "Skill", "ToolSearch", "WebFetch", "Write"]

同时历史消息里存在一个嵌套的 tool reference,形状大致如下:

{
  "role": "user",
  "content": [
    {
      "type": "tool_result",
      "tool_use_id": "toolu_xxx",
      "content": [
        {
          "type": "tool_reference",
          "tool_name": "WebFetch"
        }
      ]
    }
  ]
}

怀疑原因:Claude OAuth mimicry 里,当工具数量超过阈值后会触发动态工具名改写。当前实现会改写:

  • tools[*].name
  • tool_choice.name

但看起来没有同步改写历史消息里的嵌套引用,例如:

  • messages[*].content[*].content[*].tool_name,当该块为 type: "tool_reference" 时

这样转发到上游时,tools 列表里的 WebFetch 可能已经被改成假名,但历史消息中的 tool_reference.tool_name 仍然保留 WebFetch,于是上游校验时认为 WebFetch 不在 available tools 中。

关联代码位置可能在:

  • backend/internal/service/gateway_tool_rewrite.go
    • buildToolNameRewriteFromBody
    • applyToolNameRewriteToBody

我没有贴真实 request id、账号名、代理、域名、完整请求体或日志,以上只保留了复现这个校验错误所需的结构信息。

Brisbanehuang avatar Apr 26 '26 01:04 Brisbanehuang