Skip to main content
Glama

pilot_evaluate

Evaluate a JavaScript expression or function in the browser page context to read DOM, extract data, perform calculations, or call APIs. Supports async/await for promises.

Instructions

Execute a JavaScript expression or function in the browser page context and return the result. Use when the user wants to run custom JavaScript on the page, read or modify DOM elements, extract data, or perform calculations. Supports async/await — use "await" to wait for promises. Multi-line code with await is automatically wrapped in an async IIFE.

Parameters:

  • expression: JavaScript expression to evaluate (e.g., "document.title", "JSON.stringify(localStorage)", "await fetch('/api').then(r => r.json())"). Maximum 50 KB.

Returns: The expression result as a string, or pretty-printed JSON for objects/arrays.

Errors:

  • "Evaluation failed": The JavaScript threw an error. Fix the expression syntax or handle the error in the page context.

  • "Promise rejected": An awaited promise rejected. Check the API endpoint or async logic.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
expressionYesJavaScript expression to evaluate (max 50 KB)

Implementation Reference

  • The tool registration and handler for 'pilot_evaluate'. Accepts a JavaScript expression (max 50KB), wraps it with async IIFE if it uses await, evaluates it in the browser page context via Playwright's page.evaluate(), and returns the result as a string or pretty-printed JSON.
      server.tool(
        'pilot_evaluate',
        `Execute a JavaScript expression or function in the browser page context and return the result.
    Use when the user wants to run custom JavaScript on the page, read or modify DOM elements, extract data, or perform calculations. Supports async/await — use "await" to wait for promises. Multi-line code with await is automatically wrapped in an async IIFE.
    
    Parameters:
    - expression: JavaScript expression to evaluate (e.g., "document.title", "JSON.stringify(localStorage)", "await fetch('/api').then(r => r.json())"). Maximum 50 KB.
    
    Returns: The expression result as a string, or pretty-printed JSON for objects/arrays.
    
    Errors:
    - "Evaluation failed": The JavaScript threw an error. Fix the expression syntax or handle the error in the page context.
    - "Promise rejected": An awaited promise rejected. Check the API endpoint or async logic.`,
          { expression: z.string().max(MAX_EXPRESSION_LENGTH).describe('JavaScript expression to evaluate (max 50 KB)') },
        async ({ expression }) => {
          await bm.ensureBrowser();
          try {
            const wrapped = wrapForEvaluate(expression);
            const result = await bm.getPage().evaluate(wrapped);
            const text = typeof result === 'object' ? JSON.stringify(result, null, 2) : String(result ?? '');
            return { content: [{ type: 'text' as const, text }] };
          } catch (err) {
            return { content: [{ type: 'text' as const, text: wrapError(err) }], isError: true };
          }
        }
      );
  • Input schema for pilot_evaluate: a single string parameter 'expression' with max 50 KB limit, validated via Zod.
    { expression: z.string().max(MAX_EXPRESSION_LENGTH).describe('JavaScript expression to evaluate (max 50 KB)') },
  • The tool is registered via registerInspectionTools() which is called from registerAllTools() in src/tools/register.ts.
    registerInspectionTools(effectiveServer, bm);
  • Helper that wraps JavaScript code in an async IIFE if it contains 'await'. Supports multi-line expressions and statement-level constructs (const, let, etc.).
    function wrapForEvaluate(code: string): string {
      if (!hasAwait(code)) return code;
      const trimmed = code.trim();
      return needsBlockWrapper(trimmed)
        ? `(async()=>{\n${code}\n})()`
        : `(async()=>(${trimmed}))()`;
    }
  • Helper that detects whether code contains `await` (stripping comments first) to determine if async wrapper is needed.
    function hasAwait(code: string): boolean {
      const stripped = code.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
      return /\bawait\b/.test(stripped);
Behavior5/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description fully covers behavior: async/await support, auto-wrapping in IIFE, return format (string or JSON), and two error types with causes. This provides comprehensive transparency.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is well-structured with clear sections: purpose, usage guide, parameter details, return values, errors. Every sentence adds value without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no output schema, the description adequately explains return values and errors. With only one parameter, all necessary information is present for correct usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so baseline 3. The description adds value with concrete examples (document.title, fetch) and async usage context, beyond the schema's maxLength description.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states 'Execute a JavaScript expression or function in the browser page context', providing a specific verb and resource. It distinguishes this tool from siblings as it is the only one that runs custom JavaScript.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description explicitly lists use cases: 'when the user wants to run custom JavaScript, read/modify DOM, extract data, perform calculations'. It does not explicitly state when not to use, but the positive guidance is clear.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/TacosyHorchata/Pilot'

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