evaluate_javascript
Execute JavaScript in the browser to automate interactions, manipulate page content, or retrieve live data, supporting dynamic workflows beyond simple navigation.
Instructions
Execute JavaScript in the browser context
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/index.ts:55-57 (schema)Zod schema for evaluate_javascript tool - validates that 'script' is a required string.
const EvaluateJavaScriptSchema = z.object({ script: z.string() }); - src/index.ts:281-294 (registration)Tool registration entry in the ListToolsRequestSchema handler, defining the tool name, description, and JSON Schema input specification.
{ name: 'evaluate_javascript', description: 'Execute JavaScript in the browser context', inputSchema: { type: 'object', properties: { script: { type: 'string', description: 'JavaScript code to execute' } }, required: ['script'] } }, - src/index.ts:583-599 (handler)Handler that executes the evaluate_javascript tool logic: validates params via Zod schema, calls currentPage.evaluate(script), and returns the JSON-stringified result.
case 'evaluate_javascript': { if (!currentPage) { throw new Error('No browser page available. Launch a browser first.'); } const params = EvaluateJavaScriptSchema.parse(args); const result = await currentPage.evaluate(params.script); return { content: [ { type: 'text', text: `JavaScript result: ${JSON.stringify(result, null, 2)}` } ] }; }