execute_js
Execute JavaScript on webpages to automate tasks, interact with elements, or modify content. Provides a URL and script input for targeted browser automation.
Instructions
Execute JavaScript code on a webpage
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | The JavaScript code to execute | |
| steps | No | Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit") | |
| url | Yes | The URL to navigate to |
Implementation Reference
- src/browser_handler.py:222-249 (handler)Core handler logic for the 'execute_js' tool. Validates inputs, uses an LLM agent to navigate to the URL and execute optional steps, then runs the JavaScript script on the browser page.elif command == 'execute_js': if not args.get('url') or not args.get('script'): return { 'success': False, 'error': 'URL and script are required for execute_js command' } task = f"1. Go to {args['url']}" if args.get('steps'): steps = args['steps'].split(',') for i, step in enumerate(steps, 2): task += f"\n{i}. {step.strip()}" task += f"\n{len(steps) + 2}. Execute JavaScript: {args['script']}" else: task += f"\n2. Execute JavaScript: {args['script']}" use_vision = os.getenv('USE_VISION', 'false').lower() == 'true' agent = Agent(task=task, llm=llm, use_vision=use_vision, browser_context=context) await agent.run() try: result = await context.execute_javascript(args['script']) return { 'success': True, 'result': result } finally: await context.close() elif command == 'get_console_logs':
- src/index.ts:192-213 (registration)Registers the 'execute_js' tool with the MCP server in the listTools response, including its name, description, and input schema.{ name: 'execute_js', description: 'Execute JavaScript code on a webpage', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL to navigate to', }, script: { type: 'string', description: 'The JavaScript code to execute', }, steps: { type: 'string', description: 'Comma-separated actions or sentences describing steps to take after page load (e.g., "click #submit, scroll down" or "Fill the login form and submit")', }, }, required: ['url', 'script'], }, },
- src/index.ts:252-257 (schema)Input validation schema enforcement for 'execute_js' tool arguments in the callToolRequest handler.if (request.params.name === 'execute_js' && typeof args.script !== 'string') { throw new McpError( ErrorCode.InvalidParams, 'Script must be a string' ); }
- src/index.ts:300-308 (handler)Processes the result from the Python handler for 'execute_js' (falls into the else clause) and formats it for MCP response.} else { return { content: [ { type: 'text', text: JSON.stringify(result.result, null, 2), }, ], };