@aidalinfo/m365-mcp 0.4.0 → 0.4.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/README.md CHANGED
@@ -195,10 +195,19 @@ identity even when `M365_ALLOW_BROAD_PERMISSIONS=true`.
195
195
 
196
196
  Graph has no API for mailbox permissions: this family calls Exchange Online through the same
197
197
  `InvokeCommand` endpoint as `m365_mailbox_permissions` (the one the ExchangeOnlineManagement v3 module
198
- uses), with a **fifth dedicated identity** `m365-mcp-exchange`. It is registered only when
199
- `exchange-delegation` is explicitly listed in `M365_WRITE_FAMILIES` (full mode), with exactly one
200
- certificate or federated credential in `M365_EXCHANGE_*` (a client secret is a startup error), a
201
- client id different from every other identity, and `M365_WRITE_KILL_SWITCH_FILE` configured.
198
+ uses), with a dedicated identity `m365-mcp-exchange` — or, since 0.4.1, the write application
199
+ `m365-mcp-write` itself. It is registered only when `exchange-delegation` is explicitly listed in
200
+ `M365_WRITE_FAMILIES` (full mode), with exactly one certificate or federated credential in
201
+ `M365_EXCHANGE_*` (a client secret is a startup error), a client id different from the read, entra
202
+ and audit identities, and `M365_WRITE_KILL_SWITCH_FILE` configured.
203
+
204
+ **Reusing the write application** (`M365_EXCHANGE_CLIENT_ID` = `M365_WRITE_CLIENT_ID`) is accepted:
205
+ add `Exchange.ManageAsApp` to `m365-mcp-write` and give its enterprise app the scoped role group below.
206
+ No Entra administrator role is needed (and one would be refused). The Exchange token is a separate
207
+ token (Exchange audience) still checked for exactly `Exchange.ManageAsApp`; the Graph token of the app
208
+ is unaffected. `M365_EXCHANGE_*` still needs its own certificate or federated file (it may be the same
209
+ certificate as the write identity). Trade-off, shown as a startup warning: one compromised write
210
+ credential also reaches shared-mailbox delegation.
202
211
 
203
212
  | Lock | Where |
204
213
  |---|---|
@@ -475,7 +484,7 @@ or the smoke script): history builds up to the retention. Unified audit must be
475
484
  | `M365_WRITE_*` | — | Same variables for the write application, **read only when `M365_MODE=full`** — missing ⇒ startup failure; same client id as read ⇒ startup failure |
476
485
  | `M365_WRITE_FAMILIES` | `users,licenses,groups,teams` | Comma list; add `entra-applications` or `exchange-delegation` explicitly to enable those families, or use `none` |
477
486
  | `M365_ENTRA_*` | — | Third dedicated identity, loaded only for `entra-applications`; same credential forms as read/write; client id must differ from both |
478
- | `M365_EXCHANGE_*` | — | Dedicated Exchange Online identity, loaded only for `exchange-delegation` (full mode); certificate or federated token file only — a secret is refused; client id must differ from every other identity |
487
+ | `M365_EXCHANGE_*` | — | Exchange Online identity, loaded only for `exchange-delegation` (full mode); certificate or federated token file only — a secret is refused; client id must differ from read, entra and audit (it may be the write one) |
479
488
  | `M365_WRITE_KILL_SWITCH_FILE` | — | While this file exists, every write is refused (checked at each call); absolute path, mandatory when `entra-applications` or `exchange-delegation` is enabled |
480
489
  | `M365_AUDIT_LOG_FILE` | — | Also append the audit trail to this file |
481
490
  | `M365_AUDIT_TARGET_ID` | `id` | `id` or `hash` (keyed HMAC-SHA256 of the object id — a plain hash of a GUID would be reversible by anyone who can list the directory) |
@@ -655,6 +664,13 @@ snapshot, signals and comparison end to end.
655
664
 
656
665
  ## Validation status
657
666
 
