evaluate
Execute JavaScript code in browser contexts to automate interactions, extract content, or manipulate web pages programmatically.
Instructions
Execute JavaScript code in the browser context and return the result
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| script | Yes | JavaScript code to execute | |
| tabId | No | Tab ID to operate on (uses active tab if not specified) |
Implementation Reference
- src/tools/content.ts:22-44 (handler)Handler function for the 'evaluate' tool. Uses Puppeteer's page.evaluate to execute the provided JavaScript script in the browser context using new Function constructor, handles errors with evaluationError.async ({ script, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Use Function constructor to evaluate the script const result = await page.evaluate((code) => { // eslint-disable-next-line no-new-func const fn = new Function(code); return fn(); }, script); return handleResult(ok({ result })); } catch (error) { if (error instanceof Error) { return handleResult(err(evaluationError(error.message))); } return handleResult(err(normalizeError(error))); }
- src/tools/content.ts:18-46 (registration)Registration of the 'evaluate' tool on the MCP server within registerContentTools, specifying name, description, input schema, and handler function.server.tool( 'evaluate', 'Execute JavaScript code in the browser context and return the result', evaluateSchema.shape, async ({ script, tabId }) => { const pageResult = await getPageForOperation(tabId); if (!pageResult.success) { return handleResult(pageResult); } const page = pageResult.data; try { // Use Function constructor to evaluate the script const result = await page.evaluate((code) => { // eslint-disable-next-line no-new-func const fn = new Function(code); return fn(); }, script); return handleResult(ok({ result })); } catch (error) { if (error instanceof Error) { return handleResult(err(evaluationError(error.message))); } return handleResult(err(normalizeError(error))); } } );
- src/schemas.ts:111-114 (schema)Zod schema defining the input parameters for the evaluate tool: script (string, 1-50000 chars) and optional tabId.export const evaluateSchema = z.object({ script: z.string().min(1).max(50000).describe('JavaScript code to execute'), tabId: tabIdSchema, });
- src/schemas.ts:213-213 (schema)TypeScript type alias for the input of evaluate tool inferred from the Zod schema.export type EvaluateInput = z.infer<typeof evaluateSchema>;
- src/types.ts:89-92 (schema)TypeScript interface defining the output structure of the evaluate tool: { result: unknown }.export interface EvaluateResult { /** Result of JavaScript evaluation (serialized) */ result: unknown; }