Compare commits
12 Commits
copilot/fi
...
copilot/fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e35f0ac562 | ||
|
|
bb9de30ef9 | ||
|
|
9588845cc3 | ||
|
|
f00f78491a | ||
|
|
173637e1d2 | ||
|
|
efe3ff0c7c | ||
|
|
e3df209b96 | ||
|
|
29711d07d3 | ||
|
|
b0be1ee256 | ||
|
|
d3867affed | ||
|
|
1eee30fd45 | ||
|
|
29ac29e6bb |
44
.github/workflows/copilot-setup-steps.yml
vendored
Normal file
44
.github/workflows/copilot-setup-steps.yml
vendored
Normal file
@@ -0,0 +1,44 @@
|
||||
name: "Copilot Setup Steps"
|
||||
|
||||
# Automatically run the setup steps when they are changed to allow for easy validation, and
|
||||
# allow manual testing through the repository's "Actions" tab
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
paths:
|
||||
- .github/workflows/copilot-setup-steps.yml
|
||||
pull_request:
|
||||
paths:
|
||||
- .github/workflows/copilot-setup-steps.yml
|
||||
|
||||
jobs:
|
||||
# The job MUST be called `copilot-setup-steps` or it will not be picked up by Copilot.
|
||||
copilot-setup-steps:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
# Set the permissions to the lowest permissions possible needed for your steps.
|
||||
# Copilot will be given its own token for its operations.
|
||||
permissions:
|
||||
# If you want to clone the repository as part of your setup steps, for example to install dependencies, you'll need the `contents: read` permission. If you don't clone the repository in your setup steps, Copilot will do this for you automatically after the steps complete.
|
||||
contents: read
|
||||
|
||||
# You can define any steps you want, and they will run before the agent starts.
|
||||
# If you do not check out your code, Copilot will do this for you.
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "18.19"
|
||||
cache: "npm"
|
||||
|
||||
- name: Install JavaScript dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Playwright install
|
||||
run: npx playwright install --with-deps
|
||||
|
||||
- name: Build
|
||||
run: npm run build
|
||||
@@ -164,6 +164,8 @@ Playwright MCP server supports following arguments. They can be provided in the
|
||||
"http://myproxy:3128" or "socks5://myproxy:8080"
|
||||
--save-trace Whether to save the Playwright Trace of the
|
||||
session into the output directory.
|
||||
--save-session Whether to save the session log with tool calls
|
||||
and snapshots into the output directory.
|
||||
--storage-state <path> path to the storage state file for isolated
|
||||
sessions.
|
||||
--user-agent <ua string> specify user agent string
|
||||
@@ -303,19 +305,19 @@ npx @playwright/mcp@latest --config path/to/config.json
|
||||
### Standalone MCP server
|
||||
|
||||
When running headed browser on system w/o display or from worker processes of the IDEs,
|
||||
run the MCP server from environment with the DISPLAY and pass the `--port` flag to enable SSE transport.
|
||||
run the MCP server from environment with the DISPLAY and pass the `--port` flag to enable HTTP transport.
|
||||
|
||||
```bash
|
||||
npx @playwright/mcp@latest --port 8931
|
||||
```
|
||||
|
||||
And then in MCP client config, set the `url` to the SSE endpoint:
|
||||
And then in MCP client config, set the `url` to the HTTP endpoint:
|
||||
|
||||
```js
|
||||
{
|
||||
"mcpServers": {
|
||||
"playwright": {
|
||||
"url": "http://localhost:8931/sse"
|
||||
"url": "http://localhost:8931/mcp"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -534,6 +536,7 @@ http.createServer(async (req, res) => {
|
||||
- `filename` (string, optional): File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.
|
||||
- `element` (string, optional): Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.
|
||||
- `ref` (string, optional): Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.
|
||||
- `fullPage` (boolean, optional): When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.
|
||||
- Read-only: **true**
|
||||
|
||||
<!-- NOTE: This has been generated via update-readme.js -->
|
||||
|
||||
5
config.d.ts
vendored
5
config.d.ts
vendored
@@ -90,6 +90,11 @@ export type Config = {
|
||||
*/
|
||||
saveTrace?: boolean;
|
||||
|
||||
/**
|
||||
* Whether to save the session log with tool calls and snapshots into the output directory.
|
||||
*/
|
||||
saveSession?: boolean;
|
||||
|
||||
/**
|
||||
* The directory to save output files.
|
||||
*/
|
||||
|
||||
32
extension/connect.html
Normal file
32
extension/connect.html
Normal file
@@ -0,0 +1,32 @@
|
||||
<!--
|
||||
Copyright (c) Microsoft Corporation.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Playwright MCP extension</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h3>Playwright MCP extension</h3>
|
||||
</div>
|
||||
<div id="status-container"></div>
|
||||
<div class="button-row">
|
||||
<button id="continue-btn">Continue</button>
|
||||
<button id="reject-btn">Reject</button>
|
||||
</div>
|
||||
<script src="lib/connect.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
BIN
extension/icons/icon-128.png
Normal file
BIN
extension/icons/icon-128.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.2 KiB |
BIN
extension/icons/icon-16.png
Normal file
BIN
extension/icons/icon-16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 571 B |
BIN
extension/icons/icon-32.png
Normal file
BIN
extension/icons/icon-32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 KiB |
BIN
extension/icons/icon-48.png
Normal file
BIN
extension/icons/icon-48.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 KiB |
40
extension/manifest.json
Normal file
40
extension/manifest.json
Normal file
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"manifest_version": 3,
|
||||
"name": "Playwright MCP Bridge",
|
||||
"version": "1.0.0",
|
||||
"description": "Share browser tabs with Playwright MCP server",
|
||||
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9nMS2b0WCohjVHPGb8D9qAdkbIngDqoAjTeSccHJijgcONejge+OJxOQOMLu7b0ovt1c9BiEJa5JcpM+EHFVGL1vluBxK71zmBy1m2f9vZF3HG0LSCp7YRkum9rAIEthDwbkxx6XTvpmAY5rjFa/NON6b9Hlbo+8peUSkoOK7HTwYnnI36asZ9eUTiveIf+DMPLojW2UX33vDWG2UKvMVDewzclb4+uLxAYshY7Mx8we/b44xu+Anb/EBLKjOPk9Yh541xJ5Ozc8EiP/5yxOp9c/lRiYUHaRW+4r0HKZyFt0eZ52ti2iM4Nfk7jRXR7an3JPsUIf5deC/1cVM/+1ZQIDAQAB",
|
||||
|
||||
"permissions": [
|
||||
"debugger",
|
||||
"activeTab",
|
||||
"tabs",
|
||||
"storage"
|
||||
],
|
||||
|
||||
"host_permissions": [
|
||||
"<all_urls>"
|
||||
],
|
||||
|
||||
"background": {
|
||||
"service_worker": "lib/background.js",
|
||||
"type": "module"
|
||||
},
|
||||
|
||||
"action": {
|
||||
"default_title": "Playwright MCP Bridge",
|
||||
"default_icon": {
|
||||
"16": "icons/icon-16.png",
|
||||
"32": "icons/icon-32.png",
|
||||
"48": "icons/icon-48.png",
|
||||
"128": "icons/icon-128.png"
|
||||
}
|
||||
},
|
||||
|
||||
"icons": {
|
||||
"16": "icons/icon-16.png",
|
||||
"32": "icons/icon-32.png",
|
||||
"48": "icons/icon-48.png",
|
||||
"128": "icons/icon-128.png"
|
||||
}
|
||||
}
|
||||
109
extension/src/background.ts
Normal file
109
extension/src/background.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { RelayConnection, debugLog } from './relayConnection.js';
|
||||
|
||||
type PageMessage = {
|
||||
type: 'connectToMCPRelay';
|
||||
mcpRelayUrl: string;
|
||||
};
|
||||
|
||||
class TabShareExtension {
|
||||
private _activeConnection: RelayConnection | undefined;
|
||||
private _connectedTabId: number | null = null;
|
||||
|
||||
constructor() {
|
||||
chrome.tabs.onRemoved.addListener(this._onTabRemoved.bind(this));
|
||||
chrome.tabs.onUpdated.addListener(this._onTabUpdated.bind(this));
|
||||
chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
|
||||
}
|
||||
|
||||
// Promise-based message handling is not supported in Chrome: https://issues.chromium.org/issues/40753031
|
||||
private _onMessage(message: PageMessage, sender: chrome.runtime.MessageSender, sendResponse: (response: any) => void) {
|
||||
switch (message.type) {
|
||||
case 'connectToMCPRelay':
|
||||
const tabId = sender.tab?.id;
|
||||
if (!tabId) {
|
||||
sendResponse({ success: false, error: 'No tab id' });
|
||||
return true;
|
||||
}
|
||||
this._connectTab(tabId, message.mcpRelayUrl!).then(
|
||||
() => sendResponse({ success: true }),
|
||||
(error: any) => sendResponse({ success: false, error: error.message }));
|
||||
return true; // Return true to indicate that the response will be sent asynchronously
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private async _connectTab(tabId: number, mcpRelayUrl: string): Promise<void> {
|
||||
try {
|
||||
debugLog(`Connecting tab ${tabId} to bridge at ${mcpRelayUrl}`);
|
||||
const socket = new WebSocket(mcpRelayUrl);
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
socket.onopen = () => resolve();
|
||||
socket.onerror = () => reject(new Error('WebSocket error'));
|
||||
setTimeout(() => reject(new Error('Connection timeout')), 5000);
|
||||
});
|
||||
|
||||
const connection = new RelayConnection(socket);
|
||||
connection.setConnectedTabId(tabId);
|
||||
const connectionClosed = (m: string) => {
|
||||
debugLog(m);
|
||||
if (this._activeConnection === connection) {
|
||||
this._activeConnection = undefined;
|
||||
void this._setConnectedTabId(null);
|
||||
}
|
||||
};
|
||||
socket.onclose = () => connectionClosed('WebSocket closed');
|
||||
socket.onerror = error => connectionClosed(`WebSocket error: ${error}`);
|
||||
this._activeConnection = connection;
|
||||
|
||||
await this._setConnectedTabId(tabId);
|
||||
debugLog(`Tab ${tabId} connected successfully`);
|
||||
} catch (error: any) {
|
||||
debugLog(`Failed to connect tab ${tabId}:`, error.message);
|
||||
await this._setConnectedTabId(null);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private async _setConnectedTabId(tabId: number | null): Promise<void> {
|
||||
const oldTabId = this._connectedTabId;
|
||||
this._connectedTabId = tabId;
|
||||
if (oldTabId && oldTabId !== tabId)
|
||||
await this._updateBadge(oldTabId, { text: '', color: null });
|
||||
if (tabId)
|
||||
await this._updateBadge(tabId, { text: '●', color: '#4CAF50' });
|
||||
}
|
||||
|
||||
private async _updateBadge(tabId: number, { text, color }: { text: string; color: string | null }): Promise<void> {
|
||||
await chrome.action.setBadgeText({ tabId, text });
|
||||
if (color)
|
||||
await chrome.action.setBadgeBackgroundColor({ tabId, color });
|
||||
}
|
||||
|
||||
private async _onTabRemoved(tabId: number): Promise<void> {
|
||||
if (this._connectedTabId === tabId)
|
||||
this._activeConnection!.setConnectedTabId(null);
|
||||
}
|
||||
|
||||
private async _onTabUpdated(tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab): Promise<void> {
|
||||
if (changeInfo.status === 'complete' && this._connectedTabId === tabId)
|
||||
await this._setConnectedTabId(tabId);
|
||||
}
|
||||
}
|
||||
|
||||
new TabShareExtension();
|
||||
70
extension/src/connect.ts
Normal file
70
extension/src/connect.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
const statusContainer = document.getElementById('status-container') as HTMLElement;
|
||||
const continueBtn = document.getElementById('continue-btn') as HTMLButtonElement;
|
||||
const rejectBtn = document.getElementById('reject-btn') as HTMLButtonElement;
|
||||
const buttonRow = document.querySelector('.button-row') as HTMLElement;
|
||||
|
||||
function showStatus(type: 'connected' | 'error' | 'connecting', message: string) {
|
||||
const div = document.createElement('div');
|
||||
div.className = `status ${type}`;
|
||||
div.textContent = message;
|
||||
statusContainer.replaceChildren(div);
|
||||
}
|
||||
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
const mcpRelayUrl = params.get('mcpRelayUrl');
|
||||
|
||||
if (!mcpRelayUrl) {
|
||||
buttonRow.style.display = 'none';
|
||||
showStatus('error', 'Missing mcpRelayUrl parameter in URL.');
|
||||
return;
|
||||
}
|
||||
|
||||
let clientInfo = 'unknown';
|
||||
try {
|
||||
const client = JSON.parse(params.get('client') || '{}');
|
||||
clientInfo = `${client.name}/${client.version}`;
|
||||
} catch (e) {
|
||||
showStatus('error', 'Failed to parse client version.');
|
||||
return;
|
||||
}
|
||||
|
||||
showStatus('connecting', `MCP client "${clientInfo}" is trying to connect. Do you want to continue?`);
|
||||
|
||||
rejectBtn.addEventListener('click', async () => {
|
||||
buttonRow.style.display = 'none';
|
||||
showStatus('error', 'Connection rejected. This tab can be closed.');
|
||||
});
|
||||
|
||||
continueBtn.addEventListener('click', async () => {
|
||||
buttonRow.style.display = 'none';
|
||||
try {
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
type: 'connectToMCPRelay',
|
||||
mcpRelayUrl
|
||||
});
|
||||
if (response?.success)
|
||||
showStatus('connected', `MCP client "${clientInfo}" connected.`);
|
||||
else
|
||||
showStatus('error', response?.error || `MCP client "${clientInfo}" failed to connect.`);
|
||||
} catch (e) {
|
||||
showStatus('error', `MCP client "${clientInfo}" failed to connect: ${e}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
176
extension/src/relayConnection.ts
Normal file
176
extension/src/relayConnection.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export function debugLog(...args: unknown[]): void {
|
||||
const enabled = true;
|
||||
if (enabled) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('[Extension]', ...args);
|
||||
}
|
||||
}
|
||||
|
||||
type ProtocolCommand = {
|
||||
id: number;
|
||||
method: string;
|
||||
params?: any;
|
||||
};
|
||||
|
||||
type ProtocolResponse = {
|
||||
id?: number;
|
||||
method?: string;
|
||||
params?: any;
|
||||
result?: any;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export class RelayConnection {
|
||||
private _debuggee: chrome.debugger.Debuggee = {};
|
||||
private _rootSessionId = '';
|
||||
private _ws: WebSocket;
|
||||
private _eventListener: (source: chrome.debugger.DebuggerSession, method: string, params: any) => void;
|
||||
private _detachListener: (source: chrome.debugger.Debuggee, reason: string) => void;
|
||||
|
||||
constructor(ws: WebSocket) {
|
||||
this._ws = ws;
|
||||
this._ws.onmessage = this._onMessage.bind(this);
|
||||
// Store listeners for cleanup
|
||||
this._eventListener = this._onDebuggerEvent.bind(this);
|
||||
this._detachListener = this._onDebuggerDetach.bind(this);
|
||||
chrome.debugger.onEvent.addListener(this._eventListener);
|
||||
chrome.debugger.onDetach.addListener(this._detachListener);
|
||||
}
|
||||
|
||||
setConnectedTabId(tabId: number | null): void {
|
||||
if (!tabId) {
|
||||
this._debuggee = { };
|
||||
this._rootSessionId = '';
|
||||
return;
|
||||
}
|
||||
this._debuggee = { tabId };
|
||||
this._rootSessionId = `pw-tab-${tabId}`;
|
||||
}
|
||||
|
||||
close(message?: string): void {
|
||||
chrome.debugger.onEvent.removeListener(this._eventListener);
|
||||
chrome.debugger.onDetach.removeListener(this._detachListener);
|
||||
this._ws.close(1000, message || 'Connection closed');
|
||||
}
|
||||
|
||||
private async _detachDebugger(): Promise<void> {
|
||||
await chrome.debugger.detach(this._debuggee);
|
||||
}
|
||||
|
||||
private _onDebuggerEvent(source: chrome.debugger.DebuggerSession, method: string, params: any): void {
|
||||
if (source.tabId !== this._debuggee.tabId)
|
||||
return;
|
||||
debugLog('Forwarding CDP event:', method, params);
|
||||
const sessionId = source.sessionId || this._rootSessionId;
|
||||
this._sendMessage({
|
||||
method: 'forwardCDPEvent',
|
||||
params: {
|
||||
sessionId,
|
||||
method,
|
||||
params,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private _onDebuggerDetach(source: chrome.debugger.Debuggee, reason: string): void {
|
||||
if (source.tabId !== this._debuggee.tabId)
|
||||
return;
|
||||
this._sendMessage({
|
||||
method: 'detachedFromTab',
|
||||
params: {
|
||||
tabId: this._debuggee.tabId,
|
||||
reason,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private _onMessage(event: MessageEvent): void {
|
||||
this._onMessageAsync(event).catch(e => debugLog('Error handling message:', e));
|
||||
}
|
||||
|
||||
private async _onMessageAsync(event: MessageEvent): Promise<void> {
|
||||
let message: ProtocolCommand;
|
||||
try {
|
||||
message = JSON.parse(event.data);
|
||||
} catch (error: any) {
|
||||
debugLog('Error parsing message:', error);
|
||||
this._sendError(-32700, `Error parsing message: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
debugLog('Received message:', message);
|
||||
|
||||
const response: ProtocolResponse = {
|
||||
id: message.id,
|
||||
};
|
||||
try {
|
||||
response.result = await this._handleCommand(message);
|
||||
} catch (error: any) {
|
||||
debugLog('Error handling command:', error);
|
||||
response.error = error.message;
|
||||
}
|
||||
debugLog('Sending response:', response);
|
||||
this._sendMessage(response);
|
||||
}
|
||||
|
||||
private async _handleCommand(message: ProtocolCommand): Promise<any> {
|
||||
if (!this._debuggee.tabId)
|
||||
throw new Error('No tab is connected. Please go to the Playwright MCP extension and select the tab you want to connect to.');
|
||||
if (message.method === 'attachToTab') {
|
||||
debugLog('Attaching debugger to tab:', this._debuggee);
|
||||
await chrome.debugger.attach(this._debuggee, '1.3');
|
||||
const result: any = await chrome.debugger.sendCommand(this._debuggee, 'Target.getTargetInfo');
|
||||
return {
|
||||
sessionId: this._rootSessionId,
|
||||
targetInfo: result?.targetInfo,
|
||||
};
|
||||
}
|
||||
if (message.method === 'detachFromTab') {
|
||||
debugLog('Detaching debugger from tab:', this._debuggee);
|
||||
return await this._detachDebugger();
|
||||
}
|
||||
if (message.method === 'forwardCDPCommand') {
|
||||
const { sessionId, method, params } = message.params;
|
||||
debugLog('CDP command:', method, params);
|
||||
const debuggerSession: chrome.debugger.DebuggerSession = { ...this._debuggee };
|
||||
// Pass session id, unless it's the root session.
|
||||
if (sessionId && sessionId !== this._rootSessionId)
|
||||
debuggerSession.sessionId = sessionId;
|
||||
// Forward CDP command to chrome.debugger
|
||||
return await chrome.debugger.sendCommand(
|
||||
debuggerSession,
|
||||
method,
|
||||
params
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private _sendError(code: number, message: string): void {
|
||||
this._sendMessage({
|
||||
error: {
|
||||
code,
|
||||
message,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private _sendMessage(message: any): void {
|
||||
this._ws.send(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
15
extension/tsconfig.json
Normal file
15
extension/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ESNext",
|
||||
"esModuleInterop": true,
|
||||
"moduleResolution": "node",
|
||||
"strict": true,
|
||||
"module": "ESNext",
|
||||
"rootDir": "src",
|
||||
"outDir": "./lib",
|
||||
"resolveJsonModule": true,
|
||||
},
|
||||
"include": [
|
||||
"src",
|
||||
],
|
||||
}
|
||||
18
package-lock.json
generated
18
package-lock.json
generated
@@ -9,7 +9,7 @@
|
||||
"version": "0.0.31",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.11.0",
|
||||
"@modelcontextprotocol/sdk": "^1.16.0",
|
||||
"commander": "^13.1.0",
|
||||
"debug": "^4.4.1",
|
||||
"mime": "^4.0.7",
|
||||
@@ -234,15 +234,17 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@modelcontextprotocol/sdk": {
|
||||
"version": "1.11.0",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.11.0.tgz",
|
||||
"integrity": "sha512-k/1pb70eD638anoi0e8wUGAlbMJXyvdV4p62Ko+EZ7eBe1xMx8Uhak1R5DgfoofsK5IBBnRwsYGTaLZl+6/+RQ==",
|
||||
"version": "1.16.0",
|
||||
"resolved": "https://registry.npmjs.org/@modelcontextprotocol/sdk/-/sdk-1.16.0.tgz",
|
||||
"integrity": "sha512-8ofX7gkZcLj9H9rSd50mCgm3SSF8C7XoclxJuLoV0Cz3rEQ1tv9MZRYYvJtm9n1BiEQQMzSmE/w2AEkNacLYfg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"ajv": "^6.12.6",
|
||||
"content-type": "^1.0.5",
|
||||
"cors": "^2.8.5",
|
||||
"cross-spawn": "^7.0.3",
|
||||
"cross-spawn": "^7.0.5",
|
||||
"eventsource": "^3.0.2",
|
||||
"eventsource-parser": "^3.0.0",
|
||||
"express": "^5.0.1",
|
||||
"express-rate-limit": "^7.5.0",
|
||||
"pkce-challenge": "^5.0.0",
|
||||
@@ -713,7 +715,6 @@
|
||||
"version": "6.12.6",
|
||||
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz",
|
||||
"integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"fast-deep-equal": "^3.1.1",
|
||||
@@ -1850,7 +1851,6 @@
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/fast-glob": {
|
||||
@@ -1887,7 +1887,6 @@
|
||||
"version": "2.1.0",
|
||||
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
|
||||
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/fast-levenshtein": {
|
||||
@@ -2808,7 +2807,6 @@
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
|
||||
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/json-stable-stringify-without-jsonify": {
|
||||
@@ -3367,7 +3365,6 @@
|
||||
"version": "2.3.1",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
|
||||
"integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
|
||||
"dev": true,
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
@@ -4161,7 +4158,6 @@
|
||||
"version": "4.4.1",
|
||||
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
|
||||
"integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==",
|
||||
"dev": true,
|
||||
"license": "BSD-2-Clause",
|
||||
"dependencies": {
|
||||
"punycode": "^2.1.0"
|
||||
|
||||
@@ -17,15 +17,17 @@
|
||||
"license": "Apache-2.0",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"build:extension": "tsc --project extension",
|
||||
"lint": "npm run update-readme && eslint . && tsc --noEmit",
|
||||
"update-readme": "node utils/update-readme.js",
|
||||
"watch": "tsc --watch",
|
||||
"watch:extension": "tsc --watch --project extension",
|
||||
"test": "playwright test",
|
||||
"ctest": "playwright test --project=chrome",
|
||||
"ftest": "playwright test --project=firefox",
|
||||
"wtest": "playwright test --project=webkit",
|
||||
"run-server": "node lib/browserServer.js",
|
||||
"clean": "rm -rf lib",
|
||||
"clean": "rm -rf lib extension/lib",
|
||||
"npm-publish": "npm run clean && npm run build && npm run test && npm publish"
|
||||
},
|
||||
"exports": {
|
||||
@@ -36,7 +38,7 @@
|
||||
}
|
||||
},
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.11.0",
|
||||
"@modelcontextprotocol/sdk": "^1.16.0",
|
||||
"commander": "^13.1.0",
|
||||
"debug": "^4.4.1",
|
||||
"mime": "^4.0.7",
|
||||
|
||||
@@ -36,7 +36,7 @@ export function contextFactory(browserConfig: FullConfig['browser']): BrowserCon
|
||||
}
|
||||
|
||||
export interface BrowserContextFactory {
|
||||
createContext(): Promise<{ browserContext: playwright.BrowserContext, close: () => Promise<void> }>;
|
||||
createContext(clientInfo: { name: string, version: string }): Promise<{ browserContext: playwright.BrowserContext, close: () => Promise<void> }>;
|
||||
}
|
||||
|
||||
class BaseContextFactory implements BrowserContextFactory {
|
||||
|
||||
@@ -44,6 +44,7 @@ export type CLIOptions = {
|
||||
proxyBypass?: string;
|
||||
proxyServer?: string;
|
||||
saveTrace?: boolean;
|
||||
saveSession?: boolean;
|
||||
storageState?: string;
|
||||
userAgent?: string;
|
||||
userDataDir?: string;
|
||||
@@ -133,7 +134,7 @@ export function configFromCLIOptions(cliOptions: CLIOptions): Config {
|
||||
};
|
||||
|
||||
// --no-sandbox was passed, disable the sandbox
|
||||
if (!cliOptions.sandbox)
|
||||
if (cliOptions.sandbox === false)
|
||||
launchOptions.chromiumSandbox = false;
|
||||
|
||||
if (cliOptions.proxyServer) {
|
||||
@@ -191,6 +192,7 @@ export function configFromCLIOptions(cliOptions: CLIOptions): Config {
|
||||
blockedOrigins: cliOptions.blockedOrigins,
|
||||
},
|
||||
saveTrace: cliOptions.saveTrace,
|
||||
saveSession: cliOptions.saveSession,
|
||||
outputDir: cliOptions.outputDir,
|
||||
imageResponses: cliOptions.imageResponses,
|
||||
};
|
||||
@@ -221,6 +223,7 @@ function configFromEnv(): Config {
|
||||
options.proxyBypass = envToString(process.env.PLAYWRIGHT_MCP_PROXY_BYPASS);
|
||||
options.proxyServer = envToString(process.env.PLAYWRIGHT_MCP_PROXY_SERVER);
|
||||
options.saveTrace = envToBoolean(process.env.PLAYWRIGHT_MCP_SAVE_TRACE);
|
||||
options.saveSession = envToBoolean(process.env.PLAYWRIGHT_MCP_SAVE_SESSION);
|
||||
options.storageState = envToString(process.env.PLAYWRIGHT_MCP_STORAGE_STATE);
|
||||
options.userAgent = envToString(process.env.PLAYWRIGHT_MCP_USER_AGENT);
|
||||
options.userDataDir = envToString(process.env.PLAYWRIGHT_MCP_USER_DATA_DIR);
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
import debug from 'debug';
|
||||
import * as playwright from 'playwright';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
|
||||
import { callOnPageNoTrace, waitForCompletion } from './tools/utils.js';
|
||||
import { ManualPromise } from './manualPromise.js';
|
||||
@@ -42,6 +44,8 @@ export class Context {
|
||||
private _modalStates: (ModalState & { tab: Tab })[] = [];
|
||||
private _pendingAction: PendingAction | undefined;
|
||||
private _downloads: { download: playwright.Download, finished: boolean, outputFile: string }[] = [];
|
||||
private _sessionFile: string | undefined;
|
||||
private _sessionFileInitialized: Promise<void> | undefined;
|
||||
clientVersion: { name: string; version: string; } | undefined;
|
||||
|
||||
constructor(tools: Tool[], config: FullConfig, browserContextFactory: BrowserContextFactory) {
|
||||
@@ -49,6 +53,8 @@ export class Context {
|
||||
this.config = config;
|
||||
this._browserContextFactory = browserContextFactory;
|
||||
testDebug('create context');
|
||||
if (this.config.saveSession)
|
||||
this._sessionFileInitialized = this._initializeSessionFile();
|
||||
}
|
||||
|
||||
clientSupportsImages(): boolean {
|
||||
@@ -129,6 +135,50 @@ export class Context {
|
||||
return await this.listTabsMarkdown();
|
||||
}
|
||||
|
||||
private async _initializeSessionFile() {
|
||||
if (!this.config.saveSession)
|
||||
return;
|
||||
|
||||
const timestamp = new Date().toISOString();
|
||||
const fileName = `session${timestamp}.yml`;
|
||||
this._sessionFile = await outputFile(this.config, fileName);
|
||||
|
||||
// Initialize empty session file
|
||||
await fs.promises.writeFile(this._sessionFile, '# Session log started at ' + timestamp + '\n', 'utf8');
|
||||
}
|
||||
|
||||
private async _logSessionEntry(toolName: string, params: Record<string, unknown>, snapshotFile?: string) {
|
||||
if (!this.config.saveSession)
|
||||
return;
|
||||
|
||||
// Ensure session file is initialized before proceeding
|
||||
if (this._sessionFileInitialized)
|
||||
await this._sessionFileInitialized;
|
||||
|
||||
// After initialization, session file should always be defined when saveSession is true
|
||||
if (!this._sessionFile)
|
||||
throw new Error('Session file not initialized despite saveSession being enabled');
|
||||
|
||||
const entry = [
|
||||
`- ${toolName}:`,
|
||||
' params:',
|
||||
];
|
||||
|
||||
// Add parameters with proper YAML indentation
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
const yamlValue = typeof value === 'string' ? value : JSON.stringify(value);
|
||||
entry.push(` ${key}: ${yamlValue}`);
|
||||
}
|
||||
|
||||
// Add snapshot reference if provided
|
||||
if (snapshotFile)
|
||||
entry.push(` snapshot: ${path.basename(snapshotFile)}`);
|
||||
|
||||
entry.push(''); // Empty line for readability
|
||||
|
||||
await fs.promises.appendFile(this._sessionFile, entry.join('\n') + '\n', 'utf8');
|
||||
}
|
||||
|
||||
async run(tool: Tool, params: Record<string, unknown> | undefined) {
|
||||
// Tab management is done outside of the action() call.
|
||||
const toolResult = await tool.handle(this, tool.schema.inputSchema.parse(params || {}));
|
||||
@@ -147,6 +197,8 @@ export class Context {
|
||||
}
|
||||
|
||||
const tab = this.currentTabOrDie();
|
||||
let snapshotFile: string | undefined;
|
||||
|
||||
// TODO: race against modal dialogs to resolve clicks.
|
||||
const actionResult = await this._raceAgainstModalDialogs(async () => {
|
||||
try {
|
||||
@@ -155,11 +207,23 @@ export class Context {
|
||||
else
|
||||
return await action?.() ?? undefined;
|
||||
} finally {
|
||||
if (captureSnapshot && !this._javaScriptBlocked())
|
||||
if (captureSnapshot && !this._javaScriptBlocked()) {
|
||||
await tab.captureSnapshot();
|
||||
// Save snapshot to file if session logging is enabled
|
||||
if (this.config.saveSession && tab.hasSnapshot()) {
|
||||
const timestamp = new Date().toISOString();
|
||||
const snapshotFileName = `${timestamp}.snapshot.yaml`;
|
||||
snapshotFile = await outputFile(this.config, snapshotFileName);
|
||||
await fs.promises.writeFile(snapshotFile, tab.snapshotOrDie().text(), 'utf8');
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Log session entry if enabled
|
||||
if (this.config.saveSession)
|
||||
await this._logSessionEntry(tool.schema.name, params || {}, snapshotFile);
|
||||
|
||||
const result: string[] = [];
|
||||
result.push(`### Ran Playwright code
|
||||
\`\`\`js
|
||||
@@ -336,7 +400,7 @@ ${code.join('\n')}
|
||||
|
||||
private async _setupBrowserContext(): Promise<{ browserContext: playwright.BrowserContext, close: () => Promise<void> }> {
|
||||
// TODO: move to the browser context factory to make it based on isolation mode.
|
||||
const result = await this._browserContextFactory.createContext();
|
||||
const result = await this._browserContextFactory.createContext(this.clientVersion!);
|
||||
const { browserContext } = result;
|
||||
await this._setupRequestInterception(browserContext);
|
||||
for (const page of browserContext.pages())
|
||||
|
||||
397
src/extension/cdpRelay.ts
Normal file
397
src/extension/cdpRelay.ts
Normal file
@@ -0,0 +1,397 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* WebSocket server that bridges Playwright MCP and Chrome Extension
|
||||
*
|
||||
* Endpoints:
|
||||
* - /cdp/guid - Full CDP interface for Playwright MCP
|
||||
* - /extension/guid - Extension connection for chrome.debugger forwarding
|
||||
*/
|
||||
|
||||
import { WebSocket, WebSocketServer } from 'ws';
|
||||
import type websocket from 'ws';
|
||||
import http from 'node:http';
|
||||
import debug from 'debug';
|
||||
import { promisify } from 'node:util';
|
||||
import { exec } from 'node:child_process';
|
||||
import { httpAddressToString, startHttpServer } from '../transport.js';
|
||||
import { BrowserContextFactory } from '../browserContextFactory.js';
|
||||
import { Browser, chromium, type BrowserContext } from 'playwright';
|
||||
|
||||
const debugLogger = debug('pw:mcp:relay');
|
||||
|
||||
type CDPCommand = {
|
||||
id: number;
|
||||
sessionId?: string;
|
||||
method: string;
|
||||
params?: any;
|
||||
};
|
||||
|
||||
type CDPResponse = {
|
||||
id?: number;
|
||||
sessionId?: string;
|
||||
method?: string;
|
||||
params?: any;
|
||||
result?: any;
|
||||
error?: { code?: number; message: string };
|
||||
};
|
||||
|
||||
export class CDPRelayServer {
|
||||
private _wsHost: string;
|
||||
private _cdpPath: string;
|
||||
private _extensionPath: string;
|
||||
private _wss: WebSocketServer;
|
||||
private _playwrightConnection: WebSocket | null = null;
|
||||
private _extensionConnection: ExtensionConnection | null = null;
|
||||
private _connectedTabInfo: {
|
||||
targetInfo: any;
|
||||
// Page sessionId that should be used by this connection.
|
||||
sessionId: string;
|
||||
} | undefined;
|
||||
private _extensionConnectionPromise: Promise<void>;
|
||||
private _extensionConnectionResolve: (() => void) | null = null;
|
||||
|
||||
constructor(server: http.Server) {
|
||||
this._wsHost = httpAddressToString(server.address()).replace(/^http/, 'ws');
|
||||
|
||||
const uuid = crypto.randomUUID();
|
||||
this._cdpPath = `/cdp/${uuid}`;
|
||||
this._extensionPath = `/extension/${uuid}`;
|
||||
|
||||
this._extensionConnectionPromise = new Promise(resolve => {
|
||||
this._extensionConnectionResolve = resolve;
|
||||
});
|
||||
this._wss = new WebSocketServer({ server });
|
||||
this._wss.on('connection', this._onConnection.bind(this));
|
||||
}
|
||||
|
||||
cdpEndpoint() {
|
||||
return `${this._wsHost}${this._cdpPath}`;
|
||||
}
|
||||
|
||||
extensionEndpoint() {
|
||||
return `${this._wsHost}${this._extensionPath}`;
|
||||
}
|
||||
|
||||
async ensureExtensionConnectionForMCPContext(clientInfo: { name: string, version: string }) {
|
||||
if (this._extensionConnection)
|
||||
return;
|
||||
await this._connectBrowser(clientInfo);
|
||||
await this._extensionConnectionPromise;
|
||||
}
|
||||
|
||||
private async _connectBrowser(clientInfo: { name: string, version: string }) {
|
||||
const mcpRelayEndpoint = `${this._wsHost}${this._extensionPath}`;
|
||||
// Need to specify "key" in the manifest.json to make the id stable when loading from file.
|
||||
const url = new URL('chrome-extension://jakfalbnbhgkpmoaakfflhflbfpkailf/connect.html');
|
||||
url.searchParams.set('mcpRelayUrl', mcpRelayEndpoint);
|
||||
url.searchParams.set('client', JSON.stringify(clientInfo));
|
||||
const href = url.toString();
|
||||
const command = `'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' '${href}'`;
|
||||
try {
|
||||
await promisify(exec)(command);
|
||||
} catch (err) {
|
||||
debugLogger('Failed to run command:', err);
|
||||
}
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
this._playwrightConnection?.close();
|
||||
this._extensionConnection?.close();
|
||||
}
|
||||
|
||||
private _onConnection(ws: WebSocket, request: http.IncomingMessage): void {
|
||||
const url = new URL(`http://localhost${request.url}`);
|
||||
debugLogger(`New connection to ${url.pathname}`);
|
||||
if (url.pathname === this._cdpPath) {
|
||||
this._handlePlaywrightConnection(ws);
|
||||
} else if (url.pathname === this._extensionPath) {
|
||||
this._handleExtensionConnection(ws);
|
||||
} else {
|
||||
debugLogger(`Invalid path: ${url.pathname}`);
|
||||
ws.close(4004, 'Invalid path');
|
||||
}
|
||||
}
|
||||
|
||||
private _handlePlaywrightConnection(ws: WebSocket): void {
|
||||
this._playwrightConnection = ws;
|
||||
ws.on('message', async data => {
|
||||
try {
|
||||
const message = JSON.parse(data.toString());
|
||||
await this._handlePlaywrightMessage(message);
|
||||
} catch (error) {
|
||||
debugLogger('Error parsing Playwright message:', error);
|
||||
}
|
||||
});
|
||||
ws.on('close', () => {
|
||||
if (this._playwrightConnection === ws) {
|
||||
this._playwrightConnection = null;
|
||||
this._closeExtensionConnection();
|
||||
debugLogger('Playwright MCP disconnected');
|
||||
}
|
||||
});
|
||||
ws.on('error', error => {
|
||||
debugLogger('Playwright WebSocket error:', error);
|
||||
});
|
||||
debugLogger('Playwright MCP connected');
|
||||
}
|
||||
|
||||
private _closeExtensionConnection() {
|
||||
this._connectedTabInfo = undefined;
|
||||
this._extensionConnection?.close();
|
||||
this._extensionConnection = null;
|
||||
this._extensionConnectionPromise = new Promise(resolve => {
|
||||
this._extensionConnectionResolve = resolve;
|
||||
});
|
||||
}
|
||||
|
||||
private _handleExtensionConnection(ws: WebSocket): void {
|
||||
if (this._extensionConnection) {
|
||||
ws.close(1000, 'Another extension connection already established');
|
||||
return;
|
||||
}
|
||||
this._extensionConnection = new ExtensionConnection(ws);
|
||||
this._extensionConnection.onclose = c => {
|
||||
if (this._extensionConnection === c)
|
||||
this._extensionConnection = null;
|
||||
};
|
||||
this._extensionConnection.onmessage = this._handleExtensionMessage.bind(this);
|
||||
this._extensionConnectionResolve?.();
|
||||
}
|
||||
|
||||
private _handleExtensionMessage(method: string, params: any) {
|
||||
switch (method) {
|
||||
case 'forwardCDPEvent':
|
||||
this._sendToPlaywright({
|
||||
sessionId: params.sessionId,
|
||||
method: params.method,
|
||||
params: params.params
|
||||
});
|
||||
break;
|
||||
case 'detachedFromTab':
|
||||
debugLogger('← Debugger detached from tab:', params);
|
||||
this._connectedTabInfo = undefined;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async _handlePlaywrightMessage(message: CDPCommand): Promise<void> {
|
||||
debugLogger('← Playwright:', `${message.method} (id=${message.id})`);
|
||||
if (!this._extensionConnection) {
|
||||
debugLogger('Extension not connected, sending error to Playwright');
|
||||
this._sendToPlaywright({
|
||||
id: message.id,
|
||||
error: { message: 'Extension not connected' }
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (await this._interceptCDPCommand(message))
|
||||
return;
|
||||
await this._forwardToExtension(message);
|
||||
}
|
||||
|
||||
private async _interceptCDPCommand(message: CDPCommand): Promise<boolean> {
|
||||
switch (message.method) {
|
||||
case 'Browser.getVersion': {
|
||||
this._sendToPlaywright({
|
||||
id: message.id,
|
||||
result: {
|
||||
protocolVersion: '1.3',
|
||||
product: 'Chrome/Extension-Bridge',
|
||||
userAgent: 'CDP-Bridge-Server/1.0.0',
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
case 'Browser.setDownloadBehavior': {
|
||||
this._sendToPlaywright({
|
||||
id: message.id
|
||||
});
|
||||
return true;
|
||||
}
|
||||
case 'Target.setAutoAttach': {
|
||||
// Simulate auto-attach behavior with real target info
|
||||
if (!message.sessionId) {
|
||||
this._connectedTabInfo = await this._extensionConnection!.send('attachToTab');
|
||||
debugLogger('Simulating auto-attach for target:', message);
|
||||
this._sendToPlaywright({
|
||||
method: 'Target.attachedToTarget',
|
||||
params: {
|
||||
sessionId: this._connectedTabInfo!.sessionId,
|
||||
targetInfo: {
|
||||
...this._connectedTabInfo!.targetInfo,
|
||||
attached: true,
|
||||
},
|
||||
waitingForDebugger: false
|
||||
}
|
||||
});
|
||||
this._sendToPlaywright({
|
||||
id: message.id
|
||||
});
|
||||
} else {
|
||||
await this._forwardToExtension(message);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
case 'Target.getTargetInfo': {
|
||||
debugLogger('Target.getTargetInfo', message);
|
||||
this._sendToPlaywright({
|
||||
id: message.id,
|
||||
result: this._connectedTabInfo?.targetInfo
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private async _forwardToExtension(message: CDPCommand): Promise<void> {
|
||||
try {
|
||||
if (!this._extensionConnection)
|
||||
throw new Error('Extension not connected');
|
||||
const { id, sessionId, method, params } = message;
|
||||
const result = await this._extensionConnection.send('forwardCDPCommand', { sessionId, method, params });
|
||||
this._sendToPlaywright({ id, sessionId, result });
|
||||
} catch (e) {
|
||||
debugLogger('Error in the extension:', e);
|
||||
this._sendToPlaywright({
|
||||
id: message.id,
|
||||
sessionId: message.sessionId,
|
||||
error: { message: (e as Error).message }
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private _sendToPlaywright(message: CDPResponse): void {
|
||||
debugLogger('→ Playwright:', `${message.method ?? `response(id=${message.id})`}`);
|
||||
this._playwrightConnection?.send(JSON.stringify(message));
|
||||
}
|
||||
}
|
||||
|
||||
class ExtensionContextFactory implements BrowserContextFactory {
|
||||
private _relay: CDPRelayServer;
|
||||
private _browserPromise: Promise<Browser> | undefined;
|
||||
|
||||
constructor(relay: CDPRelayServer) {
|
||||
this._relay = relay;
|
||||
}
|
||||
|
||||
async createContext(clientInfo: { name: string, version: string }): Promise<{ browserContext: BrowserContext, close: () => Promise<void> }> {
|
||||
// First call will establish the connection to the extension.
|
||||
if (!this._browserPromise)
|
||||
this._browserPromise = this._obtainBrowser(clientInfo);
|
||||
const browser = await this._browserPromise;
|
||||
return {
|
||||
browserContext: browser.contexts()[0],
|
||||
close: async () => {}
|
||||
};
|
||||
}
|
||||
|
||||
private async _obtainBrowser(clientInfo: { name: string, version: string }): Promise<Browser> {
|
||||
await this._relay.ensureExtensionConnectionForMCPContext(clientInfo);
|
||||
return await chromium.connectOverCDP(this._relay.cdpEndpoint());
|
||||
}
|
||||
}
|
||||
|
||||
export async function startCDPRelayServer(port: number) {
|
||||
const httpServer = await startHttpServer({ port });
|
||||
const cdpRelayServer = new CDPRelayServer(httpServer);
|
||||
process.on('exit', () => cdpRelayServer.stop());
|
||||
debugLogger(`CDP relay server started, extension endpoint: ${cdpRelayServer.extensionEndpoint()}.`);
|
||||
return new ExtensionContextFactory(cdpRelayServer);
|
||||
}
|
||||
|
||||
class ExtensionConnection {
|
||||
private readonly _ws: WebSocket;
|
||||
private readonly _callbacks = new Map<number, { resolve: (o: any) => void, reject: (e: Error) => void }>();
|
||||
private _lastId = 0;
|
||||
|
||||
onmessage?: (method: string, params: any) => void;
|
||||
onclose?: (self: ExtensionConnection) => void;
|
||||
|
||||
constructor(ws: WebSocket) {
|
||||
this._ws = ws;
|
||||
this._ws.on('message', this._onMessage.bind(this));
|
||||
this._ws.on('close', this._onClose.bind(this));
|
||||
this._ws.on('error', this._onError.bind(this));
|
||||
}
|
||||
|
||||
async send(method: string, params?: any, sessionId?: string): Promise<any> {
|
||||
if (this._ws.readyState !== WebSocket.OPEN)
|
||||
throw new Error('WebSocket closed');
|
||||
const id = ++this._lastId;
|
||||
this._ws.send(JSON.stringify({ id, method, params, sessionId }));
|
||||
return new Promise((resolve, reject) => {
|
||||
this._callbacks.set(id, { resolve, reject });
|
||||
});
|
||||
}
|
||||
|
||||
close(message?: string) {
|
||||
debugLogger('closing extension connection:', message);
|
||||
this._ws.close(1000, message ?? 'Connection closed');
|
||||
this.onclose?.(this);
|
||||
}
|
||||
|
||||
private _onMessage(event: websocket.RawData) {
|
||||
const eventData = event.toString();
|
||||
let parsedJson;
|
||||
try {
|
||||
parsedJson = JSON.parse(eventData);
|
||||
} catch (e: any) {
|
||||
debugLogger(`<closing ws> Closing websocket due to malformed JSON. eventData=${eventData} e=${e?.message}`);
|
||||
this._ws.close();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
this._handleParsedMessage(parsedJson);
|
||||
} catch (e: any) {
|
||||
debugLogger(`<closing ws> Closing websocket due to failed onmessage callback. eventData=${eventData} e=${e?.message}`);
|
||||
this._ws.close();
|
||||
}
|
||||
}
|
||||
|
||||
private _handleParsedMessage(object: any) {
|
||||
if (object.id && this._callbacks.has(object.id)) {
|
||||
const callback = this._callbacks.get(object.id)!;
|
||||
this._callbacks.delete(object.id);
|
||||
if (object.error)
|
||||
callback.reject(new Error(object.error.message));
|
||||
else
|
||||
callback.resolve(object.result);
|
||||
} else if (object.id) {
|
||||
debugLogger('← Extension: unexpected response', object);
|
||||
} else {
|
||||
this.onmessage?.(object.method, object.params);
|
||||
}
|
||||
}
|
||||
|
||||
private _onClose(event: websocket.CloseEvent) {
|
||||
debugLogger(`<ws closed> code=${event.code} reason=${event.reason}`);
|
||||
this._dispose();
|
||||
}
|
||||
|
||||
private _onError(event: websocket.ErrorEvent) {
|
||||
debugLogger(`<ws error> message=${event.message} type=${event.type} target=${event.target}`);
|
||||
this._dispose();
|
||||
}
|
||||
|
||||
private _dispose() {
|
||||
for (const callback of this._callbacks.values())
|
||||
callback.reject(new Error('WebSocket closed'));
|
||||
this._callbacks.clear();
|
||||
}
|
||||
}
|
||||
35
src/extension/main.ts
Normal file
35
src/extension/main.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { resolveCLIConfig } from '../config.js';
|
||||
import { startHttpServer, startHttpTransport, startStdioTransport } from '../transport.js';
|
||||
import { Server } from '../server.js';
|
||||
import { startCDPRelayServer } from './cdpRelay.js';
|
||||
|
||||
export async function runWithExtension(options: any) {
|
||||
const config = await resolveCLIConfig({ });
|
||||
const contextFactory = await startCDPRelayServer(9225);
|
||||
|
||||
const server = new Server(config, contextFactory);
|
||||
server.setupExitWatchdog();
|
||||
|
||||
if (options.port !== undefined) {
|
||||
const httpServer = await startHttpServer({ port: options.port });
|
||||
startHttpTransport(httpServer, server);
|
||||
} else {
|
||||
await startStdioTransport(server);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { startHttpServer, startHttpTransport, startStdioTransport } from './tran
|
||||
import { commaSeparatedList, resolveCLIConfig, semicolonSeparatedList } from './config.js';
|
||||
import { Server } from './server.js';
|
||||
import { packageJSON } from './package.js';
|
||||
import { runWithExtension } from './extension/main.js';
|
||||
|
||||
program
|
||||
.version('Version ' + packageJSON.version)
|
||||
@@ -46,27 +47,35 @@ program
|
||||
.option('--proxy-bypass <bypass>', 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"')
|
||||
.option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
|
||||
.option('--save-trace', 'Whether to save the Playwright Trace of the session into the output directory.')
|
||||
.option('--save-session', 'Whether to save the session log with tool calls and snapshots into the output directory.')
|
||||
.option('--storage-state <path>', 'path to the storage state file for isolated sessions.')
|
||||
.option('--user-agent <ua string>', 'specify user agent string')
|
||||
.option('--user-data-dir <path>', 'path to the user data directory. If not specified, a temporary directory will be created.')
|
||||
.option('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"')
|
||||
.addOption(new Option('--extension', 'Connect to a running browser instance (Edge/Chrome only). Requires the "Playwright MCP Bridge" browser extension to be installed.').hideHelp())
|
||||
.addOption(new Option('--vision', 'Legacy option, use --caps=vision instead').hideHelp())
|
||||
.action(async options => {
|
||||
if (options.extension) {
|
||||
await runWithExtension(options);
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.vision) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error('The --vision option is deprecated, use --caps=vision instead');
|
||||
options.caps = 'vision';
|
||||
}
|
||||
const config = await resolveCLIConfig(options);
|
||||
const httpServer = config.server.port !== undefined ? await startHttpServer(config.server) : undefined;
|
||||
|
||||
const server = new Server(config);
|
||||
server.setupExitWatchdog();
|
||||
|
||||
if (httpServer)
|
||||
if (config.server.port !== undefined) {
|
||||
const httpServer = await startHttpServer(config.server);
|
||||
startHttpTransport(httpServer, server);
|
||||
else
|
||||
} else {
|
||||
await startStdioTransport(server);
|
||||
}
|
||||
|
||||
if (config.saveTrace) {
|
||||
const server = await startTraceViewerServer();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { createConnection } from './connection.js';
|
||||
import { contextFactory } from './browserContextFactory.js';
|
||||
import { contextFactory as defaultContextFactory } from './browserContextFactory.js';
|
||||
|
||||
import type { FullConfig } from './config.js';
|
||||
import type { Connection } from './connection.js';
|
||||
@@ -28,10 +28,10 @@ export class Server {
|
||||
private _browserConfig: FullConfig['browser'];
|
||||
private _contextFactory: BrowserContextFactory;
|
||||
|
||||
constructor(config: FullConfig) {
|
||||
constructor(config: FullConfig, contextFactory?: BrowserContextFactory) {
|
||||
this.config = config;
|
||||
this._browserConfig = config.browser;
|
||||
this._contextFactory = contextFactory(this._browserConfig);
|
||||
this._contextFactory = contextFactory ?? defaultContextFactory(this._browserConfig);
|
||||
}
|
||||
|
||||
async createConnection(transport: Transport): Promise<Connection> {
|
||||
|
||||
@@ -28,11 +28,17 @@ const screenshotSchema = z.object({
|
||||
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
|
||||
element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
|
||||
ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
|
||||
fullPage: z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
|
||||
}).refine(data => {
|
||||
return !!data.element === !!data.ref;
|
||||
}, {
|
||||
message: 'Both element and ref must be provided or neither.',
|
||||
path: ['ref', 'element']
|
||||
}).refine(data => {
|
||||
return !(data.fullPage && (data.element || data.ref));
|
||||
}, {
|
||||
message: 'fullPage cannot be used with element screenshots.',
|
||||
path: ['fullPage']
|
||||
});
|
||||
|
||||
const screenshot = defineTool({
|
||||
@@ -50,11 +56,18 @@ const screenshot = defineTool({
|
||||
const snapshot = tab.snapshotOrDie();
|
||||
const fileType = params.raw ? 'png' : 'jpeg';
|
||||
const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
|
||||
const options: playwright.PageScreenshotOptions = { type: fileType, quality: fileType === 'png' ? undefined : 50, scale: 'css', path: fileName };
|
||||
const options: playwright.PageScreenshotOptions = {
|
||||
type: fileType,
|
||||
quality: fileType === 'png' ? undefined : 50,
|
||||
scale: 'css',
|
||||
path: fileName,
|
||||
...(params.fullPage !== undefined && { fullPage: params.fullPage })
|
||||
};
|
||||
const isElementScreenshot = params.element && params.ref;
|
||||
|
||||
const screenshotTarget = isElementScreenshot ? params.element : (params.fullPage ? 'full page' : 'viewport');
|
||||
const code = [
|
||||
`// Screenshot ${isElementScreenshot ? params.element : 'viewport'} and save it as ${fileName}`,
|
||||
`// Screenshot ${screenshotTarget} and save it as ${fileName}`,
|
||||
];
|
||||
|
||||
const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null;
|
||||
|
||||
@@ -23,8 +23,11 @@ import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
||||
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
|
||||
import { logUnhandledError } from './log.js';
|
||||
|
||||
import type { AddressInfo } from 'node:net';
|
||||
import type { Server } from './server.js';
|
||||
import type { Connection } from './connection.js';
|
||||
|
||||
export async function startStdioTransport(server: Server) {
|
||||
await server.createConnection(new StdioServerTransport());
|
||||
@@ -55,8 +58,7 @@ async function handleSSE(server: Server, req: http.IncomingMessage, res: http.Se
|
||||
res.on('close', () => {
|
||||
testDebug(`delete SSE session: ${transport.sessionId}`);
|
||||
sessions.delete(transport.sessionId);
|
||||
// eslint-disable-next-line no-console
|
||||
void connection.close().catch(e => console.error(e));
|
||||
void connection.close().catch(logUnhandledError);
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -65,10 +67,10 @@ async function handleSSE(server: Server, req: http.IncomingMessage, res: http.Se
|
||||
res.end('Method not allowed');
|
||||
}
|
||||
|
||||
async function handleStreamable(server: Server, req: http.IncomingMessage, res: http.ServerResponse, sessions: Map<string, StreamableHTTPServerTransport>) {
|
||||
async function handleStreamable(server: Server, req: http.IncomingMessage, res: http.ServerResponse, sessions: Map<string, { transport: StreamableHTTPServerTransport, connection: Connection }>) {
|
||||
const sessionId = req.headers['mcp-session-id'] as string | undefined;
|
||||
if (sessionId) {
|
||||
const transport = sessions.get(sessionId);
|
||||
const { transport } = sessions.get(sessionId) ?? {};
|
||||
if (!transport) {
|
||||
res.statusCode = 404;
|
||||
res.end('Session not found');
|
||||
@@ -80,15 +82,22 @@ async function handleStreamable(server: Server, req: http.IncomingMessage, res:
|
||||
if (req.method === 'POST') {
|
||||
const transport = new StreamableHTTPServerTransport({
|
||||
sessionIdGenerator: () => crypto.randomUUID(),
|
||||
onsessioninitialized: sessionId => {
|
||||
sessions.set(sessionId, transport);
|
||||
onsessioninitialized: async sessionId => {
|
||||
testDebug(`create http session: ${transport.sessionId}`);
|
||||
const connection = await server.createConnection(transport);
|
||||
sessions.set(sessionId, { transport, connection });
|
||||
}
|
||||
});
|
||||
|
||||
transport.onclose = () => {
|
||||
if (transport.sessionId)
|
||||
sessions.delete(transport.sessionId);
|
||||
const result = transport.sessionId ? sessions.get(transport.sessionId) : undefined;
|
||||
if (!result)
|
||||
return;
|
||||
sessions.delete(result.transport.sessionId!);
|
||||
testDebug(`delete http session: ${transport.sessionId}`);
|
||||
result.connection.close().catch(logUnhandledError);
|
||||
};
|
||||
await server.createConnection(transport);
|
||||
|
||||
await transport.handleRequest(req, res);
|
||||
return;
|
||||
}
|
||||
@@ -112,13 +121,13 @@ export async function startHttpServer(config: { host?: string, port?: number }):
|
||||
|
||||
export function startHttpTransport(httpServer: http.Server, mcpServer: Server) {
|
||||
const sseSessions = new Map<string, SSEServerTransport>();
|
||||
const streamableSessions = new Map<string, StreamableHTTPServerTransport>();
|
||||
const streamableSessions = new Map();
|
||||
httpServer.on('request', async (req, res) => {
|
||||
const url = new URL(`http://localhost${req.url}`);
|
||||
if (url.pathname.startsWith('/mcp'))
|
||||
await handleStreamable(mcpServer, req, res, streamableSessions);
|
||||
else
|
||||
if (url.pathname.startsWith('/sse'))
|
||||
await handleSSE(mcpServer, req, res, url, sseSessions);
|
||||
else
|
||||
await handleStreamable(mcpServer, req, res, streamableSessions);
|
||||
});
|
||||
const url = httpAddressToString(httpServer.address());
|
||||
const message = [
|
||||
@@ -127,11 +136,11 @@ export function startHttpTransport(httpServer: http.Server, mcpServer: Server) {
|
||||
JSON.stringify({
|
||||
'mcpServers': {
|
||||
'playwright': {
|
||||
'url': `${url}/sse`
|
||||
'url': `${url}/mcp`
|
||||
}
|
||||
}
|
||||
}, undefined, 2),
|
||||
'If your client supports streamable HTTP, you can use the /mcp endpoint instead.',
|
||||
'For legacy SSE transport support, you can use the /sse endpoint instead.',
|
||||
].join('\n');
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(message);
|
||||
|
||||
@@ -61,3 +61,20 @@ test.describe(() => {
|
||||
})).toContainTextContent(`Firefox`);
|
||||
});
|
||||
});
|
||||
|
||||
test.describe('sandbox configuration', () => {
|
||||
test('should enable sandbox by default (no --no-sandbox flag)', async () => {
|
||||
const { configFromCLIOptions } = await import('../lib/config.js');
|
||||
const config = configFromCLIOptions({ sandbox: undefined });
|
||||
// When --no-sandbox is not passed, chromiumSandbox should not be set to false
|
||||
// This allows the default (true) to be used
|
||||
expect(config.browser?.launchOptions?.chromiumSandbox).toBeUndefined();
|
||||
});
|
||||
|
||||
test('should disable sandbox when --no-sandbox flag is passed', async () => {
|
||||
const { configFromCLIOptions } = await import('../lib/config.js');
|
||||
const config = configFromCLIOptions({ sandbox: false });
|
||||
// When --no-sandbox is passed, chromiumSandbox should be explicitly set to false
|
||||
expect(config.browser?.launchOptions?.chromiumSandbox).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -49,3 +49,23 @@ test('browser_evaluate (element)', async ({ client, server }) => {
|
||||
},
|
||||
})).toContainTextContent(`- Result: "red"`);
|
||||
});
|
||||
|
||||
test('browser_evaluate (error)', async ({ client, server }) => {
|
||||
expect(await client.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
})).toContainTextContent(`- Page Title: Title`);
|
||||
|
||||
const result = await client.callTool({
|
||||
name: 'browser_evaluate',
|
||||
arguments: {
|
||||
function: '() => nonExistentVariable',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content?.[0]?.text).toContain('nonExistentVariable');
|
||||
// Check for common error patterns across browsers
|
||||
const errorText = result.content?.[0]?.text || '';
|
||||
expect(errorText).toMatch(/not defined|Can't find variable/);
|
||||
});
|
||||
|
||||
259
tests/http.spec.ts
Normal file
259
tests/http.spec.ts
Normal file
@@ -0,0 +1,259 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import fs from 'node:fs';
|
||||
import url from 'node:url';
|
||||
|
||||
import { ChildProcess, spawn } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
|
||||
import { test as baseTest, expect } from './fixtures.js';
|
||||
import type { Config } from '../config.d.ts';
|
||||
|
||||
// NOTE: Can be removed when we drop Node.js 18 support and changed to import.meta.filename.
|
||||
const __filename = url.fileURLToPath(import.meta.url);
|
||||
|
||||
const test = baseTest.extend<{ serverEndpoint: (options?: { args?: string[], noPort?: boolean }) => Promise<{ url: URL, stderr: () => string }> }>({
|
||||
serverEndpoint: async ({ mcpHeadless }, use, testInfo) => {
|
||||
let cp: ChildProcess | undefined;
|
||||
const userDataDir = testInfo.outputPath('user-data-dir');
|
||||
await use(async (options?: { args?: string[], noPort?: boolean }) => {
|
||||
if (cp)
|
||||
throw new Error('Process already running');
|
||||
|
||||
cp = spawn('node', [
|
||||
path.join(path.dirname(__filename), '../cli.js'),
|
||||
...(options?.noPort ? [] : ['--port=0']),
|
||||
'--user-data-dir=' + userDataDir,
|
||||
...(mcpHeadless ? ['--headless'] : []),
|
||||
...(options?.args || []),
|
||||
], {
|
||||
stdio: 'pipe',
|
||||
env: {
|
||||
...process.env,
|
||||
DEBUG: 'pw:mcp:test',
|
||||
DEBUG_COLORS: '0',
|
||||
DEBUG_HIDE_DATE: '1',
|
||||
},
|
||||
});
|
||||
let stderr = '';
|
||||
const url = await new Promise<string>(resolve => cp!.stderr?.on('data', data => {
|
||||
stderr += data.toString();
|
||||
const match = stderr.match(/Listening on (http:\/\/.*)/);
|
||||
if (match)
|
||||
resolve(match[1]);
|
||||
}));
|
||||
|
||||
return { url: new URL(url), stderr: () => stderr };
|
||||
});
|
||||
cp?.kill('SIGTERM');
|
||||
},
|
||||
});
|
||||
|
||||
test('http transport', async ({ serverEndpoint }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
const transport = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
});
|
||||
|
||||
test('http transport (config)', async ({ serverEndpoint }) => {
|
||||
const config: Config = {
|
||||
server: {
|
||||
port: 0,
|
||||
}
|
||||
};
|
||||
const configFile = test.info().outputPath('config.json');
|
||||
await fs.promises.writeFile(configFile, JSON.stringify(config, null, 2));
|
||||
|
||||
const { url } = await serverEndpoint({ noPort: true, args: ['--config=' + configFile] });
|
||||
const transport = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
});
|
||||
|
||||
test('http transport browser lifecycle (isolated)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint({ args: ['--isolated'] });
|
||||
|
||||
const transport1 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
/**
|
||||
* src/client/streamableHttp.ts
|
||||
* Clients that no longer need a particular session
|
||||
* (e.g., because the user is leaving the client application) SHOULD send an
|
||||
* HTTP DELETE to the MCP endpoint with the Mcp-Session-Id header to explicitly
|
||||
* terminate the session.
|
||||
*/
|
||||
await transport1.terminateSession();
|
||||
await client1.close();
|
||||
|
||||
const transport2 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
await transport2.terminateSession();
|
||||
await client2.close();
|
||||
|
||||
await expect(async () => {
|
||||
const lines = stderr().split('\n');
|
||||
expect(lines.filter(line => line.match(/create http session/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/delete http session/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/create context/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/close context/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/create browser context \(isolated\)/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/close browser context \(isolated\)/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/obtain browser \(isolated\)/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/close browser \(isolated\)/)).length).toBe(2);
|
||||
}).toPass();
|
||||
});
|
||||
|
||||
test('http transport browser lifecycle (isolated, multiclient)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint({ args: ['--isolated'] });
|
||||
|
||||
const transport1 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
const transport2 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
await transport1.terminateSession();
|
||||
await client1.close();
|
||||
|
||||
const transport3 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client3 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client3.connect(transport3);
|
||||
await client3.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
await transport2.terminateSession();
|
||||
await client2.close();
|
||||
await transport3.terminateSession();
|
||||
await client3.close();
|
||||
|
||||
await expect(async () => {
|
||||
const lines = stderr().split('\n');
|
||||
expect(lines.filter(line => line.match(/create http session/)).length).toBe(3);
|
||||
expect(lines.filter(line => line.match(/delete http session/)).length).toBe(3);
|
||||
|
||||
expect(lines.filter(line => line.match(/create context/)).length).toBe(3);
|
||||
expect(lines.filter(line => line.match(/close context/)).length).toBe(3);
|
||||
|
||||
expect(lines.filter(line => line.match(/create browser context \(isolated\)/)).length).toBe(3);
|
||||
expect(lines.filter(line => line.match(/close browser context \(isolated\)/)).length).toBe(3);
|
||||
|
||||
expect(lines.filter(line => line.match(/obtain browser \(isolated\)/)).length).toBe(1);
|
||||
expect(lines.filter(line => line.match(/close browser \(isolated\)/)).length).toBe(1);
|
||||
}).toPass();
|
||||
});
|
||||
|
||||
test('http transport browser lifecycle (persistent)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint();
|
||||
|
||||
const transport1 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
await transport1.terminateSession();
|
||||
await client1.close();
|
||||
|
||||
const transport2 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
await transport2.terminateSession();
|
||||
await client2.close();
|
||||
|
||||
await expect(async () => {
|
||||
const lines = stderr().split('\n');
|
||||
expect(lines.filter(line => line.match(/create http session/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/delete http session/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/create context/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/close context/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/create browser context \(persistent\)/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/close browser context \(persistent\)/)).length).toBe(2);
|
||||
|
||||
expect(lines.filter(line => line.match(/lock user data dir/)).length).toBe(2);
|
||||
expect(lines.filter(line => line.match(/release user data dir/)).length).toBe(2);
|
||||
}).toPass();
|
||||
});
|
||||
|
||||
test('http transport browser lifecycle (persistent, multiclient)', async ({ serverEndpoint, server }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
|
||||
const transport1 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
const transport2 = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
const response = await client2.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
expect(response.isError).toBe(true);
|
||||
expect(response.content?.[0].text).toContain('use --isolated to run multiple instances of the same browser');
|
||||
|
||||
await client1.close();
|
||||
await client2.close();
|
||||
});
|
||||
|
||||
test('http transport (default)', async ({ serverEndpoint }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
const transport = new StreamableHTTPClientTransport(url);
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
expect(transport.sessionId, 'has session support').toBeDefined();
|
||||
});
|
||||
@@ -201,3 +201,52 @@ test('browser_take_screenshot (imageResponses=omit)', async ({ startClient, serv
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('browser_take_screenshot (fullPage: true)', async ({ startClient, server }, testInfo) => {
|
||||
const { client } = await startClient({
|
||||
config: { outputDir: testInfo.outputPath('output') },
|
||||
});
|
||||
expect(await client.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
})).toContainTextContent(`Navigate to http://localhost`);
|
||||
|
||||
expect(await client.callTool({
|
||||
name: 'browser_take_screenshot',
|
||||
arguments: { fullPage: true },
|
||||
})).toEqual({
|
||||
content: [
|
||||
{
|
||||
data: expect.any(String),
|
||||
mimeType: 'image/jpeg',
|
||||
type: 'image',
|
||||
},
|
||||
{
|
||||
text: expect.stringContaining(`Screenshot full page and save it as`),
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
test('browser_take_screenshot (fullPage with element should error)', async ({ startClient, server }, testInfo) => {
|
||||
const { client } = await startClient({
|
||||
config: { outputDir: testInfo.outputPath('output') },
|
||||
});
|
||||
expect(await client.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
})).toContainTextContent(`[ref=e1]`);
|
||||
|
||||
const result = await client.callTool({
|
||||
name: 'browser_take_screenshot',
|
||||
arguments: {
|
||||
fullPage: true,
|
||||
element: 'hello button',
|
||||
ref: 'e1',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.isError).toBe(true);
|
||||
expect(result.content?.[0]?.text).toContain('fullPage cannot be used with element screenshots');
|
||||
});
|
||||
|
||||
78
tests/session.spec.ts
Normal file
78
tests/session.spec.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { test, expect } from './fixtures.js';
|
||||
|
||||
test('check that session is saved', async ({ startClient, server, mcpMode }, testInfo) => {
|
||||
const outputDir = testInfo.outputPath('output');
|
||||
|
||||
const { client } = await startClient({
|
||||
args: ['--save-session', `--output-dir=${outputDir}`],
|
||||
});
|
||||
|
||||
expect(await client.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
})).toContainTextContent(`Navigate to http://localhost`);
|
||||
|
||||
// Check that session file exists
|
||||
const files = fs.readdirSync(outputDir);
|
||||
const sessionFiles = files.filter(f => f.startsWith('session') && f.endsWith('.yml'));
|
||||
expect(sessionFiles.length).toBe(1);
|
||||
|
||||
// Check session file content
|
||||
const sessionContent = fs.readFileSync(path.join(outputDir, sessionFiles[0]), 'utf8');
|
||||
expect(sessionContent).toContain('- browser_navigate:');
|
||||
expect(sessionContent).toContain('params:');
|
||||
expect(sessionContent).toContain('url: ' + server.HELLO_WORLD);
|
||||
expect(sessionContent).toContain('snapshot:');
|
||||
});
|
||||
|
||||
test('check that session includes multiple tool calls', async ({ startClient, server, mcpMode }, testInfo) => {
|
||||
const outputDir = testInfo.outputPath('output');
|
||||
|
||||
const { client } = await startClient({
|
||||
args: ['--save-session', `--output-dir=${outputDir}`],
|
||||
});
|
||||
|
||||
// Navigate to a page
|
||||
await client.callTool({
|
||||
name: 'browser_navigate',
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
// Take a snapshot
|
||||
await client.callTool({
|
||||
name: 'browser_snapshot',
|
||||
arguments: {},
|
||||
});
|
||||
|
||||
// Check that session file exists and contains both calls
|
||||
const files = fs.readdirSync(outputDir);
|
||||
const sessionFiles = files.filter(f => f.startsWith('session') && f.endsWith('.yml'));
|
||||
expect(sessionFiles.length).toBe(1);
|
||||
|
||||
const sessionContent = fs.readFileSync(path.join(outputDir, sessionFiles[0]), 'utf8');
|
||||
expect(sessionContent).toContain('- browser_navigate:');
|
||||
expect(sessionContent).toContain('- browser_snapshot:');
|
||||
|
||||
// Check that snapshot files exist
|
||||
const snapshotFiles = files.filter(f => f.includes('snapshot.yaml'));
|
||||
expect(snapshotFiles.length).toBeGreaterThan(0);
|
||||
});
|
||||
@@ -20,7 +20,6 @@ import url from 'node:url';
|
||||
import { ChildProcess, spawn } from 'node:child_process';
|
||||
import path from 'node:path';
|
||||
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
||||
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
||||
|
||||
import { test as baseTest, expect } from './fixtures.js';
|
||||
@@ -68,7 +67,7 @@ const test = baseTest.extend<{ serverEndpoint: (options?: { args?: string[], noP
|
||||
|
||||
test('sse transport', async ({ serverEndpoint }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
const transport = new SSEClientTransport(url);
|
||||
const transport = new SSEClientTransport(new URL('/sse', url));
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
@@ -84,7 +83,7 @@ test('sse transport (config)', async ({ serverEndpoint }) => {
|
||||
await fs.promises.writeFile(configFile, JSON.stringify(config, null, 2));
|
||||
|
||||
const { url } = await serverEndpoint({ noPort: true, args: ['--config=' + configFile] });
|
||||
const transport = new SSEClientTransport(url);
|
||||
const transport = new SSEClientTransport(new URL('/sse', url));
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
@@ -93,7 +92,7 @@ test('sse transport (config)', async ({ serverEndpoint }) => {
|
||||
test('sse transport browser lifecycle (isolated)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint({ args: ['--isolated'] });
|
||||
|
||||
const transport1 = new SSEClientTransport(url);
|
||||
const transport1 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
@@ -102,7 +101,7 @@ test('sse transport browser lifecycle (isolated)', async ({ serverEndpoint, serv
|
||||
});
|
||||
await client1.close();
|
||||
|
||||
const transport2 = new SSEClientTransport(url);
|
||||
const transport2 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
@@ -130,7 +129,7 @@ test('sse transport browser lifecycle (isolated)', async ({ serverEndpoint, serv
|
||||
test('sse transport browser lifecycle (isolated, multiclient)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint({ args: ['--isolated'] });
|
||||
|
||||
const transport1 = new SSEClientTransport(url);
|
||||
const transport1 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
@@ -138,7 +137,7 @@ test('sse transport browser lifecycle (isolated, multiclient)', async ({ serverE
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
const transport2 = new SSEClientTransport(url);
|
||||
const transport2 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
@@ -147,7 +146,7 @@ test('sse transport browser lifecycle (isolated, multiclient)', async ({ serverE
|
||||
});
|
||||
await client1.close();
|
||||
|
||||
const transport3 = new SSEClientTransport(url);
|
||||
const transport3 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client3 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client3.connect(transport3);
|
||||
await client3.callTool({
|
||||
@@ -177,7 +176,7 @@ test('sse transport browser lifecycle (isolated, multiclient)', async ({ serverE
|
||||
test('sse transport browser lifecycle (persistent)', async ({ serverEndpoint, server }) => {
|
||||
const { url, stderr } = await serverEndpoint();
|
||||
|
||||
const transport1 = new SSEClientTransport(url);
|
||||
const transport1 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
@@ -186,7 +185,7 @@ test('sse transport browser lifecycle (persistent)', async ({ serverEndpoint, se
|
||||
});
|
||||
await client1.close();
|
||||
|
||||
const transport2 = new SSEClientTransport(url);
|
||||
const transport2 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
await client2.callTool({
|
||||
@@ -214,7 +213,7 @@ test('sse transport browser lifecycle (persistent)', async ({ serverEndpoint, se
|
||||
test('sse transport browser lifecycle (persistent, multiclient)', async ({ serverEndpoint, server }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
|
||||
const transport1 = new SSEClientTransport(url);
|
||||
const transport1 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client1 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client1.connect(transport1);
|
||||
await client1.callTool({
|
||||
@@ -222,7 +221,7 @@ test('sse transport browser lifecycle (persistent, multiclient)', async ({ serve
|
||||
arguments: { url: server.HELLO_WORLD },
|
||||
});
|
||||
|
||||
const transport2 = new SSEClientTransport(url);
|
||||
const transport2 = new SSEClientTransport(new URL('/sse', url));
|
||||
const client2 = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client2.connect(transport2);
|
||||
const response = await client2.callTool({
|
||||
@@ -235,12 +234,3 @@ test('sse transport browser lifecycle (persistent, multiclient)', async ({ serve
|
||||
await client1.close();
|
||||
await client2.close();
|
||||
});
|
||||
|
||||
test('streamable http transport', async ({ serverEndpoint }) => {
|
||||
const { url } = await serverEndpoint();
|
||||
const transport = new StreamableHTTPClientTransport(new URL('/mcp', url));
|
||||
const client = new Client({ name: 'test', version: '1.0.0' });
|
||||
await client.connect(transport);
|
||||
await client.ping();
|
||||
expect(transport.sessionId, 'has session support').toBeDefined();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user