@absol-labs/agent 0.9.1 → 0.11.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.
Files changed (293) hide show
  1. package/README.md +184 -26
  2. package/dist/capability/invocation-capability.d.ts +18 -6
  3. package/dist/capability/invocation-capability.d.ts.map +1 -1
  4. package/dist/capability/invocation-capability.js.map +1 -1
  5. package/dist/cli/amounts.d.ts +26 -0
  6. package/dist/cli/amounts.d.ts.map +1 -0
  7. package/dist/cli/amounts.js +71 -0
  8. package/dist/cli/amounts.js.map +1 -0
  9. package/dist/cli/args.d.ts +32 -0
  10. package/dist/cli/args.d.ts.map +1 -0
  11. package/dist/cli/args.js +119 -0
  12. package/dist/cli/args.js.map +1 -0
  13. package/dist/cli/attesters.d.ts +51 -0
  14. package/dist/cli/attesters.d.ts.map +1 -0
  15. package/dist/cli/attesters.js +58 -0
  16. package/dist/cli/attesters.js.map +1 -0
  17. package/dist/cli/bin.d.ts +3 -0
  18. package/dist/cli/bin.d.ts.map +1 -0
  19. package/dist/cli/bin.js +5 -0
  20. package/dist/cli/bin.js.map +1 -0
  21. package/dist/cli/commands/claim.d.ts +13 -0
  22. package/dist/cli/commands/claim.d.ts.map +1 -0
  23. package/dist/cli/commands/claim.js +102 -0
  24. package/dist/cli/commands/claim.js.map +1 -0
  25. package/dist/cli/commands/close.d.ts +3 -0
  26. package/dist/cli/commands/close.d.ts.map +1 -0
  27. package/dist/cli/commands/close.js +55 -0
  28. package/dist/cli/commands/close.js.map +1 -0
  29. package/dist/cli/commands/discover.d.ts +3 -0
  30. package/dist/cli/commands/discover.d.ts.map +1 -0
  31. package/dist/cli/commands/discover.js +183 -0
  32. package/dist/cli/commands/discover.js.map +1 -0
  33. package/dist/cli/commands/invoke.d.ts +3 -0
  34. package/dist/cli/commands/invoke.d.ts.map +1 -0
  35. package/dist/cli/commands/invoke.js +142 -0
  36. package/dist/cli/commands/invoke.js.map +1 -0
  37. package/dist/cli/commands/mcp.d.ts +3 -0
  38. package/dist/cli/commands/mcp.d.ts.map +1 -0
  39. package/dist/cli/commands/mcp.js +113 -0
  40. package/dist/cli/commands/mcp.js.map +1 -0
  41. package/dist/cli/commands/open.d.ts +3 -0
  42. package/dist/cli/commands/open.d.ts.map +1 -0
  43. package/dist/cli/commands/open.js +331 -0
  44. package/dist/cli/commands/open.js.map +1 -0
  45. package/dist/cli/commands/reclaim-unverified.d.ts +3 -0
  46. package/dist/cli/commands/reclaim-unverified.d.ts.map +1 -0
  47. package/dist/cli/commands/reclaim-unverified.js +172 -0
  48. package/dist/cli/commands/reclaim-unverified.js.map +1 -0
  49. package/dist/cli/commands/reclaim.d.ts +33 -0
  50. package/dist/cli/commands/reclaim.d.ts.map +1 -0
  51. package/dist/cli/commands/reclaim.js +308 -0
  52. package/dist/cli/commands/reclaim.js.map +1 -0
  53. package/dist/cli/commands/status.d.ts +3 -0
  54. package/dist/cli/commands/status.d.ts.map +1 -0
  55. package/dist/cli/commands/status.js +43 -0
  56. package/dist/cli/commands/status.js.map +1 -0
  57. package/dist/cli/commands/streams.d.ts +3 -0
  58. package/dist/cli/commands/streams.d.ts.map +1 -0
  59. package/dist/cli/commands/streams.js +257 -0
  60. package/dist/cli/commands/streams.js.map +1 -0
  61. package/dist/cli/commands/watch.d.ts +3 -0
  62. package/dist/cli/commands/watch.d.ts.map +1 -0
  63. package/dist/cli/commands/watch.js +55 -0
  64. package/dist/cli/commands/watch.js.map +1 -0
  65. package/dist/cli/config.d.ts +61 -0
  66. package/dist/cli/config.d.ts.map +1 -0
  67. package/dist/cli/config.js +140 -0
  68. package/dist/cli/config.js.map +1 -0
  69. package/dist/cli/context.d.ts +29 -0
  70. package/dist/cli/context.d.ts.map +1 -0
  71. package/dist/cli/context.js +2 -0
  72. package/dist/cli/context.js.map +1 -0
  73. package/dist/cli/deps.d.ts +191 -0
  74. package/dist/cli/deps.d.ts.map +1 -0
  75. package/dist/cli/deps.js +277 -0
  76. package/dist/cli/deps.js.map +1 -0
  77. package/dist/cli/descriptor.d.ts +61 -0
  78. package/dist/cli/descriptor.d.ts.map +1 -0
  79. package/dist/cli/descriptor.js +78 -0
  80. package/dist/cli/descriptor.js.map +1 -0
  81. package/dist/cli/errors.d.ts +48 -0
  82. package/dist/cli/errors.d.ts.map +1 -0
  83. package/dist/cli/errors.js +129 -0
  84. package/dist/cli/errors.js.map +1 -0
  85. package/dist/cli/escape-hatch.d.ts +50 -0
  86. package/dist/cli/escape-hatch.d.ts.map +1 -0
  87. package/dist/cli/escape-hatch.js +45 -0
  88. package/dist/cli/escape-hatch.js.map +1 -0
  89. package/dist/cli/index.d.ts +8 -0
  90. package/dist/cli/index.d.ts.map +1 -0
  91. package/dist/cli/index.js +7 -0
  92. package/dist/cli/index.js.map +1 -0
  93. package/dist/cli/listings.d.ts +15 -0
  94. package/dist/cli/listings.d.ts.map +1 -0
  95. package/dist/cli/listings.js +56 -0
  96. package/dist/cli/listings.js.map +1 -0
  97. package/dist/cli/mandate.d.ts +66 -0
  98. package/dist/cli/mandate.d.ts.map +1 -0
  99. package/dist/cli/mandate.js +61 -0
  100. package/dist/cli/mandate.js.map +1 -0
  101. package/dist/cli/output.d.ts +33 -0
  102. package/dist/cli/output.d.ts.map +1 -0
  103. package/dist/cli/output.js +34 -0
  104. package/dist/cli/output.js.map +1 -0
  105. package/dist/cli/preflight.d.ts +38 -0
  106. package/dist/cli/preflight.d.ts.map +1 -0
  107. package/dist/cli/preflight.js +72 -0
  108. package/dist/cli/preflight.js.map +1 -0
  109. package/dist/cli/privy.d.ts +83 -0
  110. package/dist/cli/privy.d.ts.map +1 -0
  111. package/dist/cli/privy.js +230 -0
  112. package/dist/cli/privy.js.map +1 -0
  113. package/dist/cli/progress.d.ts +36 -0
  114. package/dist/cli/progress.d.ts.map +1 -0
  115. package/dist/cli/progress.js +40 -0
  116. package/dist/cli/progress.js.map +1 -0
  117. package/dist/cli/run.d.ts +25 -0
  118. package/dist/cli/run.d.ts.map +1 -0
  119. package/dist/cli/run.js +173 -0
  120. package/dist/cli/run.js.map +1 -0
  121. package/dist/cli/scan-range.d.ts +55 -0
  122. package/dist/cli/scan-range.d.ts.map +1 -0
  123. package/dist/cli/scan-range.js +57 -0
  124. package/dist/cli/scan-range.js.map +1 -0
  125. package/dist/cli/spend-policy.d.ts +60 -0
  126. package/dist/cli/spend-policy.d.ts.map +1 -0
  127. package/dist/cli/spend-policy.js +65 -0
  128. package/dist/cli/spend-policy.js.map +1 -0
  129. package/dist/cli/status.d.ts +90 -0
  130. package/dist/cli/status.d.ts.map +1 -0
  131. package/dist/cli/status.js +252 -0
  132. package/dist/cli/status.js.map +1 -0
  133. package/dist/cli/timeout.d.ts +30 -0
  134. package/dist/cli/timeout.d.ts.map +1 -0
  135. package/dist/cli/timeout.js +55 -0
  136. package/dist/cli/timeout.js.map +1 -0
  137. package/dist/cli/version.d.ts +12 -0
  138. package/dist/cli/version.d.ts.map +1 -0
  139. package/dist/cli/version.js +22 -0
  140. package/dist/cli/version.js.map +1 -0
  141. package/dist/cli/wallet.d.ts +164 -0
  142. package/dist/cli/wallet.d.ts.map +1 -0
  143. package/dist/cli/wallet.js +234 -0
  144. package/dist/cli/wallet.js.map +1 -0
  145. package/dist/frameworks/crewai.d.ts +10 -0
  146. package/dist/frameworks/crewai.d.ts.map +1 -1
  147. package/dist/frameworks/crewai.js +12 -2
  148. package/dist/frameworks/crewai.js.map +1 -1
  149. package/dist/gateway/caller-auth-gateway.d.ts +38 -3
  150. package/dist/gateway/caller-auth-gateway.d.ts.map +1 -1
  151. package/dist/gateway/caller-auth-gateway.js +25 -1
  152. package/dist/gateway/caller-auth-gateway.js.map +1 -1
  153. package/dist/gateway/http-server.d.ts +53 -0
  154. package/dist/gateway/http-server.d.ts.map +1 -1
  155. package/dist/gateway/http-server.js +189 -4
  156. package/dist/gateway/http-server.js.map +1 -1
  157. package/dist/gateway/server-entry.d.ts +1 -0
  158. package/dist/gateway/server-entry.js +1 -0
  159. package/dist/gateway/server-entry.js.map +1 -1
  160. package/dist/gateway/x402-mode.d.ts +123 -0
  161. package/dist/gateway/x402-mode.d.ts.map +1 -0
  162. package/dist/gateway/x402-mode.js +212 -0
  163. package/dist/gateway/x402-mode.js.map +1 -0
  164. package/dist/index.d.ts +6 -4
  165. package/dist/index.d.ts.map +1 -1
  166. package/dist/index.js +8 -3
  167. package/dist/index.js.map +1 -1
  168. package/dist/mandates/env.d.ts +3 -2
  169. package/dist/mandates/env.d.ts.map +1 -1
  170. package/dist/mandates/env.js +3 -2
  171. package/dist/mandates/env.js.map +1 -1
  172. package/dist/mcp/http-server.d.ts +1 -0
  173. package/dist/mcp/http-server.js +1 -0
  174. package/dist/mcp/http-server.js.map +1 -1
  175. package/dist/mcp/server.d.ts +49 -0
  176. package/dist/mcp/server.d.ts.map +1 -1
  177. package/dist/mcp/server.js +533 -25
  178. package/dist/mcp/server.js.map +1 -1
  179. package/dist/mcp/service-invoker.d.ts +97 -0
  180. package/dist/mcp/service-invoker.d.ts.map +1 -0
  181. package/dist/mcp/service-invoker.js +167 -0
  182. package/dist/mcp/service-invoker.js.map +1 -0
  183. package/dist/mcp/stdio.d.ts +1 -0
  184. package/dist/mcp/stdio.js +1 -0
  185. package/dist/mcp/stdio.js.map +1 -1
  186. package/dist/sdk/client.d.ts +25 -1
  187. package/dist/sdk/client.d.ts.map +1 -1
  188. package/dist/sdk/client.js +20 -1
  189. package/dist/sdk/client.js.map +1 -1
  190. package/dist/sdk/invoke.d.ts +19 -4
  191. package/dist/sdk/invoke.d.ts.map +1 -1
  192. package/dist/sdk/invoke.js +69 -5
  193. package/dist/sdk/invoke.js.map +1 -1
  194. package/dist/x402/delivery-oracle.d.ts +108 -0
  195. package/dist/x402/delivery-oracle.d.ts.map +1 -0
  196. package/dist/x402/delivery-oracle.js +174 -0
  197. package/dist/x402/delivery-oracle.js.map +1 -0
  198. package/dist/x402/facilitator-server-entry.d.ts +3 -0
  199. package/dist/x402/facilitator-server-entry.d.ts.map +1 -0
  200. package/dist/x402/facilitator-server-entry.js +27 -0
  201. package/dist/x402/facilitator-server-entry.js.map +1 -0
  202. package/dist/x402/facilitator-server.d.ts +73 -0
  203. package/dist/x402/facilitator-server.d.ts.map +1 -0
  204. package/dist/x402/facilitator-server.js +305 -0
  205. package/dist/x402/facilitator-server.js.map +1 -0
  206. package/dist/x402/facilitator.d.ts +69 -17
  207. package/dist/x402/facilitator.d.ts.map +1 -1
  208. package/dist/x402/facilitator.js +76 -33
  209. package/dist/x402/facilitator.js.map +1 -1
  210. package/dist/x402/payment-requirements.d.ts +106 -0
  211. package/dist/x402/payment-requirements.d.ts.map +1 -0
  212. package/dist/x402/payment-requirements.js +138 -0
  213. package/dist/x402/payment-requirements.js.map +1 -0
  214. package/dist/x402/relayer.d.ts +41 -0
  215. package/dist/x402/relayer.d.ts.map +1 -0
  216. package/dist/x402/relayer.js +64 -0
  217. package/dist/x402/relayer.js.map +1 -0
  218. package/dist/x402/resource-index.d.ts +75 -0
  219. package/dist/x402/resource-index.d.ts.map +1 -0
  220. package/dist/x402/resource-index.js +126 -0
  221. package/dist/x402/resource-index.js.map +1 -0
  222. package/dist/x402/verified-delivery-facilitator.d.ts +285 -0
  223. package/dist/x402/verified-delivery-facilitator.d.ts.map +1 -0
  224. package/dist/x402/verified-delivery-facilitator.js +423 -0
  225. package/dist/x402/verified-delivery-facilitator.js.map +1 -0
  226. package/dist/zktls/t2-delivery-proof.d.ts +6 -6
  227. package/docs/agent-layer.md +14 -9
  228. package/docs/cli.md +705 -0
  229. package/docs/crewai.md +6 -2
  230. package/docs/eliza.md +6 -1
  231. package/docs/mcp-hosted.md +7 -5
  232. package/docs/mcp-reference.md +175 -0
  233. package/docs/quickstart.md +36 -9
  234. package/docs/threat-model.md +4 -3
  235. package/docs/x402.md +648 -0
  236. package/package.json +33 -10
  237. package/src/capability/invocation-capability.ts +17 -1
  238. package/src/cli/amounts.ts +102 -0
  239. package/src/cli/args.ts +150 -0
  240. package/src/cli/attesters.ts +86 -0
  241. package/src/cli/bin.ts +5 -0
  242. package/src/cli/commands/claim.ts +126 -0
  243. package/src/cli/commands/close.ts +69 -0
  244. package/src/cli/commands/discover.ts +228 -0
  245. package/src/cli/commands/invoke.ts +178 -0
  246. package/src/cli/commands/mcp.ts +135 -0
  247. package/src/cli/commands/open.ts +397 -0
  248. package/src/cli/commands/reclaim-unverified.ts +218 -0
  249. package/src/cli/commands/reclaim.ts +377 -0
  250. package/src/cli/commands/status.ts +56 -0
  251. package/src/cli/commands/streams.ts +344 -0
  252. package/src/cli/commands/watch.ts +70 -0
  253. package/src/cli/config.ts +208 -0
  254. package/src/cli/context.ts +30 -0
  255. package/src/cli/deps.ts +528 -0
  256. package/src/cli/descriptor.ts +133 -0
  257. package/src/cli/errors.ts +162 -0
  258. package/src/cli/escape-hatch.ts +92 -0
  259. package/src/cli/index.ts +27 -0
  260. package/src/cli/listings.ts +77 -0
  261. package/src/cli/mandate.ts +126 -0
  262. package/src/cli/output.ts +63 -0
  263. package/src/cli/preflight.ts +88 -0
  264. package/src/cli/privy.ts +396 -0
  265. package/src/cli/progress.ts +75 -0
  266. package/src/cli/run.ts +209 -0
  267. package/src/cli/scan-range.ts +76 -0
  268. package/src/cli/spend-policy.ts +139 -0
  269. package/src/cli/status.ts +381 -0
  270. package/src/cli/timeout.ts +64 -0
  271. package/src/cli/version.ts +21 -0
  272. package/src/cli/wallet.ts +372 -0
  273. package/src/frameworks/crewai.ts +13 -2
  274. package/src/gateway/caller-auth-gateway.ts +77 -6
  275. package/src/gateway/http-server.ts +280 -6
  276. package/src/gateway/server-entry.ts +1 -0
  277. package/src/gateway/x402-mode.ts +350 -0
  278. package/src/index.ts +40 -5
  279. package/src/mandates/env.ts +3 -2
  280. package/src/mcp/http-server.ts +1 -0
  281. package/src/mcp/server.ts +706 -30
  282. package/src/mcp/service-invoker.ts +285 -0
  283. package/src/mcp/stdio.ts +1 -0
  284. package/src/sdk/client.ts +33 -1
  285. package/src/sdk/invoke.ts +90 -12
  286. package/src/x402/delivery-oracle.ts +285 -0
  287. package/src/x402/facilitator-server-entry.ts +35 -0
  288. package/src/x402/facilitator-server.ts +445 -0
  289. package/src/x402/facilitator.ts +125 -46
  290. package/src/x402/payment-requirements.ts +231 -0
  291. package/src/x402/relayer.ts +122 -0
  292. package/src/x402/resource-index.ts +228 -0
  293. package/src/x402/verified-delivery-facilitator.ts +744 -0
