#!/usr/bin/env python3
"""Inspect a declared MCP tool policy offline; this does NOT validate bearer tokens."""
import json
import sys

def review(policy):
    failures = []
    if not isinstance(policy, dict):
        return ["policy must be an object"]
    if policy.get("audience") != "https://mcp.example.invalid":
        failures.append("unexpected declared audience")
    if policy.get("tokenPassthrough") is not False:
        failures.append("downstream token passthrough must be disabled")
    tools = policy.get("tools")
    if not isinstance(tools, list) or not tools:
        failures.append("explicit tools are required")
        tools = []
    allowed = {"incident.summary", "monitoring.health"}
    for tool in tools:
        if not isinstance(tool, dict):
            failures.append("malformed tool entry")
            continue
        if tool.get("name") not in allowed or tool.get("access") != "read":
            failures.append("tool outside the reviewed read-only scope")
        if tool.get("maxRecords") != 20:
            failures.append("unexpected result bound")
    return failures

def main():
    policy = json.load(open(sys.argv[1], encoding="utf-8"))
    failures = review(policy)
    print(json.dumps({"declaredPolicyAccepted": not failures,
      "runtimeAuthorizationVerified": False, "failures": failures}, indent=2))
    return 1 if failures else 0

if __name__ == "__main__":
    try:
        sys.exit(main())
    except (ValueError, IndexError, OSError) as error:
        print(str(error), file=sys.stderr)
        sys.exit(2)
