playwright-mcp/src/tools/navigate.ts

89 lines
2.4 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 { defineTool, defineTabTool } from './tool.js';
const navigate = defineTool({
capability: 'core',
schema: {
name: 'browser_navigate',
title: 'Navigate to a URL',
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: 'Navigate to a URL. Returns page snapshot after navigation (configurable via browser_configure_snapshots).',
inputSchema: z.object({
url: z.string().describe('The URL to navigate to'),
}),
type: 'destructive',
},
2025-07-22 16:36:21 -07:00
handle: async (context, params, response) => {
// Smart recording: Begin action
await context.beginVideoAction('navigate');
const tab = await context.ensureTab();
await tab.navigate(params.url);
// Smart recording: End action (auto-pause in smart mode)
await context.endVideoAction('navigate');
response.setIncludeSnapshot();
2025-07-22 16:36:21 -07:00
response.addCode(`// Navigate to ${params.url}`);
response.addCode(`await page.goto('${params.url}');`);
},
});
const goBack = defineTabTool({
capability: 'core',
schema: {
name: 'browser_navigate_back',
title: 'Go back',
description: 'Go back to the previous page',
inputSchema: z.object({}),
type: 'readOnly',
},
2025-07-22 16:36:21 -07:00
handle: async (tab, params, response) => {
await tab.page.goBack();
response.setIncludeSnapshot();
2025-07-22 16:36:21 -07:00
response.addCode(`// Navigate back`);
response.addCode(`await page.goBack();`);
},
});
const goForward = defineTabTool({
capability: 'core',
schema: {
name: 'browser_navigate_forward',
title: 'Go forward',
description: 'Go forward to the next page',
inputSchema: z.object({}),
type: 'readOnly',
},
2025-07-22 16:36:21 -07:00
handle: async (tab, params, response) => {
await tab.page.goForward();
response.setIncludeSnapshot();
2025-07-22 16:36:21 -07:00
response.addCode(`// Navigate forward`);
response.addCode(`await page.goForward();`);
},
});
export default [
navigate,
goBack,
goForward,
];