evaluate_javascript
Execute custom JavaScript code within a browser context using the MCP Browser Server, enabling automation for web interactions, data extraction, and dynamic content manipulation.
Instructions
Execute JavaScript in the browser context
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute |
Implementation Reference
- src/index.ts:583-599 (handler)The handler function for evaluate_javascript tool. It checks for currentPage, parses args with schema, evaluates the JS script on the page using Playwright's evaluate method, and returns the result as JSON stringified text.
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)}` } ] }; } - src/index.ts:55-57 (schema)Defines the input schema for the evaluate_javascript tool using Zod, requiring a 'script' string.
const EvaluateJavaScriptSchema = z.object({ script: z.string() }); - src/index.ts:281-294 (registration)Registers the evaluate_javascript tool in the list of available tools, including name, description, and input schema.
{ name: 'evaluate_javascript', description: 'Execute JavaScript in the browser context', inputSchema: { type: 'object', properties: { script: { type: 'string', description: 'JavaScript code to execute' } }, required: ['script'] } },