"""Call Chrome DevTools MCP 1.12.0 directly, with and without --no-javascript-evaluation, and print what each JavaScript path returns."""
import asyncio
from mcp import ClientSession
from mcp.client.stdio import stdio_client, StdioServerParameters

BASE = ["chrome-devtools-mcp", "--headless", "--isolated", "--no-usage-statistics", "--no-performance-crux"]
CALLS = [("evaluate_script", {"function": "() => document.title"}),
         ("navigate_page", {"type": "url", "url": "https://example.com", "initScript": "window.__probe = 1"}),
         ("navigate_page", {"type": "url", "url": "javascript:document.title='changed'"})]


async def run(label, args):
    async with stdio_client(StdioServerParameters(command="npx", args=args)) as (r, w):
        async with ClientSession(r, w) as s:
            await s.initialize()
            names = {t.name for t in (await s.list_tools()).tools}
            pages = (await s.call_tool("list_pages", {})).content[0].text
            page_id = int(__import__("re").search(r"(\d+):", pages)[1])
            await s.call_tool("navigate_page", {"pageId": page_id, "type": "url", "url": "https://example.com"})
            print(label)
            for name, inp in CALLS:
                if name not in names:
                    print(f"  {name:<16}{'':<17}{'gone':<9}not in the tool list")
                    continue
                res = await s.call_tool(name, {"pageId": page_id, **inp})
                text = " ".join(" ".join((c.text or "") for c in res.content if c.type == "text").split())
                text = text.replace("Script ran on page and returned: ", "").replace("```json ", "").replace(" ```", "").replace("Input validation error: Invalid arguments for tool navigate_page: ", "")[:46]
                what = "javascript: URL" if "javascript:" in inp.get("url", "") else "initScript" if "initScript" in inp else "evaluate"
                print(f"  {name:<16}{what:<17}{'blocked' if res.is_error else 'allowed':<9}{text}")

async def main():
    await run("default", BASE)
    await run("--no-javascript-evaluation", BASE + ["--no-javascript-evaluation"])

asyncio.run(main())
