Skip to content

Commit 190952b

Browse files
committed
fix(mcp): share one token across servers and send dispatches to the Store tab
- mcp-token: create the token file with an exclusive link from a 0600 temp file; when another server wrote it first, keep and return its token - devframe: a dispatch, dispatch-again or back-to-latest request without pageId counts only tabs with an @ngrx/store Store; a restore still counts every tab, since it can target a signal store - docs: list allowLan for the Express hub in the Capacitor setup table, and date the privacy policy change Refs #375
1 parent ed42596 commit 190952b

6 files changed

Lines changed: 98 additions & 13 deletions

File tree

‎apps/docs/src/content/guides/capacitor.md‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ This guide adds the devtools to an Ionic or Capacitor *Angular app. The app runs
2020

2121
## Which setup you need
2222

23-
| Your devtools server | iOS simulator, Android emulator or USB device | Phone over Wi-Fi, and any iPhone or iPad |
24-
| -------------------------------- | ----------------------------------------------------- | -------------------------------------------------------------------------- |
25-
| Express hub in `server.ts` | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `allowedOrigins` |
26-
| Vite plugin (Analog) | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `allowLan` |
27-
| Standalone CLI behind `ng serve` | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `--allowed-origin` |
28-
| Standalone CLI on its own | [A bundled build](#a-bundled-build) | [A bundled build](#a-bundled-build) |
23+
| Your devtools server | iOS simulator, Android emulator or USB device | Phone over Wi-Fi, and any iPhone or iPad |
24+
| -------------------------------- | ----------------------------------------------------- | --------------------------------------------------------------------------------------- |
25+
| Express hub in `server.ts` | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `allowLan` and `allowedOrigins` |
26+
| Vite plugin (Analog) | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `allowLan` |
27+
| Standalone CLI behind `ng serve` | [Live reload on localhost](#live-reload-on-localhost) | [Live reload over Wi-Fi](#live-reload-over-wi-fi), with `--allowed-origin` |
28+
| Standalone CLI on its own | [A bundled build](#a-bundled-build) | [A bundled build](#a-bundled-build) |
2929

3030
USB applies to Android only: `adb reverse` forwards the port. An iPhone or iPad reaches your machine over the network, even when it is plugged in. Over Wi-Fi, the page's origin is your machine's network address, not `localhost`, so the server has to allow that origin. A bundled build works with the standalone CLI and the Express hub, and with the Vite plugin for iOS. See [Run the server where the device reaches it](#run-the-server-where-the-device-reaches-it).
3131

‎docs/privacy-policy.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</head>
1818
<body>
1919
<h1>Privacy Policy</h1>
20-
<p class="updated">Last updated: September 30, 2026</p>
20+
<p class="updated">Last updated: October 12, 2026</p>
2121

2222
<p>Pangular Inspector ("the Extension") is a browser extension that helps developers inspect Angular applications. This privacy policy explains what data the Extension accesses and how it is used.</p>
2323

‎packages/devtools/src/__tests__/hub.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { mkdtempSync, readFileSync, statSync, writeFileSync } from 'node:fs';
1+
import { mkdtempSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
22
import { connect } from 'node:net';
33
import { tmpdir } from 'node:os';
44
import { join } from 'node:path';
55
import { Hono } from 'hono';
66
import { afterEach, describe, expect, it, vi } from 'vitest';
77
import { hubDefaultOrigins, initPangularHub, type PangularHubOptions } from '../hub.ts';
88
import { httpRegistry } from '../http-rules.ts';
9+
import { writeTokenOnce } from '../mcp-token.ts';
910
import { BASE_FILES, makeProject } from './analog-fixture.ts';
1011

1112
const hubs: { close: () => Promise<void> }[] = [];
@@ -320,6 +321,21 @@ describe('Pangular Inspector hub MCP route', () => {
320321
if (process.platform !== 'win32') expect(statSync(file).mode & 0o777).toBe(0o600);
321322
});
322323

324+
it('keeps the token another server wrote first, so both servers share one token', () => {
325+
const dir = mkdtempSync(join(tmpdir(), 'pangular-token-'));
326+
const file = join(dir, 'mcp-token');
327+
expect(writeTokenOnce(file, 'first-server-token-0001')).toBe('first-server-token-0001');
328+
// The second server read no file, then the first one wrote it before the second could.
329+
expect(writeTokenOnce(file, 'second-server-token-002')).toBe('first-server-token-0001');
330+
expect(readFileSync(file, 'utf8').trim()).toBe('first-server-token-0001');
331+
if (process.platform !== 'win32') expect(statSync(file).mode & 0o777).toBe(0o600);
332+
// A file without a valid token is replaced.
333+
writeFileSync(file, 'bad\n');
334+
expect(writeTokenOnce(file, 'third-server-token-0003')).toBe('third-server-token-0003');
335+
expect(readFileSync(file, 'utf8').trim()).toBe('third-server-token-0003');
336+
expect(readdirSync(dir)).toEqual(['mcp-token']);
337+
});
338+
323339
it('keeps the token and the mcp setting next to the devtools config', async () => {
324340
vi.stubEnv('PANGULAR_MCP_TOKEN', 'env-secret');
325341
const guarded = await bootMcp({ inspectors: { http: false }, agent: { readOnly: true } });

‎packages/devtools/src/__tests__/ngrx-mcp.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,38 @@ describe('dispatch-ngrx-action tool', () => {
220220
expect(sent.map((options) => options.args[0].pageId)).toEqual(['p1', 'p2']);
221221
});
222222

223+
it('counts only tabs with a Store for a dispatch without pageId, and every tab for a restore', async () => {
224+
const { ctx, push } = await boot();
225+
const sent: Broadcast[] = [];
226+
vi.spyOn(ctx.rpc, 'broadcast').mockImplementation((async (options: Broadcast) => {
227+
if (!options.method.endsWith('ngrx-action')) return;
228+
sent.push(options);
229+
const { requestId, pageId } = options.args[0];
230+
setTimeout(
231+
() => void push('ngrx-action-result', { requestId, pageId, result: { ok: true } }),
232+
5,
233+
);
234+
}) as never);
235+
await push('push-ngrx-state', page('p1', true));
236+
await push('push-ngrx-state', page('p2', false));
237+
238+
// p2 reports only signal stores, so the dispatch has one place to go.
239+
for (const request of [
240+
{ type: 'dispatch', action: '[A] B' },
241+
{ type: 'dispatch-again', seq: 1 },
242+
{ type: 'latest' },
243+
]) {
244+
expect(await push('request-ngrx-action', { request })).toEqual({ ok: true });
245+
}
246+
expect(sent.map((options) => options.args[0].pageId)).toEqual(['p1', 'p1', 'p1']);
247+
248+
// A restore can target a signal store on either tab, so it still needs a page.
249+
expect(await push('request-ngrx-action', { request: { type: 'restore', seq: 1 } })).toEqual({
250+
error: expect.stringMatching(/^2 pages report NgRx state: /),
251+
});
252+
expect(sent).toHaveLength(3);
253+
});
254+
223255
it('is left out when the ngrx write action is off, and the RPC refuses dispatches', async () => {
224256
const { ctx, push } = await boot({ actions: { ngrx: false } });
225257
const tools = ctx.agent.list().tools.map((tool) => tool.id);

‎packages/devtools/src/devframe.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,10 @@ const pangular = defineDevframe({
19011901
const requestNgrxAction = (page: unknown, request: unknown) =>
19021902
new Promise<NgrxRequestResult>((resolve) => {
19031903
if (!config.actions.ngrx) return resolve({ error: actionBlockedMessage('ngrx') });
1904-
const pages = ngrxStateOf(ngrxPages).pages;
1904+
const all = ngrxStateOf(ngrxPages).pages;
1905+
// A restore can target a signal store; every other request needs the classic Store.
1906+
const kind = (request as { type?: unknown } | null)?.type;
1907+
const pages = kind === 'restore' ? all : all.filter((p) => p.classic);
19051908
// Without a page, the request would reach every tab, or one the caller did not choose.
19061909
if (typeof page !== 'string' && pages.length !== 1) {
19071910
return resolve({

‎packages/devtools/src/mcp-token.ts‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
import { randomBytes } from 'node:crypto';
2-
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2+
import {
3+
chmodSync,
4+
linkSync,
5+
mkdirSync,
6+
readFileSync,
7+
renameSync,
8+
rmSync,
9+
writeFileSync,
10+
} from 'node:fs';
311
import { homedir } from 'node:os';
412
import { dirname, join } from 'node:path';
513

@@ -26,6 +34,33 @@ function readToken(file: string): string | undefined {
2634
}
2735
}
2836

37+
/**
38+
* Puts `token` in `file` unless another process created the file first, and
39+
* returns the token the file holds. The token goes to a temp file in the same
40+
* folder, which is then linked to `file`: the link fails when `file` exists,
41+
* so two servers starting at once end up with the same token.
42+
*/
43+
export function writeTokenOnce(file: string, token: string): string {
44+
const temp = `${file}.${process.pid}.${randomBytes(6).toString('hex')}.tmp`;
45+
try {
46+
writeFileSync(temp, `${token}\n`, { mode: 0o600, flag: 'wx' });
47+
chmodSync(temp, 0o600);
48+
try {
49+
linkSync(temp, file);
50+
return token;
51+
} catch (error) {
52+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error;
53+
}
54+
const existing = readToken(file);
55+
if (existing) return existing;
56+
// The file holds no valid token: replace it in one step.
57+
renameSync(temp, file);
58+
return token;
59+
} finally {
60+
rmSync(temp, { force: true });
61+
}
62+
}
63+
2964
/**
3065
* The MCP token: `PANGULAR_MCP_TOKEN` when set, else the token in the token
3166
* file, else a new one written to that file with mode 0600. The token itself
@@ -39,12 +74,11 @@ export function loadMcpToken(
3974
if (fromEnv) return fromEnv;
4075
const file = mcpTokenFile(env);
4176
const existing = readToken(file);
42-
const token = existing ?? randomBytes(24).toString('base64url');
77+
let token = existing ?? randomBytes(24).toString('base64url');
4378
if (!existing) {
4479
try {
4580
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
46-
writeFileSync(file, `${token}\n`, { mode: 0o600 });
47-
chmodSync(file, 0o600);
81+
token = writeTokenOnce(file, token);
4882
} catch (error) {
4983
log(
5084
`\n Pangular Inspector could not write its MCP token to ${file}: ${(error as Error).message}\n` +

0 commit comments

Comments
 (0)