@@ -0,0 +1,372 @@
1
+ import { privateKeyToAccount } from "viem/accounts";
2
+ import type { Account, LocalAccount, Transport } from "viem";
3
+
4
+ import type { AutonomousWalletCredentialStore } from "../wallet/autonomous-wallet.js";
5
+ import { resolveAgentWallet } from "../wallet/provider.js";
6
+ import { configError } from "./errors.js";
7
+ import {
8
+ PRIVY_ENV_VARS,
9
+ PRIVY_SESSION_ENV_VARS,
10
+ PRIVY_WALLET_KEY_ENV,
11
+ resolvePrivyCliWallet,
12
+ type PrivyCliWalletSource,
13
+ type PrivySpendPolicy,
14
+ } from "./privy.js";
15
+
16
+ /**
17
+ * The ONLY place the CLI resolves a signing wallet.
18
+ *
19
+ * There is deliberately NO `--private-key` flag, and no flag for any other
20
+ * credential. A secret passed on the command line lands in shell history, in
21
+ * `ps` output, and in any CI log that echoes the command; an environment
22
+ * variable does none of those. Secrets are also never echoed back: commands
23
+ * print the derived ADDRESS only.
24
+ *
25
+ * ## Resolution order (first match wins, checked in this order)
26
+ *
27
+ * 1. `METRIK_AGENT_PRIVATE_KEY` — a raw Base Sepolia test key.
28
+ * 2. The `CDP_*` / `METRIK_AGENT_CDP_*` quartet — a Coinbase CDP Server Wallet
29
+ * v2 account, resolved through this package's own `resolveAgentWallet()`.
30
+ * 3. The `METRIK_AGENT_PRIVY_*` variables — a Privy wallet reached through a
31
+ * broker, either agent-owned (`METRIK_AGENT_PRIVY_WALLET_KEY`) or a
32
+ * user-owned broker session. See `./privy.ts`.
33
+ *
34
+ * The first two match `parseAgentWalletEnv()` (the MCP server's resolution), so
35
+ * one environment configures those surfaces the same way. The order is FIXED
36
+ * rather than "whichever is configured", because an environment carrying two is
37
+ * ambiguous about which address funds the stream — and that ambiguity is a
38
+ * fund-moving one. When more than one is present the earlier entry wins and
39
+ * `resolveCliWallet` says so on stderr rather than silently picking.
40
+ *
41
+ * ## What is different about Privy
42
+ *
43
+ * A private key and a CDP account both sign LOCALLY: `privateKeyToAccount()`
44
+ * and `toAccount(cdpServerAccount)` are viem local accounts, so they drop
45
+ * straight into the CLI's `http(rpcUrl)` transport with no other change.
46
+ *
47
+ * Privy does not. Its account is a JSON-RPC account with no `signTransaction`:
48
+ * it signs and broadcasts by sending `eth_sendTransaction` /
49
+ * `eth_signTypedData_v4` to a provider, so the wallet client's TRANSPORT must
50
+ * be that provider rather than a plain RPC. That is what {@link CliWallet.transport}
51
+ * carries, and the only reason this file knows Privy exists. The signer check
52
+ * below is therefore about CAPABILITY, not about locality: it refuses an
53
+ * account that genuinely cannot sign, and refuses a non-local account only on
54
+ * the paths that broadcast through `--rpc-url`.
55
+ *
56
+ * See `docs/cli.md` § Wallet.
57
+ */
58
+ export const PRIVATE_KEY_ENV = "METRIK_AGENT_PRIVATE_KEY";
59
+
60
+ /** Every environment variable the CDP path reads. Named in errors, never printed. */
61
+ export const CDP_ENV_VARS = [
62
+ "CDP_API_KEY_ID",
63
+ "CDP_API_KEY_SECRET",
64
+ "CDP_WALLET_SECRET",
65
+ "METRIK_AGENT_CDP_OWNER_NAME",
66
+ ] as const;
67
+
68
+ export {
69
+ PRIVY_ENV_VARS,
70
+ PRIVY_SESSION_ENV_VARS,
71
+ PRIVY_WALLET_KEY_ENV,
72
+ } from "./privy.js";
73
+
74
+ /**
75
+ * What the key is for, in the words of the command that needs it. `invoke`
76
+ * signs an EIP-712 invocation capability and broadcasts NOTHING; telling its
77
+ * caller "this command signs a transaction" was simply false.
78
+ */
79
+ export type KeyPurpose = "transaction" | "capability";
80
+
81
+ const PURPOSE_TEXT: Record<KeyPurpose, string> = {
82
+ transaction: "signs a transaction",
83
+ capability:
84
+ "signs an off-chain EIP-712 invocation capability (no transaction is broadcast)",
85
+ };
86
+
87
+ /** Which environment configured the signer, for output and for error text. */
88
+ export type CliWalletSource = "private-key" | "cdp" | PrivyCliWalletSource;
89
+
90
+ /**
91
+ * An account every CLI signing path can actually use.
92
+ *
93
+ * `signTypedData` is required rather than optional because it is the one
94
+ * capability EVERY path needs: fund-moving commands self-sign an EIP-712
95
+ * `SpendMandate`, and `invoke` signs an EIP-712 `InvocationCapability`. An
96
+ * account without it type-checks as a viem `Account` and then fails at the
97
+ * first signature with a `TypeError`.
98
+ */
99
+ export type CliSignerAccount = Account & Pick<LocalAccount, "signTypedData">;
100
+
101
+ export interface CliWallet {
102
+ readonly source: CliWalletSource;
103
+ readonly account: CliSignerAccount;
104
+ /**
105
+ * The transport fund-moving commands must broadcast through, when the wallet
106
+ * REQUIRES one. `undefined` means "a plain `http(--rpc-url)` is correct" —
107
+ * true for a local signer, false for Privy, whose provider IS the transport.
108
+ */
109
+ readonly transport?: Transport;
110
+ /** One line naming the signer's origin. Safe to print: no secret in it. */
111
+ readonly description: string;
112
+ /**
113
+ * Set when this wallet's spend ceiling is enforced where the CLI cannot reach
114
+ * it — i.e. by a Privy broker policy, not by a locally self-signed mandate.
115
+ *
116
+ * Its presence is the signal that the CLI should NOT self-sign a
117
+ * `SpendMandate` for this wallet, and should read and enforce the real
118
+ * ceiling instead. See `./spend-policy.ts` for why that substitution is
119
+ * strictly stronger than the mandate it replaces, and why re-adding the
120
+ * mandate "for consistency" would be a regression.
121
+ */
122
+ readonly readSpendPolicy?: () => Promise<PrivySpendPolicy>;
123
+ }
124
+
125
+ const WALLET_HINT =
126
+ `export ${PRIVATE_KEY_ENV}=0x… (a funded Base Sepolia test key), configure a Coinbase CDP wallet with ` +
127
+ `${CDP_ENV_VARS.join(", ")}, or configure a Privy wallet with ${PRIVY_WALLET_KEY_ENV} (agent-owned) or ` +
128
+ `${PRIVY_SESSION_ENV_VARS.join(", ")} (broker session). All are read from the environment only — never pass a credential as a command-line flag.`;
129
+
130
+ export interface ResolveCliWalletOptions {
131
+ readonly purpose?: KeyPurpose;
132
+ /**
133
+ * `--rpc-url`. Only the Privy paths use it at resolution time: their provider
134
+ * serves the wallet client's READS as well as its writes, so without this a
135
+ * Privy command would read from a different endpoint than `--rpc-url` names.
136
+ */
137
+ readonly rpcUrl?: string;
138
+ readonly note?: (line: string) => void;
139
+ /**
140
+ * Test seam, same role as `ResolveAgentWalletOptions.createCdpClient`: the
141
+ * credential store an agent-owned Privy wallet is read from. Unset in every
142
+ * real run, where the store is selected from the environment.
143
+ */
144
+ readonly store?: AutonomousWalletCredentialStore;
145
+ /** Test seam: the `fetch` the Privy broker is reached through. */
146
+ readonly fetchImpl?: typeof fetch;
147
+ }
148
+
149
+ /**
150
+ * Resolves the signing wallet, or throws an actionable configuration error.
151
+ *
152
+ * `note` receives one line when the environment is ambiguous (more than one
153
+ * wallet source configured). It is optional so `resolveCliWallet` stays usable
154
+ * from a context with no printer; commands always pass `out.note`.
155
+ */
156
+ export async function resolveCliWallet(
157
+ env: NodeJS.ProcessEnv,
158
+ options: ResolveCliWalletOptions = {},
159
+ ): Promise<CliWallet> {
160
+ const wallet = await optionalCliWallet(env, options);
161
+ if (wallet === null) {
162
+ throw configError(
163
+ `this command ${PURPOSE_TEXT[options.purpose ?? "transaction"]} and needs a wallet: none of ${PRIVATE_KEY_ENV}, the CDP variables or the Privy variables are set`,
164
+ WALLET_HINT,
165
+ );
166
+ }
167
+ return wallet;
168
+ }
169
+
170
+ /** True for both Privy shapes, which reach their key through a broker. */
171
+ export function isPrivyWalletSource(
172
+ source: CliWalletSource,
173
+ ): source is PrivyCliWalletSource {
174
+ return source === "privy-autonomous" || source === "privy-session";
175
+ }
176
+
177
+ /**
178
+ * Whether this wallet's ceiling is enforced somewhere the CLI cannot reach.
179
+ *
180
+ * The one question every fund-moving command asks before deciding whether to
181
+ * self-sign a `SpendMandate`. See `./spend-policy.ts`.
182
+ */
183
+ export function isPolicyEnforced(wallet: CliWallet): wallet is CliWallet & {
184
+ readonly readSpendPolicy: () => Promise<PrivySpendPolicy>;
185
+ } {
186
+ return wallet.readSpendPolicy !== undefined;
187
+ }
188
+
189
+ /**
190
+ * The same resolution, but "nothing configured" is not an error. Used by
191
+ * look-before-you-leap paths (`open --dry-run`, `streams --address`) that
192
+ * broadcast nothing and must therefore work for someone who does not yet have a
193
+ * funded wallet — which is exactly who a dry run is for.
194
+ *
195
+ * A wallet that is PARTIALLY configured is still an error here: "you set three
196
+ * of the four CDP variables" and "you configured no wallet" are different
197
+ * facts, and rendering the first as the second sends the reader looking in the
198
+ * wrong place. The same rule applies to a partial Privy setup.
199
+ */
200
+ export async function optionalCliWallet(
201
+ env: NodeJS.ProcessEnv,
202
+ options: Omit<ResolveCliWalletOptions, "purpose"> = {},
203
+ ): Promise<CliWallet | null> {
204
+ const privateKeyAccount = optionalCliAccount(env);
205
+ const cdpConfigured = CDP_ENV_VARS.some((name) => isSet(env[name]));
206
+ const privyConfigured = PRIVY_ENV_VARS.some((name) => isSet(env[name]));
207
+
208
+ if (privateKeyAccount !== null) {
209
+ const shadowed = [
210
+ ...(cdpConfigured ? ["CDP"] : []),
211
+ ...(privyConfigured ? ["Privy"] : []),
212
+ ];
213
+ if (shadowed.length > 0) {
214
+ options.note?.(
215
+ `warning: both ${PRIVATE_KEY_ENV} and ${shadowed.join(" and ")} wallet variables are set; using ${PRIVATE_KEY_ENV} (${privateKeyAccount.address}). Unset it to sign with the ${shadowed[0]} wallet.`,
216
+ );
217
+ }
218
+ return {
219
+ source: "private-key",
220
+ account: privateKeyAccount,
221
+ description: `${privateKeyAccount.address} (from ${PRIVATE_KEY_ENV})`,
222
+ };
223
+ }
224
+
225
+ if (cdpConfigured) {
226
+ if (privyConfigured) {
227
+ options.note?.(
228
+ "warning: both CDP and Privy wallet variables are set; using the CDP wallet. Unset the CDP variables to sign with the Privy wallet.",
229
+ );
230
+ }
231
+ return await resolveCdpWallet(env);
232
+ }
233
+
234
+ if (!privyConfigured) return null;
235
+
236
+ const privy = await resolvePrivyCliWallet(env, options);
237
+ // Unreachable: `privyConfigured` is exactly the condition under which
238
+ // `resolvePrivyCliWallet` returns non-null or throws. Kept so a future change
239
+ // to either predicate fails loudly here rather than as a null deref.
240
+ if (privy === null) return null;
241
+ return {
242
+ source: privy.source,
243
+ account: assertCliSigner(privy.account, privy.source),
244
+ transport: privy.transport,
245
+ description: privy.description,
246
+ readSpendPolicy: privy.readSpendPolicy,
247
+ };
248
+ }
249
+
250
+ async function resolveCdpWallet(env: NodeJS.ProcessEnv): Promise<CliWallet> {
251
+ const missing = CDP_ENV_VARS.filter((name) => !isSet(env[name]));
252
+ if (missing.length > 0) {
253
+ throw configError(
254
+ `the CDP wallet is partially configured: ${missing.join(", ")} ${missing.length === 1 ? "is" : "are"} not set`,
255
+ `a CDP wallet needs all of ${CDP_ENV_VARS.join(", ")}. Set the missing one${missing.length === 1 ? "" : "s"}, or unset the others and use ${PRIVATE_KEY_ENV} instead.`,
256
+ );
257
+ }
258
+
259
+ // A CDP SMART account is an ERC-4337 contract account at a DIFFERENT address
260
+ // from its owner EOA, and it is reached through a bundler — which this CLI
261
+ // does not route to: every command broadcasts through `http(--rpc-url)`. So a
262
+ // smart-account environment would silently sign and spend from the OWNER,
263
+ // while the user funded the smart account. Refuse instead.
264
+ if (
265
+ isSet(env.METRIK_AGENT_CDP_SMART_ACCOUNT_NAME) ||
266
+ isTruthy(env.METRIK_AGENT_CDP_CREATE_SMART_ACCOUNT)
267
+ ) {
268
+ throw configError(
269
+ "the CLI cannot sign from a CDP smart account: METRIK_AGENT_CDP_CREATE_SMART_ACCOUNT / METRIK_AGENT_CDP_SMART_ACCOUNT_NAME is set",
270
+ "a CDP smart account is an ERC-4337 account at a different address from its owner, reached through a bundler; this CLI broadcasts through --rpc-url and would spend from the OWNER address instead. Unset those two variables to use the owner account directly, or use the SDK's account-abstraction path.",
271
+ );
272
+ }
273
+
274
+ const ownerName = env.METRIK_AGENT_CDP_OWNER_NAME as string;
275
+ let account: Account;
276
+ try {
277
+ const resolved = await resolveAgentWallet({
278
+ cdp: {
279
+ apiKeyId: env.CDP_API_KEY_ID as string,
280
+ apiKeySecret: env.CDP_API_KEY_SECRET as string,
281
+ walletSecret: env.CDP_WALLET_SECRET as string,
282
+ ownerName,
283
+ },
284
+ });
285
+ account = resolved.account;
286
+ } catch (error) {
287
+ // Reaching CDP is a network call against an authenticated API, so this is
288
+ // where a wrong key, a revoked key or an outage surfaces. Say which wallet
289
+ // failed; the underlying message carries the rest.
290
+ throw configError(
291
+ `could not resolve the CDP wallet "${ownerName}"`,
292
+ `check ${CDP_ENV_VARS.join(", ")}, and that '@coinbase/cdp-sdk' (an optional peer dependency) is installed. Underlying error: ${error instanceof Error ? error.message : String(error)}`,
293
+ );
294
+ }
295
+
296
+ return {
297
+ source: "cdp",
298
+ account: assertCliSigner(account, "cdp"),
299
+ description: `${account.address} (CDP account "${ownerName}")`,
300
+ };
301
+ }
302
+
303
+ /**
304
+ * The raw `METRIK_AGENT_PRIVATE_KEY` read, kept separate so the wallet-source
305
+ * precedence above reads as one list rather than as nested conditionals.
306
+ */
307
+ export function optionalCliAccount(
308
+ env: NodeJS.ProcessEnv,
309
+ ): LocalAccount | null {
310
+ const raw = env[PRIVATE_KEY_ENV];
311
+ if (raw === undefined || raw.length === 0) return null;
312
+ if (!/^0x[0-9a-fA-F]{64}$/.test(raw)) {
313
+ throw configError(
314
+ `${PRIVATE_KEY_ENV} must be a 0x-prefixed 32-byte hex private key`,
315
+ );
316
+ }
317
+ return privateKeyToAccount(raw as `0x${string}`);
318
+ }
319
+
320
+ /**
321
+ * Whether this wallet source broadcasts through the CLI's own
322
+ * `http(--rpc-url)` transport, and therefore needs an account that can produce
323
+ * a signed raw transaction by itself.
324
+ */
325
+ function broadcastsLocally(source: CliWalletSource): boolean {
326
+ return source === "private-key" || source === "cdp";
327
+ }
328
+
329
+ /**
330
+ * Refuses an account that genuinely cannot sign — and nothing else.
331
+ *
332
+ * Two distinct capabilities are checked, because two distinct things fail:
333
+ *
334
+ * - `signTypedData` is needed by EVERY command. Fund-moving commands self-sign
335
+ * an EIP-712 `SpendMandate`; `invoke` signs an EIP-712
336
+ * `InvocationCapability`. Without it the run dies at the first signature
337
+ * with a `TypeError` from inside viem.
338
+ * - A LOCAL account is needed only where the CLI broadcasts through
339
+ * `http(--rpc-url)`, which cannot sign. A Privy account is deliberately NOT
340
+ * local: its provider is the transport and performs `eth_sendTransaction`
341
+ * itself, so demanding locality there would reject a wallet that works.
342
+ *
343
+ * The earlier version of this guard collapsed both into "must be local", which
344
+ * is why Privy was refused. Deleting the guard outright was not the fix: it
345
+ * exists so a broken wallet fails here, named, instead of deep inside viem.
346
+ */
347
+ export function assertCliSigner(
348
+ account: Account,
349
+ source: CliWalletSource,
350
+ ): CliSignerAccount {
351
+ if (typeof account.signTypedData !== "function") {
352
+ throw configError(
353
+ `the ${source} wallet (${account.address}) cannot sign EIP-712 typed data`,
354
+ "every command signs typed data: fund-moving commands self-sign a spend mandate, and `invoke` signs an invocation capability.",
355
+ );
356
+ }
357
+ if (broadcastsLocally(source) && account.type !== "local") {
358
+ throw configError(
359
+ `the ${source} wallet returned a ${account.type} account, which cannot sign locally`,
360
+ "this wallet source broadcasts through --rpc-url, which cannot sign, so it needs a local signer.",
361
+ );
362
+ }
363
+ return account as CliSignerAccount;
364
+ }
365
+
366
+ function isSet(value: string | undefined): boolean {
367
+ return value !== undefined && value.length > 0;
368
+ }
369
+
370
+ function isTruthy(value: string | undefined): boolean {
371
+ return value === "true" || value === "1";
372
+ }
@@ -23,13 +23,24 @@ export interface CrewAiVerifiedStreamMcpConfigOptions {
23
23
  readonly cacheToolsList?: boolean;
24
24
  }
