"""Call a Shopify store's WebMCP get_product tool from a plain Playwright script, with no agent and no flags."""
import asyncio, json, sys
from playwright.async_api import async_playwright

URL = "https://www.allbirds.com/products/womens-allbirds-flip-flop-dusty-pink"
JS = """async () => {
  if (!('modelContext' in document)) return null;
  const tool = (await document.modelContext.getTools()).find(t => t.name === 'get_product');
  if (!tool) return null;
  try {
    return await document.modelContext.executeTool(tool, {});      // Chrome 156 takes an object
  } catch (e) {
    return await document.modelContext.executeTool(tool, '{}');    // Chromium 151 takes a JSON string
  }
}"""

async def main(channel):
    async with async_playwright() as p:
        browser = await p.chromium.launch(channel=channel) if channel else await p.chromium.launch()
        page = await browser.new_page()
        await page.goto(URL, wait_until="domcontentloaded")
        await page.wait_for_timeout(6000)
        raw = await page.evaluate(JS)
        print(f"{channel or 'bundled chromium'}: {browser.version}")
        if raw is None:
            print("  no WebMCP tools")
        else:
            data = json.loads(raw) if isinstance(raw, str) else raw
            p_ = data["structuredContent"]
            print(f"  {p_['title']} | {p_['price_range']['min']['display']} | sizes {p_['options'][0]['values']}")
            print(f"  in stock: {[o[0]['value'] for o in p_['available_options']]}")
        await browser.close()

asyncio.run(main(sys.argv[1] if len(sys.argv) > 1 else None))
