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
18 changes: 13 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
"ws": "^8.17.1",
"xml2js": "^0.5.0",
"yaml": "2.6.0",
"zod": "^3.25.76",
"zod-to-json-schema": "^3.25.1"
"zod": "^4.3.5"
}
}
8 changes: 5 additions & 3 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelector
};

// @ts-ignore this will be any if zod is not installed
type ZodTypeAny = import('zod').ZodTypeAny;
import { ZodTypeAny, z } from 'zod';
// @ts-ignore this will be any if zod is not installed
type ZodInfer<T extends ZodTypeAny> = import('zod').infer<T>;
import * as z3 from 'zod/v3';
type ZodSchema = ZodTypeAny | z3.ZodTypeAny;
type InferZodSchema<T extends ZodSchema> = T extends z3.ZodTypeAny ? z3.infer<T> : T extends ZodTypeAny ? z.infer<T> : never;

/**
* Page provides methods to interact with a single tab in a [Browser](https://playwright.dev/docs/api/class-browser),
Expand Down Expand Up @@ -5305,7 +5307,7 @@ export interface PageAgent {
* @param schema
* @param options
*/
extract<Schema extends ZodTypeAny>(query: string, schema: Schema): Promise<{ result: ZodInfer<Schema>, usage: { turns: number, inputTokens: number, outputTokens: number } }>;
extract<Schema extends ZodSchema>(query: string, schema: Schema): Promise<{ result: InferZodSchema<Schema>, usage: { turns: number, inputTokens: number, outputTokens: number } }>;
/**
* Emitted when the agent makes a turn.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright-core/ThirdPartyNotices.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ This project incorporates components from the projects listed below. The origina
- yauzl@3.2.0 (https://github.com/thejoshwolfe/yauzl)
- yazl@2.5.1 (https://github.com/thejoshwolfe/yazl)
- zod-to-json-schema@3.25.1 (https://github.com/StefanTerdell/zod-to-json-schema)
- zod@3.25.76 (https://github.com/colinhacks/zod)
- zod@4.3.5 (https://github.com/colinhacks/zod)

%% @hono/node-server@1.19.8 NOTICES AND INFORMATION BEGIN HERE
=========================================
Expand Down Expand Up @@ -4043,7 +4043,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
=========================================
END OF zod-to-json-schema@3.25.1 AND INFORMATION

%% zod@3.25.76 NOTICES AND INFORMATION BEGIN HERE
%% zod@4.3.5 NOTICES AND INFORMATION BEGIN HERE
=========================================
MIT License

Expand All @@ -4067,7 +4067,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
=========================================
END OF zod@3.25.76 AND INFORMATION
END OF zod@4.3.5 AND INFORMATION

SUMMARY BEGIN HERE
=========================================
Expand Down
9 changes: 4 additions & 5 deletions packages/playwright-core/bundles/mcp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/playwright-core/bundles/mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"dependencies": {
"@lowire/loop": "^0.0.23",
"@modelcontextprotocol/sdk": "^1.25.2",
"zod": "^3.25.76",
"zod": "^4.3.5",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please measure the bundle compilation time and the output size (e.g. ls -lh packages/playwright-core/lib/mcpBundleImpl/index.js) with old/new version. Rolling sdk and zod without any hack for sdk would leave in a bad state (see modelcontextprotocol/typescript-sdk#1313), let's make sure this doesn't regress because of the zod.

Copy link
Member Author

@Skn0tt Skn0tt Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old

886k

==== Running esbuild: packages/playwright-core/bundles/mcp/src/mcpBundleImpl.ts

Package contribution to bundle:
zod                            119.9 KB  29.35%
ajv                            102.0 KB  24.97%
@modelcontextprotocol/sdk      97.1 KB  23.77%
@lowire/loop                   23.5 KB  5.75%
zod-to-json-schema             20.3 KB  4.98%
ajv-formats                    9.9 KB  2.42%
fast-uri                       9.3 KB  2.27%
@hono/node-server              7.7 KB  1.88%
eventsource                    5.2 KB  1.27%
cross-spawn                    2.9 KB  0.70%
content-type                   1.9 KB  0.46%
eventsource-parser             1.8 KB  0.44%
isexe                          1.5 KB  0.37%
which                          1.4 KB  0.35%
json-schema-traverse           1.2 KB  0.30%
bytes                          1.0 KB  0.26%
pkce-challenge                 0.8 KB  0.20%
fast-deep-equal                0.7 KB  0.18%
path-key                       0.2 KB  0.06%
shebang-command                0.2 KB  0.05%
shebang-regex                  0.1 KB  0.01%
==== Done in 114 ms

new

666K

==== Running esbuild: packages/playwright-core/bundles/mcp/src/mcpBundleImpl.ts

Package contribution to bundle:
zod                            374.6 KB  56.46%
ajv                            102.2 KB  15.40%
@modelcontextprotocol/sdk      97.1 KB  14.64%
@lowire/loop                   23.5 KB  3.54%
zod-to-json-schema             20.4 KB  3.07%
ajv-formats                    9.9 KB  1.49%
fast-uri                       9.3 KB  1.40%
@hono/node-server              7.7 KB  1.16%
eventsource                    5.2 KB  0.79%
cross-spawn                    2.9 KB  0.43%
content-type                   1.9 KB  0.28%
eventsource-parser             1.8 KB  0.27%
isexe                          1.5 KB  0.23%
which                          1.4 KB  0.22%
json-schema-traverse           1.2 KB  0.18%
bytes                          1.0 KB  0.16%
pkce-challenge                 0.8 KB  0.12%
fast-deep-equal                0.7 KB  0.11%
path-key                       0.2 KB  0.03%
shebang-command                0.2 KB  0.03%
shebang-regex                  0.1 KB  0.01%
==== Done in 132 ms

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size is shrinking, yay!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did something wrong when measuring. it now shows 411K on main, so the size actually grew

"zod-to-json-schema": "^3.25.1"
}
}
2 changes: 1 addition & 1 deletion packages/playwright-core/bundles/mcp/src/mcpBundleImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/
export { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
export { CallToolRequestSchema, ListRootsRequestSchema, ListToolsRequestSchema, PingRequestSchema, ProgressNotificationSchema } from '@modelcontextprotocol/sdk/types.js';
export { Loop } from '@lowire/loop';
export { z } from 'zod';
export * as z from 'zod';
export { zodToJsonSchema } from 'zod-to-json-schema';
12 changes: 1 addition & 11 deletions packages/playwright-core/bundles/utils/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/playwright-core/bundles/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"signal-exit": "3.0.7",
"socks-proxy-agent": "8.0.5",
"ws": "8.17.1",
"yaml": "^2.6.0",
"zod": "^3.25.76"
"yaml": "^2.6.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
Expand Down
3 changes: 0 additions & 3 deletions packages/playwright-core/bundles/utils/src/utilsBundleImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@ export const wsSender = Sender;

import yamlLibrary from 'yaml';
export const yaml = yamlLibrary;

import zodLibrary from 'zod';
export const zod = zodLibrary;
3 changes: 1 addition & 2 deletions packages/playwright-core/src/client/pageAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Events } from './events';
import { Page } from './page';

import type * as api from '../../types/types';
import type z from 'zod';
import type * as channels from '@protocol/channels';

type PageAgentOptions = {
Expand Down Expand Up @@ -51,7 +50,7 @@ export class PageAgent extends ChannelOwner<channels.PageAgentChannel> implement
return { usage };
}

async extract<Schema extends z.ZodTypeAny>(query: string, schema: Schema, options: PageAgentOptions = {}): Promise<{ result: z.infer<Schema>, usage: channels.AgentUsage }> {
async extract<Schema extends any>(query: string, schema: Schema, options: PageAgentOptions = {}): Promise<{ result: any, usage: channels.AgentUsage }> {
const { result, usage } = await this._channel.extract({ query, schema: this._page._platform.zodToJsonSchema(schema), ...options });
return { result, usage };
}
Expand Down
6 changes: 3 additions & 3 deletions packages/playwright-core/src/server/agent/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import { zod } from '../../utilsBundle';
import type z from 'zod';
import { z as zod } from '../../mcpBundle';
import type * as z from 'zod';

const modifiersSchema = zod.array(
zod.enum(['Alt', 'Control', 'ControlOrMeta', 'Meta', 'Shift'])
Expand Down Expand Up @@ -139,7 +139,7 @@ const actionWithCodeSchema = actionSchema.and(zod.object({
}));
export type ActionWithCode = z.infer<typeof actionWithCodeSchema>;

export const cachedActionsSchema = zod.record(zod.object({
export const cachedActionsSchema = zod.record(zod.string(), zod.object({
actions: zod.array(actionWithCodeSchema),
}));
export type CachedActions = z.infer<typeof cachedActionsSchema>;
4 changes: 2 additions & 2 deletions packages/playwright-core/src/server/agent/pageAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import path from 'path';

import { toolsForLoop } from './tool';
import { debug } from '../../utilsBundle';
import { Loop } from '../../mcpBundle';
import { Loop, z as zod } from '../../mcpBundle';
import { runAction } from './actionRunner';
import { Context } from './context';
import performTools from './performTools';
Expand Down Expand Up @@ -205,7 +205,7 @@ async function cachedActions(cacheFile: string): Promise<Cache> {
const text = await fs.promises.readFile(cacheFile, 'utf-8').catch(() => '{}');
const parsed = actions.cachedActionsSchema.safeParse(JSON.parse(text));
if (parsed.error)
throw new Error(`Failed to parse cache file ${cacheFile}: ${parsed.error.issues.map(issue => issue.message).join(', ')}`);
throw new Error(`Failed to parse cache file ${cacheFile}:\n${zod.prettifyError(parsed.error)}`);
cache = { actions: parsed.data, newActions: {} };
allCaches.set(cacheFile, cache);
}
Expand Down
5 changes: 2 additions & 3 deletions packages/playwright-core/src/server/agent/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { zodToJsonSchema } from '../../mcpBundle';

import { z } from '../../mcpBundle';
import type zod from 'zod';
import type * as loopTypes from '@lowire/loop';
import type { Context } from './context';
Expand All @@ -40,7 +39,7 @@ export function toolsForLoop(progress: Progress, context: Context, toolDefinitio
const result: loopTypes.Tool = {
name: tool.schema.name,
description: tool.schema.description,
inputSchema: zodToJsonSchema(tool.schema.inputSchema) as loopTypes.Schema,
inputSchema: z.toJSONSchema(tool.schema.inputSchema) as loopTypes.Schema,
};
return result;
});
Expand Down
11 changes: 9 additions & 2 deletions packages/playwright-core/src/server/utils/nodePlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import { colors } from '../../utilsBundle';
import { debugLogger } from './debugLogger';
import { currentZone, emptyZone } from './zones';
import { debugMode, isUnderTest } from './debug';
import { zodToJsonSchema } from '../../mcpBundle';
import { zodToJsonSchema as zodToJsonSchemaV3, z } from '../../mcpBundle';
import type zod3 from 'zod/v3';
import type zod4 from 'zod';

import type { Platform, Zone } from '../../client/platform';
import type { Zone as ZoneImpl } from './zones';
Expand Down Expand Up @@ -124,7 +126,12 @@ export const nodePlatform: Platform = {
return new WritableStreamImpl(channel);
},

zodToJsonSchema,
zodToJsonSchema: (schema: zod3.Schema | zod4.Schema): any => {
// https://zod.dev/library-authors?id=how-to-support-zod-3-and-zod-4-simultaneously
if ('_zod' in schema)
return z.toJSONSchema(schema);
return zodToJsonSchemaV3(schema);
},

zones: {
current: () => new NodeZone(currentZone()),
Expand Down
1 change: 0 additions & 1 deletion packages/playwright-core/src/utilsBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const wsReceiver = require('./utilsBundleImpl').wsReceiver;
export const wsSender = require('./utilsBundleImpl').wsSender;
export const yaml: typeof import('../bundles/utils/node_modules/yaml') = require('./utilsBundleImpl').yaml;
export type { Range as YAMLRange, Scalar as YAMLScalar, YAMLError, YAMLMap, YAMLSeq } from '../bundles/utils/node_modules/yaml';
export const zod: typeof import('../bundles/utils/node_modules/zod') = require('./utilsBundleImpl').zod;
export type { Command } from '../bundles/utils/node_modules/commander';
export type { EventEmitter as WebSocketEventEmitter, RawData as WebSocketRawData, WebSocket, WebSocketServer } from '../bundles/utils/node_modules/@types/ws';

Expand Down
8 changes: 5 additions & 3 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelector
};

// @ts-ignore this will be any if zod is not installed
type ZodTypeAny = import('zod').ZodTypeAny;
import { ZodTypeAny, z } from 'zod';
// @ts-ignore this will be any if zod is not installed
type ZodInfer<T extends ZodTypeAny> = import('zod').infer<T>;
import * as z3 from 'zod/v3';
type ZodSchema = ZodTypeAny | z3.ZodTypeAny;
type InferZodSchema<T extends ZodSchema> = T extends z3.ZodTypeAny ? z3.infer<T> : T extends ZodTypeAny ? z.infer<T> : never;

/**
* Page provides methods to interact with a single tab in a [Browser](https://playwright.dev/docs/api/class-browser),
Expand Down Expand Up @@ -5305,7 +5307,7 @@ export interface PageAgent {
* @param schema
* @param options
*/
extract<Schema extends ZodTypeAny>(query: string, schema: Schema): Promise<{ result: ZodInfer<Schema>, usage: { turns: number, inputTokens: number, outputTokens: number } }>;
extract<Schema extends ZodSchema>(query: string, schema: Schema): Promise<{ result: InferZodSchema<Schema>, usage: { turns: number, inputTokens: number, outputTokens: number } }>;
/**
* Emitted when the agent makes a turn.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/common/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { zod } from 'playwright-core/lib/utilsBundle';
import { z as zod } from 'playwright-core/lib/mcpBundle';

import type { TestAnnotation, TestDetailsAnnotation } from '../../types/test';
import type { Location } from '../../types/testReporter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class BrowserServerBackend implements ServerBackend {
const tool = this._tools.find(tool => tool.schema.name === name)!;
if (!tool)
throw new Error(`Tool "${name}" not found`);
const parsedArguments = tool.schema.inputSchema.parse(rawArguments || {});
const parsedArguments = tool.schema.inputSchema.parse(rawArguments || {}) as any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this used to be any, now it's unknown. we're casting it back so we can use it

const context = this._context!;
const response = new Response(context, name, parsedArguments);
response.logBegin();
Expand Down
5 changes: 2 additions & 3 deletions packages/playwright/src/mcp/sdk/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
* limitations under the License.
*/

import { zodToJsonSchema } from 'playwright-core/lib/mcpBundle';

import { z as zod } from 'playwright-core/lib/mcpBundle';
import type { z } from 'zod';
import type * as mcpServer from './server';

Expand All @@ -32,7 +31,7 @@ export function toMcpTool(tool: ToolSchema<any>): mcpServer.Tool {
return {
name: tool.name,
description: tool.description,
inputSchema: zodToJsonSchema(tool.inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'],
inputSchema: zod.toJSONSchema(tool.inputSchema) as mcpServer.Tool['inputSchema'],
annotations: {
title: tool.title,
readOnlyHint: readOnly,
Expand Down
Loading
Loading