playwright-mcp/src/tools/keyboard.ts

92 lines
3.1 KiB
TypeScript
Raw Normal View History

/**
* 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 { z } from 'zod';
import { defineTabTool } from './tool.js';
import { elementSchema } from './snapshot.js';
import { generateLocator } from './utils.js';
import * as javascript from '../javascript.js';
const pressKey = defineTabTool({
capability: 'core',
schema: {
name: 'browser_press_key',
title: 'Press a key',
feat: add session-configurable snapshot settings via browser_configure_snapshots Implements dynamic snapshot configuration that MCP clients can control during sessions without requiring server restarts or CLI changes. New tool: browser_configure_snapshots - Configure includeSnapshots, maxSnapshotTokens, differentialSnapshots at runtime - Changes take effect immediately for subsequent tool calls - Shows current settings when called with no parameters - Provides helpful tips and usage guidance Key improvements: 1. **Runtime Configuration**: Update snapshot behavior during active sessions 2. **Client Control**: MCP clients can adapt to different workflows dynamically 3. **Immediate Effect**: No server restart required - changes apply instantly 4. **State Tracking**: Context maintains current session configuration 5. **User Friendly**: Clear feedback on current settings and changes Updated tool descriptions: - All interactive tools now mention "configurable via browser_configure_snapshots" - Removed references to CLI-only configuration - Enhanced browser_snapshot description for explicit snapshots Benefits for users: 🔄 Dynamic configuration without restarts 🎛️ Client-controlled snapshot behavior 📊 View current settings anytime ⚡ Instant configuration changes 🎯 Adapt settings per workflow/task Example usage: ```json { "includeSnapshots": false, "maxSnapshotTokens": 25000, "differentialSnapshots": true } ``` This transforms snapshot configuration from static CLI options into a flexible session management system that adapts to client needs in real-time. Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-22 08:28:36 -06:00
description: 'Press a key on the keyboard. Returns page snapshot after keypress (configurable via browser_configure_snapshots).',
inputSchema: z.object({
key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'),
}),
type: 'destructive',
},
2025-07-22 16:36:21 -07:00
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
response.addCode(`// Press ${params.key}`);
response.addCode(`await page.keyboard.press('${params.key}');`);
await tab.waitForCompletion(async () => {
2025-07-22 16:36:21 -07:00
await tab.page.keyboard.press(params.key);
});
},
});
const typeSchema = elementSchema.extend({
text: z.string().describe('Text to type into the element'),
submit: z.boolean().optional().describe('Whether to submit entered text (press Enter after)'),
slowly: z.boolean().optional().describe('Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.'),
});
const type = defineTabTool({
capability: 'core',
schema: {
name: 'browser_type',
title: 'Type text',
feat: add session-configurable snapshot settings via browser_configure_snapshots Implements dynamic snapshot configuration that MCP clients can control during sessions without requiring server restarts or CLI changes. New tool: browser_configure_snapshots - Configure includeSnapshots, maxSnapshotTokens, differentialSnapshots at runtime - Changes take effect immediately for subsequent tool calls - Shows current settings when called with no parameters - Provides helpful tips and usage guidance Key improvements: 1. **Runtime Configuration**: Update snapshot behavior during active sessions 2. **Client Control**: MCP clients can adapt to different workflows dynamically 3. **Immediate Effect**: No server restart required - changes apply instantly 4. **State Tracking**: Context maintains current session configuration 5. **User Friendly**: Clear feedback on current settings and changes Updated tool descriptions: - All interactive tools now mention "configurable via browser_configure_snapshots" - Removed references to CLI-only configuration - Enhanced browser_snapshot description for explicit snapshots Benefits for users: 🔄 Dynamic configuration without restarts 🎛️ Client-controlled snapshot behavior 📊 View current settings anytime ⚡ Instant configuration changes 🎯 Adapt settings per workflow/task Example usage: ```json { "includeSnapshots": false, "maxSnapshotTokens": 25000, "differentialSnapshots": true } ``` This transforms snapshot configuration from static CLI options into a flexible session management system that adapts to client needs in real-time. Co-Authored-By: Claude <noreply@anthropic.com>
2025-08-22 08:28:36 -06:00
description: 'Type text into editable element. Returns page snapshot after typing (configurable via browser_configure_snapshots).',
inputSchema: typeSchema,
type: 'destructive',
},
2025-07-22 16:36:21 -07:00
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
2025-07-22 16:36:21 -07:00
const locator = await tab.refLocator(params);
await tab.waitForCompletion(async () => {
2025-07-22 16:36:21 -07:00
if (params.slowly) {
response.addCode(`// Press "${params.text}" sequentially into "${params.element}"`);
response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
await locator.pressSequentially(params.text);
} else {
response.addCode(`// Fill "${params.text}" into "${params.element}"`);
response.addCode(`await page.${await generateLocator(locator)}.fill(${javascript.quote(params.text)});`);
await locator.fill(params.text);
}
2025-07-22 16:36:21 -07:00
if (params.submit) {
response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
await locator.press('Enter');
}
});
},
});
export default [
pressKey,
type,
];