@playwright/mcp 0.0.36 → 0.0.37-alpha-2025-09-09

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +82 -47
  2. package/cli.js +7 -1
  3. package/config.d.ts +24 -0
  4. package/index.d.ts +1 -1
  5. package/index.js +2 -2
  6. package/package.json +14 -40
  7. package/lib/browserContextFactory.js +0 -211
  8. package/lib/browserServerBackend.js +0 -77
  9. package/lib/config.js +0 -246
  10. package/lib/context.js +0 -226
  11. package/lib/extension/cdpRelay.js +0 -358
  12. package/lib/extension/extensionContextFactory.js +0 -56
  13. package/lib/extension/protocol.js +0 -18
  14. package/lib/index.js +0 -40
  15. package/lib/loop/loop.js +0 -69
  16. package/lib/loop/loopClaude.js +0 -152
  17. package/lib/loop/loopOpenAI.js +0 -141
  18. package/lib/loop/main.js +0 -60
  19. package/lib/loopTools/context.js +0 -67
  20. package/lib/loopTools/main.js +0 -54
  21. package/lib/loopTools/perform.js +0 -32
  22. package/lib/loopTools/snapshot.js +0 -29
  23. package/lib/loopTools/tool.js +0 -18
  24. package/lib/mcp/http.js +0 -135
  25. package/lib/mcp/inProcessTransport.js +0 -72
  26. package/lib/mcp/manualPromise.js +0 -111
  27. package/lib/mcp/mdb.js +0 -198
  28. package/lib/mcp/proxyBackend.js +0 -104
  29. package/lib/mcp/server.js +0 -123
  30. package/lib/mcp/tool.js +0 -32
  31. package/lib/program.js +0 -132
  32. package/lib/response.js +0 -165
  33. package/lib/sessionLog.js +0 -121
  34. package/lib/tab.js +0 -249
  35. package/lib/tools/common.js +0 -55
  36. package/lib/tools/console.js +0 -33
  37. package/lib/tools/dialogs.js +0 -47
  38. package/lib/tools/evaluate.js +0 -53
  39. package/lib/tools/files.js +0 -44
  40. package/lib/tools/form.js +0 -57
  41. package/lib/tools/install.js +0 -53
  42. package/lib/tools/keyboard.js +0 -78
  43. package/lib/tools/mouse.js +0 -99
  44. package/lib/tools/navigate.js +0 -54
  45. package/lib/tools/network.js +0 -41
  46. package/lib/tools/pdf.js +0 -40
  47. package/lib/tools/screenshot.js +0 -79
  48. package/lib/tools/snapshot.js +0 -139
  49. package/lib/tools/tabs.js +0 -59
  50. package/lib/tools/tool.js +0 -33
  51. package/lib/tools/utils.js +0 -74
  52. package/lib/tools/verify.js +0 -137
  53. package/lib/tools/wait.js +0 -55
  54. package/lib/tools.js +0 -54
  55. package/lib/utils/codegen.js +0 -49
  56. package/lib/utils/fileUtils.js +0 -36
  57. package/lib/utils/guid.js +0 -22
  58. package/lib/utils/log.js +0 -21
  59. package/lib/utils/package.js +0 -20
  60. package/lib/vscode/host.js +0 -128
  61. package/lib/vscode/main.js +0 -62
