// Drives the official ext-apps basic-host (already running on :8080, see scripts/basic-host.sh) and writes output/08. import { chromium } from "playwright"; import { readFileSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import assert from "node:assert/strict"; const here = dirname(fileURLToPath(import.meta.url)); const outDir = join(here, "../output"); const browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || undefined }); const out = ["# The official ext-apps basic-host against the Spring AI server", ""]; const consoleLines = []; const wire = []; async function open(toolName) { const page = await browser.newPage({ viewport: { width: 900, height: 900 } }); page.on("console", (m) => consoleLines.push(m.text())); page.on("request", (r) => { if (r.url().endsWith("/mcp") && r.postData()) wire.push(r.postData()); }); await page.route("https://unpkg.com/**", (route) => route.fulfill({ status: 200, contentType: "text/javascript", headers: { "Access-Control-Allow-Origin": "*" }, body: readFileSync(join(here, "node_modules/@modelcontextprotocol/ext-apps/dist/src/app-with-deps.js")) })); await page.goto("http://localhost:8080/"); await page.locator("option[value=show_inventory]").waitFor({ state: "attached" }); const tools = await page.locator("select").nth(1).locator("option").allTextContents(); await page.locator("select").nth(1).selectOption(toolName); await page.getByRole("button", { name: "Call Tool" }).click(); await page.waitForTimeout(5000); let view; for (const f of page.frames()) if (await f.locator("#rows").count().catch(() => 0)) view = f; assert.ok(view, "view frame not found"); return { page, view, tools }; } const first = await open("show_inventory"); out.push("tools offered in basic-host's tool list: " + JSON.stringify(first.tools)); await first.view.locator("tr[data-sku=KB-01] button").click(); await first.view.getByText("Restocked KB-01").waitFor(); out.push("show_inventory, after clicking +10 on KB-01: " + await first.view.locator("#status").innerText()); await first.view.evaluate((code) => { const s = document.createElement("script"); s.textContent = code; document.body.append(s); }, readFileSync(join(here, "probes.js"), "utf8")); await first.view.waitForFunction(() => window.__probes); out.push("", "probes in a view that declares no CSP domains:", ...await first.view.evaluate(() => window.__probes)); await first.page.waitForTimeout(1000); await first.page.screenshot({ path: join(outDir, "basic-host.png"), fullPage: true }); const body = await first.page.locator("body").innerText(); out.push("", "basic-host Model Context panel shows: " + body.match(/The user restocked[^\n]*/)?.[0]); await first.page.close(); const second = await open("show_inventory_sdk"); out.push("", "show_inventory_sdk renders: " + (await second.view.locator("#rows li").allInnerTexts()).join(" | ")); await second.page.close(); const requests = wire.map((w) => JSON.parse(w)); const init = requests.find((r) => r.method === "initialize"); out.push("", "initialize sent by basic-host: " + JSON.stringify(init.params), "methods basic-host sent while loading and running the first tool: " + [...new Set(requests.map((r) => r.method))].join(", ")); writeFileSync(join(outDir, "08-basic-host.txt"), out.join("\n") + "\n"); console.log(out.join("\n")); await browser.close();