Skip to main content
Glama

evaluate_xpath

Evaluate XPath expressions to locate and extract specific DOM elements from web pages for debugging, testing, and data extraction workflows.

Instructions

Avalia uma expressão XPath e retorna os elementos correspondentes

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
xpathYesExpressão XPath

Implementation Reference

  • Main handler function that executes XPath evaluation on the current browser page using Puppeteer's evaluate method with document.evaluate. Returns count and summaries of matching elements (tagName, truncated textContent, outerHTML).
    export async function handleEvaluateXPath(args: unknown, currentPage: Page): Promise<ToolResponse> {
      const typedArgs = args as unknown as EvaluateXPathArgs;
      const { xpath } = typedArgs;
    
      const elements = await currentPage.evaluate((xp: string) => {
        const result = document.evaluate(
          xp,
          document,
          null,
          XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
          null
        );
    
        const nodes = [];
        for (let i = 0; i < result.snapshotLength; i++) {
          const node = result.snapshotItem(i) as Element;
          nodes.push({
            tagName: node.tagName,
            textContent: (node.textContent || '').substring(0, 100),
            outerHTML: node.outerHTML.substring(0, 200),
          });
        }
        return nodes;
      }, xpath);
    
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(
              {
                count: elements.length,
                elements: elements,
              },
              null,
              2
            ),
          },
        ],
      };
    }
  • TypeScript interface defining the input arguments for the evaluate_xpath tool: requires 'xpath' string.
    export interface EvaluateXPathArgs {
      xpath: string;
    }
  • JSON schema definition for the tool's input in the tools registry, matching the TypeScript type.
    inputSchema: {
      type: 'object',
      properties: {
        xpath: {
          type: 'string',
          description: 'Expressão XPath',
        },
      },
      required: ['xpath'],
    },
  • src/tools.ts:250-263 (registration)
    Tool registration object in the exported tools array, including name, description, and inputSchema. Used by MCP listTools.
    {
      name: 'evaluate_xpath',
      description: 'Avalia uma expressão XPath e retorna os elementos correspondentes',
      inputSchema: {
        type: 'object',
        properties: {
          xpath: {
            type: 'string',
            description: 'Expressão XPath',
          },
        },
        required: ['xpath'],
      },
    },
  • src/index.ts:119-122 (registration)
    Dispatch logic in the main MCP server request handler (CallToolRequestSchema) that initializes browser page and calls the handler for evaluate_xpath.
    case 'evaluate_xpath': {
      const currentPage = await initBrowser();
      return await handleEvaluateXPath(args, currentPage);
    }
Behavior2/5

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

With no annotations, the description carries full burden but only states basic functionality. It doesn't disclose behavioral traits such as error handling (e.g., invalid XPath), performance implications, whether it returns all matches or first match, or if it requires a loaded DOM context. This leaves significant gaps for an agent to understand tool behavior.

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 a single, efficient sentence in Portuguese that directly states the tool's purpose. It is front-loaded with no wasted words, making it highly concise and well-structured for quick understanding.

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

Completeness2/5

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

Given no annotations, no output schema, and a single parameter, the description is incomplete. It lacks details on return values (e.g., format of elements), error cases, or dependencies on page state. For a tool that interacts with DOM elements, more context is needed for effective use.

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

Parameters3/5

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

Schema description coverage is 100% with one parameter 'xpath' documented as 'Expressão XPath'. The description adds no additional meaning beyond this, such as XPath syntax examples or constraints. Baseline 3 is appropriate since the schema adequately covers the parameter.

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

Purpose4/5

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

The description clearly states the verb 'avalia' (evaluates) and the resource 'expressão XPath' (XPath expression), specifying it returns matching elements. It distinguishes from siblings like get_dom or query_selector_all by focusing on XPath evaluation. However, it doesn't explicitly differentiate from execute_js which might also evaluate expressions.

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

Usage Guidelines2/5

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

No guidance is provided on when to use this tool versus alternatives like query_selector_all (for CSS selectors) or get_element (for simpler element retrieval). The description implies usage for XPath evaluation but lacks explicit context, prerequisites, or exclusions.

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/EmmanuelBarbosaMonteiro/mcp-server-browser'

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