@happyvertical/smrt-app-mcp 0.62.4 → 0.62.6

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,8 +1,63 @@
1
- import { i as MCP_TOOL_ACCESS_DENIED_CODE, l as authoredMcpToolError, n as MCP_CONFIRM_REQUIRED_CODE, o as MCP_TOOL_NOT_FOUND_CODE, s as McpAccessError, t as MCP_AUTHENTICATION_REQUIRED_CODE, u as safeMcpToolError } from "./errors-C704k8Mv.js";
1
+ import { i as MCP_TOOL_ACCESS_DENIED_CODE, l as authoredMcpToolError, n as MCP_CONFIRM_REQUIRED_CODE, o as MCP_TOOL_NOT_FOUND_CODE, s as McpAccessError, t as MCP_AUTHENTICATION_REQUIRED_CODE, u as safeMcpToolError } from "./errors-BuM1XuuV.js";
2
2
  import { McpTaskNotFoundError, McpTaskStore, SmrtJobCollection } from "@happyvertical/smrt-jobs";
3
3
  import { MCPGenerator, MCP_STABLE_CATALOG_TTL_MS, assertMcpJsonSchemaSafety, mcpToolAnnotationsFor } from "@happyvertical/smrt-core/generators/mcp";
4
4
  import { ObjectRegistry, isTenantScopedClassResolved } from "@happyvertical/smrt-core";
5
5
  import { createHash } from "node:crypto";
6
+ //#region src/approval-gate.ts
7
+ var MCP_APPROVAL_PENDING_CODE = "approval_pending";
8
+ var MCP_APPROVAL_REFUSED_CODE = "approval_refused";
9
+ var MCP_APPROVAL_UNAVAILABLE_CODE = "approval_unavailable";
10
+ var MCP_APPROVAL_REQUESTED_CODE = "approval_requested";
11
+ var MCP_APPROVAL_CONSUMED_CODE = "approval_consumed";
12
+ var MAX_DEPTH = 32;
13
+ function canonicalize(value, depth) {
14
+ if (depth > MAX_DEPTH) throw new TypeError("arguments too deeply nested");
15
+ if (value === null) return "null";
16
+ switch (typeof value) {
17
+ case "string": return JSON.stringify(value);
18
+ case "boolean": return value ? "true" : "false";
19
+ case "number":
20
+ if (!Number.isFinite(value)) throw new TypeError("non-finite number");
21
+ return JSON.stringify(value);
22
+ case "object": {
23
+ if (Array.isArray(value)) return `[${value.map((item) => canonicalize(item, depth + 1)).join(",")}]`;
24
+ const proto = Object.getPrototypeOf(value);
25
+ if (proto !== Object.prototype && proto !== null) throw new TypeError("non-plain object");
26
+ return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonicalize(value[key], depth + 1)}`).join(",")}}`;
27
+ }
28
+ default: throw new TypeError(`unsupported argument type: ${typeof value}`);
29
+ }
30
+ }
31
+ async function mcpApprovalBindingHash(input) {
32
+ const text = canonicalize([
33
+ "smrt-app-mcp.approval.v1",
34
+ input.tool,
35
+ input.tenantId,
36
+ input.principalId,
37
+ input.args
38
+ ], 0);
39
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", new TextEncoder().encode(text));
40
+ return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
41
+ }
42
+ var SECRET_KEY = /pass|pwd|secret|token|key|credential|auth|cookie|session|signature|bearer|\bpin\b|^pin|pin$|otp|jwt|dsn|conn(ection)?[-_]?string|private|seed|mnemonic|cert/i;
43
+ var MCP_APPROVAL_REDACTED = "[redacted]";
44
+ function redactMcpApprovalArguments(value, depth = 0) {
45
+ if (depth > MAX_DEPTH) return MCP_APPROVAL_REDACTED;
46
+ if (Array.isArray(value)) return value.map((item) => redactMcpApprovalArguments(item, depth + 1));
47
+ if (value && typeof value === "object") {
48
+ const out = {};
49
+ for (const key of Object.keys(value).sort()) out[key] = SECRET_KEY.test(key) ? MCP_APPROVAL_REDACTED : redactMcpApprovalArguments(value[key], depth + 1);
50
+ return out;
51
+ }
52
+ return value;
53
+ }
54
+ function buildMcpApprovalSummary(input) {
55
+ const redacted = redactMcpApprovalArguments(input.redact ? input.redact(input.tool, input.args) : input.args);
56
+ const snapshot = JSON.stringify(redacted);
57
+ if (snapshot.length > 3e3) return null;
58
+ return `MCP tool ${input.tool} requested by ${input.principalKind ?? "unknown"} principal ${input.principalId}. Arguments (secrets redacted): ${snapshot}`;
59
+ }
60
+ //#endregion
6
61
  //#region src/audit.ts
