Skip to content

Bug 2081626 - [firefox-mcp] Report the focused element and its value after type_text - #255

Merged
juliandescottes merged 2 commits into
mozilla:mainfrom
Relacosm:bug-2081626-type-text-focused-element
Nov 30, 2026
Merged

juliandescottes merged 2 commits into
mozilla:mainfrom
Relacosm:bug-2081626-type-text-focused-element

Conversation

@Relacosm

@Relacosm Relacosm commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Problem

type_text only replied "type_text N chars", so an agent could not tell whether the text reached the element it expected. It only found out later, from a snapshot or a failed step. Also, when the uid could not take focus, the error started with "pressKey:" even when it came from type_text.

Fix

  • New FirefoxClient.getFocusedElement(), called by handleTypeText after typing. It returns the tag of the element that has focus, plus its value for input, textarea and select. The value of password inputs is left out. It looks into open shadow roots and same-origin iframes, so typing into a shadow DOM input or an iframe reports the input, not the host or the <iframe>.
  • The lookup always starts from the top-level document, so it also follows focus when the submit key moves it out of an iframe.
  • The tool truncates the value to 500 characters.
  • The tool now replies, for example:
    type_text 3 chars on e0
    Focused: input, value: "b\"c"
    
    For an element without a value, such as a contenteditable, it is just Focused: div. After a submit key like Tab, it shows the element that has focus afterwards.
  • The focus error no longer has the "pressKey:" prefix, since both tools use it.
  • Added one sentence to the type_text description and regenerated docs/tools.md.

Tests

  • tests/integration/firefox/type-text.test.ts: new cases for an input, the element focused after Tab, a contenteditable, a password input, an open shadow root, a same-origin iframe, and Tab moving focus out of an iframe. The existing focus-error case now checks the message has no prefix.
  • New tests/integration/mcp/input.test.ts for the reply format (value is JSON-quoted; long value truncated; no value for a contenteditable). It replaces the two mock-based success tests for handleTypeText in tests/unit/tools/input.test.ts, following docs/testing.md.
  • npm run typecheck, typecheck:tests, lint and format:check pass. Unit tests pass, apart from save-output.test.ts > should not be fooled by a symlink out of the allowed root, which also fails on main on my machine (Windows 11, creating symlinks needs extra rights). Two integration test files also fail here both with and without the change: connect-existing.test.ts (EPERM when removing its temp folder) and restricted-session-restart.test.ts (5 s timeout). Firefox 160.0.1, Node 24.

…after type_text

type_text only replied with the number of characters typed, so an agent
could not tell whether the text reached the element it meant to type
into. After typing, it now reads the element that has focus, looking
into open shadow roots and same-origin iframes, and returns its tag and
its value for inputs, textareas and selects.

The error for a uid that cannot take focus no longer starts with
"pressKey:", since type_text uses the same check.

The two mock-based handler tests for the success path are replaced by
MCP harness tests.

@juliandescottes juliandescottes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

As mentioned in one of the inline comments, we are moving towards a more global solution for reporting (bug 2081596), so my main comment is about making sure we can reuse this work rather than having to rework it completely.

Comment thread src/firefox/dom.ts Outdated
}
await this.sendKeyActions(context, actions);

return await this.getFocusedElement(frame);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are working on consistent reporting across tools (bug 2081596), we should keep this lookup reusable.

Could you expose it as its own method on FirefoxClient (for example getFocusedElement()) and call it from handleTypeText after typing? typeText can then go back to returning void.

press_key and the other input tools can reuse it later. It would also be useful for bug 2081604, which will add a wait after input actions: the value should be read after that wait, not inside typeText.

Comment thread src/firefox/dom.ts Outdated
if (!el) {
return { tag: 'none' };
}
return ['input', 'textarea', 'select'].includes(el.localName)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid returning the value of password inputs. take_snapshot already leaves it out (see treeWalker.ts), and type_text should do the same. Returning only the tag is fine.

Comment thread src/tools/input.ts Outdated
const target = uid ? ` on ${uid}` : '';
const suffix = submitKey ? `, then ${submitKey}` : '';
return successResponse(`type_text ${text.length} chars${target}${suffix}`);
const value = focused.value === undefined ? '' : `, value: ${JSON.stringify(focused.value)}`;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a large textarea the reply could be very long. Could you truncate the value, e.g. with truncateText?

Comment thread src/firefox/dom.ts Outdated
}
await this.sendKeyActions(context, actions);

return await this.getFocusedElement(frame);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check what happens if the uid is inside an iframe and the submit key moves focus out of the iframe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! You're right, it was reporting body from the iframe instead of the input that got focus. It now always starts from the top-level page, and I added a test for that.

…ew comments

typeText returns void again, and handleTypeText reads the focused element
through the new FirefoxClient.getFocusedElement(). The lookup always
starts from the top-level document, so it follows focus when the submit
key moves it out of an iframe.

The value of password inputs is left out, and long values are truncated.
@juliandescottes juliandescottes added the run-integration Trigger integration tests for a PR label Nov 30, 2026

@juliandescottes juliandescottes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, all tests are passing and the new version looks good.

@juliandescottes
juliandescottes merged commit faddb6a into mozilla:main Nov 30, 2026
6 checks passed
@juliandescottes

Copy link
Copy Markdown
Collaborator

Thank you for your patch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-integration Trigger integration tests for a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants