Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/browserServerBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ export class BrowserServerBackend implements ServerBackend {
const context = this._context!;
const response = new Response(context, schema.name, parsedArguments);
const tool = this._tools.find(tool => tool.schema.name === schema.name)!;
await context.setInputRecorderEnabled(false);
context.setRunningTool(true);
try {
await tool.handle(context, parsedArguments, response);
} catch (error) {
await response.finish();
this._sessionLog?.logResponse(response);
} catch (error: any) {
response.addError(String(error));
} finally {
await context.setInputRecorderEnabled(true);
context.setRunningTool(false);
}
await this._sessionLog?.logResponse(response);
return await response.serialize();
return response.serialize();
}

serverInitialized(version: mcpServer.ClientVersion | undefined) {
Expand Down
118 changes: 33 additions & 85 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ import type { Tool } from './tools/tool.js';
import type { FullConfig } from './config.js';
import type { BrowserContextFactory } from './browserContextFactory.js';
import type * as actions from './actions.js';
import type { Action, SessionLog } from './sessionLog.js';
import type { SessionLog } from './sessionLog.js';

const testDebug = debug('pw:mcp:test');

export class Context {
readonly tools: Tool[];
readonly config: FullConfig;
readonly sessionLog: SessionLog | undefined;
private _browserContextPromise: Promise<{ browserContext: playwright.BrowserContext, close: () => Promise<void> }> | undefined;
private _browserContextFactory: BrowserContextFactory;
private _tabs: Tab[] = [];
Expand All @@ -40,14 +41,13 @@ export class Context {

private static _allContexts: Set<Context> = new Set();
private _closeBrowserContextPromise: Promise<void> | undefined;
private _inputRecorder: InputRecorder | undefined;
private _sessionLog: SessionLog | undefined;
private _isRunningTool: boolean = false;

constructor(tools: Tool[], config: FullConfig, browserContextFactory: BrowserContextFactory, sessionLog: SessionLog | undefined) {
this.tools = tools;
this.config = config;
this._browserContextFactory = browserContextFactory;
this._sessionLog = sessionLog;
this.sessionLog = sessionLog;
testDebug('create context');
Context._allContexts.add(this);
}
Expand Down Expand Up @@ -93,29 +93,6 @@ export class Context {
return this._currentTab!;
}

async listTabsMarkdown(force: boolean = false): Promise<string[]> {
if (this._tabs.length === 1 && !force)
return [];

if (!this._tabs.length) {
return [
'### Open tabs',
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
'',
];
}

const lines: string[] = ['### Open tabs'];
for (let i = 0; i < this._tabs.length; i++) {
const tab = this._tabs[i];
const title = await tab.title();
const url = tab.page.url();
const current = tab === this._currentTab ? ' (current)' : '';
lines.push(`- ${i}:${current} [${title}] (${url})`);
}
lines.push('');
return lines;
}

async closeTab(index: number | undefined): Promise<string> {
const tab = index === undefined ? this._currentTab : this._tabs[index];
Expand Down Expand Up @@ -152,8 +129,12 @@ export class Context {
this._closeBrowserContextPromise = undefined;
}

async setInputRecorderEnabled(enabled: boolean) {
await this._inputRecorder?.setEnabled(enabled);
isRunningTool() {
return this._isRunningTool;
}

setRunningTool(isRunningTool: boolean) {
this._isRunningTool = isRunningTool;
}

private async _closeBrowserContextImpl() {
Expand Down Expand Up @@ -208,8 +189,8 @@ export class Context {
const result = await this._browserContextFactory.createContext(this.clientVersion!);
const { browserContext } = result;
await this._setupRequestInterception(browserContext);
if (this._sessionLog)
this._inputRecorder = await InputRecorder.create(this._sessionLog, browserContext);
if (this.sessionLog)
await InputRecorder.create(this, browserContext);
for (const page of browserContext.pages())
this._onPageCreated(page);
browserContext.on('page', page => this._onPageCreated(page));
Expand All @@ -226,87 +207,54 @@ export class Context {
}

export class InputRecorder {
private _actions: Action[] = [];
private _enabled = false;
private _sessionLog: SessionLog;
private _context: Context;
private _browserContext: playwright.BrowserContext;
private _flushTimer: NodeJS.Timeout | undefined;

private constructor(sessionLog: SessionLog, browserContext: playwright.BrowserContext) {
this._sessionLog = sessionLog;
private constructor(context: Context, browserContext: playwright.BrowserContext) {
this._context = context;
this._browserContext = browserContext;
}

static async create(sessionLog: SessionLog, browserContext: playwright.BrowserContext) {
const recorder = new InputRecorder(sessionLog, browserContext);
static async create(context: Context, browserContext: playwright.BrowserContext) {
const recorder = new InputRecorder(context, browserContext);
await recorder._initialize();
await recorder.setEnabled(true);
return recorder;
}

private async _initialize() {
const sessionLog = this._context.sessionLog!;
await (this._browserContext as any)._enableRecorder({
mode: 'recording',
recorderMode: 'api',
}, {
actionAdded: (page: playwright.Page, data: actions.ActionInContext, code: string) => {
if (!this._enabled)
if (this._context.isRunningTool())
return;
const tab = Tab.forPage(page);
this._actions.push({ ...data, tab, code: code.trim(), timestamp: performance.now() });
this._scheduleFlush();
if (tab)
sessionLog.logUserAction(data.action, tab, code, false);
},
actionUpdated: (page: playwright.Page, data: actions.ActionInContext, code: string) => {
if (!this._enabled)
if (this._context.isRunningTool())
return;
const tab = Tab.forPage(page);
this._actions[this._actions.length - 1] = { ...data, tab, code: code.trim(), timestamp: performance.now() };
this._scheduleFlush();
if (tab)
sessionLog.logUserAction(data.action, tab, code, true);
},
signalAdded: (page: playwright.Page, data: actions.SignalInContext) => {
if (this._context.isRunningTool())
return;
if (data.signal.name !== 'navigation')
return;
const tab = Tab.forPage(page);
this._actions.push({
frame: data.frame,
action: {
name: 'navigate',
url: data.signal.url,
signals: [],
},
startTime: data.timestamp,
endTime: data.timestamp,
tab,
code: `await page.goto('${data.signal.url}');`,
timestamp: performance.now(),
});
this._scheduleFlush();
const navigateAction: actions.Action = {
name: 'navigate',
url: data.signal.url,
signals: [],
};
if (tab)
sessionLog.logUserAction(navigateAction, tab, `await page.goto('${data.signal.url}');`, false);
},
});
}

async setEnabled(enabled: boolean) {
this._enabled = enabled;
if (!enabled)
await this._flush();
}

private _clearTimer() {
if (this._flushTimer) {
clearTimeout(this._flushTimer);
this._flushTimer = undefined;
}
}

private _scheduleFlush() {
this._clearTimer();
this._flushTimer = setTimeout(() => this._flush(), 1000);
}

private async _flush() {
this._clearTimer();
const actions = this._actions;
this._actions = [];
await this._sessionLog.logActions(actions);
}
}
58 changes: 40 additions & 18 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

import { renderModalStates } from './tab.js';

import type { TabSnapshot } from './tab.js';
import type { ModalState } from './tools/tool.js';
import type { Tab, TabSnapshot } from './tab.js';
import type { ImageContent, TextContent } from '@modelcontextprotocol/sdk/types.js';
import type { Context } from './context.js';

Expand All @@ -28,7 +27,7 @@ export class Response {
private _context: Context;
private _includeSnapshot = false;
private _includeTabs = false;
private _snapshot: { tabSnapshot?: TabSnapshot, modalState?: ModalState } | undefined;
private _tabSnapshot: TabSnapshot | undefined;

readonly toolName: string;
readonly toolArgs: Record<string, any>;
Expand Down Expand Up @@ -81,17 +80,20 @@ export class Response {
this._includeTabs = true;
}

async snapshot(): Promise<{ tabSnapshot?: TabSnapshot, modalState?: ModalState }> {
if (this._snapshot)
return this._snapshot;
async finish() {
// All the async snapshotting post-action is happening here.
// Everything below should race against modal states.
if (this._includeSnapshot && this._context.currentTab())
this._snapshot = await this._context.currentTabOrDie().captureSnapshot();
else
this._snapshot = {};
return this._snapshot;
this._tabSnapshot = await this._context.currentTabOrDie().captureSnapshot();
for (const tab of this._context.tabs())
await tab.updateTitle();
}

async serialize(): Promise<{ content: (TextContent | ImageContent)[], isError?: boolean }> {
tabSnapshot(): TabSnapshot | undefined {
return this._tabSnapshot;
}

serialize(): { content: (TextContent | ImageContent)[], isError?: boolean } {
const response: string[] = [];

// Start with command result.
Expand All @@ -112,16 +114,14 @@ ${this._code.join('\n')}

// List browser tabs.
if (this._includeSnapshot || this._includeTabs)
response.push(...(await this._context.listTabsMarkdown(this._includeTabs)));
response.push(...renderTabsMarkdown(this._context.tabs(), this._includeTabs));

// Add snapshot if provided.
const snapshot = await this.snapshot();
if (snapshot?.modalState) {
response.push(...renderModalStates(this._context, [snapshot.modalState]));
if (this._tabSnapshot?.modalStates.length) {
response.push(...renderModalStates(this._context, this._tabSnapshot.modalStates));
response.push('');
}
if (snapshot?.tabSnapshot) {
response.push(renderTabSnapshot(snapshot.tabSnapshot));
} else if (this._tabSnapshot) {
response.push(renderTabSnapshot(this._tabSnapshot));
response.push('');
}

Expand Down Expand Up @@ -172,6 +172,28 @@ function renderTabSnapshot(tabSnapshot: TabSnapshot): string {
return lines.join('\n');
}

function renderTabsMarkdown(tabs: Tab[], force: boolean = false): string[] {
if (tabs.length === 1 && !force)
return [];

if (!tabs.length) {
return [
'### Open tabs',
'No open tabs. Use the "browser_navigate" tool to navigate to a page first.',
'',
];
}

const lines: string[] = ['### Open tabs'];
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
const current = tab.isCurrentTab() ? ' (current)' : '';
lines.push(`- ${i}:${current} [${tab.lastTitle()}] (${tab.page.url()})`);
}
lines.push('');
return lines;
}

function trim(text: string, maxLength: number) {
if (text.length <= maxLength)
return text;
Expand Down
Loading
Loading