`switch_app()` reports success without verifying the window reached the foreground
Summary
switch_app() (src/windows_mcp/desktop/service.py:537) calls bring_window_to_top() and then unconditionally returns a success string:
def switch_app(self, name: str):
try:
window, error = self._find_window_by_name(name)
if window is None:
return error, 1
target_handle = window.handle
was_minimized = uia.IsIconic(target_handle)
self.bring_window_to_top(target_handle)
if was_minimized:
content = f"Restored {window.name.title()} from minimized and switched to it."
else:
content = f"Switched to {window.name.title()} window."
return content, 0GetForegroundWindow() is never compared against target_handle afterwards. Windows frequently refuses foreground changes (SetForegroundWindow is subject to the foreground lock), so the call can do nothing at all and still report status 0.
Impact
This is the worst possible failure mode for an agent: the tool asserts the target is focused, so the agent proceeds to send keystrokes, and they land in whatever window actually holds focus.
Observed directly — App(mode="switch", name="hello") returned:
Switched to *Hello - Notepad window.while the immediately following Snapshot() showed:
Focused Window:
Name Depth Status Width Height Handle
------ ------- --------- ------- -------- --------
Claude 16 Maximized 1942 1054 265138Notepad never came forward. A subsequent Shortcut("alt+f4") therefore went to the wrong application. Two consecutive switch calls both reported success without ever changing focus.
Note on #314
This is not the opt-in expected_window_title / expected_process guard API proposed in #314 (closed as NOT_PLANNED). That was a new feature for input tools. This is a correctness bug in an existing function's return value: switch_app already knows the handle it was asked to activate and already returns a status code, so it can verify its own postcondition with no API change and no new parameters.
Reproduction
Depends on Windows' foreground lock, so it is timing-sensitive, but it was reliable here while the calling application (Claude Desktop) held focus and was actively rendering.
App(mode="switch", name="<some background window>")
Snapshot() # Focused Window is unchanged, but switch reported successSuggested fix
After bring_window_to_top(), poll win32gui.GetForegroundWindow() against target_handle for a short bounded interval, and return a non-zero status with an explicit message if it never matches:
self.bring_window_to_top(target_handle)
if not self._wait_for_foreground(target_handle, timeout=1.0):
return (
f"Failed to bring {window.name} to the foreground "
f"(foreground is still {win32gui.GetWindowText(win32gui.GetForegroundWindow())!r}).",
1,
)The same postcondition check would be worth applying in App(mode="launch"), which has a comparable "sent, but window not detected yet" path.
Environment
- Windows-MCP: 0.7.2 (Claude Desktop extension channel, ant.dir.cursortouch.windows-mcp)
- OS: Windows 11 Pro 10.0.26200
- Python: CPython 3.13.15 (uv-managed)
- uv: 0.12.5
- Displays: 2 - primary 2560x1600 (laptop, Intel iGPU), secondary 1920x1080 (external, NVIDIA RTX 5070 Laptop)
- GPUs: hybrid - Device[0] Intel(R) Graphics, Device[1] NVIDIA GeForce RTX 5070 Laptop GPU
- Screenshot backend: autoSource: CursorTouch/Windows-MCP