Skip to main content
AgentScope provides a unified 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 ToolResponse object, or a generator that yields ToolResponse objects
  • Has a docstring describing its functionality and parameters
Instance methods and class methods can also be used as tool functions. The self and cls parameters are automatically ignored during schema extraction.
AgentScope provides several built-in tool functions under 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

The Toolkit class manages tool functions, extracts their JSON Schema from docstrings, and provides a unified interface for execution.

Basic Usage

After registration, retrieve the JSON Schema with get_json_schemas():
To preset arguments (e.g., API keys) so they are hidden from the model:
The 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.
The 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.
Non-streaming interruption — the toolkit yields a predefined interrupted response:
Streaming interruption — the interrupted message is attached to the last yielded chunk:
The agent can check 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.
Tools registered without a group name are placed in the basic group, which is always active. This ensures backward compatibility if you don’t need group features.
Only tools in active groups are visible to the model via get_json_schemas(). Activate or deactivate groups with:
Meta tool: reset_equipped_tools Toolkit provides a built-in meta tool that lets the agent itself decide which tool groups to activate:
When the agent calls 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.
You can also retrieve the notes of all currently active groups to inject into the system prompt:

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 calling next_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() and close() 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.
Both client types expose list_tools() and get_callable_function().

Server-Level Management

Register all tools from an MCP server into a Toolkit at once:
To remove tools:

Function-Level Management

For fine-grained control, retrieve a specific MCP tool as a callable Python object:
This lets you wrap MCP tools in your own functions, add post-processing, or compose them with other tools.

Agent Skills

Agent Skills is an approach proposed by Anthropic to improve agent capabilities on specific tasks. AgentScope provides built-in support through the Toolkit class.

SKILL.md Format

Each skill lives in its own directory and must contain a SKILL.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:
When using agent skills, the agent must be equipped with file reading or shell command tools so it can access the SKILL.md instructions at runtime.