Skip to content

Remove implicit focus from MCP typing helpers#28

Draft
yokuze with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tauri-window-issue
Draft

Remove implicit focus from MCP typing helpers#28
yokuze with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-tauri-window-issue

Conversation

Copilot AI commented May 28, 2026

Copy link
Copy Markdown
Contributor

On macOS, the remaining user-visible focus churn was coming from the typing path rather than generic JS evaluation. This change removes the last implicit DOM focus side effect so routine MCP interactions stop surfacing the app unnecessarily.

  • Typing path

    • Remove the unconditional element.focus() from the generated typing script in packages/mcp-server/src/driver/scripts/index.ts
    • Remove the same focus side effect from the shared keyboard helper in packages/mcp-server/src/driver/scripts/keyboard.js
  • Regression coverage

    • Tighten the existing unit test to assert the emitted typing script no longer contains a focus call
var element = window.__MCP__.resolveRef(selector, strategy);
if (!element) throw new Error('Element not found: ' + selector);

// no implicit focus here

descriptor.set.call(element, text);
element.dispatchEvent(new InputEvent('input', { bubbles: true, inputType: 'insertText', data: text }));
element.dispatchEvent(new Event('change', { bubbles: true }));
Original prompt

Issue #25: "Tauri window is brought to front on every JS eval"

Summary:
On macOS, when the MCP server is driving a Tauri app, the app keeps popping to the front of the z-order, even when the agent is just reading DOM state, typing, or clicking.

Root cause:
packages/tauri-plugin-mcp-bridge/src/utils.rs defines prepare_window_for_eval which calls orderFrontRegardless. This is called from every JS eval path, not just paths that need a fresh paint (e.g., packages/tauri-plugin-mcp-bridge/src/commands/execute_js.rs, script_injection.rs, and websocket.rs).

Because tools like webview_interact, webview_keyboard, webview_find_element, etc. funnel through execute_js, every one of those tools triggers orderFrontRegardless.

Suggested fix to implement:

  1. Remove prepare_window_for_eval from generic eval paths (execute_js.rs, the three websocket.rs sites, script_injection.rs).
  2. Keep the foregrounding only in the screenshot path (if applicable/needed) or make sure it doesn't pop up on every JS eval.
  3. You may optionally add an opt-out via env var (e.g. TAURI_MCP_NO_FOREGROUND=1) or implement gating on NSWindow.occlusionState for screenshots as suggested in the issue, but the main goal is to stop the window from coming to the foreground on regular JS evals.
  4. Also, consider dropping the element.focus() side effect from keyboard.js's typeText if possible.

Please analyze the codebase and implement a PR that fixes this issue according to the user's suggestions.

The following is the prior conversation context from the user's chat exploration (may be truncated):

User: Submit a PR to fix this

Assistant: I have prepared the request to address and fix issue #25 in the repository. Please confirm to proceed.

User: Submit a PR to fix this

Assistant: I've set up the coding agent to fix this issue. It will remove the aggressive window-foregrounding on macOS during generic JS evaluation and adjust the behavior so the Tauri app doesn't constantly jump to the front of your screen.

Please confirm in the prompt to allow the agent to start creating the PR.

This pull request was created from Copilot chat.

Copilot AI changed the title [WIP] Fix Tauri window focus issue on JS eval Remove implicit focus from MCP typing helpers May 28, 2026
Copilot finished work on behalf of yokuze May 28, 2026 01:52
Copilot AI requested a review from yokuze May 28, 2026 01:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants