Skip to content

Tools API Reference

This module provides the @tool decorator used to mark Python functions as tools for LLM interaction.

Tool Decorator

maticlib.tools.tool.tool

tool(func)

A decorator to mark a function as a tool and automatically generate its JSON schema.

This decorator inspects the function signature and docstring to create a metadata dictionary used by LLM providers for function calling.

Source code in maticlib/tools/tool.py
def tool(func):
    """
    A decorator to mark a function as a tool and automatically generate its JSON schema.

    This decorator inspects the function signature and docstring to create a 
    metadata dictionary used by LLM providers for function calling.
    """
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    # Extract metadata
    name = func.__name__
    description = inspect.getdoc(func) or f"Function {name}"

    # Generate signature/parameters schema
    signature = inspect.signature(func)
    type_hints = get_type_hints(func)

    properties = {}
    required = []

    for param_name, param in signature.parameters.items():
        if param_name == "self" or param_name == "cls":
            continue

        param_type = type_hints.get(param_name, Any)
        json_type = _get_json_type(param_type)

        properties[param_name] = {
            "type": json_type,
            "description": f"The {param_name} parameter." # Generic description if not available
        }

        if param.default is inspect.Parameter.empty:
            required.append(param_name)

    # Attach tool metadata to the wrapper function
    wrapper.matic_tool_metadata = {
        "name": name,
        "description": description,
        "parameters": {
            "type": "object",
            "properties": properties,
            "required": required
        }
    }

    return wrapper