liveBook · Manning

Tool decorator

As explained in Digital Twins in Action

A decorator that marks a Python function as a callable tool for an AI agent.

The Strands SDK’s `@tool` decorator turns a Python function into a tool an AI agent can call. The function supplies the action; its documentation tells the agent when to use it and what inputs and results to expect. section 8.4.3

Listing 8.10 Tool to query Fronius Symo inverter power data
from strands import tool

@tool  #1
def get_realtime_solar_data(scope: str = "System") -> Dict[str, Any]:
    """  #2
    Fetches real-time solar production metrics from the local Fronius
    inverter.

    Use this tool when you need to know the current solar output (PAC) or
    the total energy produced today (DAY_ENERGY).

    Args:
        scope: The data scope to query. Defaults to "System".

    Returns:
        Dict[str, Any]: A dictionary containing:
            - current_power_w: Current AC power output in Watts.
            - day_energy_wh: Total energy produced today in Watt-hours.
            - timestamp: The time of the reading.
    """
    url = "http://192.168.1.180/solar_api/v1/GetInverterRealtimeData.cgi"  #3

    try:
        response = requests.get(url, params={"Scope": scope}, timeout=5)
        response.raise_for_status()
        data = response.json()

        realtime_data = data.get("Body", {}).get("Data", {})

        pac_value = realtime_data.get("PAC", {}).get("Values", {}).
        get("1", 0)
        day_energy = realtime_data.get("DAY_ENERGY", {}).get("Values",
        {}).get("1", 0)

        return {
            "current_power_w": pac_value,
            "day_energy_wh": day_energy,
            "timestamp": data.get("Head", {}).get("Timestamp"),
            "status": "success",
        }

    except requests.RequestException as e:
        return {
            "status": "error",
            "message": f"Failed to connect to inverter: {str(e)}",
        }

The decorated `get_realtime_solar_data` function queries a local inverter. Its docstring identifies the optional `scope` input and describes the returned power, daily energy, and timestamp fields. section 8.4.3

This tool gives a home-climate agent current solar generation. Separate tools provide weather forecasts and indoor conditions; the agent loop manages reasoning, tool execution, and context across steps. section 8.4.3

How Digital Twins in Action covers it

How deep it goes
Shows a decorated function and its input-and-return documentation, but does not explain decorator internals.
Examples
Uses a local inverter query alongside weather-forecast and home-digital-twin API tools.
What it assumes
Assumes familiarity with Python functions and API calls while introducing the role of agent tools.
What you can do after
Apply it via the Strands SDK to make a documented Python function callable by an agent.
How it connects
Connects Python functions to agent tools, API calls, docstrings, and the agent loop.

Also explained in

Compare how 2 books explain tool decorator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.