@chrischall/microsoft-teams-mcp 0.6.2 → 0.8.0

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.
@@ -29,6 +29,7 @@
29
29
  import { captureBearerHeader, createFetchproxyTransport } from '@chrischall/mcp-utils/fetchproxy';
30
30
  import { readPortEnv } from '@chrischall/mcp-utils';
31
31
  import { captureWindowMs } from './capture-window.js';
32
+ import { BRIDGE_TRUST_DOMAINS, CAPTURE_DECL, withBridgeScope } from './bridge-scope.js';
32
33
  import { PACKAGE_NAME, VERSION } from './version.js';
33
34
  export { stripBearer } from '@chrischall/mcp-utils/fetchproxy';
34
35
  /**
@@ -43,48 +44,43 @@ export function getWsPort() {
43
44
  /**
44
45
  * The one request we capture from: any chat-service call on the New Teams
45
46
  * host. An idle tab may make none — opening a chat is what makes the worker
46
- * issue one, hence the guidance in the capture error below.
47
+ * issue one, hence the guidance in the capture error below. Defined in
48
+ * `bridge-scope.ts`, beside the other captures it is declared with.
47
49
  */
48
- export const CAPTURE_DECL = {
49
- host: 'teams.cloud.microsoft',
50
- path: '/api/chatsvc/*',
51
- headerName: 'authorization',
52
- };
50
+ export { CAPTURE_DECL } from './bridge-scope.js';
53
51
  /**
54
- * The extension's trust boundary for the chat-service path: exactly the host we
55
- * capture from. Every API call afterwards is a plain server-side `fetch` the
56
- * bridge never sees, so nothing wider is needed here.
57
- *
58
- * Shared-file download adds a SECOND, independent trust scope —
59
- * `SHAREPOINT_TRUST_DOMAINS = ['sharepoint.com']` in `sharepoint-auth.ts` — used
60
- * only when a file is opened. It is additive: the SharePoint capture declares
61
- * `sharepoint.com` on its own transport and does NOT widen this chat-service
62
- * capture decl, which stays `teams.cloud.microsoft` alone. Granting the broader
63
- * SharePoint scope takes a one-time re-pair of `microsoft-teams-mcp` in the
64
- * ContextMint Bridge popup; until then shared files degrade to metadata + link.
52
+ * The domains every capture of this server trusts — see `bridge-scope.ts` for
53
+ * why the chat-service capture declares the SharePoint and Substrate domains
54
+ * too, and every capture header beside its own.
65
55
  */
66
- export const TRUST_DOMAINS = [CAPTURE_DECL.host];
56
+ export const TRUST_DOMAINS = [...BRIDGE_TRUST_DOMAINS];
67
57
  /**
68
58
  * Build the capture transport through this module's import binding, so a
69
59
  * test's `vi.mock('@chrischall/mcp-utils/fetchproxy')` stub is the one used.
70
60
  * Shared by the SharePoint and Substrate captures.
71
61
  */
72
62
  export const createCaptureTransport = (opts) => createFetchproxyTransport(opts);
63
+ /**
64
+ * A capture transport factory that declares the one bridge scope
65
+ * (`withBridgeScope`), whichever single header the capture reads.
66
+ * `sharePointHost` is the host a SharePoint capture is about to try.
67
+ */
68
+ export const bridgeScopedTransport = (env, sharePointHost) => (opts) => createCaptureTransport(withBridgeScope(opts, env, sharePointHost));
73
69
  /**
74
70
  * Capture a chat-service access token from the user's signed-in Teams tab.
75
71
  * Returns the bare token (no `Bearer ` prefix).
76
72
  *
77
73
  * `captureBearerHeader` declares only `capture_request_header` (beside the
78
- * `fetch` base verb mcp-utils always keeps) — the same set as before the
79
- * Activity feed left the page, so an existing pairing needs no re-approval —
80
- * sets the bridge deadline to the capture window + 15 s, starts the transport
74
+ * `fetch` base verb mcp-utils always keeps), widened to the one bridge scope
75
+ * (`bridge-scope.ts`) so this capture never re-pairs a grant the search or
76
+ * SharePoint capture made — sets the bridge deadline to the capture window + 15 s, starts the transport
81
77
  * (skipping `start()` makes the first verb throw "ensureConnected called before
82
78
  * listen()", which reads exactly like a pairing problem), guards against a
83
79
  * header too short to be a token, and closes the transport whatever happened.
84
80
  */
