#!/usr/bin/env python3
import json
from pathlib import Path


tools = {
    "read_policy": {"risk": "read", "requires_approval": False},
    "refund_order": {"risk": "write", "requires_approval": True},
}


def authorize(tool_name, approved=False):
    tool = tools.get(tool_name)
    if tool is None:
        return {"allowed": False, "reason": "unknown_tool"}
    if tool["requires_approval"] and not approved:
        return {"allowed": False, "reason": "approval_required"}
    return {"allowed": True, "reason": "policy_passed"}


result = {
    "mcp_discovery": list(tools),
    "read_call": authorize("read_policy"),
    "write_without_approval": authorize("refund_order"),
    "write_with_approval": authorize("refund_order", approved=True),
}
Path(__file__).with_name("experiment-result.json").write_text(
    json.dumps(result, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
print(json.dumps(result, ensure_ascii=False, indent=2))