667
+ - 0.4.2: a connection dropped while the answer is read (Node's `terminated`) is retried like a failed
668
+ request, then reported as `transient` with the HTTP status and the Microsoft request-id — it used to
669
+ surface as `unexpected: terminated`, which hid whether Exchange or the network had refused.
670
+
671
+ - 0.4.1: `M365_EXCHANGE_CLIENT_ID` may equal `M365_WRITE_CLIENT_ID` (warning at startup); every other
672
+ check unchanged.
673
+
658
674
  - 0.4.0 (`exchange-delegation`): unit tests passing against a fake Exchange; **to validate on an
659
675
  acceptance shared mailbox** — the scoped role group, the exact `InvokeCommand` answers of
660
676
  `Add-/Remove-MailboxPermission` and `Add-/Remove-RecipientPermission` (including on a duplicate),
package/dist/config.js CHANGED
@@ -109,8 +109,10 @@ export function loadConfig(env) {
109
109
  // this identity can change who reads a shared mailbox: no secret, and an application of its own
110
110
  if (exchange.auth.kind === "secret")
111
111
  throw new ConfigError("the exchange identity (M365_EXCHANGE_*) must use a certificate or workload identity federation — a client secret is refused");
112
- if ([read.clientId, write?.clientId, entra?.clientId].filter(Boolean).some((id) => id.toLowerCase() === exchange.clientId.toLowerCase())) {
113
- throw new ConfigError("M365_EXCHANGE_CLIENT_ID must be a different Entra application than the read, write and entra identities");
112
+ // the write application may carry the Exchange role (same app, Exchange audience): the token check,
113
+ // the cmdlet allowlist and the scoped Exchange role still apply; read and entra stay separate
114
+ if ([read.clientId, entra?.clientId].filter(Boolean).some((id) => id.toLowerCase() === exchange.clientId.toLowerCase())) {
115
+ throw new ConfigError("M365_EXCHANGE_CLIENT_ID must be a different Entra application than the read and entra identities (it may be the write one)");
114
116
  }
115
117
  }
116
118
  // ---- phase 2: audit identity (Exchange Online, Management Activity, SharePoint REST)
@@ -161,6 +163,9 @@ export function loadConfig(env) {
161
163
  throw new ConfigError("M365_AUDIT_TARGET_ID=hash needs M365_AUDIT_HASH_KEY (at least 16 characters, kept secret)");
162
164
  }
163
165
  const warnings = [];
166
+ if (exchange && write && exchange.clientId.toLowerCase() === write.clientId.toLowerCase()) {
167
+ warnings.push("ℹ the exchange-delegation identity is the write application (M365_EXCHANGE_CLIENT_ID = M365_WRITE_CLIENT_ID): one compromised write credential also reaches shared-mailbox delegation — keep its Exchange role scoped to shared mailboxes");
168
+ }
164
169
  for (const id of [read, write, entra, exchange, audit]) {
165
170
  if (id?.auth.kind === "secret") {
166
171
  warnings.push(`⚠ the ${id.role} identity authenticates with a client secret — acceptable for temporary acceptance testing only; use workload identity federation or a certificate in production`);
@@ -1,6 +1,6 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { M365Error } from "../errors.js";
3
- import { defaultFetcher, defaultSleep, DEFAULT_TIMEOUT_MS } from "../http.js";
3
+ import { bodyError, defaultFetcher, defaultSleep, DEFAULT_TIMEOUT_MS } from "../http.js";
4
4
  import { redactString } from "../redact.js";
5
5
  const RETRY = new Set([429, 502, 503, 504]);
6
6
  const MAX_RETRIES = 3;
@@ -102,7 +102,22 @@ export class ExternalClient {
102
102
  throw new M365Error("transient", `transient: ${this.opts.name} ${method} failed (${err instanceof Error ? err.message : String(err)})`, { clientRequestId });
103
103
  }
104
104
  const requestId = res.headers.get("request-id") ?? res.headers.get("x-ms-request-id") ?? res.headers.get("sprequestguid") ?? undefined;
105
- const text = await res.text();
105
+ let text;
106
+ try {
107
+ text = await res.text();
108
+ }
109
+ catch (err) {
110
+ // the connection dropped while the answer was read: same policy as a failed request, never "unexpected"
111
+ if (attempt < MAX_RETRIES) {
112
+ await this.sleep(1000 * 2 ** attempt);
113
+ continue;
114
+ }
115
+ throw new M365Error("transient", `transient: ${this.opts.name} ${method} — connection closed while reading the HTTP ${res.status} answer (${bodyError(err)})`, {
116
+ status: res.status,
117
+ requestId,
118
+ clientRequestId,
119
+ });
120
+ }
106
121
  if (text.length > MAX_BODY_CHARS)
107
122
  throw new M365Error("unexpected", `unexpected: ${this.opts.name} response larger than ${MAX_BODY_CHARS / 1e6} M characters`, { requestId });
108
123
  if (res.ok) {
@@ -1,6 +1,6 @@
1
1
  import { randomUUID } from "node:crypto";
2
2
  import { fromGraphResponse, M365Error } from "./errors.js";
3
- import { defaultFetcher, defaultSleep, DEFAULT_TIMEOUT_MS } from "./http.js";
3
+ import { bodyError, defaultFetcher, defaultSleep, DEFAULT_TIMEOUT_MS } from "./http.js";
4
4
  /** Stable Graph only. `beta` is never used by this server. */
5
5
  export const GRAPH_BASE = "https://graph.microsoft.com/v1.0";
6
6
  /** Statuses retried with a bounded backoff (429 always; 5xx only for idempotent requests). */
@@ -160,7 +160,17 @@ export class GraphClient {
160
160
  }
161
161
  throw new M365Error("transient", `transient: report download failed (${err instanceof Error ? err.message : String(err)})`, { requestId: res.requestId });
162
162
  }
163
- const text = await dl.text();
163
+ let text;
164
+ try {
165
+ text = await dl.text();
166
+ }
167
+ catch (err) {
168
+ if (attempt < MAX_RETRIES) {
169
+ await this.sleep(this.backoff(attempt));
170
+ continue;
171
+ }
172
+ throw new M365Error("transient", `transient: report download — connection closed while reading the answer (${bodyError(err)})`, { requestId: res.requestId });
173
+ }
164
174
  if (dl.ok)
165
175
  return { csv: text, requestId: res.requestId };
166
176
  if (RETRY_STATUSES.has(dl.status) && attempt < MAX_RETRIES) {
@@ -244,7 +254,22 @@ export class GraphClient {
244
254
  throw new M365Error("unexpected", `unexpected: HTTP ${res.status} without Location on ${new URL(url).pathname}`, { requestId, clientRequestId });
245
255
  return { status: res.status, data: undefined, requestId, clientRequestId, url, location };
246
256
  }
247
- const text = await res.text();
257
+ let text;
258
+ try {
259
+ text = await res.text();
260
+ }
261
+ catch (err) {
262
+ // the connection dropped mid-answer: retried only when the request is idempotent, never "unexpected"
263
+ if (idempotent && attempt < MAX_RETRIES) {
264
+ await this.sleep(this.backoff(attempt));
265
+ continue;
266
+ }
267
+ throw new M365Error("transient", `transient: ${method} ${new URL(url).pathname} — connection closed while reading the HTTP ${res.status} answer (${bodyError(err)})`, {
268
+ status: res.status,
269
+ requestId,
270
+ clientRequestId,
271
+ });
272
+ }
248
273
  if (res.ok) {
249
274
  let data = undefined;
250
275
  if (text) {
package/dist/http.js CHANGED
@@ -1,3 +1,5 @@
1
1
  export const DEFAULT_TIMEOUT_MS = 30_000;
2
2
  export const defaultFetcher = (url, init) => fetch(url, init);
3
+ /** Message of a failure while reading a response body ("terminated" when the connection drops mid-body). */
4
+ export const bodyError = (err) => (err instanceof Error ? err.message : String(err));
3
5
  export const defaultSleep = (ms) => new Promise((r) => setTimeout(r, ms));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aidalinfo/m365-mcp",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "Microsoft 365 MCP server over Microsoft Graph — Entra ID users, groups and applications, licences, Teams, SharePoint, usage reports, service health, roles, MFA, audit, access review, shared mailbox delegation (Exchange Online). Read-only by default; full mode uses separate scoped identities with two-step confirmed, audited writes",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",