replywizard-whatsapp-mcp 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replywizard-whatsapp-mcp",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Local MCP server that exposes WhatsApp Web (read + value tools) to AI clients via the Reply Wizard Chrome extension.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/daemon.js CHANGED
@@ -89,7 +89,26 @@ export function createDaemon({
89
89
  return [`127.0.0.1:${port}`, `localhost:${port}`];
90
90
  };
91
91
 
92
+ // Relay the distinct names of currently-connected MCP clients to the extension,
93
+ // so the options page can show which AI assistants are attached. Names come from
94
+ // each client's `initialize` clientInfo (the stdio connector pumps frames raw, so
95
+ // the real client's name — "Antigravity", "Cursor", … — reaches us verbatim).
96
+ function publishClients() {
97
+ if (!bridge || typeof bridge.setConnectedClients !== 'function') return;
98
+ const seen = new Set();
99
+ const clients = [];
100
+ for (const s of sessions.values()) {
101
+ const name = (s.clientInfo && s.clientInfo.name) || 'Unknown assistant';
102
+ if (seen.has(name)) continue;
103
+ seen.add(name);
104
+ clients.push({ name, version: (s.clientInfo && s.clientInfo.version) || null });
105
+ }
106
+ bridge.setConnectedClients(clients);
107
+ }
108
+
92
109
  async function newSession(req, res, body) {
110
+ // The client's name/version live in the initialize request's clientInfo.
111
+ const clientInfo = (body && body.params && body.params.clientInfo) || null;
93
112
  // Capture the session id at init: transport.sessionId may already be cleared by
94
113
  // the time onclose fires, so we must not rely on it for cleanup.
95
114
  let sid = null;
@@ -97,9 +116,13 @@ export function createDaemon({
97
116
  sessionIdGenerator: () => randomUUID(),
98
117
  enableDnsRebindingProtection: true,
99
118
  allowedHosts: allowedHosts(),
100
- onsessioninitialized: (id) => { sid = id; sessions.set(id, { transport, server, lastSeen: now() }); },
119
+ onsessioninitialized: (id) => {
120
+ sid = id;
121
+ sessions.set(id, { transport, server, lastSeen: now(), clientInfo });
122
+ publishClients();
123
+ },
101
124
  });
102
- transport.onclose = () => { if (sid) sessions.delete(sid); };
125
+ transport.onclose = () => { if (sid && sessions.delete(sid)) publishClients(); };
103
126
  const server = createMcpServer(bridge);
104
127
  await server.connect(transport);
105
128
  await transport.handleRequest(req, res, body);
package/src/protocol.js CHANGED
@@ -43,7 +43,9 @@ export const CONTROL = Object.freeze({
43
43
  PAIRING_REQUEST: 'pairing_request',
44
44
  PAIRING_RESPONSE: 'pairing_response',
45
45
  PAIRING_COMPLETE: 'pairing_complete',
46
+ PAIRING_FAILED: 'pairing_failed',
46
47
  AUTH_SUCCESS: 'auth_success',
48
+ CLIENTS_UPDATE: 'clients_update',
47
49
  PING: 'ping',
48
50
  PONG: 'pong',
49
51
  });
package/src/wsServer.js CHANGED
@@ -23,9 +23,10 @@
23
23
 
24
24
  import { WebSocketServer } from 'ws';
25
25
  import { randomBytes, randomUUID } from 'node:crypto';
26
- import { homedir } from 'node:os';
26
+ import { homedir, platform } from 'node:os';
27
27
  import { join } from 'node:path';
28
28
  import { mkdirSync, readFileSync, writeFileSync, rmSync } from 'node:fs';
29
+ import { spawn } from 'node:child_process';
29
30
  import { CONTROL, DEFAULT_PORT, PROTOCOL_VERSION, VALUE_ACTIONS } from './protocol.js';
30
31
 
31
32
  const log = (...args) => console.error('[replywizard-mcp]', ...args);
@@ -57,6 +58,45 @@ function sixDigitCode() {
57
58
  return String(parseInt(randomBytes(4).toString('hex'), 16) % 1_000_000).padStart(6, '0');
58
59
  }
59
60
 
61
+ // Surface the pairing code as an OS desktop notification so a non-technical user
62
+ // sees it WITHOUT a terminal (the connector launches us in the background, where
63
+ // stderr is invisible). This is a valid out-of-band channel: only someone at the
64
+ // physical machine sees the toast, so it does not weaken pairing the way echoing
65
+ // the code over the (untrusted) extension socket would. Best-effort: never throws,
66
+ // never blocks (detached + unref + stdio ignore), silent if the tool is missing —
67
+ // the 0600 file and stderr remain as backstops. `code` is digits-only (no shell/
68
+ // AppleScript injection risk). Exported for tests.
69
+ export function notifyPairingCode(code) {
70
+ const title = 'Reply Wizard pairing code';
71
+ const body = `Enter ${code} in Reply Wizard (AI Assistants > MCP) to connect.`;
72
+ try {
73
+ let cmd, args;
74
+ const os = platform();
75
+ if (os === 'darwin') {
76
+ cmd = 'osascript';
77
+ args = ['-e', `display notification "${body}" with title "${title}"`];
78
+ } else if (os === 'win32') {
79
+ // PowerShell balloon via WinForms — no external module required.
80
+ const ps =
81
+ 'Add-Type -AssemblyName System.Windows.Forms;' +
82
+ '$n=New-Object System.Windows.Forms.NotifyIcon;' +
83
+ '$n.Icon=[System.Drawing.SystemIcons]::Information;' +
84
+ `$n.BalloonTipTitle=${JSON.stringify(title)};` +
85
+ `$n.BalloonTipText=${JSON.stringify(body)};` +
86
+ '$n.Visible=$true;$n.ShowBalloonTip(20000);Start-Sleep -Seconds 8;$n.Dispose()';
87
+ cmd = 'powershell';
88
+ args = ['-NoProfile', '-WindowStyle', 'Hidden', '-Command', ps];
89
+ } else {
90
+ // Linux / *nix: libnotify.
91
+ cmd = 'notify-send';
92
+ args = ['-u', 'normal', '-t', '30000', title, body];
93
+ }
94
+ const child = spawn(cmd, args, { stdio: 'ignore', detached: true });
95
+ child.on('error', () => {}); // tool absent (e.g. no notify-send) — stay silent
96
+ child.unref();
97
+ } catch { /* notifications are best-effort */ }
98
+ }
99
+
60
100
  export class ExtensionBridge {
61
101
  constructor(opts = {}) {
62
102
  const { port = DEFAULT_PORT, host = '127.0.0.1' } = opts;
@@ -71,6 +111,11 @@ export class ExtensionBridge {
71
111
  this.requestTimeoutMs = opts.requestTimeoutMs ?? 25_000;
72
112
  this.listening = false;
73
113
  this.portConflict = false; // true if the port was already in use at startup
114
+ // Desktop-notification fn for the pairing code (injectable for tests).
115
+ this._notify = opts.notify ?? notifyPairingCode;
116
+ // Connected MCP clients (by name), set by the daemon and relayed to the
117
+ // extension so the options page can show which AI assistants are attached.
118
+ this.connectedClients = [];
74
119
  // Concurrency scheduler (WS-A3): all client sessions multiplex onto the single
75
120
  // extension socket. Reads and value-actions run in INDEPENDENT lanes so a slow
76
121
  // value-action (transcribe/summarize/suggest → LLM/STT) never head-of-line-blocks
@@ -150,15 +195,18 @@ export class ExtensionBridge {
150
195
  if (token && this.sessionToken && token === this.sessionToken) {
151
196
  this.paired = true;
152
197
  this._send({ method: CONTROL.AUTH_SUCCESS, params: { protocol: PROTOCOL_VERSION } });
198
+ this._sendClients(); // sync current assistants to a freshly reconnected extension
153
199
  log('Reconnected with a valid session token (no pairing needed).');
154
200
  } else {
155
201
  this.pairingCode = sixDigitCode();
156
202
  writePairingCode(this.pairingCode);
203
+ try { this._notify(this.pairingCode); } catch { /* notifier is best-effort */ }
157
204
  log('');
158
205
  log(' ┌──────────────────────────────────────────────┐');
159
206
  log(` │ Reply Wizard pairing code: ${this.pairingCode} │`);
160
207
  log(' └──────────────────────────────────────────────┘');
161
208
  log(' Enter it in Reply Wizard → popup → AI client access.');
209
+ log(' (A desktop notification with the code should also appear.)');
162
210
  log(` (Running in the background? Read it from ${pairingFile()})`);
163
211
  this._send({ method: CONTROL.PAIRING_REQUEST, params: { hub_name: 'Reply Wizard MCP', protocol: PROTOCOL_VERSION } });
164
212
  }
@@ -174,8 +222,13 @@ export class ExtensionBridge {
174
222
  clearPairingCode();
175
223
  this._send({ method: CONTROL.PAIRING_COMPLETE, params: { session_id: randomUUID(), session_token: this.sessionToken } });
176
224
  this._send({ method: CONTROL.AUTH_SUCCESS, params: {} });
225
+ this._sendClients(); // sync current assistants right after pairing
177
226
  log('Pairing approved.');
178
227
  } else {
228
+ // Tell the extension so it can show "incorrect code" instead of just
229
+ // silently staying on "Pairing needed". A rejected code keeps the same
230
+ // pairing code active so the user can retry without a new handshake.
231
+ this._send({ method: CONTROL.PAIRING_FAILED, params: { reason: 'invalid_code' } });
179
232
  log('Pairing rejected or wrong code.');
180
233
  }
181
234
  return;
@@ -280,6 +333,18 @@ export class ExtensionBridge {
280
333
  if (this.socket && this.socket.readyState === this.socket.OPEN) this.socket.send(JSON.stringify(obj));
281
334
  }
282
335
 
336
+ // Set the list of connected MCP clients (called by the daemon on session
337
+ // open/close) and relay it to the extension. Idempotent and safe to call when
338
+ // no extension is connected (the _send is a no-op then; re-sent on next auth).
339
+ setConnectedClients(clients) {
340
+ this.connectedClients = Array.isArray(clients) ? clients : [];
341
+ this._sendClients();
342
+ }
343
+
344
+ _sendClients() {
345
+ this._send({ method: CONTROL.CLIENTS_UPDATE, params: { clients: this.connectedClients } });
346
+ }
347
+
283
348
  // Reject every queued and in-flight action (e.g. on disconnect / shutdown).
284
349
  _failAll(err) {
285
350
  for (const lane of Object.values(this.lanes)) {