praval.tools
Tool decorator and utilities for Praval Framework.
This module provides the @tool decorator for creating tools that can be registered and used by agents. Tools are automatically registered in the global tool registry and can be associated with specific agents.
Functions
|
Discover tools based on various criteria. |
|
Get information about a @tool decorated function. |
|
Check if a function is decorated with @tool. |
|
List tools with optional filtering. |
|
Register an existing tool with an agent at runtime. |
|
Decorator to register a function as a tool in the Praval framework. |
|
Unregister a tool from an agent at runtime. |
Classes
|
A collection of related tools that can be managed as a group. |
- praval.tools.tool(tool_name=None, owned_by=None, description=None, category='general', shared=False, version='1.0.0', author='', tags=None)[source]
Decorator to register a function as a tool in the Praval framework.
The @tool decorator automatically registers functions as tools that can be used by agents. Tools can be owned by specific agents, shared across all agents, or organized by category.
- Parameters:
tool_name (
Optional[str]) – Name of the tool (defaults to function name)description (
Optional[str]) – Description of what the tool does (defaults to docstring)category (
str) – Category for organizing toolsshared (
bool) – Whether this tool is available to all agentsversion (
str) – Version of the toolauthor (
str) – Author of the tool
- Return type:
- Returns:
Decorated function with tool metadata attached
- Raises:
ToolError – If tool registration fails or validation errors occur
Examples
Basic tool owned by specific agent: ```python @tool(“add_numbers”, owned_by=”calculator”) def add(x: float, y: float) -> float:
‘’’Add two numbers together.’’’ return x + y
Shared tool available to all agents: ```python @tool(“logger”, shared=True, category=”utility”) def log_message(level: str, message: str) -> str:
‘’’Log a message at the specified level.’’’ import logging logger = logging.getLogger(“praval.tools”) getattr(logger, level.lower())(message) return f”Logged: {message}”
Tool with metadata: ```python @tool(
“validate_email”, owned_by=”data_processor”, category=”validation”, tags=[“email”, “validation”, “data”], version=”2.0.0”, author=”Praval Team”
) def validate_email(email: str) -> bool:
‘’’Validate email address format.’’’ import re pattern = r’^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$’ return bool(re.match(pattern, email))
- praval.tools.get_tool_info(tool_func)[source]
Get information about a @tool decorated function.
- Parameters:
tool_func (
Callable) – Function decorated with @tool- Return type:
- Returns:
Dictionary with tool metadata
- Raises:
ValueError – If function is not decorated with @tool
- praval.tools.discover_tools(module=None, pattern=None, category=None)[source]
Discover tools based on various criteria.
- praval.tools.list_tools(agent_name=None, category=None, shared_only=False)[source]
List tools with optional filtering.
- praval.tools.register_tool_with_agent(tool_name, agent_name)[source]
Register an existing tool with an agent at runtime.
- praval.tools.unregister_tool_from_agent(tool_name, agent_name)[source]
Unregister a tool from an agent at runtime.
- class praval.tools.ToolCollection(name, description='')[source]
Bases:
objectA collection of related tools that can be managed as a group.
Useful for organizing tools by functionality or creating tool suites that can be easily assigned to agents.