Skip to content

Conversation

@AdamParker19
Copy link

Adds geolocation mocking support to Playwright MCP via a new \�rowser_set_geolocation\ tool that uses the browser context's setGeolocation() method. Also adds \�rowser_clear_geolocation\ to reset the mocked state.

The tool:

  • Accepts latitude (-90 to 90), longitude (-180 to 180), and optional accuracy
  • Automatically grants geolocation permissions
  • Works at the browser context level, affecting all pages

Fixes microsoft/playwright-mcp#1114

import { defineTool } from './tool';

const setGeolocation = defineTool({
capability: 'core',
Copy link
Member

Choose a reason for hiding this comment

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

These don't sound core to me, please kick them out into a separate geolocation capability.

Copy link
Member

@pavelfeldman pavelfeldman left a comment

Choose a reason for hiding this comment

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

Can't have a patch w/o tests either.

@AdamParker19
Copy link
Author

@pavelfeldman

Thanks for the review! I've addressed both feedback items:

1. Separate capability: Changed from capability: 'core' to capability: 'geolocation'. The tools now require --caps=geolocation to enable.

2. Added tests: Created tests/mcp/geolocation.spec.ts with:

  • Capability availability test (verifies tools appear with --caps=geolocation)
  • browser_set_geolocation functional test (sets coords, navigates, verifies mocked location)
  • browser_clear_geolocation test

All 3 tests pass locally.

Let me know your feedback :)

accuracy: params.accuracy ?? 0,
});

response.addCode(`await context.grantPermissions(['geolocation']);`);
Copy link
Member

Choose a reason for hiding this comment

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

Let's generate page.context().grantPermission to give LLM a better clue if it needs to generate the source code.

const browserContext = await context.ensureBrowserContext();

// Clear permissions revokes geolocation access
await browserContext.clearPermissions();
Copy link
Member

Choose a reason for hiding this comment

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

You would want to keep the permission and run browserContext.setGeolocation(null) instead.

import { defineTool } from './tool';

const setGeolocation = defineTool({
capability: 'geolocation',
Copy link
Member

Choose a reason for hiding this comment

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

This property is typed, make sure you update the types and the docs.

Adds geolocation mocking support to Playwright MCP via a new
\�rowser_set_geolocation\ tool that uses the browser context's
setGeolocation() method. Also adds \�rowser_clear_geolocation\
to reset the mocked state.

The tool:
- Accepts latitude (-90 to 90), longitude (-180 to 180), and optional accuracy
- Automatically grants geolocation permissions
- Works at the browser context level, affecting all pages

Fixes microsoft/playwright-mcp#1114
Addresses PR review feedback:
- Changed capability from 'core' to 'geolocation'
- Tools now require --caps=geolocation flag to enable
- Added geolocation.spec.ts with tests for:
  - Capability availability with --caps=geolocation
  - browser_set_geolocation functional test
  - browser_clear_geolocation test
- Use page.context().grantPermissions/setGeolocation for better LLM code hints
- Use setGeolocation(null) instead of clearPermissions() to preserve permissions
- Add 'geolocation' to ToolCapability type in config.d.ts
- Update capabilities documentation in config.d.ts
- Update --caps CLI help text to include geolocation
- Update test assertions to match new generated code format
@AdamParker19
Copy link
Author

Thanks for the detailed review @pavelfeldman! I've addressed all feedback and rebased on latest main:

Changes made:

1. Generated code uses page.context() for better LLM hints

- await context.grantPermissions(['geolocation']);
- await context.setGeolocation({ latitude: 37.77, longitude: -122.42, accuracy: 100 });
+ await page.context().grantPermissions(['geolocation']);
+ await page.context().setGeolocation({ latitude: 37.77, longitude: -122.42, accuracy: 100 });

2. browser_clear_geolocation now uses setGeolocation(null) to preserve permissions

- await browserContext.clearPermissions();
+ await browserContext.setGeolocation(null);

3. Updated types and documentation

  • Added 'geolocation' to ToolCapability type in config.d.ts
  • Updated capability documentation
  • Updated --caps CLI help text

4. Rebased on latest main - resolved conflict with new --allow-unrestricted-file-access option

All 3 tests pass locally:

✓ test geolocation capability available with --caps=geolocation
✓ browser_set_geolocation  
✓ browser_clear_geolocation

@github-actions
Copy link
Contributor

github-actions bot commented Jan 2, 2026

Test results for "MCP"

14 failed
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [msedge] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [chromium] › mcp/test-debug.spec.ts:48 › test_debug (pause/resume) @mcp-macos-15
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15

2847 passed, 116 skipped


Merge workflow run.

Fixed 41 ESLint indentation errors - project style requires 2-space
indentation, not 4-space.
@AdamParker19
Copy link
Author

Good morning and Happy new year to you @pavelfeldman

I have fixed the indentation issue and the docs & lint test should pass now what do you want me to do about the other tests please let me know

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.

[Feature] Add geolocation mocking support

2 participants