@agentwares/agentguard 0.1.3 → 0.1.4

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-NF4BQXVM.js
3
+ // ../../packages/mcp-kit/dist/chunk-7LRFDQNC.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,18 @@ import {
12
12
  import { z } from "zod";
13
13
 
14
14
  // ../../packages/x402/dist/index.js
15
+ var USDC_BASE = "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913";
16
+ var USDC_DECIMALS = 6;
17
+ var USDC_EIP712_DOMAIN = { name: "USD Coin", version: "2" };
18
+ function toAtomicUnits(amountUsd, decimals = USDC_DECIMALS) {
19
+ const trimmed = amountUsd.trim();
20
+ if (!/^\d+(\.\d+)?$/.test(trimmed)) throw new Error(`not a decimal amount: ${amountUsd}`);
21
+ const [whole = "0", fraction = ""] = trimmed.split(".");
22
+ if (fraction.length > decimals) {
23
+ throw new Error(`${amountUsd} has more than ${decimals} decimal places`);
24
+ }
25
+ return `${whole}${fraction.padEnd(decimals, "0")}`.replace(/^0+(?=\d)/, "");
26
+ }
15
27
  var DEFAULT_X402_NETWORK = "eip155:8453";
16
28
  var X402_HEADER = "X-Payment-Requirements";
17
29
  var X402_REQUIRED_HEADER = "PAYMENT-REQUIRED";
@@ -27,13 +39,18 @@ function buildPaymentRequired(opts) {
27
39
  if (opts.payTo) {
28
40
  accepts.push({
29
41
  rail: "x402",
42
+ scheme: "exact",
30
43
  amountUsd,
44
+ amount: toAtomicUnits(amountUsd, opts.assetDecimals),
31
45
  resource: opts.resource,
32
46
  description: opts.description,
47
+ mimeType: "application/json",
33
48
  network: opts.network ?? DEFAULT_X402_NETWORK,
34
- asset: "USDC",
49
+ asset: opts.asset ?? USDC_BASE,
50
+ extra: opts.assetDomain ?? USDC_EIP712_DOMAIN,
35
51
  payTo: opts.payTo,
36
- maxTimeoutSeconds: 300
52
+ maxTimeoutSeconds: 300,
53
+ ...opts.bazaar ? { bazaar: opts.bazaar } : {}
37
54
  });
38
55
  }
39
56
  if (creditsPayTo) {
@@ -51,7 +68,11 @@ function buildPaymentRequired(opts) {
51
68
  }
52
69
  const live = opts.live === true;
53
70
  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.";
71
+ const x402Rail = accepts.find((a) => a.rail === "x402" && a.payTo);
72
+ const usdcFix = x402Rail ? `Pay in USDC with no account and no human: take the \`accepts\` entry with \`rail: "x402"\`, sign an EIP-3009 transferWithAuthorization for ${x402Rail.amountUsd} USDC to ${x402Rail.payTo} on ${x402Rail.network}, and retry with it base64 in the X-PAYMENT header. The facilitator submits the transfer, so you need USDC and no gas. ` : "";
73
+ const sampleFix = opts.sampleMode ? "Free first: retry with sample=true \u2014 no key, no payment. " : "";
74
+ const cardFix = selfServe ? `Or pay by card: POST ${selfServe.keysUrl} (no auth, no signup) returns a key and a Stripe Checkout URL. Funding that key needs a person at a browser; if you are running unattended, surface the URL to your operator rather than retrying, or use the USDC rail above. Once funded, send Authorization: Bearer <key>, plus Idempotency-Key so a repeated call is charged once.` : "";
75
+ const liveFix = usdcFix || cardFix ? `${sampleFix}${usdcFix}${cardFix}`.trim() : "Retry with a PAYMENT header satisfying one of `accepts`, or send an API key with credit balance.";
55
76
  const body = {
56
77
  code: "PAYMENT_REQUIRED",
57
78
  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.`,
@@ -60,10 +81,19 @@ function buildPaymentRequired(opts) {
60
81
  priceUsd: opts.priceUsd,
61
82
  accepts,
62
83
  comingSoon: !live,
63
- alternatives: ["api_key_credits", "sample_mode"],
64
- ...selfServe ? { selfServe } : {}
84
+ alternatives: [
85
+ ...creditsPayTo ? ["api_key_credits"] : [],
86
+ ...opts.sampleMode ? ["sample_mode"] : []
87
+ ],
88
+ ...selfServe ? { selfServe } : {},
89
+ ...opts.bazaar ? { extensions: { bazaar: opts.bazaar } } : {}
65
90
  };
66
- const encoded = JSON.stringify({ x402Version: 2, accepts, resource: opts.resource });
91
+ const encoded = JSON.stringify({
92
+ x402Version: 2,
93
+ accepts,
94
+ resource: opts.resource,
95
+ ...opts.bazaar ? { extensions: { bazaar: opts.bazaar } } : {}
96
+ });
67
97
  const headers = {
68
98
  "Content-Type": "application/json",
69
99
  "WWW-Authenticate": `Payment realm="agentwares", methods="x402 prepaid_credits", price="${amountUsd} USD"`,
@@ -84,7 +114,7 @@ var StubVerifier = class {
84
114
  }
85
115
  };
86
116
 
87
- // ../../packages/mcp-kit/dist/chunk-NF4BQXVM.js
117
+ // ../../packages/mcp-kit/dist/chunk-7LRFDQNC.js
88
118
  import { z as z2 } from "zod";
89
119
  import { z as z3 } from "zod";
90
120
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
@@ -556,21 +586,26 @@ function withPayment(tool, opts) {
556
586
  if (auth.ok) return tool.handler(record, { ...ctx, userId: auth.userId });
557
587
  reason = auth.reason;
558
588
  }
589
+ const resource = ctx.resourceUrl ?? tool.name;
559
590
  const required = buildPaymentRequired({
560
- resource: tool.name,
591
+ resource,
561
592
  priceUsd: opts.priceUsd,
562
593
  live: opts.live,
563
594
  description: opts.description,
564
595
  payTo: opts.payTo,
565
596
  creditsPayTo: opts.creditsPayTo,
566
597
  signupUrl: opts.signupUrl,
567
- selfServe: opts.selfServe
598
+ selfServe: opts.selfServe,
599
+ // withPayment always adds a free `sample` flag to the tool's input schema
600
+ sampleMode: true,
601
+ ...opts.bazaar ? { bazaar: opts.bazaar } : {}
568
602
  });
569
603
  const advertised = required.body.accepts.find((a) => a.rail === "prepaid_credits") ?? required.body.accepts[0];
570
604
  const requirement = advertised ?? {
571
605
  rail: "prepaid_credits",
572
606
  amountUsd: String(required.body.priceUsd),
573
- resource: tool.name
607
+ resource,
608
+ ...opts.description ? { description: opts.description } : {}
574
609
  };
575
610
  const verified = await verifier.verify(
576
611
  new Request(`https://mcp.local/${tool.name}`, { headers: ctx.headers }),