@abapify/adt-mcp 0.4.34 → 0.4.37

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.
@@ -1,4 +1,4 @@
1
- import { a as isMcpDestinationKey, o as isMcpOperationClass, t as createMcpServer } from "./server-BgM8hx8V.mjs";
1
+ import { a as isMcpDestinationKey, o as isMcpOperationClass, t as createMcpServer } from "./server-Dylu87TZ.mjs";
2
2
  import { Buffer as Buffer$1 } from "node:buffer";
3
3
  import { randomUUID, timingSafeEqual } from "node:crypto";
4
4
  import { readFile } from "node:fs/promises";
@@ -203,6 +203,20 @@ function extractScopes(claims) {
203
203
  }
204
204
  return parts;
205
205
  }
206
+ function fallbackUser(record) {
207
+ for (const c of [
208
+ "preferred_username",
209
+ "email",
210
+ "sub"
211
+ ]) {
212
+ const v = record[c];
213
+ if (typeof v === "string" && v.length > 0) return v;
214
+ }
215
+ }
216
+ function extractGroups(raw) {
217
+ if (Array.isArray(raw)) return raw.filter((v) => typeof v === "string");
218
+ if (typeof raw === "string" && raw.length > 0) return raw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
219
+ }
206
220
  function extractUserHint(claims, userClaim, emailClaim, groupsClaim) {
207
221
  const record = claims;
208
222
  let user;
@@ -210,28 +224,13 @@ function extractUserHint(claims, userClaim, emailClaim, groupsClaim) {
210
224
  const v = record[userClaim];
211
225
  if (typeof v === "string" && v.length > 0) user = v;
212
226
  }
213
- if (!user) for (const c of [
214
- "preferred_username",
215
- "email",
216
- "sub"
217
- ]) {
218
- const v = record[c];
219
- if (typeof v === "string" && v.length > 0) {
220
- user = v;
221
- break;
222
- }
223
- }
227
+ if (!user) user = fallbackUser(record);
224
228
  if (!user) return void 0;
225
229
  const emailRaw = record[emailClaim];
226
- const email = typeof emailRaw === "string" ? emailRaw : void 0;
227
- const groupsRaw = record[groupsClaim];
228
- let groups;
229
- if (Array.isArray(groupsRaw)) groups = groupsRaw.filter((v) => typeof v === "string");
230
- else if (typeof groupsRaw === "string" && groupsRaw.length > 0) groups = groupsRaw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
231
230
  return {
232
231
  user,
233
- email,
234
- groups
232
+ email: typeof emailRaw === "string" ? emailRaw : void 0,
233
+ groups: extractGroups(record[groupsClaim])
235
234
  };
236
235
  }
237
236
  /**
@@ -339,7 +338,7 @@ function extractBearer(req) {
339
338
  if (trimmed.slice(0, 6).toLowerCase() !== "bearer") return void 0;
340
339
  const rest = trimmed.slice(6);
341
340
  if (rest.length === 0) return void 0;
342
- const first = rest.charCodeAt(0);
341
+ const first = rest.codePointAt(0);
343
342
  if (!(first === 32 || first === 9 || first === 10 || first === 13 || first === 11 || first === 12)) return void 0;
344
343
  const token = rest.trimStart();
345
344
  return token.length > 0 ? token : void 0;
@@ -648,7 +647,7 @@ function parseScopedToolNames(value, operationClass, safeExecutePolicy) {
648
647
  });
649
648
  if (!toolNames) return void 0;
650
649
  if (operationClass === "read") return safeExecutePolicy === void 0 ? toolNames : void 0;
651
- return safeExecutePolicy && toolNames[0] === safeExecutePolicy.operationId ? toolNames : void 0;
650
+ return toolNames[0] === safeExecutePolicy?.operationId ? toolNames : void 0;
652
651
  }
653
652
  function parseExecutionAuthorizationToken(value) {
654
653
  return typeof value === "string" && value.length <= MAX_SAFE_EXECUTE_GRANT_BYTES && compactJwsPattern.test(value) ? value : void 0;
@@ -1288,8 +1287,42 @@ function writeJsonError(res, status, message) {
1288
1287
  }));
1289
1288
  }
1290
1289
  /**
1291
- * Creates a reusable MCP HTTP handler without binding a Node listener.
1290
+ * Derive the identity and access snapshot captured at MCP session
1291
+ * initialization. A failed derivation never falls back to caller-selected
1292
+ * identity material — it degrades to an audit-safe `{ principal: 'unknown' }`
1293
+ * identity with no access.
1292
1294
  */
