import ast, operator
from decimal import Decimal, ROUND_HALF_UP

class CalculatorTool:
    name = "calculate"

    async def execute(self, expression: str) -> dict:
        allowed_ops = {
            ast.Add: operator.add, ast.Sub: operator.sub,
            ast.Mult: operator.mul, ast.Div: operator.truediv,
        }
        def _eval(node):
            if isinstance(node, ast.Num): return Decimal(str(node.n))
            elif isinstance(node, ast.BinOp):
                return allowed_ops[type(node.op)](_eval(node.left), _eval(node.right))
            raise ValueError(f"Unsupported: {ast.dump(node)}")
        tree = ast.parse(expression, mode='eval')
        result = float(_eval(tree.body).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP))
        return {"expression": expression, "result": result}
