@haven_ai/mcp 0.10.0-alpha.0 → 0.12.0-alpha.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/dist/index.cjs CHANGED
@@ -238,6 +238,18 @@ async function warnIfCredentialFilePermissive(path, log = (message) => process.s
238
238
  );
239
239
  }
240
240
  }
241
+ function chainRpcsFromEnv(env) {
242
+ const chainRpcs = {};
243
+ for (const [name, value] of Object.entries(env)) {
244
+ const match = /^HAVEN_RPC_URL_(\d+)$/.exec(name);
245
+ if (!match) continue;
246
+ const chainId = Number.parseInt(match[1], 10);
247
+ if (!Number.isSafeInteger(chainId) || chainId <= 0) continue;
248
+ const url = value?.trim();
249
+ if (url) chainRpcs[chainId] = url;
250
+ }
251
+ return chainRpcs;
252
+ }
241
253
  var TASK_BUDGET_MIN_SIGNER_VERSION = "0.6.0-alpha.0";
242
254
  var SIGNER_CAPABILITY_KEY = "haven/signer-compatibility";
243
255
  function taskSignerCompatibilityNotice() {
@@ -250,6 +262,20 @@ function taskSignerCompatibilityNotice() {
250
262
  };
251
263
  }
252
264
  var headersSchema = v3.z.record(v3.z.string(), v3.z.string()).optional();
