Skip to content

Commit 139ce60

Browse files
authored
feat: Add offline network emulation support to emulate_network command (#326)
#320 Issue: It would be very useful to have the ability to simulate offline network conditions directly through the MCP emulate_network command, similar to how it's possible in DevTools. Currently, the command doesn’t support an Offline state, which limits testing for PWA and offline-first web applications. Adding this option would make it easier to automate and validate offline behavior through MCP scripts or tools that rely on this API. Solution - Add 'Offline' option to throttlingOptions for simulating offline network conditions - Update emulate_network tool to handle offline mode with proper network conditions - Update documentation to include offline option and usage instructions - Add test coverage for offline network emulation functionality - Enable testing of PWA and offline-first web applications through MCP
1 parent 4892a6e commit 139ce60

File tree

5 files changed

+43
-17
lines changed

5 files changed

+43
-17
lines changed

docs/tool-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,11 @@
200200

201201
### `emulate_network`
202202

203-
**Description:** Emulates network conditions such as throttling on the selected page.
203+
**Description:** Emulates network conditions such as throttling or offline mode on the selected page.
204204

205205
**Parameters:**
206206

207-
- **throttlingOption** (enum: "No emulation", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") **(required)**: The network throttling option to emulate. Available throttling options are: No emulation, Slow 3G, Fast 3G, Slow 4G, Fast 4G. Set to "No emulation" to disable.
207+
- **throttlingOption** (enum: "No emulation", "Offline", "Slow 3G", "Fast 3G", "Slow 4G", "Fast 4G") **(required)**: The network throttling option to emulate. Available throttling options are: No emulation, Offline, Slow 3G, Fast 3G, Slow 4G, Fast 4G. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.
208208

209209
---
210210

package-lock.json

Lines changed: 9 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040
"@modelcontextprotocol/sdk": "1.19.1",
4141
"core-js": "3.45.1",
4242
"debug": "4.4.3",
43-
"puppeteer-core": "24.23.0",
44-
"yargs": "18.0.0"
43+
"puppeteer-core": "^24.23.0",
44+
"yargs": "18.0.0",
45+
"zod": "^3.25.76"
4546
},
4647
"devDependencies": {
4748
"@eslint/js": "^9.35.0",

src/tools/emulation.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import {defineTool} from './ToolDefinition.js';
1212

1313
const throttlingOptions: [string, ...string[]] = [
1414
'No emulation',
15+
'Offline',
1516
...Object.keys(PredefinedNetworkConditions),
1617
];
1718

1819
export const emulateNetwork = defineTool({
1920
name: 'emulate_network',
20-
description: `Emulates network conditions such as throttling on the selected page.`,
21+
description: `Emulates network conditions such as throttling or offline mode on the selected page.`,
2122
annotations: {
2223
category: ToolCategories.EMULATION,
2324
readOnlyHint: false,
@@ -26,7 +27,7 @@ export const emulateNetwork = defineTool({
2627
throttlingOption: z
2728
.enum(throttlingOptions)
2829
.describe(
29-
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable.`,
30+
`The network throttling option to emulate. Available throttling options are: ${throttlingOptions.join(', ')}. Set to "No emulation" to disable. Set to "Offline" to simulate offline network conditions.`,
3031
),
3132
},
3233
handler: async (request, _response, context) => {
@@ -39,6 +40,17 @@ export const emulateNetwork = defineTool({
3940
return;
4041
}
4142

43+
if (conditions === 'Offline') {
44+
await page.emulateNetworkConditions({
45+
offline: true,
46+
download: 0,
47+
upload: 0,
48+
latency: 0,
49+
});
50+
context.setNetworkConditions('Offline');
51+
return;
52+
}
53+
4254
if (conditions in PredefinedNetworkConditions) {
4355
const networkCondition =
4456
PredefinedNetworkConditions[

tests/tools/emulation.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ import {withBrowser} from '../utils.js';
1111

1212
describe('emulation', () => {
1313
describe('network', () => {
14+
it('emulates offline network conditions', async () => {
15+
await withBrowser(async (response, context) => {
16+
await emulateNetwork.handler(
17+
{
18+
params: {
19+
throttlingOption: 'Offline',
20+
},
21+
},
22+
response,
23+
context,
24+
);
25+
26+
assert.strictEqual(context.getNetworkConditions(), 'Offline');
27+
});
28+
});
1429
it('emulates network throttling when the throttling option is valid ', async () => {
1530
await withBrowser(async (response, context) => {
1631
await emulateNetwork.handler(

0 commit comments

Comments
 (0)