From 77edadd4290e0e08a3a81e444feaefe21787d1c7 Mon Sep 17 00:00:00 2001 From: "Tommy D. Rossi" Date: Sun, 16 Nov 2025 15:39:27 +0100 Subject: [PATCH] more globals --- playwriter/src/mcp.ts | 29 +++++++++++++++++++++++++---- playwriter/src/prompt.md | 1 + 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/playwriter/src/mcp.ts b/playwriter/src/mcp.ts index 91b71ae..22d324e 100644 --- a/playwriter/src/mcp.ts +++ b/playwriter/src/mcp.ts @@ -11,6 +11,23 @@ import { getCdpUrl } from './utils.js' const require = createRequire(import.meta.url) +const usefulGlobals = { + setTimeout, + setInterval, + clearTimeout, + clearInterval, + URL, + URLSearchParams, + fetch, + Buffer, + TextEncoder, + TextDecoder, + crypto, + AbortController, + AbortSignal, + structuredClone, +} as const + interface State { isConnected: boolean page: Page | null @@ -35,6 +52,8 @@ interface VMContext { import: (specifier: string) => Promise } +type VMContextWithGlobals = VMContext & typeof usefulGlobals + const state: State = { isConnected: false, page: null, @@ -215,7 +234,7 @@ server.tool( throw new Error('accessibilitySnapshot is not available on this page') } - let vmContextObj: VMContext = { + let vmContextObj: VMContextWithGlobals = { page, context, state, @@ -226,7 +245,7 @@ server.tool( Object.keys(state).forEach(key => delete state[key]) - const resetObj: VMContext = { + const resetObj: VMContextWithGlobals = { page: newPage, context: newContext, state, @@ -234,14 +253,16 @@ server.tool( accessibilitySnapshot, resetPlaywright: vmContextObj.resetPlaywright, require, - import: vmContextObj.import + import: vmContextObj.import, + ...usefulGlobals } Object.keys(vmContextObj).forEach(key => delete (vmContextObj as any)[key]) Object.assign(vmContextObj, resetObj) return { page: newPage, context: newContext } }, require, - import: (specifier: string) => import(specifier) + import: (specifier: string) => import(specifier), + ...usefulGlobals } const vmContext = vm.createContext(vmContextObj) diff --git a/playwriter/src/prompt.md b/playwriter/src/prompt.md index 5e5f1e4..72f88a1 100644 --- a/playwriter/src/prompt.md +++ b/playwriter/src/prompt.md @@ -15,6 +15,7 @@ it will control an existing user Chrome window. The js code will be run in a sa - page, the first page the user opened and made it accessible to this MCP. do things like `page.url()` to see current url. assume the user wants you to use this page for your playwright code - require: node's require function to load CommonJS modules - import: async import function to dynamically import ES modules. for example `const fs = await import('node:fs')` +- all standard Node.js globals: setTimeout, setInterval, clearTimeout, clearInterval, URL, URLSearchParams, fetch, Buffer, TextEncoder, TextDecoder, crypto, AbortController, AbortSignal, structuredClone the chrome window can have more than one page. you can see other pages with `context.pages().find((p) => p.url().includes('localhost'))`. you can also open and close pages: `state.newPage = await context.newPage()`. store the page in state so that you can reuse it later