1295
+ function deriveSessionIdentity(destinationServer, authMode, userHint, invocation) {
1296
+ let identity;
1297
+ let binding;
1298
+ try {
1299
+ identity = authMode === "invocation" ? invocationRequestIdentity(invocation) : destinationServer.requestIdentity({
1300
+ userHint,
1301
+ invocation
1302
+ });
1303
+ binding = sessionIdentityBinding(identity, invocation?.tokenId);
1304
+ } catch {
1305
+ return {
1306
+ identity: { principal: "unknown" },
1307
+ access: void 0,
1308
+ binding: void 0
1309
+ };
1310
+ }
1311
+ let access;
1312
+ try {
1313
+ access = authMode === "invocation" ? invocationRequestAccess(invocation) : snapshotRequestAccess(destinationServer.requestAccess({
1314
+ userHint,
1315
+ invocation
1316
+ }));
1317
+ } catch {
1318
+ access = void 0;
1319
+ }
1320
+ return {
1321
+ identity,
1322
+ access,
1323
+ binding
1324
+ };
1325
+ }
1293
1326
  function createHttpMcpHandler(options = {}) {
1294
1327
  const host = options.host ?? process.env.MCP_HOST ?? "127.0.0.1";
1295
1328
  const log = options.log ?? defaultLog;
@@ -1340,98 +1373,97 @@ function createHttpMcpHandler(options = {}) {
1340
1373
  writeJsonError(res, 403, "mcp_session_identity_mismatch");
1341
1374
  return false;
1342
1375
  };
1376
+ const buildServerOptions = (sessionIdentity, sessionAccess) => {
1377
+ const base = {
1378
+ registry,
1379
+ resolveSystem: (id) => multiSystem.resolve(id)
1380
+ };
1381
+ if (!destinationServer) return base;
1382
+ return {
1383
+ ...base,
1384
+ destinationRegistry: destinationServer.destinationRegistry,
1385
+ requestIdentity: () => sessionIdentity ?? { principal: "unknown" },
1386
+ requestAccess: () => sessionAccess,
1387
+ ...destinationServer.resolveFrozenSource ? { resolveFrozenSource: destinationServer.resolveFrozenSource } : {},
1388
+ ...destinationServer.consumeExecutionAuthorization ? { consumeExecutionAuthorization: destinationServer.consumeExecutionAuthorization } : {},
1389
+ ...destinationServer.reportExecutionOutcome ? { reportExecutionOutcome: destinationServer.reportExecutionOutcome } : {},
1390
+ ...destinationServer.executeWithDeadline ? { executeWithDeadline: destinationServer.executeWithDeadline } : {}
1391
+ };
1392
+ };
1393
+ const handleInitialize = async (req, res, body, userHint, invocation) => {
1394
+ let initializedSessionBinding;
1395
+ const transport = new StreamableHTTPServerTransport({
1396
+ sessionIdGenerator: () => randomUUID(),
1397
+ onsessioninitialized: (id) => {
1398
+ transports.set(id, transport);
1399
+ if (initializedSessionBinding) sessionIdentityBindings.set(id, initializedSessionBinding);
1400
+ log("info", `session initialized: ${id}`);
1401
+ },
1402
+ onsessionclosed: async (id) => {
1403
+ log("info", `session closed by client: ${id}`);
1404
+ await releaseSessionResources(id);
1405
+ }
1406
+ });
1407
+ transport.onclose = () => {
1408
+ const id = transport.sessionId;
1409
+ if (!id) return;
1410
+ releaseSessionResources(id);
1411
+ };
1412
+ let sessionIdentity;
1413
+ let sessionAccess;
1414
+ if (destinationServer) {
1415
+ const derived = deriveSessionIdentity(destinationServer, authMode, userHint, invocation);
1416
+ sessionIdentity = derived.identity;
1417
+ sessionAccess = derived.access;
1418
+ initializedSessionBinding = derived.binding;
1419
+ }
1420
+ const mcp = createMcpServer(buildServerOptions(sessionIdentity, sessionAccess));
1421
+ await mcp.connect(transport);
1422
+ if (transport.sessionId) {
1423
+ transports.set(transport.sessionId, transport);
1424
+ servers.set(transport.sessionId, mcp);
1425
+ }
1426
+ await transport.handleRequest(req, res, body);
1427
+ if (transport.sessionId && !servers.has(transport.sessionId)) servers.set(transport.sessionId, mcp);
1428
+ };
1429
+ const handlePost = async (req, res, sessionId, userHint, invocation) => {
1430
+ let body;
1431
+ try {
1432
+ body = await readJsonBody(req);
1433
+ } catch (err) {
1434
+ writeJsonError(res, 400, `Invalid JSON body: ${err instanceof Error ? err.message : String(err)}`);
1435
+ return;
1436
+ }
1437
+ if (sessionId && transports.has(sessionId)) {
1438
+ if (!sessionMatchesAuthenticatedIdentity(sessionId, userHint, invocation, res)) return;
1439
+ registry.touch(sessionId);
1440
+ await transports.get(sessionId).handleRequest(req, res, body);
1441
+ return;
1442
+ }
1443
+ if (!sessionId && isInitializeRequest(body)) {
1444
+ await handleInitialize(req, res, body, userHint, invocation);
1445
+ return;
1446
+ }
1447
+ writeJsonError(res, 400, sessionId ? `Unknown Mcp-Session-Id: ${sessionId}` : "Missing Mcp-Session-Id header (non-initialize request)");
1448
+ };
1449
+ const handleSessionGetDelete = async (req, res, sessionId, userHint, invocation) => {
1450
+ if (!sessionId || !transports.has(sessionId)) {
1451
+ writeJsonError(res, 400, "Missing or unknown Mcp-Session-Id header");
1452
+ return;
1453
+ }
1454
+ if (!sessionMatchesAuthenticatedIdentity(sessionId, userHint, invocation, res)) return;
1455
+ registry.touch(sessionId);
1456
+ await transports.get(sessionId).handleRequest(req, res);
1457
+ };
1343
1458
  const handleMcp = async (req, res, userHint, invocation) => {
1344
1459
  const sessionHeader = req.headers["mcp-session-id"];
1345
1460
  const sessionId = Array.isArray(sessionHeader) ? sessionHeader[0] : sessionHeader;
1346
1461
  if (req.method === "POST") {
1347
- let body;
1348
- try {
1349
- body = await readJsonBody(req);
1350
- } catch (err) {
1351
- writeJsonError(res, 400, `Invalid JSON body: ${err instanceof Error ? err.message : String(err)}`);
1352
- return;
1353
- }
1354
- if (sessionId && transports.has(sessionId)) {
1355
- if (!sessionMatchesAuthenticatedIdentity(sessionId, userHint, invocation, res)) return;
1356
- registry.touch(sessionId);
1357
- await transports.get(sessionId).handleRequest(req, res, body);
1358
- return;
1359
- }
1360
- if (!sessionId && isInitializeRequest(body)) {
1361
- let initializedSessionBinding;
1362
- const transport = new StreamableHTTPServerTransport({
1363
- sessionIdGenerator: () => randomUUID(),
1364
- onsessioninitialized: (id) => {
1365
- transports.set(id, transport);
1366
- if (initializedSessionBinding) sessionIdentityBindings.set(id, initializedSessionBinding);
1367
- log("info", `session initialized: ${id}`);
1368
- },
1369
- onsessionclosed: async (id) => {
1370
- log("info", `session closed by client: ${id}`);
1371
- await releaseSessionResources(id);
1372
- }
1373
- });
1374
- transport.onclose = () => {
1375
- const id = transport.sessionId;
1376
- if (!id) return;
1377
- releaseSessionResources(id);
1378
- };
1379
- let sessionIdentity;
1380
- let sessionAccess;
1381
- if (destinationServer) {
1382
- let identityDerivationFailed = false;
1383
- try {
1384
- sessionIdentity = authMode === "invocation" ? invocationRequestIdentity(invocation) : destinationServer.requestIdentity({
1385
- userHint,
1386
- invocation
1387
- });
1388
- initializedSessionBinding = sessionIdentityBinding(sessionIdentity, invocation?.tokenId);
1389
- } catch {
1390
- sessionIdentity = { principal: "unknown" };
1391
- identityDerivationFailed = true;
1392
- }
1393
- if (!identityDerivationFailed) try {
1394
- sessionAccess = authMode === "invocation" ? invocationRequestAccess(invocation) : snapshotRequestAccess(destinationServer.requestAccess({
1395
- userHint,
1396
- invocation
1397
- }));
1398
- } catch {
1399
- sessionAccess = void 0;
1400
- }
1401
- }
1402
- const mcp = createMcpServer({
1403
- registry,
1404
- resolveSystem: (id) => multiSystem.resolve(id),
1405
- ...destinationServer ? {
1406
- destinationRegistry: destinationServer.destinationRegistry,
1407
- requestIdentity: () => sessionIdentity ?? { principal: "unknown" },
1408
- requestAccess: () => sessionAccess,
1409
- ...destinationServer.resolveFrozenSource ? { resolveFrozenSource: destinationServer.resolveFrozenSource } : {},
1410
- ...destinationServer.consumeExecutionAuthorization ? { consumeExecutionAuthorization: destinationServer.consumeExecutionAuthorization } : {},
1411
- ...destinationServer.reportExecutionOutcome ? { reportExecutionOutcome: destinationServer.reportExecutionOutcome } : {},
1412
- ...destinationServer.executeWithDeadline ? { executeWithDeadline: destinationServer.executeWithDeadline } : {}
1413
- } : {}
1414
- });
1415
- await mcp.connect(transport);
1416
- if (transport.sessionId) {
1417
- transports.set(transport.sessionId, transport);
1418
- servers.set(transport.sessionId, mcp);
1419
- }
1420
- await transport.handleRequest(req, res, body);
1421
- if (transport.sessionId && !servers.has(transport.sessionId)) servers.set(transport.sessionId, mcp);
1422
- return;
1423
- }
1424
- writeJsonError(res, 400, sessionId ? `Unknown Mcp-Session-Id: ${sessionId}` : "Missing Mcp-Session-Id header (non-initialize request)");
1462
+ await handlePost(req, res, sessionId, userHint, invocation);
1425
1463
  return;
1426
1464
  }
1427
1465
  if (req.method === "GET" || req.method === "DELETE") {
1428
- if (!sessionId || !transports.has(sessionId)) {
1429
- writeJsonError(res, 400, "Missing or unknown Mcp-Session-Id header");
1430
- return;
1431
- }
1432
- if (!sessionMatchesAuthenticatedIdentity(sessionId, userHint, invocation, res)) return;
1433
- registry.touch(sessionId);
1434
- await transports.get(sessionId).handleRequest(req, res);
1466
+ await handleSessionGetDelete(req, res, sessionId, userHint, invocation);
1435
1467
  return;
1436
1468
  }
1437
1469
  res.writeHead(405, { Allow: "POST, GET, DELETE" });
@@ -1551,4 +1583,4 @@ async function startHttpServer(options = {}) {
1551
1583
  }
1552
1584
 
1553
1585
  export { parseSafeExecutePolicy as a, createSessionRegistry as c, parseDelegatedAssistantReadPolicy as i, startHttpServer as n, parseScopedAdtInvocationPolicy as o, createMcpInvocationVerifier as r, loadMultiSystemConfig as s, createHttpMcpHandler as t };
1554
- //# sourceMappingURL=server-ClzRW10R.mjs.map
1586
+ //# sourceMappingURL=server-BW9yBxNy.mjs.map