Evaluate JavaScript
pinchtab_evalExecute JavaScript in the active browser page and return the result. Enables custom automation scripts.
Instructions
Execute JavaScript code in the page context. Returns the result of the expression.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | JavaScript code to execute in the page |
Implementation Reference
- src/tools/content.ts:59-69 (handler)The tool handler function for 'pinchtab_eval'. It sends the code to the PinchTab server via POST /evaluate with the expression, then formats the result as text. Uses the pinch() client helper.
async ({ code }) => { try { const result = await pinch("POST", "/evaluate", { expression: code }); const text = isRecord(result) && "result" in result ? String(result.result) : toJson(result); return { content: [{ text, type: "text" as const }] }; } catch (error) { return toolError(error); } }, ); - src/tools/content.ts:54-56 (schema)Input schema for pinchtab_eval: expects a single required string parameter 'code' — JavaScript code to execute in the page context.
inputSchema: z.object({ code: z.string().describe("JavaScript code to execute in the page"), }), - src/tools/content.ts:49-69 (registration)Registration of the 'pinchtab_eval' tool via server.registerTool() with the name, description, schema, and handler. Located in registerContentTools() which is called from registerAllTools() in src/tools/index.ts.
server.registerTool( "pinchtab_eval", { description: "Execute JavaScript code in the page context. Returns the result of the expression.", inputSchema: z.object({ code: z.string().describe("JavaScript code to execute in the page"), }), title: "Evaluate JavaScript", }, async ({ code }) => { try { const result = await pinch("POST", "/evaluate", { expression: code }); const text = isRecord(result) && "result" in result ? String(result.result) : toJson(result); return { content: [{ text, type: "text" as const }] }; } catch (error) { return toolError(error); } }, ); - src/pinchtab/client.ts:6-49 (helper)The pinch() helper function used by the handler to make HTTP requests to the PinchTab server. pinchtab_eval calls pinch('POST', '/evaluate', { expression: code }).
export async function pinch( method: string, path: string, body?: Record<string, unknown>, ): Promise<unknown> { if (!(await isPinchtabRunning())) { await ensurePinchtabRunning(); } const headers: Record<string, string> = { "Content-Type": "application/json", }; if (PINCHTAB_TOKEN) { headers["Authorization"] = `Bearer ${PINCHTAB_TOKEN}`; } const url = `${PINCHTAB_URL}${path}`; let res: Response; try { res = await fetch(url, { body: body ? JSON.stringify(body) : undefined, headers, method, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS), }); } catch (error) { if (error instanceof DOMException && error.name === "TimeoutError") { throw new Error(`PinchTab ${method} ${path} timed out after ${REQUEST_TIMEOUT_MS / 1000}s`); } throw error; } if (!res.ok) { const text = await res.text(); throw new Error(`PinchTab ${method} ${path} → ${res.status}: ${text}`); } const contentType = (res.headers.get("content-type") ?? "").split(";")[0].toLowerCase().trim(); if (contentType === "application/json") { return res.json(); } return res.text(); } - src/utils.ts:1-29 (helper)Utility functions (isRecord, toJson, toolError) used by the handler to process the response and format errors.
/** Type guard: checks that a value is a non-null object (record). */ export function isRecord(value: unknown): value is Record<string, unknown> { return typeof value === "object" && value !== null; } /** Pretty-print a value as indented JSON. */ export function toJson(value: unknown): string { return JSON.stringify(value, undefined, 2); } interface ToolResult { [key: string]: unknown; content: { text: string; type: "text" }[]; isError?: boolean; } /** Wrap a tool handler with standardized error handling. */ export function toolResult(data: unknown): ToolResult { return { content: [{ text: toJson(data), type: "text" as const }] }; } /** Format an error as an MCP tool error response. */ export function toolError(error: unknown): ToolResult { const message = error instanceof Error ? error.message : String(error); return { content: [{ text: message, type: "text" as const }], isError: true, }; }