Tool¶
ToolManager
¶
Centralized tool management class for registering, loading, saving, and executing tools. Tools are stored in a JSON file and can be of type 'function', 'mcp', or 'module'.
Methods:
Name | Description |
---|---|
register_mcp_tool |
Register tools from an MCP (Memory Compute Platform) server. |
register_module_tool |
Register tools from a Python module. |
load_tools |
Load existing tools from the JSON file. |
save_tools |
Save tools metadata to the JSON file. |
register_mcp_tool
async
¶
Register tools from an MCP (Memory Compute Platform) server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
client
|
DistributedMCPClient
|
Client for interacting with the MCP server. |
required |
server_name
|
str
|
Name of the MCP server. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list[Dict[str, Any]]
|
list[Dict[str, Any]]: List of registered MCP tool metadata. |
Behavior
- Fetches tools from the MCP server using the client.
- Converts MCP tools to the internal tool format.
- Assigns unique tool_call_id for each tool.
- Saves tools to the JSON file.
register_module_tool
¶
register_module_tool(module_path: str) -> None
Register tools from a Python module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_path
|
str
|
Path to the module or import path in module import format. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the module cannot be loaded or tool format is invalid. |
Behavior
- Copies the module file to the tools directory if a file path is provided.
- Imports the module and extracts tool metadata using the language model.
- Assigns a unique tool_call_id for each tool.
- Saves tools to the JSON file.
load_tools
¶
FunctionTool
¶
Utility class for executing function-type tools.
Methods:
Name | Description |
---|---|
execute |
Execute a registered function tool. |
execute
async
classmethod
¶
execute(tool_manager: ToolManager, tool_name: str, arguments: Dict[str, Any])
Execute a registered function tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_manager
|
ToolManager
|
The ToolManager instance containing registered tools. |
required |
tool_name
|
str
|
Name of the function tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the function. |
required |
Returns:
Name | Type | Description |
---|---|---|
ToolMessage |
A message containing the execution result or error details. |
Raises:
Type | Description |
---|---|
Exception
|
If the function execution fails, logs the error and returns a message. |
ModuleTool
¶
Utility class for executing module-type tools.
Methods:
Name | Description |
---|---|
execute |
Execute a module-based tool by importing and calling the specified function. |
execute
async
classmethod
¶
execute(tool_manager: ToolManager, tool_name: str, arguments: Dict[str, Any], module_path: Union[str, Path], *arg, **kwargs)
Execute a module-based tool by importing and calling the specified function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_manager
|
ToolManager
|
The ToolManager instance containing registered tools. |
required |
tool_name
|
str
|
Name of the module tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
module_path
|
Union[str, Path]
|
Path to the module containing the tool. |
required |
Returns:
Name | Type | Description |
---|---|---|
ToolMessage |
A message containing the execution result or error details. |
Raises:
Type | Description |
---|---|
(ImportError, AttributeError)
|
If the module or function cannot be loaded, logs the error and returns a message. |
MCPTool
¶
Utility class for executing MCP-type tools.
Methods:
Name | Description |
---|---|
execute |
Execute an MCP tool using the provided client and server. |
execute
async
classmethod
¶
execute(tool_manager: ToolManager, tool_name: str, arguments: Dict[str, Any], mcp_client: DistributedMCPClient, mcp_server_name: str)
Execute an MCP tool using the provided client and server.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_manager
|
ToolManager
|
The ToolManager instance containing registered tools. |
required |
tool_name
|
str
|
Name of the MCP tool to execute. |
required |
arguments
|
Dict[str, Any]
|
Arguments to pass to the tool. |
required |
mcp_client
|
DistributedMCPClient
|
Client for interacting with the MCP server. |
required |
mcp_server_name
|
str
|
Name of the MCP server. |
required |
Returns:
Name | Type | Description |
---|---|---|
ToolMessage |
A message containing the execution result or error details. |
Raises:
Type | Description |
---|---|
Exception
|
If the tool execution fails, logs the error and returns a message. |