agentscope.toolToolBase
class ToolBase(ABC)
agentscope/tool/_base.py:100–491工具协议。位于 agentscope.tool 包,继承自 ABC。核心成员包括 call、check_permissions、check_external_result、check_read_only、match_rule、generate_suggestions。
The tool protocol. Declared in agentscope.tool. Extends ABC.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(middlewares: List['ToolMiddlewareBase'] | None = None) -> None | 初始化 `ToolBase` 实例。 Initialize the tool with optional middlewares. |
属性摘要Attribute Summary
| 属性Attribute | 类型Type | 说明Description | 值Value |
|---|---|---|---|
name | str | 名称字段。 | — |
description | str | `description` 字段。 | — |
input_schema | dict[str, Any] | 输入模式字段。 | — |
is_concurrency_safe | bool | `is_concurrency_safe` 字段。 | — |
is_read_only | bool | `is_read_only` 字段。 | — |
is_external_tool | bool | 是外部工具字段。 | False |
is_state_injected | bool | `is_state_injected` 字段。 | False |
metadata_schema | dict[str, Any] | None | 元数据模式字段。 | None |
is_mcp | bool | 是 MCP字段。 | False |
mcp_name | str | None | MCP 名称字段。 | None |
dangerous_files | list[str] | `dangerous_files` 字段。 | DEFAULT_DANGEROUS_FILES |
dangerous_directories | list[str] | `dangerous_directories` 字段。 | DEFAULT_DANGEROUS_DIRECTORIES |
方法摘要Method Summary
构造器详情Constructor Detail
ToolBase
def __init__(middlewares: List['ToolMiddlewareBase'] | None = None) -> None
初始化 ToolBase 实例。
Initialize the tool with optional middlewares.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
middlewares默认default None | List['ToolMiddlewareBase'] | None | 中间件列表(类型 `List['ToolMiddlewareBase'] | None`)。A list of :class:`ToolMiddlewareBase` instances wrapping the tool execution in an onion fashion. Defaults to an empty list. |
agentscope/tool/_base.py:144–157属性详情Attribute Detail
name
str名称字段。声明为 str。
The name field.
agentscope/tool/_base.py:103description
strdescription 字段。声明为 str。
The description field.
agentscope/tool/_base.py:105input_schema
dict[str, Any]输入模式字段。声明为 dict[str, Any]。
The input schema field.
agentscope/tool/_base.py:107is_concurrency_safe
boolis_concurrency_safe 字段。声明为 bool。
The is concurrency safe field.
agentscope/tool/_base.py:109is_read_only
boolis_read_only 字段。声明为 bool。
The is read only field.
agentscope/tool/_base.py:111is_external_tool
bool = False是外部工具字段。声明为 bool。
The is external tool field.
agentscope/tool/_base.py:114is_state_injected
bool = Falseis_state_injected 字段。声明为 bool。
The is state injected field.
agentscope/tool/_base.py:117metadata_schema
dict[str, Any] | None = None元数据模式字段。声明为 dict[str, Any] | None。
The metadata schema field.
agentscope/tool/_base.py:122is_mcp
bool = False是 MCP字段。声明为 bool。
The is mcp field.
agentscope/tool/_base.py:131mcp_name
str | None = NoneMCP 名称字段。声明为 str | None。
The mcp name field.
agentscope/tool/_base.py:133dangerous_files
list[str] = DEFAULT_DANGEROUS_FILESdangerous_files 字段。声明为 list[str]。
The dangerous files field.
agentscope/tool/_base.py:138dangerous_directories
list[str] = DEFAULT_DANGEROUS_DIRECTORIESdangerous_directories 字段。声明为 list[str]。
The dangerous directories field.
agentscope/tool/_base.py:140方法详情Method Detail
call
async def call(*args: Any, **kwargs: Any) -> ToolChunk | AsyncGenerator[ToolChunk, None]
向智能体发送输入并返回最终回复。
This is the new override point for tool implementations.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
args | Any | 参数(类型 `Any`)。The args. |
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
agentscope/tool/_base.py:159–188__call__
async def __call__(*args: Any, **kwargs: Any) -> ToolChunk | AsyncGenerator[ToolChunk, None]
以可调用对象方式执行。
Tools are always invoked with keyword arguments only.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
args | Any | 参数(类型 `Any`)。The args. |
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
agentscope/tool/_base.py:190–263check_permissions
async def check_permissions(tool_input: dict[str, Any], context: PermissionContext) -> PermissionDecision
检查权限用于工具用量。
Check permissions for the tool usage.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tool_input | dict[str, Any] | 工具输入(类型 `dict[str, Any]`)。The tool input. |
context | PermissionContext | 上下文(类型 `PermissionContext`)。The context. |
agentscope/tool/_base.py:266–271check_external_result
async def check_external_result(result: 'ToolResultBlock') -> None
检查外部结果。
Called only for externally executed calls — a tool that produced its own result has nothing to check.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
result | 'ToolResultBlock' | 结果(类型 `'ToolResultBlock'`)。The result. |
- 可能抛出 `Exception`。`jsonschema.ValidationError`:
- 可能抛出 `Exception`。If the metadata does not match what the tool promised its
- 可能抛出 `Exception`。caller. The reply stays parked, so the executor can fix
- 可能抛出 `Exception`。what it sent and try again.
agentscope/tool/_base.py:273–296check_read_only
async def check_read_only(tool_input: dict[str, Any]) -> bool
检查 check_read_only 条件。
Returns the static :attr:is_read_only attribute by default.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tool_input | dict[str, Any] | 工具输入(类型 `dict[str, Any]`)。The tool input data for this invocation. |
agentscope/tool/_base.py:298–320match_rule
async def match_rule(rule_content: str | None, tool_input: dict[str, Any]) -> bool
执行 match_rule 方法。
is a tool-name-level rule that matches every invocation; a rule with content requires the tool to override this method with its own matching logic, otherwise it returns False.
说明Notes
- 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: This is an optional method. A rule with no content (`None`)
| 名称Name | 类型Type | 说明Description |
|---|---|---|
rule_content | str | None | 规则内容(类型 `str | None`)。The rule pattern to match. `None` means "match all invocations of this tool" (tool-name-level rule). |
tool_input | dict[str, Any] | 工具输入数据。The tool input data |
agentscope/tool/_base.py:322–353generate_suggestions
async def generate_suggestions(tool_input: dict[str, Any]) -> List[PermissionRule]
设置该参数。
that allows all invocations of this tool.
说明Notes
- 注意:该行为受运行时配置或底层实现影响,建议结合对应源码与版本说明确认。Note: Suggest a single tool-name-level rule (`rule_content=None`)
| 名称Name | 类型Type | 说明Description |
|---|---|---|
tool_input | dict[str, Any] | 工具输入数据。The tool input data |
agentscope/tool/_base.py:355–388