mcp-teams 0.3.50 → 0.3.51
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/README.md +4 -0
- package/dist/build.json +2 -2
- package/extension/background.js +66 -12
- package/extension/manifest.json +1 -1
- package/extension/teams.js +9 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -79,6 +79,10 @@ A tool call that fails because the chat, team list or people session is missing
|
|
|
79
79
|
|
|
80
80
|
These times live in the browser's session storage like the sessions themselves: a restart of the browser (or a reload of the extension) clears them.
|
|
81
81
|
|
|
82
|
+
### Switching accounts
|
|
83
|
+
|
|
84
|
+
When Teams switches to another account or tenant, the extension follows Teams' first request as the new account and forgets what the previous one saw. For 30 s after a switch, a request Teams retries with the previous account's older token (issued before the one that switched) doesn't switch back: it is ignored, and `get_version` counts it in `teams.droppedOldCaptures` (`count`, and `lastAt`, the time of the last one; no account data). A real switch back brings a newer token and takes effect at once. A request that had to wait before it went out (Teams' throttling) or that the extension would send again (after the throttling, or in another Teams tab after its own was closed) fails with "account changed" instead when the account changed meanwhile: it is never sent as the previous account.
|
|
85
|
+
|
|
82
86
|
## Setup
|
|
83
87
|
|
|
84
88
|
You need [Node.js](https://nodejs.org) 20 or later, and Chrome or Edge. Then, in a terminal:
|
package/dist/build.json
CHANGED
package/extension/background.js
CHANGED
|
@@ -43,6 +43,11 @@ function captureSession(details) {
|
|
|
43
43
|
|
|
44
44
|
const CAPTURED_SERVICES = ["chatsvc", "csa", "mt", "substrate"];
|
|
45
45
|
let captureQueue = Promise.resolve(); // one capture at a time, so a switch is seen in order
|
|
46
|
+
/** Whose session is stored, once the captures already queued are stored (a switch may be among them). */
|
|
47
|
+
async function signedInOid() {
|
|
48
|
+
await captureQueue;
|
|
49
|
+
return (await chrome.storage.session.get("oid")).oid;
|
|
50
|
+
}
|
|
46
51
|
|
|
47
52
|
/**
|
|
48
53
|
* Stores a capture. One from another user than the signed-in one (the user switched Teams to
|
|
@@ -52,15 +57,23 @@ let captureQueue = Promise.resolve(); // one capture at a time, so a switch is s
|
|
|
52
57
|
*
|
|
53
58
|
* The same session again (same base, headers and user: every request Teams, or this worker in
|
|
54
59
|
* its tab, makes with it) isn't written: it only marks the stored one as seen now (`seenAt`).
|
|
60
|
+
*
|
|
61
|
+
* A request Teams retries with the previous user's old token isn't a switch back (#77): see
|
|
62
|
+
* isOldToken. Such a capture is dropped (not stored, nothing cleared) and counted.
|
|
55
63
|
*/
|
|
56
64
|
async function storeCapture(service, capture) {
|
|
57
65
|
const key = `capture:${service}`;
|
|
58
|
-
const { [key]: held, oid: was } = await chrome.storage.session.get([key, "oid"]);
|
|
66
|
+
const { [key]: held, oid: was, [SWITCH_KEY]: lastSwitch, [DROPPED_KEY]: dropped = NO_DROPS } = await chrome.storage.session.get([key, "oid", SWITCH_KEY, DROPPED_KEY]);
|
|
59
67
|
if (sameSession(held, capture) && (!capture.oid || capture.oid === was)) {
|
|
60
68
|
seenAt.set(service, capture.at);
|
|
61
69
|
for (const f of [...captureSeen]) f(service);
|
|
62
70
|
return;
|
|
63
71
|
}
|
|
72
|
+
const iat = teams.tokenIssuedAt(capture.headers);
|
|
73
|
+
if (capture.oid && was && was !== capture.oid && isOldToken(capture, iat, lastSwitch)) {
|
|
74
|
+
await chrome.storage.session.set({ [DROPPED_KEY]: { count: dropped.count + 1, lastAt: capture.at } });
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
64
77
|
seenAt.delete(service);
|
|
65
78
|
const update = { [key]: capture };
|
|
66
79
|
if (capture.oid) {
|
|
@@ -70,12 +83,36 @@ async function storeCapture(service, capture) {
|
|
|
70
83
|
const stale = others.filter((k) => held[k] && held[k].oid !== capture.oid);
|
|
71
84
|
if (stale.length) await chrome.storage.session.remove(stale);
|
|
72
85
|
forgetAccount();
|
|
86
|
+
update[SWITCH_KEY] = { from: was, at: capture.at, iat };
|
|
73
87
|
}
|
|
74
88
|
update.oid = capture.oid;
|
|
75
89
|
}
|
|
76
90
|
await chrome.storage.session.set(update);
|
|
77
91
|
}
|
|
78
92
|
|
|
93
|
+
/**
|
|
94
|
+
* The last switch of the signed-in user: { from (the previous user's oid), at (ms since 1970),
|
|
95
|
+
* iat (of the token that switched, null without one) }. Kept in chrome.storage.session like the
|
|
96
|
+
* captures, so a restart of this worker keeps it and a browser restart clears it.
|
|
97
|
+
*/
|
|
98
|
+
const SWITCH_KEY = "accountSwitch";
|
|
99
|
+
/** Captures dropped as an old token of the previous user: { count, lastAt (ms since 1970) }, no account data. */
|
|
100
|
+
const DROPPED_KEY = "droppedOldCaptures";
|
|
101
|
+
const NO_DROPS = { count: 0, lastAt: null };
|
|
102
|
+
const OLD_TOKEN_WINDOW_MS = 30_000;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Whether a capture of another user than the signed-in one is the previous user's old token (a
|
|
106
|
+
* request Teams retries with it) rather than a switch: within 30 s of the switch, for the user it
|
|
107
|
+
* switched from, issued before the token that switched. A token without a readable `iat` (or a
|
|
108
|
+
* switch caused by one), a later one, a third user, or one after the 30 s switches as before.
|
|
109
|
+
*/
|
|
110
|
+
function isOldToken(capture, iat, lastSwitch) {
|
|
111
|
+
if (!lastSwitch || lastSwitch.from !== capture.oid) return false;
|
|
112
|
+
if (!(capture.at - lastSwitch.at < OLD_TOKEN_WINDOW_MS)) return false;
|
|
113
|
+
return Number.isFinite(iat) && Number.isFinite(lastSwitch.iat) && iat < lastSwitch.iat;
|
|
114
|
+
}
|
|
115
|
+
|
|
79
116
|
// service -> when Teams last sent the stored session again (in memory: no storage write for it)
|
|
80
117
|
const seenAt = new Map();
|
|
81
118
|
// Called with the service whenever its stored session is seen again (see waitForCapture).
|
|
@@ -207,6 +244,12 @@ export function savePendingCalls() {
|
|
|
207
244
|
|
|
208
245
|
const callAt = (c) => (c ? { at: new Date(c.at).toISOString(), host: c.host } : null);
|
|
209
246
|
|
|
247
|
+
/** For status: how many captures were dropped as an old token of the previous user, and when the last one was (#77). */
|
|
248
|
+
async function droppedOldCaptures() {
|
|
249
|
+
const { count, lastAt } = (await chrome.storage.session.get(DROPPED_KEY))[DROPPED_KEY] ?? NO_DROPS;
|
|
250
|
+
return { count, lastAt: lastAt == null ? null : new Date(lastAt).toISOString() };
|
|
251
|
+
}
|
|
252
|
+
|
|
210
253
|
function recordCompleted(details) {
|
|
211
254
|
if (!teams.isTeamsOrigin(details.initiator)) return;
|
|
212
255
|
const url = new URL(details.url);
|
|
@@ -696,15 +739,18 @@ const GATE_MARGIN_MS = 250;
|
|
|
696
739
|
* Before anything goes to a throttled service: waits until Teams' Retry-After is over when
|
|
697
740
|
* `wait` and that fits before the request's deadline, else fails at once as busy. A request
|
|
698
741
|
* that holds one of the hub's turns gives it back while it waits, and goes on once it has one
|
|
699
|
-
* again (35), so calls to services that aren't throttled run meanwhile.
|
|
742
|
+
* again (35), so calls to services that aren't throttled run meanwhile. Resolves with whether
|
|
743
|
+
* it waited (for the gate or for a turn back).
|
|
700
744
|
*/
|
|
701
745
|
async function passGate(key, ctx, wait) {
|
|
702
746
|
let gaveBack = false;
|
|
747
|
+
let waited = false;
|
|
703
748
|
try {
|
|
704
749
|
for (let ms = gatedFor(key); ms > 0; ms = gatedFor(key)) {
|
|
705
750
|
const deadline = Math.min(ctx?.deadline ?? Infinity, Date.now() + REQUEST_TIMEOUT_MS);
|
|
706
751
|
if (!wait || Date.now() + ms + GATE_MARGIN_MS > deadline) throw busy(Date.now() + ms);
|
|
707
752
|
if (ctx?.turn && !gaveBack) gaveBack = (ctx.turn.giveBack(), true);
|
|
753
|
+
waited = true;
|
|
708
754
|
await guarded(sleep(ms), ctx);
|
|
709
755
|
}
|
|
710
756
|
} finally {
|
|
@@ -713,7 +759,8 @@ async function passGate(key, ctx, wait) {
|
|
|
713
759
|
}
|
|
714
760
|
ctx?.check();
|
|
715
761
|
// The gate may have shut again while it waited for its turn back.
|
|
716
|
-
if (gaveBack && gatedFor(key) > 0)
|
|
762
|
+
if (gaveBack && gatedFor(key) > 0) await passGate(key, ctx, wait);
|
|
763
|
+
return waited;
|
|
717
764
|
}
|
|
718
765
|
|
|
719
766
|
const originOf = (tab) => (tab?.url ? new URL(tab.url).origin : null);
|
|
@@ -728,21 +775,27 @@ const originOf = (tab) => (tab?.url ? new URL(tab.url).origin : null);
|
|
|
728
775
|
* or gone to another page) is looked up again with `findTab`, and the read goes once more there,
|
|
729
776
|
* by the same deadline. A fetch that failed in the same tab, still on the same page (e.g.
|
|
730
777
|
* offline), isn't sent again. A post never is: it may have reached Teams.
|
|
778
|
+
*
|
|
779
|
+
* `oid`: whose headers they are. Before they go after a wait (for the gate, also on the first
|
|
780
|
+
* attempt, since another request's 429 may have shut it) or again (in the other tab), that user
|
|
781
|
+
* must still be the signed-in one; otherwise the call fails as account changed (#77) and the
|
|
782
|
+
* previous user's headers are never sent (again).
|
|
731
783
|
*/
|
|
732
|
-
async function sendTo(key, tab, findTab, url, method, headers, body, ctx, wait, repeatable = method === "GET") {
|
|
784
|
+
async function sendTo(key, tab, findTab, url, method, headers, body, ctx, wait, repeatable = method === "GET", oid = null) {
|
|
733
785
|
let foundAgain = false;
|
|
734
|
-
for (let attempt = 0; ; ) {
|
|
735
|
-
await passGate(key, ctx, wait);
|
|
786
|
+
for (let attempt = 0, repeat = false; ; repeat = true) {
|
|
787
|
+
const waited = await passGate(key, ctx, wait);
|
|
788
|
+
if ((waited || repeat) && oid && (await signedInOid()) !== oid) throw new Error(ACCOUNT_CHANGED_WHILE("a request waited to be sent", "sends it as that account"));
|
|
736
789
|
let res;
|
|
737
790
|
try {
|
|
738
791
|
res = await runInTab(tab, url, method, headers, body, ctx);
|
|
739
792
|
} catch (err) {
|
|
740
793
|
if (!err?.tabFailed || !repeatable || foundAgain) throw err;
|
|
741
794
|
foundAgain = true;
|
|
742
|
-
const
|
|
743
|
-
if (err.tabFailed === "fetch" &&
|
|
744
|
-
if (
|
|
745
|
-
tab =
|
|
795
|
+
const found = await guarded(findTab(), ctx);
|
|
796
|
+
if (err.tabFailed === "fetch" && found.id === tab.id && originOf(found) === originOf(tab)) throw err;
|
|
797
|
+
if (found.status === "loading") await guarded(waitForTabLoaded(found.id), ctx);
|
|
798
|
+
tab = found;
|
|
746
799
|
continue;
|
|
747
800
|
}
|
|
748
801
|
if (res.status !== 429) return res;
|
|
@@ -773,7 +826,7 @@ async function api(service, path, { method = "GET", body, raw = false, withLocat
|
|
|
773
826
|
const urlFor = (c) => teams.serviceUrl(path, c.base);
|
|
774
827
|
const findTab = () => findTeamsTab(new URL(cap.base).origin, ctx);
|
|
775
828
|
await onCapture?.(cap); // may refuse the session (it throws), before anything is sent
|
|
776
|
-
let res = await sendTo(gateKey(service, cap.base), tab, findTab, urlFor(cap), method, cap.headers, body, ctx, wait, repeatable);
|
|
829
|
+
let res = await sendTo(gateKey(service, cap.base), tab, findTab, urlFor(cap), method, cap.headers, body, ctx, wait, repeatable, cap.oid);
|
|
777
830
|
if (res.status === 401) {
|
|
778
831
|
// Rejected before running (expired token), so trying once more is safe. A 403 is a
|
|
779
832
|
// real answer (e.g. no longer a member of that chat).
|
|
@@ -786,7 +839,7 @@ async function api(service, path, { method = "GET", body, raw = false, withLocat
|
|
|
786
839
|
if (ctx) ctx.tabs = null;
|
|
787
840
|
tab = await guarded(findTab(), ctx);
|
|
788
841
|
await onCapture?.(cap);
|
|
789
|
-
res = await sendTo(gateKey(service, cap.base), tab, findTab, urlFor(cap), method, cap.headers, body, ctx, wait, repeatable);
|
|
842
|
+
res = await sendTo(gateKey(service, cap.base), tab, findTab, urlFor(cap), method, cap.headers, body, ctx, wait, repeatable, cap.oid);
|
|
790
843
|
}
|
|
791
844
|
if (raw) return res;
|
|
792
845
|
if (res.status >= 400) {
|
|
@@ -1231,6 +1284,7 @@ export const handlers = {
|
|
|
1231
1284
|
region: (await getCapture("chatsvc"))?.region ?? null,
|
|
1232
1285
|
signedInAs: await myMri(),
|
|
1233
1286
|
...(await teamsCalls().then((c) => ({ lastTeamsCall: callAt(c?.lastTeamsCall), lastSignedInCall: callAt(c?.lastSignedInCall) }))),
|
|
1287
|
+
droppedOldCaptures: await droppedOldCaptures(),
|
|
1234
1288
|
};
|
|
1235
1289
|
},
|
|
1236
1290
|
|
package/extension/manifest.json
CHANGED
|
@@ -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.
|
|
5
|
+
"version": "0.3.51",
|
|
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/extension/teams.js
CHANGED
|
@@ -71,6 +71,15 @@ export function tokenOid(headers) {
|
|
|
71
71
|
return null;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
/**
|
|
75
|
+
* When a request's token was issued (its `iat`, seconds since 1970), or null when it has none or
|
|
76
|
+
* one that isn't a number. Tells an old token from a fresh one of the same user (#77).
|
|
77
|
+
*/
|
|
78
|
+
export function tokenIssuedAt(headers) {
|
|
79
|
+
for (const claims of tokenClaims(headers)) if (Number.isFinite(claims?.iat)) return claims.iat;
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
74
83
|
/** The decoded payloads of a request's JWTs (unverified, nothing else of them kept). */
|
|
75
84
|
function* tokenClaims(headers) {
|
|
76
85
|
for (const value of [headers?.authorization, headers?.authentication, headers?.["x-skypetoken"]]) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mcp-teams",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.51",
|
|
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": {
|