Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

chore(mcp): relative file paths (#38857)

Yury Semikhatskyc44b9be7efb2
diff --git a/packages/playwright/src/mcp/browser/response.ts b/packages/playwright/src/mcp/browser/response.ts
index 095a3c1394..81435ea342 100644
--- a/packages/playwright/src/mcp/browser/response.ts
+++ b/packages/playwright/src/mcp/browser/response.ts
@@ -14,8 +14,11 @@
* limitations under the License.
*/
+import path from 'path';
+
import { debug } from 'playwright-core/lib/utilsBundle';
import { renderModalStates } from './tab';
+import { firstRootPath } from '../sdk/server';
import type { Tab, TabSnapshot } from './tab';
import type { CallToolResult, ImageContent, TextContent } from '@modelcontextprotocol/sdk/types.js';
@@ -161,14 +164,23 @@ export class Response {
if (this._files.length) {
const lines: string[] = [];
- for (const file of this._files)
- lines.push(`- [${file.title}](${file.fileName})`);
+ for (const file of this._files) {
+ const filePath = this._pathRelativeToRoot(file.fileName);
+ lines.push(`- [${file.title}](${filePath})`);
+ }
renderedResponse.updates.push({ category: 'files', content: lines.join('\n') });
}
return this._context.config.secrets ? renderedResponse.redact(this._context.config.secrets) : renderedResponse;
}
+ private _pathRelativeToRoot(filePath: string): string {
+ const root = firstRootPath(this._context.options.clientInfo);
+ if (!root)
+ return filePath;
+ return path.relative(root, filePath);
+ }
+
serialize(options: { _meta?: Record<string, any> } = {}): { content: (TextContent | ImageContent)[], isError?: boolean, _meta?: Record<string, any> } {
const renderedResponse = this.render();
const includeMeta = options._meta && 'dev.lowire/history' in options._meta && 'dev.lowire/state' in options._meta;
diff --git a/tests/mcp/roots.spec.ts b/tests/mcp/roots.spec.ts
index 3a3615a113..13de9263c6 100644
--- a/tests/mcp/roots.spec.ts
+++ b/tests/mcp/roots.spec.ts
@@ -126,3 +126,31 @@ test('should tolerate WSL client roots on Windows server', async ({ startClient,
function createHash(data: string): string {
return crypto.createHash('sha256').update(data).digest('hex').slice(0, 7);
}
+
+test('should return relative paths when root is specified', async ({ startClient, server }, testInfo) => {
+ const rootPath = testInfo.outputPath('workspace');
+ await fs.promises.mkdir(rootPath, { recursive: true });
+
+ const { client } = await startClient({
+ clientName: 'test-client',
+ config: { outputDir: path.join(rootPath, 'output') },
+ roots: [
+ {
+ name: 'workspace',
+ uri: pathToFileURL(rootPath).toString(),
+ },
+ ],
+ });
+
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.HELLO_WORLD },
+ });
+
+ expect(await client.callTool({
+ name: 'browser_take_screenshot',
+ arguments: { filename: 'screenshot.png' },
+ })).toHaveResponse({
+ files: expect.stringContaining(`- [Screenshot of viewport](output${path.sep}screenshot.png)`),
+ });
+});