类class
agentscope.toolMCPTool
class MCPTool(ToolBase)
源码Source
agentscope/tool/_adapters.py:197–545MCP 工具,封装可被智能体调用的能力。位于 agentscope.tool 包,继承自 ToolBase。核心成员包括 check_permissions、call。
This class wraps an MCP tool and makes it compatible with the ToolProtocol interface. It handles the conversion between MCP's result format and AgentScope's ToolChunk format.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
def __init__(mcp_name: str, tool: mcp.types.Tool, client_gen: Callable[..., AbstractAsyncContextManager[Any]] | None =… | 初始化 `MCPTool` 实例。 Initialize the MCPTool. |
属性摘要Attribute Summary
| 属性Attribute | 类型Type | 说明Description | 值Value |
|---|---|---|---|
is_mcp | bool | 是 MCP字段。 | True |
is_state_injected | bool | `is_state_injected` 字段。 | False |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
async def check_permissions(*_args: Any, **_kwargs: Any) -> PermissionDecisionasync | 检查权限用于 MCP 工具用量。 Check permissions for the MCP tool usage. |
async def call(**kwargs: Any) -> ToolChunk | AsyncGenerator[ToolChunk, None]async | 返回最终结果,或流当`stream_progress=True`。 Return the final result, or stream when ``stream_progress=True``. |
构造器详情Constructor Detail
MCPTool
def __init__(mcp_name: str, tool: mcp.types.Tool, client_gen: Callable[..., AbstractAsyncContextManager[Any]] | None = None, session: Any | None = None, timeout: float | None = None, middlewares: list[ToolMiddlewareBase] | None = None, stream_progress: bool = False) -> None
初始化 MCPTool 实例。
Initialize the MCPTool.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
mcp_name | str | MCP 服务端实例的名称。The name of the MCP server instance. |
tool | mcp.types.Tool | MCP 工具定义。The MCP tool definition. |
client_gen默认default None | Callable[..., AbstractAsyncContextManager[Any]] | None | 客户端 gen(类型 `Callable[..., AbstractAsyncContextManager[Any]] | None`)。The MCP client generator function for stateless clients. Its context manager should yield a connected `mcp.Client`. Legacy transport tuples are accepted with a deprecation warning; only their read and write streams are used. Either this or `session` must be provided. |
session默认default None | Any | None | 会话(类型 `Any | None`)。The MCP high-level client for stateful clients. Either this or `client_gen` must be provided. |
timeout默认default None | float | None | 超时 in 秒用于工具执行。The timeout in seconds for tool execution. |
middlewares默认default None | list[ToolMiddlewareBase] | None | 中间件列表(类型 `list[ToolMiddlewareBase] | None`)。Tool middlewares wrapping the tool execution. |
stream_progress默认default False | bool | 流 progress(类型 `bool`)。Return a progress/result stream instead of the final chunk. |
源码Source
agentscope/tool/_adapters.py:210–301属性详情Attribute Detail
is_mcp
bool = True是 MCP字段。声明为 bool。
The is mcp field.
源码Source
agentscope/tool/_adapters.py:205is_state_injected
bool = Falseis_state_injected 字段。声明为 bool。
The is state injected field.
源码Source
agentscope/tool/_adapters.py:207方法详情Method Detail
check_permissions
async def check_permissions(*_args: Any, **_kwargs: Any) -> PermissionDecision
async
检查权限用于 MCP 工具用量。
Default implementation allows all operations.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
_args | Any | 参数(类型 `Any`)。The args. |
_kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
PermissionDecision
返回PermissionDecision。`PermissionDecision`:
源码Source
agentscope/tool/_adapters.py:303–324call
async def call(**kwargs: Any) -> ToolChunk | AsyncGenerator[ToolChunk, None]
async
返回最终结果,或流当stream_progress=True。
Progress is best-effort snapshot delivery, not a lossless event log.
参数Parameters
| 名称Name | 类型Type | 说明Description |
|---|---|---|
kwargs | Any | 关键字参数(类型 `Any`)。The kwargs. |
ToolChunk | AsyncGenerator[ToolChunk, None]
返回ToolChunk|AsyncGenerator[ToolChunk,None]。`ToolChunk | AsyncGenerator[ToolChunk, None]`:
源码Source
agentscope/tool/_adapters.py:326–356