dump-ui-hierarchy
Capture Android UI hierarchy as XML using uiautomator for debugging and automation testing. Specify timeout to control capture duration.
Instructions
Capture the current UI hierarchy as XML using uiautomator.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| timeoutMs | No | Timeout in milliseconds |
Implementation Reference
- src/tools/deviceTool.js:86-91 (handler)Handler function that executes the dump-ui-hierarchy tool: runs 'adb shell uiautomator dump' to capture UI hierarchy as XML, reads the file, and returns the trimmed content.async (params) => { const devicePath = '/data/local/tmp/mcp_window_dump.xml'; await runAdbCommand(['shell', 'uiautomator', 'dump', devicePath], params.timeoutMs); const content = await runAdbCommand(['shell', 'cat', devicePath], params.timeoutMs); return { content: [{ type: 'text', text: content.trim() }] }; }
- src/tools/deviceTool.js:16-18 (schema)Input schema for the dump-ui-hierarchy tool, defining optional timeoutMs parameter.const dumpUiSchema = z.object({ timeoutMs: z.number().int().min(1000).max(20000).default(10000).describe('Timeout in milliseconds') });
- src/tools/deviceTool.js:79-92 (registration)Registration of the dump-ui-hierarchy tool using server.registerTool, specifying name, metadata, inputSchema, and handler function.server.registerTool( 'dump-ui-hierarchy', { title: 'Dump UI Hierarchy (XML)', description: 'Capture the current UI hierarchy as XML using uiautomator.', inputSchema: dumpUiSchema }, async (params) => { const devicePath = '/data/local/tmp/mcp_window_dump.xml'; await runAdbCommand(['shell', 'uiautomator', 'dump', devicePath], params.timeoutMs); const content = await runAdbCommand(['shell', 'cat', devicePath], params.timeoutMs); return { content: [{ type: 'text', text: content.trim() }] }; } );
- src/tools/deviceTool.js:47-63 (helper)Helper function runAdbCommand used by the handler to execute adb shell commands with timeout and error handling.async function runAdbCommand(args, timeoutMs, options = {}) { try { const { stdout } = await execFileAsync('adb', args, { timeout: timeoutMs, maxBuffer: 10 * 1024 * 1024, ...options }); return stdout; } catch (error) { const stderr = error && typeof error.stderr === 'string' ? error.stderr.trim() : ''; const message = [`adb ${args.join(' ')} failed`, error.message].filter(Boolean).join(': '); if (stderr) { throw new Error(`${message} | stderr: ${stderr}`); } throw new Error(message); } }
- src/index.js:30-30 (registration)Invocation of registerDeviceTool which registers the dump-ui-hierarchy (among other device tools) to the MCP server.registerDeviceTool(server);