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
| Name | Required | Description | Default |
|---|---|---|---|
| xpath | Yes | Expressão XPath |
Input Schema (JSON Schema)
{
"properties": {
"xpath": {
"description": "Expressão XPath",
"type": "string"
}
},
"required": [
"xpath"
],
"type": "object"
}
Implementation Reference
- src/browserTools.ts:570-610 (handler)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 ), }, ], }; }
- src/types.ts:86-88 (schema)TypeScript interface defining the input arguments for the evaluate_xpath tool: requires 'xpath' string.export interface EvaluateXPathArgs { xpath: string; }
- src/tools.ts:253-262 (schema)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); }