85
81
  export async function captureTokenViaFetchproxy(env = process.env) {
86
82
  return captureBearerHeader({
87
- captures: [CAPTURE_DECL],
83
+ captures: [{ ...CAPTURE_DECL }],
88
84
  domains: TRUST_DOMAINS,
89
85
  // Read per capture, not at import: see `capture-window.ts`.
90
86
  windowMs: captureWindowMs(env),
@@ -95,6 +91,6 @@ export async function captureTokenViaFetchproxy(env = process.env) {
95
91
  guidance: 'Open a signed-in teams.cloud.microsoft tab and click into any chat — ' +
96
92
  'that is what makes the page call the chat service; an idle tab never ' +
97
93
  'does — then retry.',
98
- createTransport: createCaptureTransport,
94
+ createTransport: bridgeScopedTransport(env),
99
95
  });
100
96
  }
@@ -0,0 +1,133 @@
1
+ /**
2
+ * The ONE scope every browser-bridge capture of this server declares.
3
+ *
4
+ * The server captures three tokens through the ContextMint Bridge — the chat
5
+ * service's (`auth-fetchproxy.ts`), Substrate search's (`substrate-auth.ts`) and
6
+ * a SharePoint host's (`sharepoint-auth.ts`) — all under one identity. The
7
+ * bridge keys its trust record on that identity and:
8
+ *
9
+ * - forces a fresh pair whenever a hello's DOMAIN SET differs from the
10
+ * approved one (`sameDomainSet` in contextmint-bridge's `background/hello.ts`);
11
+ * - offers a "grant" whenever the declared CAPTURE HEADERS exceed the approved
12
+ * ones, and approving it REPLACES the stored list with the declared one.
13
+ *
14
+ * So captures that each declared only their own host thrashed the record: a
15
+ * chat-service capture after a search re-paired, and so on — a pair prompt or a
16
+ * grant prompt on nearly every use. Every capture therefore declares the same
17
+ * domains and the same capture-header list, and captures only the one header it
18
+ * needs.
19
+ *
20
+ * The SharePoint capture host is concrete and per-tenant (the capture layer
21
+ * rejects a wildcard host), so it cannot be known up front. Each one is
22
+ * remembered in `bridge-scope.json` beside the SharePoint token cache once a
23
+ * capture of it SUCCEEDS — i.e. once the user has granted it — and every later
24
+ * hello declares it. A host is never remembered before that: a capture the
25
+ * user has not approved would otherwise grow every later hello and keep
26
+ * offering the same grant.
27
+ */
28
+ import { dirname, join } from 'node:path';
29
+ import { parseBoolEnv } from '@chrischall/mcp-utils';
30
+ import { createFileStatePersistence } from '@chrischall/mcp-utils/session';
31
+ import { sharePointTokenCachePath } from './sharepoint-token-cache.js';
32
+ import { reportCacheWriteFailure } from './token-cache.js';
33
+ /**
34
+ * The chat-service capture: any chat-service call on the New Teams host. An
35
+ * idle tab may make none — opening a chat is what makes the worker issue one.
36
+ */
37
+ export const CAPTURE_DECL = {
38
+ host: 'teams.cloud.microsoft',
39
+ path: '/api/chatsvc/*',
40
+ headerName: 'authorization',
41
+ };
42
+ /** The search capture: a search-service call the Teams tab makes. */
43
+ export const SUBSTRATE_CAPTURE_DECL = {
44
+ host: 'substrate.office.com',
45
+ path: '/searchservice/*',
46
+ headerName: 'authorization',
47
+ };
48
+ /**
49
+ * Every domain the server captures from. `sharepoint.com` is the family domain:
50
+ * the bridge accepts any `*.sharepoint.com` host under it.
51
+ */
52
+ export const BRIDGE_TRUST_DOMAINS = [CAPTURE_DECL.host, 'sharepoint.com', SUBSTRATE_CAPTURE_DECL.host];
53
+ /**
54
+ * True for a concrete SharePoint tenant host (`{label}.sharepoint.com`), such
55
+ * as `contoso-my.sharepoint.com` or `contoso.sharepoint.com`. Rejects a bare
56
+ * `sharepoint.com`, a suffix spoof (`sharepoint.com.evil.com`), and any
57
+ * non-SharePoint host. Host is lowercased before matching.
58
+ */
59
+ export function isSharePointHost(host) {
60
+ return /^[a-z0-9][a-z0-9-]*\.sharepoint\.com$/.test(host.toLowerCase());
61
+ }
62
+ /** The capture of a SharePoint token off any browser request to `host`. */
63
+ export function sharePointCaptureDecl(host) {
64
+ return { host: host.toLowerCase(), path: '/*', headerName: 'authorization' };
65
+ }
66
+ /**
67
+ * The capture headers every hello declares: the chat-service and search
68
+ * captures, then one per SharePoint host (deduplicated, sorted, and only
69
+ * concrete SharePoint hosts), so the list is the same whichever capture runs.
70
+ */
71
+ export function bridgeCaptureDecls(sharePointHosts) {
72
+ const hosts = [...new Set([...sharePointHosts].map((h) => h.toLowerCase()).filter(isSharePointHost))].sort();
73
+ return [{ ...CAPTURE_DECL }, { ...SUBSTRATE_CAPTURE_DECL }, ...hosts.map(sharePointCaptureDecl)];
74
+ }
75
+ /** Where the remembered SharePoint hosts are kept: beside the SharePoint token cache. */
76
+ export function bridgeScopePath(env = process.env) {
77
+ return join(dirname(sharePointTokenCachePath(env)), 'bridge-scope.json');
78
+ }
79
+ function validateScope(raw) {
80
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw))
81
+ return null;
82
+ const hosts = raw.sharePointHosts;
83
+ if (!Array.isArray(hosts))
84
+ return null;
85
+ return { sharePointHosts: hosts.filter((h) => typeof h === 'string' && isSharePointHost(h)) };
86
+ }
87
+ function scopeFile(env) {
88
+ return createFileStatePersistence({ filePath: bridgeScopePath(env), validate: validateScope });
89
+ }
90
+ /** The SharePoint hosts a capture has succeeded for; empty when none, or the file is unusable. */
91
+ export function knownSharePointHosts(env = process.env) {
92
+ try {
93
+ return scopeFile(env).load()?.sharePointHosts.map((h) => h.toLowerCase()) ?? [];
94
+ }
95
+ catch {
96
+ return [];
97
+ }
98
+ }
99
+ /**
100
+ * Remember `host` once a capture of it has succeeded. Off with
101
+ * `TEAMS_TOKEN_CACHE=false` (nothing of this server's is written to disk then);
102
+ * a failed write is reported, never thrown — it costs at most one more grant
103
+ * prompt.
104
+ */
105
+ export function rememberSharePointHost(host, env = process.env) {
106
+ if (!isSharePointHost(host))
107
+ return;
108
+ if (!parseBoolEnv('TEAMS_TOKEN_CACHE', { env, default: true }))
109
+ return;
110
+ const known = knownSharePointHosts(env);
111
+ const h = host.toLowerCase();
112
+ if (known.includes(h))
113
+ return;
114
+ try {
115
+ scopeFile(env).save({ sharePointHosts: [...known, h].sort() });
116
+ }
117
+ catch (err) {
118
+ reportCacheWriteFailure(err);
119
+ }
120
+ }
121
+ /**
122
+ * The hello options of a capture transport, widened to the one scope: the
123
+ * domains and capture headers mcp-utils derived from the single capture are
124
+ * replaced with {@link BRIDGE_TRUST_DOMAINS} and {@link bridgeCaptureDecls}
125
+ * (plus `extraSharePointHost`, the host a SharePoint capture is about to try).
126
+ * The verb set is unchanged — every capture is `capture_request_header`.
127
+ */
128
+ export function withBridgeScope(opts, env, extraSharePointHost) {
129
+ const hosts = knownSharePointHosts(env);
130
+ if (extraSharePointHost !== undefined)
131
+ hosts.push(extraSharePointHost);
132
+ return { ...opts, domains: [...BRIDGE_TRUST_DOMAINS], captureHeaders: bridgeCaptureDecls(hosts) };
133
+ }