Skip to content

feat(anthropic): supported Anthropic server tools - #2489

Merged
jujn merged 13 commits into
agentscope-ai:mainfrom
Leeaoyin:feat/issue-2236-supported-anthropic-server-tools
Sep 26, 2026
Merged

jujn merged 13 commits into
agentscope-ai:mainfrom
Leeaoyin:feat/issue-2236-supported-anthropic-server-tools

Conversation

@Leeaoyin

@Leeaoyin Leeaoyin commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

概述

Fixes #2236。

为 Anthropic Messages API 内置 server tools 提供端到端支持:开发者可启用 SDK 已建模的 server tools,AgentScope 会解析模型返回的 server_tool_use 和 server tool result,并在 ReAct 循环中跳过本地执行。

描述

  • 新增 AnthropicServerTool,包装 Anthropic SDK 构建的 ToolUnion。SDK 已建模的 server tools 可直接启用;SDK 未建模字段可通过 SDK builder 的 additional properties 透传。自定义 client tool 会被拒绝。
  • AnthropicChatModel.Builder 新增 addServerTool(...)。client tools 与 server tools 走统一组装路径,共同校验工具名冲突,并统一应用 toolChoice 与 parallelToolCalls。
  • 流式与非流式响应均解析 server_tool_use 及 6 类 server tool result block;新增 AnthropicServerToolHelper 集中处理 SDK union 归一、AgentScope block 转换和多轮回传。
  • server tool result 保留原始 ContentBlockParam JSON,后续请求可恢复 Anthropic 原始结果块,避免丢失 provider 专有字段。
  • core 通过 serverTool metadata 识别服务端执行的工具:ReAct 跳过本地 toolkit 执行,pending tool 恢复也不包含 server tools。
  • 新增 ServerToolResultAccumulator,将 server tool result 放回对应 tool call 后方;无匹配 result 时保留并记录 warning。
  • 已完成的 server tool 会结束本轮;未返回 result 的 server tool 会继续 reasoning,用于 pause_turn。
  • server tool result 会发出 TOOL_RESULT_START、TOOL_RESULT_TEXT_DELTA / TOOL_RESULT_DATA_DELTA、TOOL_RESULT_END 事件,流式消费者可以看到服务端工具执行结果。
  • 修复 mixed server/local tool round 中本地 return-direct 工具错误短路的问题。

用法

AnthropicServerTool webSearch =
        AnthropicServerTool.of(
                ToolUnion.ofWebSearchTool20250305(
                        WebSearchTool20250305.builder()
                                .maxUses(5)
                                .allowedDomains(List.of("example.com"))
                                .build()));

AnthropicChatModel model =
        AnthropicChatModel.builder()
                .apiKey(...)
                .modelName("claude-sonnet-4-5")
                .addServerTool(webSearch)
                .build();

Web fetch、code execution、tool search 等同理:先用对应 Anthropic SDK builder 构造 ToolUnion,再交给 AnthropicServerTool.of(...)。

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 类型支持。
Copilot AI review requested due to automatic review settings July 30, 2026 01:02
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (versioned type + passthrough params) and wires it into AnthropicChatModel request construction.
  • Extends Anthropic formatting/parsing to recognize server_tool_use and 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.

@AgentScopeJavaBot AgentScopeJavaBot added enhancement New feature or request area/build Build, CI, BOM, distribution area/core/agent Agent runtime, pipeline, hooks, plan area/core/model Model providers and formatters labels Aug 11, 2026

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

Leeaoyin and others added 5 commits August 25, 2026 20:30
@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch upstream
git checkout feat/issue-2236-supported-anthropic-server-tools
git rebase upstream/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Once the conflicts are resolved, comment @oss-maintainer please re-review and I will pick it up again.


Automated notification by github-manager-bot

- preserve server tool filtering after MessageUtils refactor and retain empty-response retry
- combine serverTools, authToken, and cacheTtl while preserving constructor compatibility
- retain Anthropic SDK 2.52.0 alongside upstream MCP 0.17.2
- add regression tests for mixed tools and authenticated server-tool requests
oss-maintainer

This comment was marked as abuse.

oss-maintainer

This comment was marked as abuse.

@jujn
jujn merged commit 857e3e6 into agentscope-ai:main Sep 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/build Build, CI, BOM, distribution area/core/agent Agent runtime, pipeline, hooks, plan area/core/model Model providers and formatters enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: 支持 Anthropic server tools / Web Search

5 participants