@softeria/ms-365-mcp-server 0.182.2 → 0.183.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.
- package/.gitattributes +1 -0
- package/PRESETS.md +15 -9
- package/README.md +1 -1
- package/dist/__tests__/log-safe.test.js +153 -0
- package/dist/app-mode.js +1 -0
- package/dist/attachment-route.js +11 -4
- package/dist/audit-log.js +2 -0
- package/dist/auth.js +4 -1
- package/dist/crash-logging.js +27 -13
- package/dist/endpoints.json +12 -2
- package/dist/generated/client.js +59 -0
- package/dist/graph-client.js +6 -5
- package/dist/graph-tools.js +25 -14
- package/dist/lib/bounded-file-transport.js +99 -0
- package/dist/lib/log-limits.js +12 -0
- package/dist/lib/log-safe.js +405 -0
- package/dist/lib/microsoft-auth.js +99 -1
- package/dist/logger.js +11 -4
- package/dist/server.js +38 -5
- package/package.json +1 -1
- package/src/endpoints.json +12 -2
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import logger from "../logger.js";
|
|
2
|
+
import { describeText, summarizeError } from "./log-safe.js";
|
|
2
3
|
import { getCloudEndpoints } from "../cloud-config.js";
|
|
3
4
|
function buildResourceMetadataUrl(req, publicUrl) {
|
|
4
5
|
if (publicUrl) {
|
|
@@ -166,9 +167,104 @@ function unwrapToken(result, failureMessage) {
|
|
|
166
167
|
});
|
|
167
168
|
throw new OAuthUpstreamError(result.status, result.raw, result.parsed);
|
|
168
169
|
}
|
|
169
|
-
logger.error(
|
|
170
|
+
logger.error(
|
|
171
|
+
`${failureMessage}: HTTP ${result.status}, unparsed body ${describeText(result.raw)}`
|
|
172
|
+
);
|
|
170
173
|
throw new Error(`${failureMessage}: ${result.raw}`);
|
|
171
174
|
}
|
|
175
|
+
const SELF_TEST_TIMEOUT_MS = 15e3;
|
|
176
|
+
const SAFE_CODE = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
177
|
+
async function checkClientCredentials(clientId, clientSecret, tenantId, cloudType = "global", timeoutMs = SELF_TEST_TIMEOUT_MS) {
|
|
178
|
+
const checkedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
179
|
+
const cloudEndpoints = getCloudEndpoints(cloudType);
|
|
180
|
+
const graphBase = cloudEndpoints.graphApi.replace(/\/$/, "");
|
|
181
|
+
const params = new URLSearchParams({
|
|
182
|
+
grant_type: "client_credentials",
|
|
183
|
+
client_id: clientId,
|
|
184
|
+
client_secret: clientSecret,
|
|
185
|
+
scope: `${graphBase}/.default`
|
|
186
|
+
});
|
|
187
|
+
const controller = new AbortController();
|
|
188
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
189
|
+
try {
|
|
190
|
+
const response = await fetch(`${cloudEndpoints.authority}/${tenantId}/oauth2/v2.0/token`, {
|
|
191
|
+
method: "POST",
|
|
192
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
193
|
+
body: params,
|
|
194
|
+
signal: controller.signal
|
|
195
|
+
});
|
|
196
|
+
const raw = await response.text();
|
|
197
|
+
let json = null;
|
|
198
|
+
try {
|
|
199
|
+
json = JSON.parse(raw);
|
|
200
|
+
} catch {
|
|
201
|
+
json = null;
|
|
202
|
+
}
|
|
203
|
+
const obj = json && typeof json === "object" ? json : null;
|
|
204
|
+
if (response.ok) {
|
|
205
|
+
if (obj && typeof obj.access_token === "string" && obj.access_token.length > 0) {
|
|
206
|
+
return { ok: true, verdict: "accepted", checkedAt, status: response.status };
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
ok: false,
|
|
210
|
+
verdict: "malformed",
|
|
211
|
+
checkedAt,
|
|
212
|
+
status: response.status,
|
|
213
|
+
detail: `HTTP ${response.status} without a token (${raw.length} chars)`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
if (obj && typeof obj.error === "string" && SAFE_CODE.test(obj.error)) {
|
|
217
|
+
const codes = Array.isArray(obj.error_codes) ? obj.error_codes.filter((c) => Number.isInteger(c)) : [];
|
|
218
|
+
const corr = typeof obj.correlation_id === "string" && /^[A-Za-z0-9-]{1,64}$/.test(obj.correlation_id) ? obj.correlation_id : void 0;
|
|
219
|
+
return {
|
|
220
|
+
ok: false,
|
|
221
|
+
verdict: "rejected",
|
|
222
|
+
checkedAt,
|
|
223
|
+
status: response.status,
|
|
224
|
+
error: obj.error,
|
|
225
|
+
error_codes: codes,
|
|
226
|
+
correlation_id: corr
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
ok: false,
|
|
231
|
+
verdict: "malformed",
|
|
232
|
+
checkedAt,
|
|
233
|
+
status: response.status,
|
|
234
|
+
detail: `HTTP ${response.status}, unparsed response (${raw.length} chars)`
|
|
235
|
+
};
|
|
236
|
+
} catch (error) {
|
|
237
|
+
const timedOut = controller.signal.aborted;
|
|
238
|
+
return {
|
|
239
|
+
ok: false,
|
|
240
|
+
verdict: "unavailable",
|
|
241
|
+
checkedAt,
|
|
242
|
+
detail: timedOut ? `no answer within ${timeoutMs} ms` : `request failed: ${summarizeError(error)}`
|
|
243
|
+
};
|
|
244
|
+
} finally {
|
|
245
|
+
clearTimeout(timer);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function credentialCheckMessage(c, tenantId) {
|
|
249
|
+
const codes = (c.error_codes ?? []).join(", ");
|
|
250
|
+
const ref = `${c.error ?? "none"}, error_codes [${codes}], correlation_id ${c.correlation_id ?? "none"}`;
|
|
251
|
+
switch (c.verdict) {
|
|
252
|
+
case "accepted":
|
|
253
|
+
return `Startup self-test: Microsoft accepted the client id and secret for tenant ${tenantId}. (App credentials only; not a test of user sign-in or Graph access.)`;
|
|
254
|
+
case "rejected":
|
|
255
|
+
if (c.error_codes?.includes(7000215)) {
|
|
256
|
+
return `Startup self-test FAILED: Microsoft rejected the client secret as not a current secret value of this app (${ref}). Sign-ins will fail until the secret value is corrected and the revision restarted.`;
|
|
257
|
+
}
|
|
258
|
+
if (c.error_codes?.includes(7000222)) {
|
|
259
|
+
return `Startup self-test FAILED: the client secret has expired (${ref}). Sign-ins will fail until a new secret is created, stored, and the revision restarted.`;
|
|
260
|
+
}
|
|
261
|
+
return `Startup self-test FAILED: Microsoft rejected the app credentials (${ref}). Look the code up in the troubleshooting table; this says nothing about the secret value.`;
|
|
262
|
+
case "unavailable":
|
|
263
|
+
return `Startup self-test could not be completed (${c.detail ?? "unknown"}); the credentials were not judged. Check network access to the Microsoft login endpoint.`;
|
|
264
|
+
default:
|
|
265
|
+
return `Startup self-test inconclusive: ${c.detail ?? "unexpected answer"}; the credentials were not judged.`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
172
268
|
async function exchangeCodeForToken(code, redirectUri, clientId, clientSecret, tenantId = "common", codeVerifier, cloudType = "global", scope) {
|
|
173
269
|
const cloudEndpoints = getCloudEndpoints(cloudType);
|
|
174
270
|
const params = new URLSearchParams({
|
|
@@ -210,6 +306,8 @@ async function refreshAccessToken(refreshToken, clientId, clientSecret, tenantId
|
|
|
210
306
|
export {
|
|
211
307
|
OAuthUpstreamError,
|
|
212
308
|
buildWwwAuthenticate,
|
|
309
|
+
checkClientCredentials,
|
|
310
|
+
credentialCheckMessage,
|
|
213
311
|
exchangeCodeForToken,
|
|
214
312
|
microsoftBearerTokenAuthMiddleware,
|
|
215
313
|
refreshAccessToken,
|
package/dist/logger.js
CHANGED
|
@@ -4,6 +4,8 @@ import { fileURLToPath } from "url";
|
|
|
4
4
|
import fs from "fs";
|
|
5
5
|
import os from "os";
|
|
6
6
|
import { redactionEnabled, redactSensitive } from "./lib/log-redactor.js";
|
|
7
|
+
import { LOG_MAX_BYTES, LOG_MAX_FILES } from "./lib/log-limits.js";
|
|
8
|
+
import { BoundedFileTransport } from "./lib/bounded-file-transport.js";
|
|
7
9
|
const redactFormat = winston.format((info) => {
|
|
8
10
|
if (!redactionEnabled()) return info;
|
|
9
11
|
if (typeof info.message === "string") {
|
|
@@ -46,14 +48,19 @@ const logger = winston.createLogger({
|
|
|
46
48
|
})
|
|
47
49
|
),
|
|
48
50
|
transports: [
|
|
49
|
-
|
|
51
|
+
// Bounded files: each rotates at LOG_MAX_BYTES, LOG_MAX_FILES kept (see log-limits).
|
|
52
|
+
new BoundedFileTransport({
|
|
50
53
|
filename: errorLogPath,
|
|
51
54
|
level: "error",
|
|
52
|
-
|
|
55
|
+
maxBytes: LOG_MAX_BYTES,
|
|
56
|
+
maxFiles: LOG_MAX_FILES,
|
|
57
|
+
mode: FILE_MODE
|
|
53
58
|
}),
|
|
54
|
-
new
|
|
59
|
+
new BoundedFileTransport({
|
|
55
60
|
filename: serverLogPath,
|
|
56
|
-
|
|
61
|
+
maxBytes: LOG_MAX_BYTES,
|
|
62
|
+
maxFiles: LOG_MAX_FILES,
|
|
63
|
+
mode: FILE_MODE
|
|
57
64
|
})
|
|
58
65
|
]
|
|
59
66
|
});
|
package/dist/server.js
CHANGED
|
@@ -8,6 +8,7 @@ import rateLimit from "express-rate-limit";
|
|
|
8
8
|
import helmet from "helmet";
|
|
9
9
|
import crypto from "node:crypto";
|
|
10
10
|
import { isIP, isIPv6 } from "node:net";
|
|
11
|
+
import { NON_TENANT_AUTHORITIES } from "./app-mode.js";
|
|
11
12
|
import { createAttachmentHandler, createAttachmentUploadHandler } from "./attachment-route.js";
|
|
12
13
|
import { registerAuthTools } from "./auth-tools.js";
|
|
13
14
|
import {
|
|
@@ -32,10 +33,13 @@ import {
|
|
|
32
33
|
buildWwwAuthenticate,
|
|
33
34
|
OAuthUpstreamError,
|
|
34
35
|
refreshAccessToken,
|
|
35
|
-
toOAuthErrorResponse
|
|
36
|
+
toOAuthErrorResponse,
|
|
37
|
+
checkClientCredentials,
|
|
38
|
+
credentialCheckMessage
|
|
36
39
|
} from "./lib/microsoft-auth.js";
|
|
37
40
|
import { isAllowedRedirectUri, parseAllowlist } from "./lib/redirect-uri-validation.js";
|
|
38
41
|
import logger, { enableConsoleLogging } from "./logger.js";
|
|
42
|
+
import { version } from "./version.js";
|
|
39
43
|
import { buildMcpServerInstructions } from "./mcp-instructions.js";
|
|
40
44
|
import { installToolSchemaRefNormalization } from "./normalize-tool-schema.js";
|
|
41
45
|
import { MicrosoftOAuthProvider } from "./oauth-provider.js";
|
|
@@ -161,6 +165,7 @@ function statelessPkceSecret(env = process.env) {
|
|
|
161
165
|
}
|
|
162
166
|
class MicrosoftGraphServer {
|
|
163
167
|
constructor(authManager, options = {}) {
|
|
168
|
+
this.credentialCheck = null;
|
|
164
169
|
this.version = "0.0.0";
|
|
165
170
|
this.multiAccount = false;
|
|
166
171
|
this.accountNames = [];
|
|
@@ -259,9 +264,9 @@ class MicrosoftGraphServer {
|
|
|
259
264
|
if (!this.options.obo || !this.secrets?.clientId) return void 0;
|
|
260
265
|
return `${this.secrets.clientId}/access_as_user offline_access`;
|
|
261
266
|
}
|
|
262
|
-
async initialize(
|
|
267
|
+
async initialize(version2) {
|
|
263
268
|
this.secrets = this.authManager.getAppSecrets?.() ?? await getSecrets();
|
|
264
|
-
this.version =
|
|
269
|
+
this.version = version2;
|
|
265
270
|
const accountRoutingAvailable = (!this.options.http || this.options.trustProxyAuth) && !this.authManager.isOAuthModeEnabled();
|
|
266
271
|
if (accountRoutingAvailable) {
|
|
267
272
|
try {
|
|
@@ -466,7 +471,7 @@ class MicrosoftGraphServer {
|
|
|
466
471
|
if (this.options.enableDynamicRegistration) {
|
|
467
472
|
app.post("/register", async (req, res) => {
|
|
468
473
|
const body = req.body;
|
|
469
|
-
logger.info("Client registration request", { body });
|
|
474
|
+
logger.info("Client registration request", { keys: Object.keys(body ?? {}) });
|
|
470
475
|
const clientId = `mcp-client-${Date.now()}`;
|
|
471
476
|
res.status(201).json({
|
|
472
477
|
client_id: clientId,
|
|
@@ -603,7 +608,7 @@ class MicrosoftGraphServer {
|
|
|
603
608
|
return;
|
|
604
609
|
}
|
|
605
610
|
if (!body.grant_type) {
|
|
606
|
-
logger.error("Token endpoint: grant_type is missing", { body });
|
|
611
|
+
logger.error("Token endpoint: grant_type is missing", { keys: Object.keys(body) });
|
|
607
612
|
res.status(400).json({
|
|
608
613
|
error: "invalid_request",
|
|
609
614
|
error_description: "grant_type parameter is required"
|
|
@@ -832,6 +837,15 @@ class MicrosoftGraphServer {
|
|
|
832
837
|
app.get("/", (req, res) => {
|
|
833
838
|
res.send("Microsoft 365 MCP Server is running");
|
|
834
839
|
});
|
|
840
|
+
app.get("/health", (req, res) => {
|
|
841
|
+
res.json({
|
|
842
|
+
status: "ok",
|
|
843
|
+
version,
|
|
844
|
+
// Startup app-credential check: did Microsoft accept the client id and secret.
|
|
845
|
+
// Not a test of user sign-in, On-Behalf-Of exchange, Graph access or Claude.
|
|
846
|
+
credentialCheck: this.credentialCheck ?? { ok: null, verdict: "not run" }
|
|
847
|
+
});
|
|
848
|
+
});
|
|
835
849
|
const mcpBound = await this.listen(app, port, host);
|
|
836
850
|
const mcp = describeBoundAddress(mcpBound);
|
|
837
851
|
logger.info(`Server listening on ${mcp.label}`);
|
|
@@ -840,6 +854,25 @@ class MicrosoftGraphServer {
|
|
|
840
854
|
logger.info(
|
|
841
855
|
` - OAuth discovery: http://${mcp.authority}/.well-known/oauth-authorization-server`
|
|
842
856
|
);
|
|
857
|
+
if (this.oboClient && this.secrets?.clientSecret && process.env.VITEST !== "true") {
|
|
858
|
+
const tenantId = this.secrets.tenantId;
|
|
859
|
+
if (!tenantId || NON_TENANT_AUTHORITIES.has(tenantId.toLowerCase())) {
|
|
860
|
+
logger.warn("Startup self-test skipped: MS365_MCP_TENANT_ID is not a tenant id");
|
|
861
|
+
} else {
|
|
862
|
+
this.credentialCheck = await checkClientCredentials(
|
|
863
|
+
this.secrets.clientId,
|
|
864
|
+
this.secrets.clientSecret,
|
|
865
|
+
this.secrets.tenantId,
|
|
866
|
+
this.secrets.cloudType
|
|
867
|
+
);
|
|
868
|
+
const c = this.credentialCheck;
|
|
869
|
+
const line = credentialCheckMessage(c, this.secrets.tenantId);
|
|
870
|
+
if (c.verdict === "accepted") logger.info(line);
|
|
871
|
+
else if (c.verdict === "rejected") logger.error(line);
|
|
872
|
+
else logger.warn(line);
|
|
873
|
+
console.error(`[ms365-mcp] ${line}`);
|
|
874
|
+
}
|
|
875
|
+
}
|
|
843
876
|
if (attachmentApp && attachmentPort !== null) {
|
|
844
877
|
let attachmentBound;
|
|
845
878
|
try {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@softeria/ms-365-mcp-server",
|
|
3
3
|
"mcpName": "io.github.Softeria/ms-365-mcp-server",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.183.0",
|
|
5
5
|
"description": " A Model Context Protocol (MCP) server for interacting with Microsoft 365 and Office services through the Graph API",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
package/src/endpoints.json
CHANGED
|
@@ -2782,6 +2782,16 @@
|
|
|
2782
2782
|
"appScopes": ["VirtualEvent.Read.All"],
|
|
2783
2783
|
"llmTip": "Lists sessions for a webinar. Each session has startDateTime, endDateTime, joinWebUrl, subject, and is essentially an online meeting associated with the webinar."
|
|
2784
2784
|
},
|
|
2785
|
+
{
|
|
2786
|
+
"pathPattern": "/places/graph.room",
|
|
2787
|
+
"method": "get",
|
|
2788
|
+
"toolName": "list-rooms",
|
|
2789
|
+
"descriptionOverride": "List the meeting rooms in the tenant: displayName, emailAddress (to book the room), capacity, building, floorNumber and address. An empty list does not prove there are no rooms: Microsoft Places may not be set up in the tenant.",
|
|
2790
|
+
"presets": ["calendar", "work"],
|
|
2791
|
+
"workScopes": ["Place.Read.All"],
|
|
2792
|
+
"appScopes": ["Place.Read.All"],
|
|
2793
|
+
"queryParams": ["select", "top", "skip", "filter", "skiptoken"]
|
|
2794
|
+
},
|
|
2785
2795
|
{
|
|
2786
2796
|
"pathPattern": "/places/{place-id}/graph.room",
|
|
2787
2797
|
"method": "get",
|
|
@@ -2808,7 +2818,7 @@
|
|
|
2808
2818
|
"presets": ["calendar", "work"],
|
|
2809
2819
|
"workScopes": ["Place.Read.All"],
|
|
2810
2820
|
"appScopes": ["Place.Read.All"],
|
|
2811
|
-
"llmTip": "The place-id
|
|
2821
|
+
"llmTip": "The place-id must be the room list's email address (from get-room-list), not its ID. Returns rooms with displayName, emailAddress, capacity, building, floorNumber, floorLabel, and AV equipment details. Combine with find-meeting-times to find available rooms for booking."
|
|
2812
2822
|
},
|
|
2813
2823
|
{
|
|
2814
2824
|
"pathPattern": "/places/{place-id}/graph.roomList/rooms/{room-id}",
|
|
@@ -2818,7 +2828,7 @@
|
|
|
2818
2828
|
"presets": ["calendar", "work"],
|
|
2819
2829
|
"workScopes": ["Place.Read.All"],
|
|
2820
2830
|
"appScopes": ["Place.Read.All"],
|
|
2821
|
-
"llmTip": "The place-id is the room list
|
|
2831
|
+
"llmTip": "The place-id is the room list's email address (as for list-room-list-rooms), and room-id is the specific room ID. Returns full room details including capacity, building, floor, and AV equipment."
|
|
2822
2832
|
},
|
|
2823
2833
|
{
|
|
2824
2834
|
"pathPattern": "/places/{place-id}",
|