safari_execute_script
Execute JavaScript code within Safari browser sessions to automate tasks, manipulate web content, and interact with page elements programmatically.
Instructions
Execute JavaScript in the browser context
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sessionId | Yes | Session identifier | |
| script | Yes | JavaScript code to execute | |
| args | No | Arguments to pass to the script |
Implementation Reference
- src/safari-driver.ts:280-292 (handler)Core implementation of the tool: retrieves the Safari session and executes the JavaScript script using Selenium WebDriver's executeScript method, with error handling.
async executeScript(sessionId: string, script: string, args: any[] = []): Promise<any> { const session = this.getSession(sessionId); if (!session) { throw new Error(`Session ${sessionId} not found`); } try { return await session.driver.executeScript(script, ...args); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : String(error); throw new Error(`Script execution failed: ${errorMessage}`); } } - src/safari-mcp-server.ts:330-341 (handler)MCP server-side wrapper for the tool: parses input arguments, delegates to SafariDriverManager, and formats the result as a text response.
private async executeScript(args: Record<string, any>): Promise<Array<{ type: string; text: string }>> { const { sessionId, script, args: scriptArgs = [] } = args; const result = await this.driverManager.executeScript(sessionId, script, scriptArgs); return [ { type: 'text', text: `Script execution result:\n${JSON.stringify(result, null, 2)}` } ]; } - src/safari-mcp-server.ts:137-152 (schema)Input schema definition for the safari_execute_script tool, including required sessionId and script, optional args array.
{ name: 'safari_execute_script', description: 'Execute JavaScript in the browser context', inputSchema: { type: 'object', properties: { sessionId: { type: 'string', description: 'Session identifier' }, script: { type: 'string', description: 'JavaScript code to execute' }, args: { type: 'array', description: 'Arguments to pass to the script' } }, required: ['sessionId', 'script'] } }, - src/safari-mcp-server.ts:243-244 (registration)Tool dispatch/registration in the handleToolCall switch statement, routing calls to the executeScript handler.
case 'safari_execute_script': return await this.executeScript(args);