@agentwares/agentguard 0.1.1 → 0.1.3

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,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- // ../../packages/mcp-kit/dist/chunk-AO7AV7MI.js
3
+ // ../../packages/mcp-kit/dist/chunk-NF4BQXVM.js
4
4
  import { completable } from "@modelcontextprotocol/sdk/server/completable.js";
5
5
  import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
6
6
  import {
@@ -12,6 +12,7 @@ import {
12
12
  import { z } from "zod";
13
13
 
14
14
  // ../../packages/x402/dist/index.js
15
+ var DEFAULT_X402_NETWORK = "eip155:8453";
15
16
  var X402_HEADER = "X-Payment-Requirements";
16
17
  var X402_REQUIRED_HEADER = "PAYMENT-REQUIRED";
17
18
  function toAmount(priceUsd) {
@@ -21,42 +22,51 @@ function toAmount(priceUsd) {
21
22
  }
22
23
  function buildPaymentRequired(opts) {
23
24
  const amountUsd = toAmount(opts.priceUsd);
24
- const accepts = [
25
- {
25
+ const creditsPayTo = opts.creditsPayTo ?? opts.selfServe?.keysUrl;
26
+ const accepts = [];
27
+ if (opts.payTo) {
28
+ accepts.push({
26
29
  rail: "x402",
27
30
  amountUsd,
28
31
  resource: opts.resource,
29
32
  description: opts.description,
30
- network: "eip155:8453",
33
+ network: opts.network ?? DEFAULT_X402_NETWORK,
31
34
  asset: "USDC",
32
35
  payTo: opts.payTo,
33
36
  maxTimeoutSeconds: 300
34
- },
35
- {
36
- rail: "mpp",
37
+ });
38
+ }
39
+ if (creditsPayTo) {
40
+ accepts.push({
41
+ rail: "prepaid_credits",
37
42
  amountUsd,
38
43
  resource: opts.resource,
39
44
  description: opts.description,
40
45
  currency: "usd",
41
- payTo: opts.payTo,
42
- maxTimeoutSeconds: 300
43
- }
44
- ];
46
+ payTo: creditsPayTo,
47
+ maxTimeoutSeconds: 300,
48
+ // funding a balance ends at a hosted Stripe Checkout page, which an agent cannot fill in
49
+ humanRequired: true
50
+ });
51
+ }
45
52
  const live = opts.live === true;
53
+ const selfServe = opts.selfServe;
54
+ const liveFix = selfServe ? `Free first: retry with sample=true \u2014 no key, no payment. To pay: POST ${selfServe.keysUrl} (no auth, no signup) returns a key and a Stripe Checkout URL. Funding that key needs a person to complete the Checkout page in a browser; if you are running unattended, surface the URL to your operator rather than retrying. Once funded, send Authorization: Bearer <key>, plus Idempotency-Key so a repeated call is charged once.` : "Retry with a PAYMENT header satisfying one of `accepts`, or send an API key with credit balance.";
46
55
  const body = {
47
56
  code: "PAYMENT_REQUIRED",
48
57
  cause: live ? `This call costs $${amountUsd}. No valid payment or credit was presented.` : `This call costs $${amountUsd}. Pay-per-call (x402 / Stripe MPP) is coming soon; use an API key with prepaid credits.`,
49
- fix: live ? "Retry with a PAYMENT header satisfying one of `accepts`, or send an API key with credit balance." : `Create an API key${opts.signupUrl ? ` at ${opts.signupUrl}` : ""} and retry with Authorization: Bearer <key>. Try the free sample mode first.`,
58
+ fix: live ? liveFix : `Create an API key${opts.signupUrl ? ` at ${opts.signupUrl}` : ""} and retry with Authorization: Bearer <key>. Try the free sample mode first.`,
50
59
  retryable: true,
51
60
  priceUsd: opts.priceUsd,
52
61
  accepts,
53
62
  comingSoon: !live,
54
- alternatives: ["api_key_credits", "sample_mode"]
63
+ alternatives: ["api_key_credits", "sample_mode"],
64
+ ...selfServe ? { selfServe } : {}
55
65
  };
56
66
  const encoded = JSON.stringify({ x402Version: 2, accepts, resource: opts.resource });
57
67
  const headers = {
58
68
  "Content-Type": "application/json",
59
- "WWW-Authenticate": `Payment realm="agentwares", methods="x402 mpp", price="${amountUsd} USD"`,
69
+ "WWW-Authenticate": `Payment realm="agentwares", methods="x402 prepaid_credits", price="${amountUsd} USD"`,
60
70
  [X402_HEADER]: encoded,
61
71
  [X402_REQUIRED_HEADER]: btoa(encoded),
62
72
  "Cache-Control": "no-store"
@@ -74,7 +84,7 @@ var StubVerifier = class {
74
84
  }
75
85
  };
76
86
 
77
- // ../../packages/mcp-kit/dist/chunk-AO7AV7MI.js
87
+ // ../../packages/mcp-kit/dist/chunk-NF4BQXVM.js
78
88
  import { z as z2 } from "zod";
79
89
  import { z as z3 } from "zod";
80
90
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -521,6 +531,7 @@ function withPayment(tool, opts) {
521
531
  );
522
532
  }
523
533
  const verifier = opts.verifier ?? new StubVerifier();
534
+ const liveRail = opts.verifier !== void 0;
524
535
  const input = tool.input.extend({
525
536
  [sampleField]: z2.boolean().default(false).describe(
526
537
  "Set true to return an example response at no charge (sample mode). Default false."
@@ -551,17 +562,35 @@ function withPayment(tool, opts) {
551
562
  live: opts.live,
552
563
  description: opts.description,
553
564
  payTo: opts.payTo,
554
- signupUrl: opts.signupUrl
565
+ creditsPayTo: opts.creditsPayTo,
566
+ signupUrl: opts.signupUrl,
567
+ selfServe: opts.selfServe
555
568
  });
556
- const requirement = required.body.accepts[0];
557
- if (requirement) {
558
- const verified = await verifier.verify(
559
- new Request(`https://mcp.local/${tool.name}`, { headers: ctx.headers }),
560
- requirement
561
- );
562
- if (verified.ok) return tool.handler(record, ctx);
563
- reason = reason ?? verified.reason;
569
+ const advertised = required.body.accepts.find((a) => a.rail === "prepaid_credits") ?? required.body.accepts[0];
570
+ const requirement = advertised ?? {
571
+ rail: "prepaid_credits",
572
+ amountUsd: String(required.body.priceUsd),
573
+ resource: tool.name
574
+ };
575
+ const verified = await verifier.verify(
576
+ new Request(`https://mcp.local/${tool.name}`, { headers: ctx.headers }),
577
+ requirement
578
+ );
579
+ if (verified.ok) {
580
+ let result;
581
+ try {
582
+ result = await tool.handler(record, ctx);
583
+ } catch (error) {
584
+ await opts.onPaidFailure?.(ctx, verified, error);
585
+ throw error;
586
+ }
587
+ const settled = await verifier.settle?.(verified);
588
+ if (settled && !settled.ok) {
589
+ opts.onSettlementFailure?.(ctx, verified, settled.reason);
590
+ }
591
+ return result;
564
592
  }
593
+ reason = liveRail ? verified.reason ?? reason : reason ?? verified.reason;
565
594
  throw paymentRequiredError(required.body, reason);
566
595
  }
567
596
  });