Skip to content

fix(mcp): await init page before screenshot#41646

Open
harshmathurx wants to merge 2 commits into
microsoft:mainfrom
harshmathurx:fix-1671
Open

fix(mcp): await init page before screenshot#41646
harshmathurx wants to merge 2 commits into
microsoft:mainfrom
harshmathurx:fix-1671

Conversation

@harshmathurx

Copy link
Copy Markdown

Summary

  • wait for MCP tab initialization before taking page screenshots
  • add a regression test for first-call browser_take_screenshot with --init-page

Fixes #41650

const screenshotTargetLabel = params.target ? params.element || 'element' : (params.fullPage ? 'full page' : 'viewport');
const target = params.target ? await tab.targetLocator({ element: params.element, target: params.target }) : null;
if (!target)
await tab.waitForInitialized();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it should be earlier in the pipeline, most likely in the context.ensureTab

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good call — ensureTab is the right place. That way all tools (snapshot, evaluate, screenshot, etc.) get the guarantee automatically instead of each one having to remember to await init. I will move the waitForInitialized() call into context.ensureTab() and keep the method on Tab for direct use when needed.

Per review feedback, await tab.waitForInitialized() in
context.ensureTab() so all tools benefit from waiting for
init-page scripts to complete, not just screenshot.

Keep waitForInitialized() on Tab for direct use when needed.
@harshmathurx

Copy link
Copy Markdown
Author

Moved waitForInitialized() from screenshot.ts into context.ensureTab() — now all tools (snapshot, evaluate, etc.) automatically wait for init-page scripts, not just screenshot. Kept the waitForInitialized() method on Tab for any direct use.

@harshmathurx

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Test results for "MCP"

1 failed
❌ [chromium] › mcp/annotate.spec.ts:447 › should switch screencast to -s session on show --annotate @mcp-macos-latest-chromium

7721 passed, 1235 skipped


Merge workflow run.

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.

[Bug]: --init-page hook races the first tool call -- Tab _initialize() is fire-and-forget and browser_take_screenshot does not await it

3 participants