Part of the mm CLI v2 effort. Core follow-up:
MetaMask/client-mcp-core#143 (draft, needs this release).
> **Summary:** An element query's `identifier` now matches only the
whole, case-sensitive identifier. A test ID that is not on screen fails
instead of matching another element whose ID contains it. A new
`ElementQuery.exact` turns off the fuzzy fallback for `label`, `text`
and `type`.
## Problem
`findElement` tries an exact match first, then falls back to the first
fuzzy match (case-insensitive equality, then **contains**) for every
field, `identifier` included. When the requested element is missing (not
rendered yet, scrolled away, or a partial accessibility tree), a longer
test ID that contains it matches instead. `waitForElement` then passes,
`getElementText` reads the wrong element, and `tapElement` taps it.
Live on iOS (wallet home, the accessibility tree fell back to a partial
read, `homepage-balance-breakdown` absent; device-mcp 0.9.0, amounts
sanitized):
```text
mm wait-for --testid homepage-balance-breakdown --timeout 3000
→ {"found":true,"target":"testId:homepage-balance-breakdown"} exit 0
mm get-text --testid homepage-balance-breakdown --timeout 3000
→ {"text":"$<AMT>, $<AMT>, (<PCT>%), Today","target":"testId:homepage-balance-breakdown"} exit 0
(the text of homepage-balance-breakdown-hero)
```
A `click` with the same target would tap
`homepage-balance-breakdown-hero`. The same applies to `account-picker`
→ `account-picker-avatar`, `wallet-screen` → `wallet-screen-…`, and on
Android to a short ID (`title`) → `io.metamask:id/title`. The wallets
under test hold real funds.
## Solution
`matchesQuery` compares `identifier` with `exactMatch` in both passes.
`findElement` skips the fuzzy pass when `query.exact` is set. The match
rule stays in one function, so every caller gets it: the three backends
(`tapElement`, `longPress`, `getElementText`), `pollForElement`
(`waitForElement`) and `scrollToElement`.
```mermaid
flowchart TD
Q["findElement(hierarchy, query)"] --> E{"exact pass:<br/>every field equal"}
E -->|found| R[return the element]
E -->|none| X{"query.exact?"}
X -->|yes| N["null: Element not found / wait times out"]
X -->|no| F{"fuzzy pass:<br/>identifier still exact,<br/>label / text / type: ignore case, contains"}
F -->|found| R
F -->|none| N2["null: Element not found / wait times out"]
classDef new fill:#fff3cd,stroke:#d39e00
classDef stop fill:#f8d7da,stroke:#c82333
class X,F new
class N,N2 stop
```
Yellow boxes are new. Red boxes stop the lookup.
## What changes
Each query run against the same tree through the published 0.9.0 build
and this branch's build:
| Query | Before | After |
|---|---|---|
| `{ identifier: 'homepage-balance-breakdown' }` (only `…-hero` on
screen) | `homepage-balance-breakdown-hero` | `null` |
| `{ identifier: 'account-picker' }` (both on screen) | `account-picker`
| `account-picker` |
| `{ identifier: 'ACCOUNT-PICKER' }` | `account-picker-avatar` | `null`
|
| `{ identifier: 'title' }` (Android `io.metamask:id/title`) |
`io.metamask:id/title` | `null` |
| `{ label: 'Send' }` (only `Send feedback`) | `Send feedback` | `Send
feedback` (no change) |
| `{ label: 'Send', exact: true }` | `Send feedback` | `null` |
## Decisions for reviewers
| Decision | Reason |
|---|---|
| `identifier` is always exact, with no opt-out | An identifier is an
ID. A partial ID match has no safe use, and every caller (core's
`--testid` and refs, the MCP tools) means one element. |
| **Breaking** (`fix!`): needs a minor bump (0.x) | A caller that relied
on a partial or case-insensitive identifier now gets `Element not
found`. On Android this includes a bare ID without the `package:id/`
prefix. |
| Keep fuzzy `label` / `text` / `type` by default | The MCP tools serve
humans and agents who type a label by hand (`device_tap_element { label:
"Confirm" }`), and `dismissAlert` looks up its button labels with it. |
| `exact` is a library field, not an MCP tool parameter | Its only
caller is core, which builds ref queries from exact snapshot text. The
tools keep their current schema. |
| Hit test unchanged | `hit-test.ts` and `android-hit-test.ts` compare
the hit with the target by equality and frame. They do not call
`matchesQuery`. |
## Review guide
Read the files in this order:
| # | File | What to check |
|---|---|---|
| 1 | `src/utils/element.ts` | `exactMatch` on `identifier` in
`matchesQuery`; the `query.exact` guard in `findElement`. |
| 2 | `src/backends/types.ts` | `ElementQuery.exact`. |
| 3 | `src/tools/*.ts` | Each tool description adds: "`label`, `text`
and `type` match partially (case-insensitive) when no element matches
exactly." |
| 4 | Tests | `element.test.ts` (match rules),
`wait-for-element.test.ts` (wait times out), `idb-backend.test.ts` and
`adb-backend.test.ts` (no tap). |
| 5 | `README.md` | "Element Identification": how each field matches. |
<details>
<summary>Testing</summary>
- `yarn build`, `yarn lint` and `yarn test` pass (990 passed, 6 skipped,
after merging `main`). Coverage did not drop: all files 83.04%
statements, `element.ts` 97.24%.
- New or changed tests:
- a missing identifier that is a substring or a prefix of another, or
differs only in case → `findElement` returns `null`; the whole
identifier still matches;
- `pollForElement` times out when only `confirm-footer` and `Confirm`
are on screen for `confirm`;
- iOS `tapElement({ identifier: 'homepage-balance-breakdown' })` with
only `…-hero` on screen → `Element not found`, no `idb ui tap`;
- Android `tapElement({ identifier: 'title' })` with
`io.metamask:id/title` on screen → `Element not found`, no `input tap`;
- label and text keep the fuzzy fallback (`send` → `Send feedback`,
`RESEND` → `Resend`); with `exact: true` they do not;
- an identifier stays exact when the label falls back to a partial
match.
- Mutation check: reverting `exactMatch` to `match` for `identifier`, or
removing the `query.exact` guard, fails these tests.
- Live, iOS sim, read-only, this build linked into the Mobile app with
yalc (core 0.15.0 unchanged), login screen:
| Command | Before (0.9.0) | After |
|---|---|---|
| `wait-for --testid homepage-balance-breakdown` (wallet home, only
`…-hero` present) | exit 0, false pass | not re-run: the relaunch after
the swap locked the wallet (one unlock per session) |
| `wait-for --testid login-password-input` | — | exit 0 |
| `wait-for` / `get-text --testid login-password` (prefix) | — | exit 5
`MM_WAIT_TIMEOUT` / exit 4 `MM_GETTEXT_FAILED` |
| `wait-for --testid log-in` (prefix), `LOG-IN-BUTTON` (case) | — | exit
5 `MM_WAIT_TIMEOUT` |
| `get-text --testid log-in-button` | — | exit 0 `"Unlock"` |
- Core needs MetaMask/client-mcp-core#143 to pass `exact: true` for
refs; `--testid` needs only this release.
</details>
<details>
<summary>Docs changed</summary>
- `README.md` "Element Identification": `identifier` exact and
case-sensitive; `label`, `text`, `type` exact first, then partial and
case-insensitive; `exact: true` (library API only) turns off the partial
match.
</details>
Part of the mm CLI v2 effort.
Bumps
@metamask/device-mcpto^0.11.0, which ships MetaMask/device-mcp#68 (exact identifier match,ElementQuery.exact) and MetaMask/device-mcp#67 (ax-tree-fallbackwarning).Problem
device-mcp falls back to a case-insensitive contains match when no element matches exactly. Core sends
--testid xas{ identifier: x }and a ref as itsidentifier:/label:/value:query, so both get that fallback. When the real element is missing (not rendered yet, scrolled away, or a partial accessibility tree), another element matches.Live on iOS, 2026-12-03 (wallet home, partial accessibility tree, core 0.15.0, device-mcp 0.9.0, amounts sanitized).
homepage-balance-breakdownis not on the screen;homepage-balance-breakdown-herois:A
clickwith the same target taps the wrong element. The wallets under test hold real funds.Solution
device-mcp 0.11.0 matches
identifierexactly, so--testidneeds no core change beyond the bump. This PR addsexact: trueto every ref query, so label and value refs also skip the fuzzy fallback.flowchart TD T["--testid x"] --> Q1["{ identifier: x }"] R["ref eN (stable identity)"] --> Q2["parseStableIdentifier + exact: true"] Q1 --> D{"device-mcp findElement"} Q2 --> D D -->|exact match| A[wait-for / get-text / click / type act on it] D -->|no exact match| N["Not found. Nothing is tapped or read."] classDef new fill:#fff3cd,stroke:#d39e00 classDef stop fill:#f8d7da,stroke:#c82333 class Q2 new class N stopYellow boxes are new. Red boxes stop the command.
What the user sees
The same commands on the login screen with the device-mcp#68 build (the same code as 0.11.0) linked into the Mobile app (live, iOS):
wait-for --testid login-password-input{"found":true}get-text --testid log-in-button{"text":"Unlock"}wait-for --testid login-password(prefix oflogin-password-input)MM_WAIT_TIMEOUTTimed out waiting for element: {"identifier":"login-password"}get-text --testid login-passwordMM_GETTEXT_FAILEDTimed out waiting for element: …wait-for --testid log-in(prefix oflog-in-button)MM_WAIT_TIMEOUTwait-for --testid LOG-IN-BUTTON(case differs)MM_WAIT_TIMEOUTThese are the existing codes of a missing mobile target. Nothing new is added to the error table.
Decisions for reviewers
exact: trueon refs only;--testidstays{ identifier }matchesExactly. The lookup now uses the same rule as the uniqueness check.$2.43does not contain$2.42. A ref to a label that changed fails as before.dependenciesand the README range sentence carry the version.ElementQuery.exactneeds 0.11.0.Review guide
src/platform/mobile-platform-driver.tsresolveTargetToQuery,a11yRefcase:{ ...parseStableIdentifier(resolution), exact: true }.src/platform/mobile-platform-driver.test.tsexact: true. New test "finds a test ID or ref only by its exact identity" runs the driver on device-mcp's realfindElement.SKILL.mdpackage.json,yarn.lock,README.md@metamask/device-mcp^0.11.0.Testing
main:yarn build,yarn lintandCI=1 yarn testpass (76 files, 2976 tests). Coverage: 98.13 / 95.54 / 97.53 / 98.23 (statements / branches / functions / lines), above the floors.findElementwith a wallet home that lackshomepage-balance-breakdown:--testid homepage-balance-breakdownand--testid HOMEPAGE-BALANCE-BREAKDOWN-HERO→ not found;label:Send|type:StaticTextwith onlySend feedback→ not found;value:2.4|type:StaticTextwith only$2.43→ not found;--testid homepage-balance-breakdown-hero→ found.exact: true, 17 tests fail.Docs changed
SKILL.md: mobile--testidand refs match the whole, case-sensitive testId, label or text, and fail when it is not on the screen.README.md: the device-mcp range the consumer must match is^0.11.0.