From 9219912d35c21da8a735aacaf4195756ef8c4050 Mon Sep 17 00:00:00 2001 From: "Tommy D. Rossi" Date: Tue, 25 Nov 2025 09:57:38 +0100 Subject: [PATCH] add reset as tool --- playwriter/package.json | 1 + playwriter/src/mcp.ts | 34 +++++++++++++++++++ playwriter/src/prompt.md | 1 - .../src/snapshots/shadcn-ui-accessibility.md | 4 +-- pnpm-lock.yaml | 9 +++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/playwriter/package.json b/playwriter/package.json index e9611b2..6ce2c01 100644 --- a/playwriter/package.json +++ b/playwriter/package.json @@ -42,6 +42,7 @@ "devtools-protocol": "^0.0.1543509", "hono": "^4.10.6", "playwright-core": "^1.56.1", + "string-dedent": "^3.0.2", "user-agents": "^1.1.669", "ws": "^8.18.3", "zod": "^3" diff --git a/playwriter/src/mcp.ts b/playwriter/src/mcp.ts index 2a2fea6..2c2fe20 100644 --- a/playwriter/src/mcp.ts +++ b/playwriter/src/mcp.ts @@ -620,6 +620,40 @@ server.tool( }, ) +server.tool( + 'reset', + `Recreates the CDP connection and resets the browser/page/context. Use this when the MCP stops responding, you get connection errors, assertion failures, page closed, or timeout issues. + +After calling this tool, the page and context variables are automatically updated in the execution environment. + +IMPORTANT: this completely resets the execution context, removing any custom properties you may have added to the global scope AND clearing all keys from the \`state\` object. Only \`page\`, \`context\`, \`state\` (empty), \`console\`, and utility functions will remain.`, + {}, + async () => { + try { + const { page, context } = await resetConnection() + const pagesCount = context.pages().length + return { + content: [ + { + type: 'text', + text: `Connection reset successfully. ${pagesCount} page(s) available. Current page URL: ${page.url()}`, + }, + ], + } + } catch (error: any) { + return { + content: [ + { + type: 'text', + text: `Failed to reset connection: ${error.message}`, + }, + ], + isError: true, + } + } + }, +) + // Start the server async function main() { await ensureRelayServer() diff --git a/playwriter/src/prompt.md b/playwriter/src/prompt.md index 449599c..feb1d63 100644 --- a/playwriter/src/prompt.md +++ b/playwriter/src/prompt.md @@ -76,7 +76,6 @@ you have access to some functions in addition to playwright methods: - `page`: the page object to snapshot - `searchString`: (optional) a string or regex to filter the snapshot. If provided, returns the first 10 matches with surrounding context - `contextLines`: (optional) number of lines of context to show around each match (default: 10) -- `async resetPlaywright()`: recreates the CDP connection and resets the browser/page/context. Use this when the MCP stops responding, you get connection errors, assertion failures, page closed, or timeout issues. After calling this, the page and context variables are automatically updated in the execution environment. IMPORTANT: this completely resets the execution context, removing any custom properties you may have added to the global scope AND clearing all keys from the `state` object. Only `page`, `context`, `state` (empty), `console`, and utility functions will remain - `getLatestLogs({ page, count, searchFilter })`: retrieves browser console logs. The system automatically captures and stores up to 5000 logs per page. Logs are cleared when a page reloads or navigates. - `page`: (optional) filter logs by a specific page instance. Only returns logs from that page - `count`: (optional) limit number of logs to return. If not specified, returns all available logs diff --git a/playwriter/src/snapshots/shadcn-ui-accessibility.md b/playwriter/src/snapshots/shadcn-ui-accessibility.md index d961ae7..888b84e 100644 --- a/playwriter/src/snapshots/shadcn-ui-accessibility.md +++ b/playwriter/src/snapshots/shadcn-ui-accessibility.md @@ -28,10 +28,10 @@ Return value: - generic [ref=e21]: - generic: ⌘ - generic: K - - link "100.6k" [ref=e22] [cursor=pointer]: + - link "100.7k" [ref=e22] [cursor=pointer]: - /url: https://github.com/shadcn-ui/ui - img - - generic [ref=e23]: 100.6k + - generic [ref=e23]: 100.7k - button "Toggle theme" [ref=e24]: - img - generic [ref=e25]: Toggle theme diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b9421a..2f13591 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -81,6 +81,9 @@ importers: playwright-core: specifier: ^1.56.1 version: 1.56.1 + string-dedent: + specifier: ^3.0.2 + version: 3.0.2 user-agents: specifier: ^1.1.669 version: 1.1.669 @@ -1575,6 +1578,10 @@ packages: std-env@3.10.0: resolution: {integrity: sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg==} + string-dedent@3.0.2: + resolution: {integrity: sha512-M4q+HpHCtGXlbyzYDOcOo7V185dlq6YXvGUPcWZqL4vttCX9gFYoWIOxcPd7v5CAYcTJsGLs3ZJCAH2TXONF/g==} + engines: {node: '>=0.12.0'} + string-width@4.2.3: resolution: {integrity: sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==} engines: {node: '>=8'} @@ -3343,6 +3350,8 @@ snapshots: std-env@3.10.0: {} + string-dedent@3.0.2: {} + string-width@4.2.3: dependencies: emoji-regex: 8.0.0