AS AgentScopeAPI 手册 / API Reference
类class agentscope.tool

MCPTool

class MCPTool(ToolBase)
源码Source agentscope/tool/_adapters.py:197–545

MCP 工具,封装可被智能体调用的能力。位于 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_mcpbool
是 MCP字段。
True
is_state_injectedbool
`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_namestrMCP 服务端实例的名称。The name of the MCP server instance.
toolmcp.types.ToolMCP 工具定义。The MCP tool definition.
client_gen默认default NoneCallable[..., 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 NoneAny | None会话(类型 `Any | None`)。The MCP high-level client for stateful clients. Either this or `client_gen` must be provided.
timeout默认default Nonefloat | None超时 in 秒用于工具执行。The timeout in seconds for tool execution.
middlewares默认default Nonelist[ToolMiddlewareBase] | None中间件列表(类型 `list[ToolMiddlewareBase] | None`)。Tool middlewares wrapping the tool execution.
stream_progress默认default Falsebool流 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:205

is_state_injected

bool = False

is_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
_argsAny参数(类型 `Any`)。The args.
_kwargsAny关键字参数(类型 `Any`)。The kwargs.
返回Returns PermissionDecision
返回PermissionDecision。`PermissionDecision`:
源码Source agentscope/tool/_adapters.py:303–324

call

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
kwargsAny关键字参数(类型 `Any`)。The kwargs.
返回Returns ToolChunk | AsyncGenerator[ToolChunk, None]
返回ToolChunk|AsyncGenerator[ToolChunk,None]。`ToolChunk | AsyncGenerator[ToolChunk, None]`:
源码Source agentscope/tool/_adapters.py:326–356

继承成员Inherited Members

基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.