7
62
  var MAX_ARGUMENT_KEYS = 32;
8
63
  var MAX_KEY_LENGTH = 120;
@@ -7172,6 +7227,9 @@ function configuredEffects(effects) {
7172
7227
  }
7173
7228
  var DENIED_FAILURE_CODES = /* @__PURE__ */ new Set([
7174
7229
  "forbidden",
7230
+ MCP_APPROVAL_PENDING_CODE,
7231
+ MCP_APPROVAL_REFUSED_CODE,
7232
+ MCP_APPROVAL_UNAVAILABLE_CODE,
7175
7233
  MCP_CONFIRM_REQUIRED_CODE,
7176
7234
  MCP_TOOL_ACCESS_DENIED_CODE,
7177
7235
  MCP_AUTHENTICATION_REQUIRED_CODE,
@@ -7420,6 +7478,73 @@ function createMcpAppServer(options) {
7420
7478
  cacheScope: "private"
7421
7479
  };
7422
7480
  }
7481
+ async function gateWithApproval(tool, args, principal) {
7482
+ const unavailable = { block: {
7483
+ failure: {
7484
+ code: MCP_APPROVAL_UNAVAILABLE_CODE,
7485
+ message: "Approval could not be checked; the call was not run."
7486
+ },
7487
+ auditCode: MCP_APPROVAL_UNAVAILABLE_CODE
7488
+ } };
7489
+ const gate = options.approvals;
7490
+ if (!gate || typeof principal?.id !== "string" || !principal.id || typeof principal.tenantId !== "string" || !principal.tenantId) return unavailable;
7491
+ try {
7492
+ const { confirm: _confirm, ...callArgs } = args;
7493
+ const bindingHash = await mcpApprovalBindingHash({
7494
+ tool: tool.name,
7495
+ tenantId: principal.tenantId,
7496
+ principalId: principal.id,
7497
+ args: callArgs
7498
+ });
7499
+ const summary = buildMcpApprovalSummary({
7500
+ tool: tool.name,
7501
+ principalId: principal.id,
7502
+ principalKind: principal.kind ?? null,
7503
+ args: callArgs,
7504
+ redact: options.approvalRedact
7505
+ });
7506
+ if (summary === null) return { block: {
7507
+ failure: {
7508
+ code: MCP_APPROVAL_REFUSED_CODE,
7509
+ message: "Approval refused: arguments_too_large_to_review. Send smaller arguments.",
7510
+ details: { reason: "arguments_too_large_to_review" }
7511
+ },
7512
+ auditCode: MCP_APPROVAL_REFUSED_CODE
7513
+ } };
7514
+ const result = await gate.check({
7515
+ tool: tool.name,
7516
+ tenantId: principal.tenantId,
7517
+ principalId: principal.id,
7518
+ principalKind: principal.kind ?? null,
7519
+ bindingHash,
7520
+ requestKey: `mcp:${bindingHash}`,
7521
+ summary
7522
+ });
7523
+ if (result?.state === "approved") return {};
7524
+ if (result?.state === "pending") return { block: {
7525
+ failure: {
7526
+ code: MCP_APPROVAL_PENDING_CODE,
7527
+ message: "Approval is pending: a person must approve this exact call, then call again with the same arguments.",
7528
+ ...result.approvalId ? { details: { approvalId: result.approvalId } } : {}
7529
+ },
7530
+ auditCode: result.created ? MCP_APPROVAL_REQUESTED_CODE : MCP_APPROVAL_PENDING_CODE
7531
+ } };
7532
+ if (result?.state === "refused") {
7533
+ const reason = String(result.reason ?? "refused").replace(/[^a-z0-9_]/gi, "_").slice(0, 64);
7534
+ return { block: {
7535
+ failure: {
7536
+ code: MCP_APPROVAL_REFUSED_CODE,
7537
+ message: reason === "changes_requested" ? "Approval refused: a reviewer requested changes. Change the arguments to open a new request." : `Approval refused: ${reason}.`,
7538
+ details: { reason }
7539
+ },
7540
+ auditCode: MCP_APPROVAL_REFUSED_CODE
7541
+ } };
7542
+ }
7543
+ return unavailable;
7544
+ } catch {
7545
+ return unavailable;
7546
+ }
7547
+ }
7423
7548
  async function authorizeCall(input) {
7424
7549
  const args = input.arguments ?? {};
7425
7550
  const principal = principalForCall(input);
@@ -7427,10 +7552,25 @@ function createMcpAppServer(options) {
7427
7552
  if (!resolved) throw new McpAccessError(404, "Unknown MCP tool.");
7428
7553
  const { tool, identity } = resolved;
7429
7554
  if (!passesBasePolicy(tool, principal, principal ? void 0 : getPublicPatterns())) throw new McpAccessError(401, `Authentication is required for MCP tool: ${input.name}`);
7430
- if (!await passesToolPolicy(tool, principal)) throw new McpAccessError(403, "MCP tool access is not permitted.", {
7431
- code: MCP_TOOL_ACCESS_DENIED_CODE,
7432
- retryable: false
7433
- });
7555
+ if (!await passesToolPolicy(tool, principal)) throw new McpAccessError(404, "Unknown MCP tool.", {}, MCP_TOOL_ACCESS_DENIED_CODE);
7556
+ if (needsConfirm(tool) && options.approvals) {
7557
+ runWorkflowAssertions(tool, identity, args, principal);
7558
+ const gated = await gateWithApproval(tool, args, principal);
7559
+ if (gated.block) return {
7560
+ args,
7561
+ principal,
7562
+ tool,
7563
+ confirmRequired: false,
7564
+ approvalBlock: gated.block
7565
+ };
7566
+ return {
7567
+ args,
7568
+ principal,
7569
+ tool,
7570
+ confirmRequired: false,
7571
+ approvalConsumed: true
7572
+ };
7573
+ }
7434
7574
  if (needsConfirm(tool) && args.confirm !== true) return {
7435
7575
  args,
7436
7576
  principal,
@@ -7510,7 +7650,7 @@ function createMcpAppServer(options) {
7510
7650
  }
7511
7651
  function deniedInfo(error) {
7512
7652
  return {
7513
- code: error.metadata.code ?? (error.status === 404 ? "mcp_tool_not_found" : error.status === 401 ? "mcp_authentication_required" : "mcp_access_denied"),
7653
+ code: error.auditCode ?? error.metadata.code ?? (error.status === 404 ? "mcp_tool_not_found" : error.status === 401 ? "mcp_authentication_required" : "mcp_access_denied"),
7514
7654
  message: error.message
7515
7655
  };
7516
7656
  }
@@ -7524,6 +7664,21 @@ function createMcpAppServer(options) {
7524
7664
  else await audit(input, principal, "error", safeMcpToolError(error));
7525
7665
  throw error;
7526
7666
  }
7667
+ if (authorized.approvalBlock) {
7668
+ const { failure, auditCode } = authorized.approvalBlock;
7669
+ await audit(input, principal, "denied", {
7670
+ code: auditCode,
7671
+ message: failure.message
7672
+ });
7673
+ return {
7674
+ isError: true,
7675
+ content: [{
7676
+ type: "text",
7677
+ text: failure.message
7678
+ }],
7679
+ structuredContent: { error: failure }
7680
+ };
7681
+ }
7527
7682
  if (authorized.confirmRequired) {
7528
7683
  const failure = {
7529
7684
  code: MCP_CONFIRM_REQUIRED_CODE,
@@ -7558,7 +7713,10 @@ function createMcpAppServer(options) {
7558
7713
  if (result.response.isError) {
7559
7714
  const failure = result.failure ?? failureOf(result.response);
7560
7715
  await audit(input, principal, DENIED_FAILURE_CODES.has(failure.code) ? "denied" : "error", failure);
7561
- } else await audit(input, principal, "ok");
7716
+ } else await audit(input, principal, "ok", authorized.approvalConsumed ? {
7717
+ code: MCP_APPROVAL_CONSUMED_CODE,
7718
+ message: "An approval was consumed by this call."
7719
+ } : void 0);
7562
7720
  return result.response;
7563
7721
  }
7564
7722
  function failureOf(response) {
@@ -7779,6 +7937,6 @@ function createDefaultMcpAppServer(options) {
7779
7937
  });
7780
7938
  }
7781
7939
  //#endregion
7782
- export { prepareMcpAppResource as _, createMcpAppServer as a, mcpArgumentKeyNames as b, compareMcpToolNames as c, isPublicToolName as d, isReadOnlyToolName as f, MCP_APP_RESOURCE_MIME as g, MCP_APP_RESOURCE_MAX_BYTES as h, mcpPrincipalScopePolicy as i, isAllowedCoreTool as l, mcpToolEffect as m, createDefaultMcpAppServer as n, MCP_TOOL_EFFECTS as o, matchesToolPattern as p, mcpAllowedClassNames as r, classNamePrefixes as s, DEFAULT_MCP_APP_SERVER_INFO as t, isPublicMcpTool as u, createMcpWorkflowTool as v, mcpAuditRecordFromEvent as x, createInMemoryMcpAuditSink as y };
7940
+ export { redactMcpApprovalArguments as A, MCP_APPROVAL_PENDING_CODE as C, MCP_APPROVAL_UNAVAILABLE_CODE as D, MCP_APPROVAL_REQUESTED_CODE as E, buildMcpApprovalSummary as O, MCP_APPROVAL_CONSUMED_CODE as S, MCP_APPROVAL_REFUSED_CODE as T, prepareMcpAppResource as _, createMcpAppServer as a, mcpArgumentKeyNames as b, compareMcpToolNames as c, isPublicToolName as d, isReadOnlyToolName as f, MCP_APP_RESOURCE_MIME as g, MCP_APP_RESOURCE_MAX_BYTES as h, mcpPrincipalScopePolicy as i, mcpApprovalBindingHash as k, isAllowedCoreTool as l, mcpToolEffect as m, createDefaultMcpAppServer as n, MCP_TOOL_EFFECTS as o, matchesToolPattern as p, mcpAllowedClassNames as r, classNamePrefixes as s, DEFAULT_MCP_APP_SERVER_INFO as t, isPublicMcpTool as u, createMcpWorkflowTool as v, MCP_APPROVAL_REDACTED as w, mcpAuditRecordFromEvent as x, createInMemoryMcpAuditSink as y };
7783
7941
 
7784
- //# sourceMappingURL=defaults-i3kpRFpB.js.map
7942
+ //# sourceMappingURL=defaults-DF29ycmk.js.map