Skip to main content
Glama
rahulkr
by rahulkr

tap_element

Tap on Android UI elements using text or resource ID for reliable interaction during app testing and development.

Instructions

Tap on an element by text or resource ID. More reliable than raw coordinates.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textNo
resource_idNo
device_serialNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The primary handler function for the 'tap_element' tool. It locates a UI element using text or resource ID via helper functions, computes its center coordinates, and performs a tap action using ADB's input tap command.
    @mcp.tool()
    def tap_element(
        text: str | None = None,
        resource_id: str | None = None,
        device_serial: str | None = None
    ) -> str:
        """
        Tap on an element by text or resource ID.
        More reliable than raw coordinates.
        """
        element = None
        
        if text:
            elements = find_element_by_text(text, partial_match=True, device_serial=device_serial)
            if elements:
                element = elements[0]
        elif resource_id:
            element = find_element_by_id(resource_id, device_serial=device_serial)
        
        if not element or 'center' not in element:
            return f"Element not found: text='{text}', resource_id='{resource_id}'"
        
        x, y = element['center']['x'], element['center']['y']
        run_adb(["shell", "input", "tap", str(x), str(y)], device_serial)
        return f"Tapped element at ({x}, {y})"
  • Helper function used by tap_element to find UI elements by text content or content description, parsing the UI hierarchy XML to extract bounds and center coordinates.
    @mcp.tool()
    def find_element_by_text(
        text: str, 
        partial_match: bool = True,
        device_serial: str | None = None
    ) -> list[dict]:
        """
        Find UI elements containing specific text.
        Returns element details including tap coordinates.
        """
        xml = get_ui_hierarchy(device_serial)
        elements = []
        
        for match in re.finditer(r'<node[^>]*>', xml):
            node = match.group()
            
            text_match = re.search(r'text="([^"]*)"', node)
            desc_match = re.search(r'content-desc="([^"]*)"', node)
            
            found_text = text_match.group(1) if text_match else ""
            found_desc = desc_match.group(1) if desc_match else ""
            
            # Check if text matches
            matches = False
            if partial_match:
                matches = text.lower() in found_text.lower() or text.lower() in found_desc.lower()
            else:
                matches = text == found_text or text == found_desc
            
            if matches:
                element = {'text': found_text, 'content_desc': found_desc}
                
                bounds_match = re.search(r'bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', node)
                if bounds_match:
                    x1, y1 = int(bounds_match.group(1)), int(bounds_match.group(2))
                    x2, y2 = int(bounds_match.group(3)), int(bounds_match.group(4))
                    element['center'] = {'x': (x1 + x2) // 2, 'y': (y1 + y2) // 2}
                    element['bounds'] = {'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2}
                
                elements.append(element)
        
        return elements
  • Helper function used by tap_element to find UI elements by partial resource ID match, parsing UI hierarchy XML for coordinates.
    @mcp.tool()
    def find_element_by_id(
        resource_id: str,
        device_serial: str | None = None
    ) -> dict | None:
        """
        Find a UI element by its resource ID.
        Returns element details including tap coordinates.
        """
        xml = get_ui_hierarchy(device_serial)
        
        # Match partial resource ID (e.g., "button_submit" matches "com.app:id/button_submit")
        for match in re.finditer(r'<node[^>]*>', xml):
            node = match.group()
            
            id_match = re.search(r'resource-id="([^"]*)"', node)
            if id_match and resource_id in id_match.group(1):
                element = {'resource_id': id_match.group(1)}
                
                text_match = re.search(r'text="([^"]*)"', node)
                if text_match:
                    element['text'] = text_match.group(1)
                
                bounds_match = re.search(r'bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"', node)
                if bounds_match:
                    x1, y1 = int(bounds_match.group(1)), int(bounds_match.group(2))
                    x2, y2 = int(bounds_match.group(3)), int(bounds_match.group(4))
                    element['center'] = {'x': (x1 + x2) // 2, 'y': (y1 + y2) // 2}
                    element['bounds'] = {'x1': x1, 'y1': y1, 'x2': x2, 'y2': y2}
                
                return element
        
        return None
Behavior2/5

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

No annotations are provided, so the description carries the full burden. It mentions reliability as a behavioral trait, which is useful, but lacks details on permissions needed, error handling, what happens if multiple elements match, or interaction effects. For a UI automation tool with zero annotation coverage, this leaves significant gaps in understanding its 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 extremely concise with two sentences that are front-loaded and waste no words. Every sentence adds value: the first states the purpose, and the second provides a key behavioral insight. This is an efficient use of language.

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

Completeness3/5

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

Given the context: no annotations, 3 parameters with 0% schema coverage, and an output schema exists (so return values needn't be explained), the description is moderately complete. It covers the core purpose and a reliability advantage but misses details on parameters, error cases, and behavioral nuances, making it adequate but with clear gaps for a UI interaction tool.

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

Parameters2/5

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

Schema description coverage is 0%, so the description must compensate. It mentions 'text or resource ID' which maps to two of the three parameters, but doesn't explain 'device_serial' or provide details on format, constraints, or how parameters interact (e.g., if both text and resource_id are provided). The description adds some meaning but doesn't fully address the undocumented parameters.

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 action ('Tap on an element') and the targeting method ('by text or resource ID'), which is specific and actionable. However, it doesn't explicitly differentiate from sibling tools like 'tap' or 'double_tap' beyond mentioning reliability compared to raw coordinates.

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 implies usage by stating 'More reliable than raw coordinates,' suggesting this tool should be preferred over coordinate-based tapping methods. However, it doesn't explicitly mention when to use this versus alternatives like 'tap' (which might use coordinates) or other interaction tools, nor does it provide exclusions or prerequisites.

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/rahulkr/r_adb_mcp_server'

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