Skip to main content
Glama

evaluate_script

Execute JavaScript code within Tauri desktop application webviews to automate tasks, test functionality, or manipulate UI elements directly through the MCP server.

Instructions

Run JS in webview

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
scriptYesJS code
windowNoWindow label (default: focused window)

Implementation Reference

  • The MCP tool handler for evaluate_script. Receives script and optional window arguments, calls socketManager.evaluateScript(), and returns the result formatted as text or JSON string.
    evaluate_script: async (args: { script: string; window?: string }) => { const result = await socketManager.evaluateScript(args.script, args.window); return { content: [ { type: 'text' as const, text: typeof result === 'string' ? result : JSON.stringify(result, null, 2), }, ], }; },
  • Tool schema definition for evaluate_script. Defines name, description, and input schema with zod validation for 'script' (required string) and 'window' (optional string) parameters.
    evaluate_script: { name: 'evaluate_script', description: 'Run JS in webview', inputSchema: z.object({ script: z.string().describe('JS code'), window: z.string().optional().describe('Window label (default: focused window)'), }), },
  • SocketManager.evaluateScript method that sends the evaluate_script command to the Tauri app via JSON-RPC over socket connection. Handles optional window label parameter and returns the result.
    async evaluateScript(script: string, windowLabel?: string): Promise<unknown> { const params: Record<string, unknown> = { script }; if (windowLabel) params.window = windowLabel; const result = await this.sendCommand('evaluate_script', params); return result; }
  • The CallToolRequestSchema handler that routes incoming tool calls to their respective handlers. When evaluate_script is called, it dispatches to the evaluate_script handler from toolHandlers.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; if (!(name in this.toolHandlers)) { throw new Error(`Unknown tool: ${name}`); } const handler = this.toolHandlers[name as ToolName]; try { return await handler(args as never); } catch (error) { return { content: [ { type: 'text' as const, text: `Error: ${(error as Error).message}`, }, ], isError: true, }; } });
  • The ListToolsRequestSchema handler that exposes all available tools including evaluate_script. Converts zod schemas to MCP tool format, filters by ESSENTIAL_TOOLS if set, and returns the tool list to clients.
    this.server.setRequestHandler(ListToolsRequestSchema, async () => { const allSchemas = Object.values(toolSchemas); // Filter tools if ESSENTIAL_TOOLS is set const filteredSchemas = essentialTools ? allSchemas.filter((schema) => essentialTools.has(schema.name)) : allSchemas; const tools: Tool[] = filteredSchemas.map((schema) => { const properties: Record<string, object> = {}; const required: string[] = []; const shape = schema.inputSchema.shape as Record<string, unknown>; for (const [key, zodValue] of Object.entries(shape)) { const zodSchema = zodValue as { _def?: { typeName?: string; description?: string }; description?: string; isOptional?: () => boolean }; properties[key] = { type: this.getZodType(zodSchema), description: zodSchema._def?.description || zodSchema.description || '', }; // Check if required (not optional) if (!zodSchema.isOptional?.()) { const typeName = zodSchema._def?.typeName; if (typeName !== 'ZodOptional') { required.push(key); } } } return { name: schema.name, description: schema.description, inputSchema: { type: 'object' as const, properties, required: required.length > 0 ? required : undefined, }, }; }); return { tools }; });

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/DaveDev42/tauri-plugin-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server