265
+ function selectQuoteOption(accepts, agent) {
266
+ if (agent.executionRail === "delegation") {
267
+ const erc7710 = sdk.selectErc7710PaymentOption(accepts);
268
+ if (erc7710) return erc7710;
269
+ }
270
+ return sdk.selectStandardPaymentOption(accepts);
271
+ }
272
+ function humanDecimalToAtomic(human, decimals) {
273
+ const match = /^([0-9]+)(?:\.([0-9]+))?$/.exec(human);
274
+ if (!match) return null;
275
+ const fraction = match[2] ?? "";
276
+ if (fraction.length > decimals) return null;
277
+ return BigInt(match[1] + fraction.padEnd(decimals, "0"));
278
+ }
253
279
  var toolSchemas = {
254
280
  haven_send: {
255
281
  asset: v3.z.enum(["ETH", "USDC"]),
@@ -281,6 +307,17 @@ var toolSchemas = {
281
307
  headers: headersSchema,
282
308
  body: v3.z.string().optional(),
283
309
  idempotency_key: v3.z.string().optional(),
310
+ /**
311
+ * #3967: the price the agent EXPECTS, in whole tokens ("0.10" = 0.1 USDC),
312
+ * compared against the amount of the option pay will actually select. A
313
+ * mismatch stops the agent — the same stop signals the hosted quote
314
+ * carries. Local parity: a hosted-only input would split the two tools'
315
+ * contracts. Same shape and name as the hosted surface's.
316
+ */
317
+ expected_amount: v3.z.string().regex(
318
+ /^[0-9]+(\.[0-9]+)?$/,
319
+ 'expected_amount must be a plain decimal amount in whole tokens, e.g. "0.10"'
320
+ ).optional(),
284
321
  /** REMOVED (#3411): declared only so the resolver can refuse it by name, never accepted. */
285
322
  idempotencyKey: v3.z.string().optional().describe("REMOVED (#3411): refused \u2014 send idempotency_key")
286
323
  },
@@ -627,8 +664,39 @@ function createToolHandlers(haven) {
627
664
  const pf = preflight("haven_quote_x402", input);
628
665
  if ("success" in pf) return pf;
629
666
  const { args, warnings } = pf;
667
+ const agentPromise = args.expected_amount !== void 0 ? haven.getAgent().then(
668
+ (a) => a,
669
+ () => void 0
670
+ ) : null;
630
671
  try {
631
672
  const data = await haven.quoteX402(args.url, requestInit(args), { idempotencyKey: args.resolvedIdempotencyKey });
673
+ if (args.expected_amount !== void 0) {
674
+ const agent = agentPromise ? await agentPromise : void 0;
675
+ const option = agent ? selectQuoteOption(data.paymentRequired.accepts, agent) : null;
676
+ if (option && data.decimals !== null) {
677
+ const expectedAtomic = humanDecimalToAtomic(args.expected_amount, data.decimals);
678
+ let optionAmount = null;
679
+ try {
680
+ optionAmount = BigInt(sdk.x402AuthorizationAmount(option));
681
+ } catch {
682
+ optionAmount = null;
683
+ }
684
+ if (optionAmount !== null && expectedAtomic !== null) {
685
+ const priceDiffers = optionAmount !== expectedAtomic;
686
+ const payload = {
687
+ ...data,
688
+ price_differs_from_expected: priceDiffers,
689
+ ...priceDiffers ? {
690
+ safe_to_continue: false,
691
+ next_action: sdk.AgentPaymentNextAction.StopAndTellUser,
692
+ next_tool_omitted_reason: "no Haven tool follows: the quoted price does not match the stated expectation \u2014 the user decides before any payment",
693
+ reason: `The merchant's quoted price (${data.amount} ${data.asset}) does not match the expected price (${args.expected_amount}) stated with this quote. Stop and tell the user before any payment; do not re-quote, re-pay, or poll. If the x402 challenge itself is wrong and nothing has been paid via x402 on this invoice, the invoice's own deposit address MAY be an alternative \u2014 but only with the user's explicit go-ahead, never automatically, and never once anything has been paid via x402.`
694
+ } : {}
695
+ };
696
+ return warnings.length > 0 ? { success: true, data: payload, warnings } : { success: true, data: payload };
697
+ }
698
+ }
699
+ }
632
700
  return warnings.length > 0 ? { success: true, data, warnings } : { success: true, data };
633
701
  } catch (err) {
634
702
  return normalizeError(err);
@@ -1084,6 +1152,24 @@ function normalizeError(err) {
1084
1152
  message: err.message
1085
1153
  };
1086
1154
  }
1155
+ if (err instanceof sdk.MerchantEgressRefusedError && !err.beforeRequest && err.paymentId) {
1156
+ const noFundingLeg = err.settlementScheme === "erc7710";
1157
+ const nextAction = noFundingLeg ? sdk.AgentPaymentNextAction.CheckStatusLater : sdk.AgentPaymentNextAction.SweepStrandedFunds;
1158
+ return {
1159
+ success: false,
1160
+ code: err.code,
1161
+ message: err.message,
1162
+ statusCode: err.statusCode,
1163
+ paymentId: err.paymentId,
1164
+ status: "merchant_unresponsive_after_funding",
1165
+ phase: noFundingLeg ? "not_delivered" : "funded_but_unsettled",
1166
+ nextAction,
1167
+ next_action: nextAction,
1168
+ next_tool: "haven_get_payment_status",
1169
+ next_arguments: { payment_id: err.paymentId },
1170
+ suggested_tool: "haven_get_payment_status"
1171
+ };
1172
+ }
1087
1173
  if (err instanceof sdk.HavenApiError) {
1088
1174
  const body = err.body;
1089
1175
  return {
@@ -1330,10 +1416,14 @@ async function resolveHavenClient(options = {}) {
1330
1416
  signerPath: options.signerPath
1331
1417
  } : void 0;
1332
1418
  const credentials = options.credentials ?? await loadCredentials(credentialSource);
1419
+ const chainRpcs = chainRpcsFromEnv(process.env);
1333
1420
  const client = new sdk.HavenClient({
1334
1421
  apiKey: credentials.apiKey,
1335
1422
  delegateKey: credentials.delegateKey,
1336
1423
  baseUrl: credentials.apiUrl,
1424
+ // Only added when non-empty so the unset-variable construction is byte-for-byte
1425
+ // today's: the SDK defaults `chainRpcs` to `{}` either way.
1426
+ ...Object.keys(chainRpcs).length > 0 ? { chainRpcs } : {},
1337
1427
  // #3303: name this package, not the SDK inside it, so the backend's
1338
1428
  // update hint and refusal speak about what the user actually installed.
1339
1429
  clientIdentity: sdk.havenClientIdentity(MCP_NAME, MCP_VERSION)
@@ -1345,7 +1435,7 @@ async function createHavenMcpServer(options = {}) {
1345
1435
  return buildMcpServer(haven);
1346
1436
  }
1347
1437
  var MCP_NAME = "@haven_ai/mcp";
1348
- var MCP_VERSION = "0.10.0-alpha.0";
1438
+ var MCP_VERSION = "0.12.0-alpha.0";
1349
1439
  var MCP_INSTRUCTIONS = [
1350
1440
  "Haven local MCP server: signs in-process with the delegate key it holds on",
1351
1441
  "this machine \u2014 the key never leaves this process. Call haven_get_agent",