mcp-teams 0.3.31 → 0.3.32

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.
@@ -159,10 +159,11 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
159
159
  * Waits until Teams sends the service a session after `since` (a new one, or the stored one
160
160
  * again); null on timeout. Listens for the capture instead of asking for it. `rejected`: the
161
161
  * headers Teams refused (401); that session sent again (by Teams, or replayed by another call)
162
- * doesn't count, only one with other credentials.
162
+ * doesn't count, only one with other credentials. `signal`: aborting it ends the wait (null).
163
163
  */
164
- function waitForCapture(service, since, timeoutMs, rejected = null) {
164
+ function waitForCapture(service, since, timeoutMs, rejected = null, signal = null) {
165
165
  const key = `capture:${service}`;
166
+ if (signal?.aborted) return Promise.resolve(null);
166
167
  return new Promise((resolve) => {
167
168
  let done = false;
168
169
  const finish = (cap) => {
@@ -171,8 +172,11 @@ function waitForCapture(service, since, timeoutMs, rejected = null) {
171
172
  clearTimeout(timer);
172
173
  chrome.storage.onChanged.removeListener(onChanged);
173
174
  captureSeen.delete(onSeen);
175
+ signal?.removeEventListener("abort", onAbort);
174
176
  resolve(cap);
175
177
  };
178
+ const onAbort = () => finish(null);
179
+ signal?.addEventListener("abort", onAbort);
176
180
  const check = async () => {
177
181
  const cap = await getCapture(service);
178
182
  if (cap && cap.at > since && !(rejected && sameHeaders(cap.headers, rejected))) finish(cap);
@@ -301,7 +305,8 @@ async function waitForTabLoaded(tabId, timeoutMs = 30_000) {
301
305
  if (await waitForTab(tabId, (t) => t.status === "complete", timeoutMs)) await sleep(SETTLE_MS);
302
306
  }
303
307
 
304
- const refreshing = new Map(); // service -> Promise<capture|null>, so parallel calls for a service wait once
308
+ // service -> { promise (capture|null), waiting (calls), kept, stop }, so parallel calls for a service wait once
309
+ const refreshing = new Map();
305
310
 
306
311
  /**
307
312
  * A captured token is lost when the extension reloads and expires after about an hour.
@@ -313,21 +318,43 @@ const refreshing = new Map(); // service -> Promise<capture|null>, so parallel c
313
318
  * used, so a tab that just loaded wouldn't bring it. Nor does a user's tab that is still loading:
314
319
  * it calls its services by itself, so the call waits for that (at most LOADING_TAB_WAIT_MS).
315
320
  * `rejected`: the headers of a session Teams refused (401), which doesn't count as a fresh one.
321
+ * A call (`ctx`) that is withdrawn stops waiting at once (its reason); once every call waiting
322
+ * for the service was withdrawn, the wait itself is dropped, so the background tab doesn't stay
323
+ * open for nobody (a call without a ctx never withdraws).
316
324
  */
317
- function refreshSession(service, tab, { quickWaitMs = 4_000, rejected = null } = {}) {
318
- if (!refreshing.has(service)) {
325
+ function refreshSession(service, tab, { quickWaitMs = 4_000, rejected = null } = {}, ctx = null) {
326
+ if (ctx?.cancelled) return Promise.reject(ctx.reason);
327
+ let entry = refreshing.get(service);
328
+ if (!entry) {
319
329
  const since = Date.now();
320
- const p = (async () => {
330
+ const stop = new AbortController();
331
+ const { signal } = stop;
332
+ entry = { waiting: 0, kept: false, stop };
333
+ const self = entry;
334
+ entry.promise = (async () => {
321
335
  const current = await chrome.tabs.get(tab.id).catch(() => null);
322
- if (current?.status === "loading") return waitForCapture(service, since, LOADING_TAB_WAIT_MS, rejected);
323
- const quick = quickWaitMs ? await waitForCapture(service, since, quickWaitMs, rejected) : null;
336
+ if (current?.status === "loading") return waitForCapture(service, since, LOADING_TAB_WAIT_MS, rejected, signal);
337
+ const quick = quickWaitMs ? await waitForCapture(service, since, quickWaitMs, rejected, signal) : null;
324
338
  if (quick) return quick;
325
- if (service === "substrate") return null;
326
- return recaptureInHelperTab(service, teamsOrigin(tab), since, rejected);
327
- })().finally(() => refreshing.delete(service));
328
- refreshing.set(service, p);
339
+ if (service === "substrate" || signal.aborted) return null;
340
+ return recaptureInHelperTab(service, teamsOrigin(tab), since, rejected, signal);
341
+ })().finally(() => refreshing.get(service) === self && refreshing.delete(service));
342
+ refreshing.set(service, entry);
343
+ }
344
+ if (!ctx) {
345
+ entry.kept = true;
346
+ return entry.promise;
329
347
  }
330
- return refreshing.get(service);
348
+ const self = entry;
349
+ self.waiting++;
350
+ const stopWatching = ctx.onCancel(() => {
351
+ if (--self.waiting > 0 || self.kept) return;
352
+ // Nobody waits for it anymore: a later call starts afresh.
353
+ if (refreshing.get(service) === self) refreshing.delete(service);
354
+ self.stop.abort();
355
+ });
356
+ self.promise.then(stopWatching, stopWatching);
357
+ return guarded(self.promise, ctx);
331
358
  }
332
359
 
333
360
  const teamsOrigin = (tab) => {
@@ -356,8 +383,10 @@ let helperRun = null;
356
383
  * late: the capture, or null. One tab at a time: a call for another service while it is open
357
384
  * waits on the same tab, which closes once every wait asked of it ended (unless the user switched
358
385
  * to it), or as soon as the user closes it. It never reloads or scripts the user's own Teams tab.
386
+ * `signal`: aborting it ends this wait (null) when nobody waits for it anymore, so the tab closes
387
+ * once no other wait is left.
359
388
  */
360
- function recaptureInHelperTab(service, origin, since = Date.now(), rejected = null) {
389
+ function recaptureInHelperTab(service, origin, since = Date.now(), rejected = null, signal = null) {
361
390
  if (!helperRun || helperRun.closing) {
362
391
  const run = { since: Date.now(), waits: [], closing: false };
363
392
  let tabClosed;
@@ -394,7 +423,7 @@ function recaptureInHelperTab(service, origin, since = Date.now(), rejected = nu
394
423
  }
395
424
  const run = helperRun;
396
425
  const left = Math.max(run.since + RECAPTURE_WAIT_MS - Date.now(), RECAPTURE_JOIN_MIN_MS);
397
- const wait = Promise.race([waitForCapture(service, since, left, rejected), run.tabGone.then(() => null)]);
426
+ const wait = Promise.race([waitForCapture(service, since, left, rejected, signal), run.tabGone.then(() => null)]);
398
427
  run.waits.push(wait);
399
428
  return wait;
400
429
  }
@@ -642,7 +671,7 @@ async function api(service, path, { method = "GET", body, raw = false, withLocat
642
671
  // Opening a tab or waiting for a session can take a minute; a withdrawn request stops waiting.
643
672
  let tab = await guarded(findTeamsTab(cap ? new URL(cap.base).origin : null, ctx), ctx);
644
673
  if (!cap) {
645
- cap = await guarded(refreshSession(service, tab, { quickWaitMs: service === "substrate" ? 2_000 : 4_000 }), ctx);
674
+ cap = await refreshSession(service, tab, { quickWaitMs: service === "substrate" ? 2_000 : 4_000 }, ctx);
646
675
  if (!cap) throw new Error(SERVICE_HELP[service]);
647
676
  }
648
677
  const urlFor = (c) => teams.serviceUrl(path, c.base);
@@ -655,7 +684,7 @@ async function api(service, path, { method = "GET", body, raw = false, withLocat
655
684
  // The session (maybe the account) is changing: lists read with it are read again.
656
685
  dropList();
657
686
  meetingSessionsCache.clear();
658
- cap = await guarded(refreshSession(service, tab, { quickWaitMs: 0, rejected: cap.headers }), ctx);
687
+ cap = await refreshSession(service, tab, { quickWaitMs: 0, rejected: cap.headers }, ctx);
659
688
  if (!cap) throw new Error(SERVICE_HELP[service]);
660
689
  // The tab may have been closed meanwhile: look the tabs up again.
661
690
  if (ctx) ctx.tabs = null;
@@ -2,7 +2,7 @@
2
2
  "manifest_version": 3,
3
3
  "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvoz9Wexl4ZqY9YtEyBZsu/Qi5Ft5EjNUNB8b30wgt3AR8TjXI5UumcPQFBIm7ZfvBsjgrh6tBJlygsgFbZ5cB1EiI+WSz/2Wcqwz4eowKMXXPvGuDGrO+2O2zbSpTG19/MwTljI+g0TLfn8Vkoqd8z9MskKk+vCZjjV0Jk8QjVEdHypgM7H8zTuUM06SO12IIZMdeE3IURzf35zbjlfPPkJiDWoo3wB9nxAvLd57xrQhPUB/lGtCJk9mS3SY+/VwOlG5kJ9WS/RCHXcs5Qs7huIlKIatmFz7YiducvTR3DKBbjqbMTyqHzXkmkUmJRqzsYtgtKcz0C0cjFOt6uNdhwIDAQAB",
4
4
  "name": "mcp-teams bridge",
5
- "version": "0.3.31",
5
+ "version": "0.3.32",
6
6
  "description": "Lets AI assistants (via the mcp-teams MCP server) read and search Microsoft Teams chats, channels and meeting transcripts and, with your explicit confirmation, post messages.",
7
7
  "minimum_chrome_version": "116",
8
8
  "icons": {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-teams",
3
- "version": "0.3.31",
3
+ "version": "0.3.32",
4
4
  "description": "MCP server that lets AI assistants work with Microsoft Teams (web) through a Chrome/Edge extension bridge",
5
5
  "keywords": ["mcp", "model-context-protocol", "teams", "microsoft-teams", "microsoft-365", "chat", "claude", "chrome-extension"],
6
6
  "repository": {