@@ -1,152 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- const model = 'claude-sonnet-4-20250514';
17
- export class ClaudeDelegate {
18
- _anthropic;
19
- async anthropic() {
20
- if (!this._anthropic) {
21
- const anthropic = await import('@anthropic-ai/sdk');
22
- this._anthropic = new anthropic.Anthropic();
23
- }
24
- return this._anthropic;
25
- }
26
- createConversation(task, tools, oneShot) {
27
- const llmTools = tools.map(tool => ({
28
- name: tool.name,
29
- description: tool.description || '',
30
- inputSchema: tool.inputSchema,
31
- }));
32
- if (!oneShot) {
33
- llmTools.push({
34
- name: 'done',
35
- description: 'Call this tool when the task is complete.',
36
- inputSchema: {
37
- type: 'object',
38
- properties: {},
39
- },
40
- });
41
- }
42
- return {
43
- messages: [{
44
- role: 'user',
45
- content: task
46
- }],
47
- tools: llmTools,
48
- };
49
- }
50
- async makeApiCall(conversation) {
51
- // Convert generic messages to Claude format
52
- const claudeMessages = [];
53
- for (const message of conversation.messages) {
54
- if (message.role === 'user') {
55
- claudeMessages.push({
56
- role: 'user',
57
- content: message.content
58
- });
59
- }
60
- else if (message.role === 'assistant') {
61
- const content = [];
62
- // Add text content
63
- if (message.content) {
64
- content.push({
65
- type: 'text',
66
- text: message.content,
67
- citations: []
68
- });
69
- }
70
- // Add tool calls
71
- if (message.toolCalls) {
72
- for (const toolCall of message.toolCalls) {
73
- content.push({
74
- type: 'tool_use',
75
- id: toolCall.id,
76
- name: toolCall.name,
77
- input: toolCall.arguments
78
- });
79
- }
80
- }
81
- claudeMessages.push({
82
- role: 'assistant',
83
- content
84
- });
85
- }
86
- else if (message.role === 'tool') {
87
- // Tool results are added differently - we need to find if there's already a user message with tool results
88
- const lastMessage = claudeMessages[claudeMessages.length - 1];
89
- const toolResult = {
90
- type: 'tool_result',
91
- tool_use_id: message.toolCallId,
92
- content: message.content,
93
- is_error: message.isError,
94
- };
95
- if (lastMessage && lastMessage.role === 'user' && Array.isArray(lastMessage.content)) {
96
- // Add to existing tool results message
97
- lastMessage.content.push(toolResult);
98
- }
99
- else {
100
- // Create new tool results message
101
- claudeMessages.push({
102
- role: 'user',
103
- content: [toolResult]
104
- });
105
- }
106
- }
107
- }
108
- // Convert generic tools to Claude format
109
- const claudeTools = conversation.tools.map(tool => ({
110
- name: tool.name,
111
- description: tool.description,
112
- input_schema: tool.inputSchema,
113
- }));
114
- const anthropic = await this.anthropic();
115
- const response = await anthropic.messages.create({
116
- model,
117
- max_tokens: 10000,
118
- messages: claudeMessages,
119
- tools: claudeTools,
120
- });
121
- // Extract tool calls and add assistant message to generic conversation
122
- const toolCalls = response.content.filter(block => block.type === 'tool_use');
123
- const textContent = response.content.filter(block => block.type === 'text').map(block => block.text).join('');
124
- const llmToolCalls = toolCalls.map(toolCall => ({
125
- name: toolCall.name,
126
- arguments: toolCall.input,
127
- id: toolCall.id,
128
- }));
129
- // Add assistant message to generic conversation
130
- conversation.messages.push({
131
- role: 'assistant',
132
- content: textContent,
133
- toolCalls: llmToolCalls.length > 0 ? llmToolCalls : undefined
134
- });
135
- return llmToolCalls;
136
- }
137
- addToolResults(conversation, results) {
138
- for (const result of results) {
139
- conversation.messages.push({
140
- role: 'tool',
141
- toolCallId: result.toolCallId,
142
- content: result.content,
143
- isError: result.isError,
144
- });
145
- }
146
- }
147
- checkDoneToolCall(toolCall) {
148
- if (toolCall.name === 'done')
149
- return toolCall.arguments.result;
150
- return null;
151
- }
152
- }
@@ -1,141 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- const model = 'gpt-4.1';
17
- export class OpenAIDelegate {
18
- _openai;
19
- async openai() {
20
- if (!this._openai) {
21
- const oai = await import('openai');
22
- this._openai = new oai.OpenAI();
23
- }
24
- return this._openai;
25
- }
26
- createConversation(task, tools, oneShot) {
27
- const genericTools = tools.map(tool => ({
28
- name: tool.name,
29
- description: tool.description || '',
30
- inputSchema: tool.inputSchema,
31
- }));
32
- if (!oneShot) {
33
- genericTools.push({
34
- name: 'done',
35
- description: 'Call this tool when the task is complete.',
36
- inputSchema: {
37
- type: 'object',
38
- properties: {},
39
- },
40
- });
41
- }
42
- return {
43
- messages: [{
44
- role: 'user',
45
- content: task
46
- }],
47
- tools: genericTools,
48
- };
49
- }
50
- async makeApiCall(conversation) {
51
- // Convert generic messages to OpenAI format
52
- const openaiMessages = [];
53
- for (const message of conversation.messages) {
54
- if (message.role === 'user') {
55
- openaiMessages.push({
56
- role: 'user',
57
- content: message.content
58
- });
59
- }
60
- else if (message.role === 'assistant') {
61
- const toolCalls = [];
62
- if (message.toolCalls) {
63
- for (const toolCall of message.toolCalls) {
64
- toolCalls.push({
65
- id: toolCall.id,
66
- type: 'function',
67
- function: {
68
- name: toolCall.name,
69
- arguments: JSON.stringify(toolCall.arguments)
70
- }
71
- });
72
- }
73
- }
74
- const assistantMessage = {
75
- role: 'assistant'
76
- };
77
- if (message.content)
78
- assistantMessage.content = message.content;
79
- if (toolCalls.length > 0)
80
- assistantMessage.tool_calls = toolCalls;
81
- openaiMessages.push(assistantMessage);
82
- }
83
- else if (message.role === 'tool') {
84
- openaiMessages.push({
85
- role: 'tool',
86
- tool_call_id: message.toolCallId,
87
- content: message.content,
88
- });
89
- }
90
- }
91
- // Convert generic tools to OpenAI format
92
- const openaiTools = conversation.tools.map(tool => ({
93
- type: 'function',
94
- function: {
95
- name: tool.name,
96
- description: tool.description,
97
- parameters: tool.inputSchema,
98
- },
99
- }));
100
- const openai = await this.openai();
101
- const response = await openai.chat.completions.create({
102
- model,
103
- messages: openaiMessages,
104
- tools: openaiTools,
105
- tool_choice: 'auto'
106
- });
107
- const message = response.choices[0].message;
108
- // Extract tool calls and add assistant message to generic conversation
109
- const toolCalls = message.tool_calls || [];
110
- const genericToolCalls = toolCalls.map(toolCall => {
111
- const functionCall = toolCall.function;
112
- return {
113
- name: functionCall.name,
114
- arguments: JSON.parse(functionCall.arguments),
115
- id: toolCall.id,
116
- };
117
- });
118
- // Add assistant message to generic conversation
119
- conversation.messages.push({
120
- role: 'assistant',
121
- content: message.content || '',
122
- toolCalls: genericToolCalls.length > 0 ? genericToolCalls : undefined
123
- });
124
- return genericToolCalls;
125
- }
126
- addToolResults(conversation, results) {
127
- for (const result of results) {
128
- conversation.messages.push({
129
- role: 'tool',
130
- toolCallId: result.toolCallId,
131
- content: result.content,
132
- isError: result.isError,
133
- });
134
- }
135
- }
136
- checkDoneToolCall(toolCall) {
137
- if (toolCall.name === 'done')
138
- return toolCall.arguments.result;
139
- return null;
140
- }
141
- }
package/lib/loop/main.js DELETED
@@ -1,60 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- /* eslint-disable no-console */
17
- import path from 'path';
18
- import url from 'url';
19
- import dotenv from 'dotenv';
20
- import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
21
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
22
- import { program } from 'commander';
23
- import { OpenAIDelegate } from './loopOpenAI.js';
24
- import { ClaudeDelegate } from './loopClaude.js';
25
- import { runTask } from './loop.js';
26
- dotenv.config();
27
- const __filename = url.fileURLToPath(import.meta.url);
28
- async function run(delegate) {
29
- const transport = new StdioClientTransport({
30
- command: 'node',
31
- args: [
32
- path.resolve(__filename, '../../../cli.js'),
33
- '--save-session',
34
- '--output-dir', path.resolve(__filename, '../../../sessions')
35
- ],
36
- stderr: 'inherit',
37
- env: process.env,
38
- });
39
- const client = new Client({ name: 'test', version: '1.0.0' });
40
- await client.connect(transport);
41
- await client.ping();
42
- for (const task of tasks) {
43
- const messages = await runTask(delegate, client, task);
44
- for (const message of messages)
45
- console.log(`${message.role}: ${message.content}`);
46
- }
47
- await client.close();
48
- }
49
- const tasks = [
50
- 'Open https://playwright.dev/',
51
- ];
52
- program
53
- .option('--model <model>', 'model to use')
54
- .action(async (options) => {
55
- if (options.model === 'claude')
56
- await run(new ClaudeDelegate());
57
- else
58
- await run(new OpenAIDelegate());
59
- });
60
- void program.parseAsync(process.argv);
@@ -1,67 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import { Client } from '@modelcontextprotocol/sdk/client/index.js';
17
- import { contextFactory } from '../browserContextFactory.js';
18
- import { BrowserServerBackend } from '../browserServerBackend.js';
19
- import { Context as BrowserContext } from '../context.js';
20
- import { runTask } from '../loop/loop.js';
21
- import { OpenAIDelegate } from '../loop/loopOpenAI.js';
22
- import { ClaudeDelegate } from '../loop/loopClaude.js';
23
- import { InProcessTransport } from '../mcp/inProcessTransport.js';
24
- import * as mcpServer from '../mcp/server.js';
25
- import { packageJSON } from '../utils/package.js';
26
- export class Context {
27
- config;
28
- _client;
29
- _delegate;
30
- constructor(config, client) {
31
- this.config = config;
32
- this._client = client;
33
- if (process.env.OPENAI_API_KEY)
34
- this._delegate = new OpenAIDelegate();
35
- else if (process.env.ANTHROPIC_API_KEY)
36
- this._delegate = new ClaudeDelegate();
37
- else
38
- throw new Error('No LLM API key found. Please set OPENAI_API_KEY or ANTHROPIC_API_KEY environment variable.');
39
- }
40
- static async create(config) {
41
- const client = new Client({ name: 'Playwright Proxy', version: packageJSON.version });
42
- const browserContextFactory = contextFactory(config);
43
- const server = mcpServer.createServer('Playwright Subagent', packageJSON.version, new BrowserServerBackend(config, browserContextFactory), false);
44
- await client.connect(new InProcessTransport(server));
45
- await client.ping();
46
- return new Context(config, client);
47
- }
48
- async runTask(task, oneShot = false) {
49
- const messages = await runTask(this._delegate, this._client, task, oneShot);
50
- const lines = [];
51
- // Skip the first message, which is the user's task.
52
- for (const message of messages.slice(1)) {
53
- // Trim out all page snapshots.
54
- if (!message.content.trim())
55
- continue;
56
- const index = oneShot ? -1 : message.content.indexOf('### Page state');
57
- const trimmedContent = index === -1 ? message.content : message.content.substring(0, index);
58
- lines.push(`[${message.role}]:`, trimmedContent);
59
- }
60
- return {
61
- content: [{ type: 'text', text: lines.join('\n') }],
62
- };
63
- }
64
- async close() {
65
- await BrowserContext.disposeAll();
66
- }
67
- }
@@ -1,54 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import dotenv from 'dotenv';
17
- import * as mcpServer from '../mcp/server.js';
18
- import { packageJSON } from '../utils/package.js';
19
- import { Context } from './context.js';
20
- import { perform } from './perform.js';
21
- import { snapshot } from './snapshot.js';
22
- import { toMcpTool } from '../mcp/tool.js';
23
- export async function runLoopTools(config) {
24
- dotenv.config();
25
- const serverBackendFactory = {
26
- name: 'Playwright',
27
- nameInConfig: 'playwright-loop',
28
- version: packageJSON.version,
29
- create: () => new LoopToolsServerBackend(config)
30
- };
31
- await mcpServer.start(serverBackendFactory, config.server);
32
- }
33
- class LoopToolsServerBackend {
34
- _config;
35
- _context;
36
- _tools = [perform, snapshot];
37
- constructor(config) {
38
- this._config = config;
39
- }
40
- async initialize() {
41
- this._context = await Context.create(this._config);
42
- }
43
- async listTools() {
44
- return this._tools.map(tool => toMcpTool(tool.schema));
45
- }
46
- async callTool(name, args) {
47
- const tool = this._tools.find(tool => tool.schema.name === name);
48
- const parsedArguments = tool.schema.inputSchema.parse(args || {});
49
- return await tool.handle(this._context, parsedArguments);
50
- }
51
- serverClosed() {
52
- void this._context.close();
53
- }
54
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- const performSchema = z.object({
19
- task: z.string().describe('The task to perform with the browser'),
20
- });
21
- export const perform = defineTool({
22
- schema: {
23
- name: 'browser_perform',
24
- title: 'Perform a task with the browser',
25
- description: 'Perform a task with the browser. It can click, type, export, capture screenshot, drag, hover, select options, etc.',
26
- inputSchema: performSchema,
27
- type: 'destructive',
28
- },
29
- handle: async (context, params) => {
30
- return await context.runTask(params.task);
31
- },
32
- });
@@ -1,29 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- import { z } from 'zod';
17
- import { defineTool } from './tool.js';
18
- export const snapshot = defineTool({
19
- schema: {
20
- name: 'browser_snapshot',
21
- title: 'Take a snapshot of the browser',
22
- description: 'Take a snapshot of the browser to read what is on the page.',
23
- inputSchema: z.object({}),
24
- type: 'readOnly',
25
- },
26
- handle: async (context, params) => {
27
- return await context.runTask('Capture browser snapshot', true);
28
- },
29
- });
@@ -1,18 +0,0 @@
1
- /**
2
- * Copyright (c) Microsoft Corporation.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- export function defineTool(tool) {
17
- return tool;
18
- }