Skip to content

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

fix(mcp): retry list roots request with streaming http (#37324)

Yury Semikhatsky44327df00d45
diff --git a/packages/playwright/src/mcp/sdk/server.ts b/packages/playwright/src/mcp/sdk/server.ts
index bf80d80494..a50f2d80f5 100644
--- a/packages/playwright/src/mcp/sdk/server.ts
+++ b/packages/playwright/src/mcp/sdk/server.ts
@@ -95,8 +95,17 @@ export function createServer(name: string, version: string, backend: ServerBacke
const capabilities = server.getClientCapabilities();
let clientRoots: Root[] = [];
if (capabilities?.roots) {
- const { roots } = await server.listRoots(undefined, { timeout: 2_000 }).catch(() => ({ roots: [] }));
- clientRoots = roots;
+ for (let i = 0; i < 2; i++) {
+ try {
+ // In the @modelcontextprotocol TypeScript SDK (and Cursor) in the streaming http
+ // mode, the SSE channel is not ready yet, when `initialized` notification arrives,
+ // `listRoots` times out in that case and we retry once.
+ const { roots } = await server.listRoots(undefined, { timeout: 2_000 });
+ clientRoots = roots;
+ } catch (e) {
+ continue;
+ }
+ }
}
const clientVersion = server.getClientVersion() ?? { name: 'unknown', version: 'unknown' };
await backend.initialize?.(server, clientVersion, clientRoots);
diff --git a/tests/mcp/http.spec.ts b/tests/mcp/http.spec.ts
index 69cc63d41c..83ed6b86f5 100644
--- a/tests/mcp/http.spec.ts
+++ b/tests/mcp/http.spec.ts
@@ -22,6 +22,7 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { test as baseTest, expect, programPath } from './fixtures';
import type { Config } from '../../packages/playwright/src/mcp/config';
+import { ListRootsRequestSchema } from 'packages/playwright/lib/mcp/sdk/bundle';
const test = baseTest.extend<{ serverEndpoint: (options?: { args?: string[], noPort?: boolean }) => Promise<{ url: URL, stderr: () => string }> }>({
serverEndpoint: async ({ mcpHeadless }, use, testInfo) => {
@@ -252,3 +253,28 @@ test('http transport (default)', async ({ serverEndpoint }) => {
await client.ping();
expect(transport.sessionId, 'has session support').toBeDefined();
});
+
+test('client should receive list roots request', async ({ serverEndpoint }) => {
+ const { url } = await serverEndpoint();
+ const transport = new StreamableHTTPClientTransport(url);
+ const client = new Client({ name: 'test', version: '1.0.0' }, { capabilities: { roots: {} } });
+ let rootsListedCallback;
+ const rootsListedPromise = new Promise((resolve, reject) => {
+ rootsListedCallback = resolve;
+ setTimeout(() => reject(new Error('timeout waiting for ListRootsRequestSchema')), 5_000);
+ });
+ client.setRequestHandler(ListRootsRequestSchema, async request => {
+ rootsListedCallback('success');
+ return {
+ roots: [
+ {
+ name: 'test',
+ uri: 'file://tmp/',
+ }
+ ],
+ };
+ });
+ await client.connect(transport);
+ await client.ping();
+ expect(await rootsListedPromise).toBe('success');
+});