Toolkit
public class Toolkit
io/agentscope/core/tool/Toolkit.java:70–1282工具包扫描 @工具注解方法,把 Java 方法转换成模型工具模式,并负责工具查找、分组、MCP 注册与权限元数据。位于 io.agentscope.core.tool 包。核心成员包括 registration、registerTool、registerAgentTool、getTool、getToolNames、registerSchema。
Toolkit manages the registration, retrieval, and execution of agent tools. This class acts as a facade, delegating specific responsibilities to specialized managers:
Managers:
-
ToolRegistry: Tool registration and lookup
-
ToolGroupManager: Tool group CRUD operations and active group management
-
ToolSchemaProvider: Tool schema generation with group filtering
-
McpClientManager: MCP client lifecycle and tool registration
-
MetaToolFactory: Creates meta tools for dynamic group control
Core Components:
-
ToolSchemaGenerator: Generates JSON schemas for tool parameters
-
ToolMethodInvoker: Handles method invocation and parameter conversion
-
ToolResultConverter: Converts method results to ToolResultBlock
-
ToolExecutor: Handles parallel/sequential tool execution with validation
Features:
-
Tool group management for dynamic tool activation
-
State management via StateModule interface (activeGroups persistence)
-
Meta tool for runtime tool group control (reset_equipped_tools)
-
MCP (Model Context Protocol) client support for external tool providers
核心精讲Deep dive
Toolkit:工具注册表Toolkit: the tool registry
Toolkit 扫描 @Tool 注解方法,把 Java 方法转换成模型工具 schema,并负责工具查找、分组、MCP 注册与权限元数据。
Toolkit scans @Tool-annotated methods, converts Java methods into model tool schemas, and handles lookup, grouping, MCP registration and permission metadata.
- registerTool 扫描对象上的 @Tool 方法,@ToolParam 描述参数。registerTool scans @Tool methods on an object, with @ToolParam describing parameters.
- registerSchema 用于手工注册已有 JSON Schema 的工具。registerSchema registers tools from existing JSON schemas.
- registerMcpClient 把 MCP 工具批量纳入注册表,closeMcpClients 统一释放。registerMcpClient adds MCP tools in bulk, and closeMcpClients releases them together.
用注解注册工具Register tools with annotations
import io.agentscope.core.tool.Tool;
import io.agentscope.core.tool.ToolParam;
import io.agentscope.core.tool.Toolkit;
public class ToolkitDemo {
static final class WeatherTools {
@Tool(name = "get_weather", description = "Return the current weather for a city.")
public String getWeather(
@ToolParam(name = "city", description = "City name, e.g. Hangzhou") String city) {
return city + ": sunny, 24C";
}
}
public static void main(String[] args) {
Toolkit toolkit = new Toolkit();
toolkit.registerTool(new WeatherTools());
System.out.println(toolkit.getToolNames());
}
}嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
ToolRegistration | 类class | 工具注册,让智能体以受控方式调用外部能力。 Fluent builder for registering tools with optional configuration. |
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public Toolkit() | 创建空工具注册表。 Create a Toolkit with default configuration (parallel execution using Reactor). |
public Toolkit(ToolkitConfig config) | 创建空工具注册表。 Create a Toolkit with custom configuration. |
方法摘要Method Summary
构造器详情Constructor Detail
Toolkit
public Toolkit()
初始化 Toolkit 实例。
Create a Toolkit with default configuration (parallel execution using Reactor).
io/agentscope/core/tool/Toolkit.java:88–90Toolkit
public Toolkit(ToolkitConfig config)
创建工具包使用自定义配置。
Create a Toolkit with custom configuration.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
config | ToolkitConfig | 配置对象(类型 `ToolkitConfig`)。Toolkit configuration (if null, uses defaultConfig()) |
io/agentscope/core/tool/Toolkit.java:97–122方法详情Method Detail
registration
public ToolRegistration registration()
返回注册。
Create a fluent builder for registering tools with optional configuration.
ToolRegistrationio/agentscope/core/tool/Toolkit.java:151–153registerTool
public void registerTool(Object toolObject)
注册工具。
Register a tool object by scanning for methods annotated with @Tool.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolObject | Object | 包含 @工具方法的普通 Java 对象。the object containing tool methods |
io/agentscope/core/tool/Toolkit.java:159–161registerAgentTool
public void registerAgentTool(AgentTool tool)
注册智能体工具。
Register an AgentTool instance directly.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tool | AgentTool | AgentTool 到注册。the AgentTool to register |
io/agentscope/core/tool/Toolkit.java:208–210getTool
public AgentTool getTool(String name)
获取工具。
Retrieves a tool by its name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | String | 工具到检索的名称。The name of the tool to retrieve |
AgentToolio/agentscope/core/tool/Toolkit.java:256–258getTool
public AgentTool getTool(String name, ToolRequestConfig requestConfig)
获取工具。
Resolve a tool by name, composing a per-call ToolRequestConfig with the shared registry (never mutating either).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
name | String | 名称(类型 `String`)。tool name |
requestConfig | ToolRequestConfig | 请求配置(类型 `ToolRequestConfig`)。per-call request config (may be `null` → `ToolRequestConfig#NONE`) |
AgentToolio/agentscope/core/tool/Toolkit.java:271–274getToolNames
public Set<String> getToolNames()
获取 getToolNames。
Gets the names of all registered tools.
Set<String>io/agentscope/core/tool/Toolkit.java:281–283registerSchema
public void registerSchema(ToolSchema schema)
注册模式。
Register an external tool using only its schema definition.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
schema | ToolSchema | 模式定义(类型 `ToolSchema`)。The tool schema containing name, description, and parameters |
NullPointerException可能抛出 `NullPointerException`。if schema is null
io/agentscope/core/tool/Toolkit.java:315–317registerSchemas
public void registerSchemas(List<ToolSchema> schemas)
注册模式。
Register multiple external tools using their schema definitions.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
schemas | List<ToolSchema> | 工具模式到注册的列出。List of tool schemas to register |
NullPointerException可能抛出 `NullPointerException`。if schemas is null
io/agentscope/core/tool/Toolkit.java:326–330isExternalTool
public boolean isExternalTool(String toolName)
判断是否外部工具。
Check if a tool is an external tool (requires execution outside the framework).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolName | String | 工具到检查的名称。The name of the tool to check |
booleanio/agentscope/core/tool/Toolkit.java:343–346isExternalTool
public boolean isExternalTool(String toolName, ToolRequestConfig requestConfig)
判断是否外部工具。
Check whether toolName resolves to an external tool under a per-call ToolRequestConfig.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolName | String | 工具到检查的名称。The name of the tool to check |
requestConfig | ToolRequestConfig | 请求配置(类型 `ToolRequestConfig`)。per-call request config (may be `null` → shared registry only) |
booleanio/agentscope/core/tool/Toolkit.java:359–362getToolSchemas
public List<ToolSchema> getToolSchemas()
获取工具模式。
Get tool schemas as ToolSchema objects.
List<ToolSchema>io/agentscope/core/tool/Toolkit.java:373–375getToolSchemas
public List<ToolSchema> getToolSchemas(Collection<String> activeGroups)
获取工具模式。
Get tool schemas filtered by an explicitly supplied set of active group names, independent of this toolkit's shared per-group activation flags.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
activeGroups | Collection<String> | active 组(类型 `Collection`)。the group names to treat as active for this resolution |
List<ToolSchema>io/agentscope/core/tool/Toolkit.java:389–391getToolSchemas
public List<ToolSchema> getToolSchemas(Collection<String> activeGroups, ToolRequestConfig requestConfig)
获取工具模式。
Stateless compose: the backend registry filtered by activeGroups, then a per-call ToolRequestConfig applied without mutating the shared registry or group manager.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
activeGroups | Collection<String> | active 组(类型 `Collection`)。the group names treated as active for this resolution |
requestConfig | ToolRequestConfig | 请求配置(类型 `ToolRequestConfig`)。per-call request config (may be `null` → `ToolRequestConfig#NONE`) |
List<ToolSchema>io/agentscope/core/tool/Toolkit.java:410–436setChunkCallback
public void setChunkCallback(BiConsumer<ToolUseBlock, ToolResultBlock> callback)
设置分块回调。
Set the chunk callback for streaming tool responses.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
callback | BiConsumer<ToolUseBlock, ToolResultBlock> | 回调函数(类型 `BiConsumer`)。Callback to invoke when tools emit chunks via ToolEmitter |
io/agentscope/core/tool/Toolkit.java:531–533callTool
public Mono<ToolResultBlock> callTool(ToolCallParam param)
调用工具。
Execute a tool with the given parameters.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
param | ToolCallParam | 参数(类型 `ToolCallParam`)。Tool call parameters containing execution information |
Mono<ToolResultBlock>io/agentscope/core/tool/Toolkit.java:559–561callTools
public Mono<List<ToolResultBlock>> callTools(List<ToolUseBlock> toolCalls, ExecutionConfig agentExecutionConfig, Agent agent, RuntimeContext agentRuntimeContext)
调用工具。
Execute multiple tools asynchronously with agent-level context (internal use by ReActAgent).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCalls | List<ToolUseBlock> | 工具调用到执行的列出。List of tool calls to execute |
agentExecutionConfig | ExecutionConfig | 智能体执行配置(类型 `ExecutionConfig`)。Execution config from agent level (can be null) |
agent | Agent | 智能体(类型 `Agent`)。The agent making the calls (may be null) |
agentRuntimeContext | RuntimeContext | 智能体运行时上下文(类型 `RuntimeContext`)。The agent-level runtime context (may be null) |
Mono<List<ToolResultBlock>>io/agentscope/core/tool/Toolkit.java:580–586callTools
public Mono<List<ToolResultBlock>> callTools(List<ToolUseBlock> toolCalls, ExecutionConfig agentExecutionConfig, Agent agent, RuntimeContext agentRuntimeContext, BiConsumer<ToolUseBlock, ToolResultBlock> internalChunkCallback)
调用工具。
Execute multiple tools with request configuration from the runtime context and a per-call chunk callback.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCalls | List<ToolUseBlock> | 工具调用(类型 `List`)。The tool calls. |
agentExecutionConfig | ExecutionConfig | 智能体执行配置(类型 `ExecutionConfig`)。The agent execution config. |
agent | Agent | 智能体(类型 `Agent`)。The agent. |
agentRuntimeContext | RuntimeContext | 智能体运行时上下文(类型 `RuntimeContext`)。The agent runtime context. |
internalChunkCallback | BiConsumer<ToolUseBlock, ToolResultBlock> | 内部分块回调(类型 `BiConsumer`)。per-call internal chunk callback (may be `null`) |
Mono<List<ToolResultBlock>>io/agentscope/core/tool/Toolkit.java:593–608callTools
public Mono<List<ToolResultBlock>> callTools(List<ToolUseBlock> toolCalls, ExecutionConfig agentExecutionConfig, Agent agent, RuntimeContext agentRuntimeContext, ToolRequestConfig requestConfig, BiConsumer<ToolUseBlock, ToolResultBlock> internalChunkCallback)
调用工具。
Execute multiple tools with a per-call ToolRequestConfig and internal chunk callback, both threaded down to every single-tool execution.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolCalls | List<ToolUseBlock> | 工具调用(类型 `List`)。The tool calls. |
agentExecutionConfig | ExecutionConfig | 智能体执行配置(类型 `ExecutionConfig`)。The agent execution config. |
agent | Agent | 智能体(类型 `Agent`)。The agent. |
agentRuntimeContext | RuntimeContext | 智能体运行时上下文(类型 `RuntimeContext`)。The agent runtime context. |
requestConfig | ToolRequestConfig | 请求配置(类型 `ToolRequestConfig`)。The request config. |
internalChunkCallback | BiConsumer<ToolUseBlock, ToolResultBlock> | 内部分块回调(类型 `BiConsumer`)。The internal chunk callback. |
Mono<List<ToolResultBlock>>io/agentscope/core/tool/Toolkit.java:614–640registerMcpClient
public Mono<Void> registerMcpClient(McpClientWrapper mcpClientWrapper)
注册MCP 客户端。
Registers an MCP client and all its tools.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mcpClientWrapper | McpClientWrapper | 已初始化的 MCP 客户端。the MCP client wrapper |
Mono<Void>io/agentscope/core/tool/Toolkit.java:653–655removeMcpClient
public Mono<Void> removeMcpClient(String mcpClientName)
移除MCP 客户端。
Removes an MCP client and all its tools.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mcpClientName | String | MCP 客户端到移除的名称。the name of the MCP client to remove |
Mono<Void>io/agentscope/core/tool/Toolkit.java:663–665closeMcpClients
public void closeMcpClients()
关闭MCP 客户端。
Releases clients registered by this toolkit.
io/agentscope/core/tool/Toolkit.java:668–677createToolGroup
public void createToolGroup(String groupName, String description, boolean active)
创建工具组。
Create a new tool group with specified activation status and default META scope.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 工具组的名称。Name of the tool group |
description | String | 描述(类型 `String`)。Description of the tool group |
active | boolean | active(类型 `boolean`)。Whether the group should be active by default |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if group already exists
io/agentscope/core/tool/Toolkit.java:689–691createToolGroup
public void createToolGroup(String groupName, String description, boolean active, ToolGroupScope scope)
创建工具组。
Create a new tool group with specified activation status and scope.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 工具组的名称。Name of the tool group |
description | String | 描述(类型 `String`)。Description of the tool group |
active | boolean | active(类型 `boolean`)。Whether the group should be active by default |
scope | ToolGroupScope | 范围(类型 `ToolGroupScope`)。Whether the group is managed by the meta tool (`ToolGroupScope#META`) or by developer code (`ToolGroupScope#EXTERNAL`) |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if group already exists
io/agentscope/core/tool/Toolkit.java:703–706createToolGroup
public void createToolGroup(String groupName, String description)
创建工具组。
Create a new tool group (active by default, META scope).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 工具组的名称。Name of the tool group |
description | String | 描述(类型 `String`)。Description of the tool group |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if group already exists
io/agentscope/core/tool/Toolkit.java:715–717createSkillToolGroup
public void createSkillToolGroup(String groupName, String description, boolean active, String activateOnSkill)
创建技能工具组。
Create a SkillToolGroup bound to a specific skill.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 工具组的名称。Name of the tool group |
description | String | 描述(类型 `String`)。Description of the tool group |
active | boolean | active(类型 `boolean`)。Whether the group should be active by default |
activateOnSkill | String | activate On 技能(类型 `String`)。The skill name that this group is bound to |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if group already exists
io/agentscope/core/tool/Toolkit.java:732–735findSkillToolGroupsByActivateOnSkill
public List<String> findSkillToolGroupsByActivateOnSkill(String skillName)
查找 findSkillToolGroupsByActivateOnSkill。
Find all SkillToolGroup instances whose activateOnSkill matches the given skill name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
skillName | String | 技能名称(类型 `String`)。The skill name to match against |
List<String>io/agentscope/core/tool/Toolkit.java:744–746registerToolGroup
public void registerToolGroup(ToolGroup group)
注册工具组。
Register a pre-built ToolGroup instance (including subclasses).
| 名称Name | 类型Type | 说明Description |
|---|---|---|
group | ToolGroup | 工具组到注册。The tool group to register |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if a group with the same name already exists
io/agentscope/core/tool/Toolkit.java:757–759addToolToGroup
public void addToolToGroup(String groupName, String toolName)
添加工具到组。
Add an already-registered tool to an existing tool group.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 组名称(类型 `String`)。Name of the existing tool group |
toolName | String | 已注册工具的名称。Name of the registered tool |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if the group or tool doesn't exist
io/agentscope/core/tool/Toolkit.java:771–777updateToolGroups
public void updateToolGroups(List<String> groupNames, boolean active)
更新工具组。
Update the activation status of tool groups.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupNames | List<String> | 组 Names(类型 `List`)。List of tool group names to update |
active | boolean | active(类型 `boolean`)。Whether to activate (true) or deactivate (false) the groups |
IllegalArgumentException可能抛出 `IllegalArgumentException`。if any group doesn't exist
io/agentscope/core/tool/Toolkit.java:792–800removeTool
public void removeTool(String toolName)
移除工具。
Remove a tool by name from the toolkit.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolName | String | 工具到移除的名称。Name of the tool to remove |
io/agentscope/core/tool/Toolkit.java:807–813removeToolIfSame
public boolean removeToolIfSame(String toolName, AgentTool expected)
移除 removeToolIfSame。
Atomically remove a tool only if the registered instance is the expected one.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolName | String | 工具到移除的名称。Name of the tool to remove |
expected | AgentTool | expected(类型 `AgentTool`)。The expected AgentTool instance (identity comparison) |
booleanio/agentscope/core/tool/Toolkit.java:822–828removeToolGroups
public void removeToolGroups(List<String> groupNames)
移除工具组。
Remove tool groups and all tools within them.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupNames | List<String> | 组 Names(类型 `List`)。List of tool group names to remove |
io/agentscope/core/tool/Toolkit.java:838–847getActiveGroups
public List<String> getActiveGroups()
获取 getActiveGroups。
Get active tool group names.
List<String>io/agentscope/core/tool/Toolkit.java:861–863setActiveGroups
public void setActiveGroups(List<String> groups)
设置 setActiveGroups。
Set the active tool groups.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groups | List<String> | 组(类型 `List`)。List of group names to set as active |
io/agentscope/core/tool/Toolkit.java:875–877getToolGroup
public ToolGroup getToolGroup(String groupName)
获取工具组通过名称。
Get a tool group by name.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
groupName | String | 工具组的名称。Name of the tool group |
ToolGroupio/agentscope/core/tool/Toolkit.java:885–887registerMetaTool
public void registerMetaTool()
注册元数据工具。
Register the meta tool that allows agents to dynamically manage tool groups.
io/agentscope/core/tool/Toolkit.java:897–905updateToolPresetParameters
public void updateToolPresetParameters(String toolName, Map<String, Object> newPresetParameters)
更新 updateToolPresetParameters。
Update preset parameters for a registered tool at runtime.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
toolName | String | 工具到更新的名称。The name of the tool to update |
newPresetParameters | Map<String, Object> | 新建 Preset 参数(类型 `Map`)。The new preset parameters (null will be treated as empty map) |
IllegalArgumentException当if 工具是不找到时抛出 `IllegalArgumentException`。if the tool is not found
io/agentscope/core/tool/Toolkit.java:918–926copy
public Toolkit copy()
返回 copy 结果。
Creates a build-time isolation copy of this toolkit.
Toolkitio/agentscope/core/tool/Toolkit.java:946–971嵌套类详情Nested Class Detail
ToolRegistration
public static class ToolRegistration工具注册,让智能体以受控方式调用外部能力。位于 io.agentscope.core.tool 包。核心成员包括 tool、agentTool、mcpClient、subAgent、mcpToolNamePrefix、enableTools。
Fluent builder for registering tools with optional configuration.
This builder provides a clear, type-safe way to register tools with various options without method proliferation.