25
25
 
26
+ /**
27
+ * The default launcher: the `metrik-mcp` bin this package ships.
28
+ *
29
+ * It used to be `pnpm mcp:stdio`, which only resolves INSIDE this repository —
30
+ * a CrewAI user who installed `@absol-labs/agent` from npm got a config that
31
+ * could never start. `metrik-mcp` is on `PATH` wherever the package is
32
+ * installed (npm/pnpm link it into `node_modules/.bin`), and `npx` resolves it
33
+ * with no install at all.
34
+ */
35
+ export const CREWAI_DEFAULT_MCP_COMMAND = "metrik-mcp";
36
+
26
37
  export function createCrewAiVerifiedStreamMcpConfig(
27
38
  options: CrewAiVerifiedStreamMcpConfigOptions = {},
28
39
  ): CrewAiVerifiedStreamMcpConfig {
29
40
  return {
30
41
  transport: "stdio",
31
- command: options.command ?? "pnpm",
32
- args: options.args ?? ["mcp:stdio"],
42
+ command: options.command ?? CREWAI_DEFAULT_MCP_COMMAND,
43
+ args: options.args ?? [],
33
44
  env: options.env ?? {},
34
45
  allowedToolNames:
35
46
  options.allowedToolNames ?? CREWAI_VERIFIED_STREAM_TOOL_NAMES,
@@ -1,6 +1,13 @@
1
1
  import { MetrikClient } from "@absol-labs/sdk";
2
2
  import { isAddress, isHex, type Address, type Hex } from "viem";
3
3
 
4
+ import {
5
+ evaluateX402Request,
6
+ X402_PAYMENT_HEADER,
7
+ type AcceptedX402Payment,
8
+ type GatewayX402Mode,
9
+ } from "./x402-mode.js";
10
+ import type { X402PaymentRequiredBody } from "../x402/payment-requirements.js";
4
11
  import {
5
12
  CAPABILITY_HEADER_NAME,
6
13
  decodeCapabilityHeader,
@@ -69,7 +76,21 @@ export type CallerAuthDenialReason =
69
76
  | "stream-not-active"
70
77
  | "stream-expired"
71
78
  | "stream-not-funded"
72
- | "stream-state-stale";
79
+ | "stream-state-stale"
80
+ /**
81
+ * No Metrik capability, and x402 mode is enabled: the caller may pay per call
82
+ * instead. Carries an x402 `402` body in
83
+ * {@link CallerAuthDecision.x402PaymentRequired}. Emitted ONLY when
84
+ * {@link CallerAuthGatewayConfig.x402} is configured — without it, a missing
85
+ * capability is still `missing-capability-header` / `403`, unchanged.
86
+ */
87
+ | "x402-payment-required"
88
+ /**
89
+ * The request carried BOTH a Metrik capability and an `X-PAYMENT` header.
90
+ * Refused rather than guessed: serving it would charge the caller through one
91
+ * path while they believe they paid through the other.
92
+ */
93
+ | "ambiguous-payment-mode";
73
94
 
74
95
  /**
75
96
  * Default freshness bound for on-chain stream state, in seconds. Base blocks
@@ -116,11 +137,22 @@ export interface CallerAuthDecision {
116
137
  readonly authorized: boolean;
117
138
  readonly reason?: CallerAuthDenialReason;
118
139
  /** Set only when `authorized === false`. */
119
- readonly httpStatus?: 402 | 403;
120
- /** Set only when `authorized === true`. */
140
+ readonly httpStatus?: 400 | 402 | 403;
141
+ /** Set only when `authorized === true` via the Metrik stream path. */
121
142
  readonly stream?: GatewayStreamView;
122
143
  /** The decoded (but not necessarily valid) capability, when one was present. */
123
144
  readonly capability?: SignedInvocationCapability;
145
+ /**
146
+ * The x402 `402` body to return verbatim. Set only with
147
+ * `reason === "x402-payment-required"`.
148
+ */
149
+ readonly x402PaymentRequired?: X402PaymentRequiredBody;
150
+ /**
151
+ * A verified x402 payment. Set only when `authorized === true` via the x402
152
+ * path. The caller MUST settle it (see `./x402-mode.ts`) — verification alone
153
+ * moves no money.
154
+ */
155
+ readonly x402Payment?: AcceptedX402Payment;
124
156
  }
125
157
 
126
158
  /** The minimal on-chain stream fields the gateway needs to authorize a call. */
@@ -316,6 +348,13 @@ export interface CallerAuthGatewayConfig {
316
348
  readonly nonceCache?: NonceReplayCache;
317
349
  /** Injectable clock (unix seconds), for tests. */
318
350
  readonly now?: () => number;
351
+ /**
352
+ * Optional standard-x402 mode. When set, a request with NO Metrik capability
353
+ * gets a real x402 `402` challenge instead of a `403`, so a caller holding
354
+ * only `x402-fetch` can pay per call. Leaving it unset preserves today's
355
+ * behaviour byte for byte.
356
+ */
357
+ readonly x402?: GatewayX402Mode;
319
358
  }
320
359
 
321
360
  export interface CallerAuthRequestInput {
@@ -341,6 +380,7 @@ export class CallerAuthGateway {
341
380
  private readonly domain: InvocationCapabilityDomainInput;
342
381
  private readonly serviceRefs: ReadonlySet<string>;
343
382
  private readonly now: () => number;
383
+ private readonly x402: GatewayX402Mode | undefined;
344
384
 
345
385
  constructor(config: CallerAuthGatewayConfig) {
346
386
  if (!isAddress(config.escrowAddress)) {
@@ -397,19 +437,50 @@ export class CallerAuthGateway {
397
437
  verifyingContract: config.escrowAddress,
398
438
  };
399
439
  this.now = config.now ?? (() => Math.floor(Date.now() / 1000));
440
+ this.x402 = config.x402;
400
441
  }
401
442
 
402
443
  async authorize(
403
444
  request: CallerAuthRequestInput,
404
445
  ): Promise<CallerAuthDecision> {
405
446
  const headerValue = lookupHeader(request.headers, this.headerName);
406
- if (headerValue === undefined || headerValue.length === 0) {
447
+ const hasCapabilityHeader =
448
+ headerValue !== undefined && headerValue.length > 0;
449
+
450
+ if (this.x402 !== undefined) {
451
+ const paymentHeader = lookupHeader(request.headers, X402_PAYMENT_HEADER);
452
+ const hasPaymentHeader =
453
+ paymentHeader !== undefined && paymentHeader.trim().length > 0;
454
+
455
+ // Exactly one payment mode per request. See `./x402-mode.ts`.
456
+ if (hasCapabilityHeader && hasPaymentHeader) {
457
+ return deny("ambiguous-payment-mode", 400);
458
+ }
459
+ if (!hasCapabilityHeader) {
460
+ const evaluated = await evaluateX402Request(
461
+ { method: request.method, path: request.path },
462
+ paymentHeader,
463
+ this.x402,
464
+ );
465
+ if (evaluated.outcome === "accepted") {
466
+ return { authorized: true, x402Payment: evaluated.payment };
467
+ }
468
+ return {
469
+ authorized: false,
470
+ reason: "x402-payment-required",
471
+ httpStatus: 402,
472
+ x402PaymentRequired: evaluated.body,
473
+ };
474
+ }
475
+ }
476
+
477
+ if (!hasCapabilityHeader) {
407
478
  return deny("missing-capability-header", 403);
408
479
  }
409
480
 
410
481
  let capability: SignedInvocationCapability;
411
482
  try {
412
- capability = decodeCapabilityHeader(headerValue);
483
+ capability = decodeCapabilityHeader(headerValue as string);
413
484
  } catch (error) {
414
485
  if (error instanceof InvalidCapabilityHeaderError) {
415
486
  return deny("malformed-capability-header", 403);
@@ -569,7 +640,7 @@ function freshestView(views: readonly GatewayStreamView[]): GatewayStreamView {
569
640
 
570
641
  function deny(
571
642
  reason: CallerAuthDenialReason,
572
- httpStatus: 402 | 403,
643
+ httpStatus: 400 | 402 | 403,
573
644
  capability?: SignedInvocationCapability,
574
645
  ): CallerAuthDecision {
575
646
  return {