feat(anthropic): supported Anthropic server tools - #2489
Open
Leeaoyin wants to merge 1 commit into
Open
Conversation
1、引入版本化的 AnthropicServerTool 抽象(显式指定 type + params,通过 SDK 的 JSON 模型转换并即时校验,支持 SDK 已知的全部 server tools:web_search、web_fetch、code_execution、tool_search 等,未知参数原样透传由服务端校验)。 2、支持在流式与非流式响应中统一解析 server_tool_use 及全部 6 种服务端工具结果块(web_search / web_fetch / code_execution / bash_code_execution / text_editor_code_execution / tool_search),结果块以原始 JSON 保留在 metadata 中并在多轮对话中原样回传;ReAct 循环跳过服务端工具的本地执行,并将已完成的服务端工具视为本轮结束。 3、将 anthropic-java 从 2.14.0 升级到 2.52.0,以解决 server tools 的稳定 API 类型支持。
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds first-class support for Anthropic “server tools” (e.g., web_search, web_fetch, code_execution, tool_search) so they can be enabled on requests, parsed from Anthropic responses (streaming and non-streaming), and correctly handled in AgentScope’s ReAct loop without being executed by the local toolkit.
Changes:
- Introduces
AnthropicServerTool(versionedtype+ passthrough params) and wires it intoAnthropicChatModelrequest construction. - Extends Anthropic formatting/parsing to recognize
server_tool_useand server tool result blocks, preserving raw result JSON in metadata for multi-turn echoing. - Updates core ReAct flow and accumulators to skip local execution for server tools and to treat inline server tool results as completing those tool calls, with added tests.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/tool/AnthropicServerTool.java | New versioned server-tool abstraction that converts to Anthropic SDK ToolUnion. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/tool/AnthropicServerToolTest.java | Unit tests validating server tool typing, param validation, and serialization passthrough. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/AnthropicChatModel.java | Adds builder support for server tools and injects them into message-create params. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicResponseParser.java | Parses server_tool_use and multiple server tool result block types; stores raw result JSON in metadata. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicResponseParserTest.java | Adds coverage for parsing server tool use + results (including error cases) and streaming start events. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/main/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicMessageConverter.java | Ensures server tool blocks stay inline and are echoed back verbatim while preserving existing client-tool splitting behavior. |
| agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic/src/test/java/io/agentscope/extensions/model/anthropic/formatter/AnthropicMessageConverterTest.java | Tests correct inline handling and echoing of server tool use/results without breaking client tool result splitting. |
| agentscope-dependencies-bom/pom.xml | Upgrades anthropic-java to 2.52.0 for server tool API type support. |
| agentscope-core/src/main/java/io/agentscope/core/message/ToolUseBlock.java | Adds metadata flag + helper for identifying provider server tool calls. |
| agentscope-core/src/main/java/io/agentscope/core/message/ToolResultBlock.java | Adds metadata flag + helper for identifying provider server tool results. |
| agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java | Skips server tools when computing pending tool executions and finishes when all tool calls are completed server tools inline. |
| agentscope-core/src/main/java/io/agentscope/core/agent/accumulator/ReasoningContext.java | Collects server tool results during streaming and places them after their corresponding tool calls in the final message. |
| agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentServerToolTest.java | New tests verifying ReAct behavior for server tools (inline result finishes; missing result continues loop). |
| agentscope-core/src/test/java/io/agentscope/core/agent/accumulator/ReasoningContextTest.java | New tests validating passthrough/ordering of server tool results and preserving prior behavior for non-server tool results. |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概述
Fixes #2236。
引入 Anthropic 专属的版本化
AnthropicServerTool抽象(显式指定 type,如web_search_20250305),支持 Claude 内置 server tools(在单次模型调用内于 Anthropic服务端执行)。描述
type+ 按 API 字段名传参,内部通过 SDK 的 JSON 模型(ToolUnion)转换并即时校验,SDK 已知的全部 server tools 均可用(web_search、web_fetch、code_execution、tool_search 各版本);未知参数原样透传由服务端校验,新增 API 字段无需改代码。server_tool_use及全部 6 种结果块,结果块以原始ContentBlockParamJSON 保留在 metadata 中,多轮对话时原样回传。用法
Web 搜索(支持域名过滤、次数限制、用户位置):
Web 抓取(抓取网页/PDF 内容,支持引用与内容长度限制):
代码执行(在 Anthropic 沙箱中运行 Python/Bash):
显式指定任意版本(上面的方法只是预填常用版本,SDK 已知的任意版本化 type 都可直接使用,无需改框架代码):
同时启用多个 server tools:
参数按 Anthropic API 字段名原样透传并由 SDK 即时校验,未知字段透传给服务端校验,
API 新增参数无需升级框架就能使用。
改动
core:ToolUseBlock/ToolResultBlock 增加 server tool 标记;ReasoningContext 透传并聚合模型返回的工具结果;ReActAgent 跳过服务端工具执行并修正结束判定。
anthropic 扩展:新增 AnthropicServerTool;Builder.addServerTool(...) 请求注入; AnthropicResponseParser / AnthropicMessageConverter 统一解析与回传。
BOM:anthropic-java 2.14.0 → 2.52.0
测试
新增单测:AnthropicServerToolTest、ReActAgentServerToolTest,以及 parser / converter / ReasoningContext 中的 server tool 用例
core + anthropic 模块全量测试通过(173 个测试,0 失败)