Skip to main content
Glama

detect_elements_visually

Identify clickable and focusable UI elements on Android screens using screenshot analysis and UI tree data to locate interactive components for automation.

Instructions

Detect interactive elements on the screen using a combination of screenshots and UI tree analysis. Returns a list of all clickable/focusable elements with their coordinates and descriptions. Use this when you need to find elements to interact with.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
device_idNoDevice serial number

Implementation Reference

  • The implementation of detectElementsVisually, which captures a screenshot and parses the UI tree to identify interactive elements.
    export async function detectElementsVisually(deviceId?: string): Promise<{
      screenshot: ScreenshotResult;
      uiTree: string;
      interactiveElements: Array<{
        description: string;
        centerX: number;
        centerY: number;
        bounds: string;
      }>;
    }> {
      const resolved = await deviceManager.resolveDeviceId(deviceId);
    
      const screenshot = await captureScreenshot(resolved);
    
      let uiTree = '';
      const interactiveElements: Array<{
        description: string;
        centerX: number;
        centerY: number;
        bounds: string;
      }> = [];
    
      try {
        const tree = await getUITree(resolved);
        uiTree = summarizeTree(tree);
    
        // Extract interactive elements
        const { flattenTree } = await import('../uiautomator/ui-tree-parser.js');
        const allElements = flattenTree(tree);
    
        for (const el of allElements) {
          if ((el.clickable || el.focusable) && el.bounds.width > 0 && el.bounds.height > 0) {
            const desc = el.text || el.contentDesc || el.resourceId || el.className.split('.').pop() || 'unknown';
            interactiveElements.push({
              description: desc,
              centerX: el.bounds.centerX,
              centerY: el.bounds.centerY,
              bounds: `[${el.bounds.left},${el.bounds.top}][${el.bounds.right},${el.bounds.bottom}]`,
            });
          }
        }
      } catch (error) {
        log.warn('UI tree unavailable for visual detection', {
          error: error instanceof Error ? error.message : String(error),
        });
      }
    
      log.info('Visual detection completed', {
        deviceId: resolved,
        interactiveCount: interactiveElements.length,
      });
    
      return { screenshot, uiTree, interactiveElements };
    }
  • The MCP tool registration for detect_elements_visually.
    server.registerTool(
      'detect_elements_visually',
      {
        description: 'Detect interactive elements on the screen using a combination of screenshots and UI tree analysis. Returns a list of all clickable/focusable elements with their coordinates and descriptions. Use this when you need to find elements to interact with.',
        inputSchema: {
          device_id: z.string().optional().describe('Device serial number'),
        },
      },
      async ({ device_id }) => {
        return await metrics.measure('detect_elements_visually', device_id || 'default', async () => {
          const detection = await detectElementsVisually(device_id);
    
          const content: Array<{ type: 'text'; text: string } | { type: 'image'; data: string; mimeType: string }> = [];
    
          content.push({
            type: 'image' as const,
            data: detection.screenshot.base64,
            mimeType: 'image/png',
          });
    
          content.push({
            type: 'text' as const,
            text: JSON.stringify({
              success: true,
              interactiveElements: detection.interactiveElements,
              elementCount: detection.interactiveElements.length,
              uiTree: detection.uiTree,
            }, null, 2),
          });
    
          return { content };
        });
      }
    );
Behavior3/5

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

With no annotations provided, the description must carry the full burden. It successfully explains the mechanism (screenshots + UI tree) and return value (list with coordinates/descriptions), but fails to disclose safety properties (read-only vs. destructive), side effects, or performance characteristics that would help an agent assess invocation risks.

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 consists of three efficient sentences with zero redundancy: the first defines the action and method, the second specifies the return value, and the third provides usage context. Information is front-loaded appropriately.

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

Completeness4/5

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

Given the absence of an output schema, the description appropriately compensates by detailing the return structure ('list of all clickable/focusable elements with their coordinates'). It covers the tool's primary function adequately, though it could strengthen completeness by clarifying the read-only nature of the operation given the lack of safety annotations.

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?

The input schema has 100% description coverage for its single parameter ('Device serial number'), establishing a baseline of 3. The description adds no additional parameter context (e.g., where to obtain the device ID or validation rules), but this is acceptable given the schema's completeness.

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 tool detects interactive elements using a specific method (screenshots + UI tree analysis) and implies comprehensive coverage ('all clickable/focusable elements'). However, it does not explicitly distinguish this from the sibling tool 'find_element', which likely serves a similar but more targeted purpose.

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

Usage Guidelines3/5

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

The description includes an explicit 'Use this when' clause ('when you need to find elements to interact with'), providing positive guidance. However, it lacks negative constraints ('do not use when...') or named alternatives (e.g., when to prefer 'find_element' or 'get_ui_tree' instead).

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/divineDev-dotcom/android_mcp'

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