Scans the tools.implementations package to find all tool classes that:
- Inherit from BaseTool (excluding BaseTool itself)
- Have a class attribute
name
that uniquely identifies them.
Returns:
Source code in src/tools/core/utils/tool_discovery.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 | def discover_custom_tools() -> Dict[str, Type[BaseTool]]:
"""
Scans the tools.implementations package to find all tool classes that:
- Inherit from BaseTool (excluding BaseTool itself)
- Have a class attribute `name` that uniquely identifies them.
Returns:
A dictionary mapping tool_name (str) to the tool class.
"""
discovered = {}
try:
import src.tools.implementations # Ensure the package is imported
logger.debug("Scanning tools.implementations for custom tools.")
except Exception as e:
logger.error(f"Failed to import tools.implementations package: {e}")
return discovered
for finder, module_name, ispkg in pkgutil.iter_modules(src.tools.implementations.__path__):
try:
module = importlib.import_module(f"src.tools.implementations.{module_name}")
logger.debug(f"Imported module: src.tools.implementations.{module_name}")
except Exception as e:
logger.error(f"Failed to import module {module_name}: {e}")
continue
for attr_name in dir(module):
attr = getattr(module, attr_name)
if (
inspect.isclass(attr)
and issubclass(attr, BaseTool)
and attr is not BaseTool
and hasattr(attr, "name")
):
tool_key = getattr(attr, "name")
discovered[tool_key] = attr
logger.debug(f"Discovered tool: '{tool_key}' -> {attr.__name__}")
# Build a single log message for the summary
if discovered:
discovered_count = len(discovered)
tool_names = sorted(discovered.keys())
log_message = "\n" + "-" * 50
log_message += f"\nTOOL DISCOVERY: Found {discovered_count} custom tool(s)"
log_message += "\n" + "-" * 50
# List discovered tools with their class names
for i, tool_name in enumerate(tool_names, 1):
tool_class = discovered[tool_name].__name__
log_message += f"\n {i}. {tool_name:<20} -> {tool_class}"
log_message += f"\n{'-' * 50}"
else:
log_message = "\n" + "-" * 50
log_message += "\nTOOL DISCOVERY: No custom tools found"
log_message += f"\n{'-' * 50}"
logger.debug(log_message)
return discovered
|