Repository navigation
Bug 2081626 - [firefox-mcp] Report the focused element and its value after type_text - #255
Conversation
…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
left a comment
There was a problem hiding this comment.
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.
| } | ||
| await this.sendKeyActions(context, actions); | ||
|
|
||
| return await this.getFocusedElement(frame); |
There was a problem hiding this comment.
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.
| if (!el) { | ||
| return { tag: 'none' }; | ||
| } | ||
| return ['input', 'textarea', 'select'].includes(el.localName) |
There was a problem hiding this comment.
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.
| 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)}`; |
There was a problem hiding this comment.
With a large textarea the reply could be very long. Could you truncate the value, e.g. with truncateText?
| } | ||
| await this.sendKeyActions(context, actions); | ||
|
|
||
| return await this.getFocusedElement(frame); |
There was a problem hiding this comment.
Can you check what happens if the uid is inside an iframe and the submit key moves focus out of the iframe?
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Thanks for the update, all tests are passing and the new version looks good.
|
Thank you for your patch! |
Problem
type_textonly 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 fromtype_text.Fix
FirefoxClient.getFocusedElement(), called byhandleTypeTextafter typing. It returns the tag of the element that has focus, plus itsvalueforinput,textareaandselect. 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>.Focused: div. After a submit key like Tab, it shows the element that has focus afterwards.type_textdescription and regenerateddocs/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.tests/integration/mcp/input.test.tsfor the reply format (value is JSON-quoted; long value truncated; no value for a contenteditable). It replaces the two mock-based success tests forhandleTypeTextintests/unit/tools/input.test.ts, following docs/testing.md.npm run typecheck,typecheck:tests, lint andformat:checkpass. Unit tests pass, apart fromsave-output.test.ts > should not be fooled by a symlink out of the allowed root, which also fails onmainon 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) andrestricted-session-restart.test.ts(5 s timeout). Firefox 160.0.1, Node 24.