Toolkit class to manage all tool-related capabilities, including:
- Registering and executing Python tool functions (sync, async, and streaming)
- Extending tool schemas dynamically and interrupting tool execution
- Automatic tool management via tool groups
- Middleware for pre/post-processing tool calls
- MCP (Model Context Protocol) integration
- Agent Skills for task-specific knowledge injection
Tool Functions
A tool function is a Python function that:- Returns a
ToolResponseobject, or a generator that yieldsToolResponseobjects - Has a docstring describing its functionality and parameters
agentscope.tool, including execute_python_code, execute_shell_command, and text file read/write utilities.
Tool functions can be synchronous, asynchronous, or streaming (async generators):
Toolkit
TheToolkit class manages tool functions, extracts their JSON Schema from docstrings, and provides a unified interface for execution.
Basic Usage
get_json_schemas():
api_key field will be excluded from the JSON Schema exposed to the model.
To execute a tool call, use call_tool_function, which accepts a ToolUseBlock and returns an async generator:
Extending JSON Schema Dynamically
Toolkit allows you to extend the JSON Schema of a tool function at runtime using a Pydantic model. This is useful for adding fields like Chain-of-Thought reasoning without modifying the original function.
The function to be extended must accept variable keyword arguments (
**kwargs) so that the additional fields can be passed through.thinking field will now appear in the tool’s JSON Schema, prompting the model to reason before acting.
Interrupting Tool Execution
Toolkit supports execution interruption for async tool functions via asyncio cancellation. When interrupted, a ToolResponse with is_interrupted=True is yielded so the agent can handle it gracefully.
Synchronous tool functions cannot be interrupted via asyncio cancellation. Interruption for sync tools is handled at the agent level.
tool_response.is_interrupted to decide whether to propagate the CancelledError.
Automatic Tool Management
For agents that need to work with large or dynamic tool sets,Toolkit supports tool groups — named collections of related tools that can be activated or deactivated at runtime.
get_json_schemas(). Activate or deactivate groups with:
reset_equipped_tools
Toolkit provides a built-in meta tool that lets the agent itself decide which tool groups to activate:
reset_equipped_tools, the specified groups are activated and the toolkit returns their usage notes as a tool response — giving the agent the context it needs to use the new tools correctly.
In
ReActAgent, you can enable this meta tool by setting enable_meta_tool=True in the constructor.Middleware
Toolkit supports a middleware system for intercepting and modifying tool execution. Middleware follows an onion model: pre-processing runs in registration order, post-processing runs in reverse.
Middleware Signature
Logging Middleware
Input/Output Transformation
Middleware can modify both the tool input and the response:Authorization Middleware
Middleware can skip tool execution entirely by not callingnext_handler:
Multiple Middleware (Onion Model)
When multiple middleware are registered, execution follows this order:The same
ToolResponse object is passed through the chain and modified in place. Middleware are applied in registration order for pre-processing, and in reverse for post-processing.Common Use Cases
Middleware is well-suited for:- Logging & Monitoring — track tool usage and latency
- Authorization — gate access to specific tools
- Rate Limiting — throttle tool call frequency
- Caching — return cached responses for repeated calls
- Error Handling — add retry logic or graceful degradation
- Input Validation — sanitize tool inputs before execution
- Output Transformation — reformat or filter tool outputs
- Metrics Collection — gather statistics on tool usage
MCP Integration
AgentScope supports the Model Context Protocol (MCP), allowing agents to use tools hosted on external MCP servers.Client Types
AgentScope provides two client types across two transport protocols:- Stateful: Maintains a persistent session. You must call
connect()andclose()explicitly. - Stateless: Creates a new session per tool call — more lightweight, no lifecycle management needed.
- The StdIO stateful client starts the MCP server locally when
connect()is called. - When multiple stateful clients are connected, close them in LIFO (Last In First Out) order to avoid errors.
list_tools() and get_callable_function().
Server-Level Management
Register all tools from an MCP server into aToolkit at once:
Function-Level Management
For fine-grained control, retrieve a specific MCP tool as a callable Python object:Agent Skills
Agent Skills is an approach proposed by Anthropic to improve agent capabilities on specific tasks. AgentScope provides built-in support through theToolkit class.
SKILL.md Format
Each skill lives in its own directory and must contain aSKILL.md file with YAML frontmatter:
Registering Skills
Customizing the Prompt Template
You can customize how skills are presented to the model:Integration with ReActAgent
ReActAgent automatically appends the agent skill prompt to the system prompt when a toolkit with registered skills is provided: