thebuyside-x402-agent 0.2.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 (90) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +148 -0
  4. package/dist/chains/adapter.d.ts +33 -0
  5. package/dist/chains/adapter.d.ts.map +1 -0
  6. package/dist/chains/adapter.js +15 -0
  7. package/dist/chains/adapter.js.map +1 -0
  8. package/dist/chains/base-usdc.d.ts +22 -0
  9. package/dist/chains/base-usdc.d.ts.map +1 -0
  10. package/dist/chains/base-usdc.js +60 -0
  11. package/dist/chains/base-usdc.js.map +1 -0
  12. package/dist/config.d.ts +17 -0
  13. package/dist/config.d.ts.map +1 -0
  14. package/dist/config.js +32 -0
  15. package/dist/config.js.map +1 -0
  16. package/dist/gateway.d.ts +26 -0
  17. package/dist/gateway.d.ts.map +1 -0
  18. package/dist/gateway.js +28 -0
  19. package/dist/gateway.js.map +1 -0
  20. package/dist/index.d.ts +7 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +12 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/log.d.ts +14 -0
  25. package/dist/log.d.ts.map +1 -0
  26. package/dist/log.js +24 -0
  27. package/dist/log.js.map +1 -0
  28. package/dist/policy/allowlist.d.ts +36 -0
  29. package/dist/policy/allowlist.d.ts.map +1 -0
  30. package/dist/policy/allowlist.js +61 -0
  31. package/dist/policy/allowlist.js.map +1 -0
  32. package/dist/policy/caps.d.ts +34 -0
  33. package/dist/policy/caps.d.ts.map +1 -0
  34. package/dist/policy/caps.js +67 -0
  35. package/dist/policy/caps.js.map +1 -0
  36. package/dist/policy/confirm.d.ts +57 -0
  37. package/dist/policy/confirm.d.ts.map +1 -0
  38. package/dist/policy/confirm.js +131 -0
  39. package/dist/policy/confirm.js.map +1 -0
  40. package/dist/policy/format.d.ts +15 -0
  41. package/dist/policy/format.d.ts.map +1 -0
  42. package/dist/policy/format.js +40 -0
  43. package/dist/policy/format.js.map +1 -0
  44. package/dist/policy/receipts.d.ts +33 -0
  45. package/dist/policy/receipts.d.ts.map +1 -0
  46. package/dist/policy/receipts.js +78 -0
  47. package/dist/policy/receipts.js.map +1 -0
  48. package/dist/registry/lookup.d.ts +28 -0
  49. package/dist/registry/lookup.d.ts.map +1 -0
  50. package/dist/registry/lookup.js +92 -0
  51. package/dist/registry/lookup.js.map +1 -0
  52. package/dist/registry/seed.candidates.json +54 -0
  53. package/dist/registry/seed.json +54 -0
  54. package/dist/registry/types.d.ts +45 -0
  55. package/dist/registry/types.d.ts.map +1 -0
  56. package/dist/registry/types.js +11 -0
  57. package/dist/registry/types.js.map +1 -0
  58. package/dist/server.d.ts +5 -0
  59. package/dist/server.d.ts.map +1 -0
  60. package/dist/server.js +43 -0
  61. package/dist/server.js.map +1 -0
  62. package/dist/signer/env-key.d.ts +18 -0
  63. package/dist/signer/env-key.d.ts.map +1 -0
  64. package/dist/signer/env-key.js +24 -0
  65. package/dist/signer/env-key.js.map +1 -0
  66. package/dist/signer/signer.d.ts +32 -0
  67. package/dist/signer/signer.d.ts.map +1 -0
  68. package/dist/signer/signer.js +14 -0
  69. package/dist/signer/signer.js.map +1 -0
  70. package/dist/tools/discover.d.ts +11 -0
  71. package/dist/tools/discover.d.ts.map +1 -0
  72. package/dist/tools/discover.js +58 -0
  73. package/dist/tools/discover.js.map +1 -0
  74. package/dist/tools/fetch.d.ts +17 -0
  75. package/dist/tools/fetch.d.ts.map +1 -0
  76. package/dist/tools/fetch.js +115 -0
  77. package/dist/tools/fetch.js.map +1 -0
  78. package/dist/tools/wallet_status.d.ts +10 -0
  79. package/dist/tools/wallet_status.d.ts.map +1 -0
  80. package/dist/tools/wallet_status.js +37 -0
  81. package/dist/tools/wallet_status.js.map +1 -0
  82. package/dist/x402/client.d.ts +85 -0
  83. package/dist/x402/client.d.ts.map +1 -0
  84. package/dist/x402/client.js +242 -0
  85. package/dist/x402/client.js.map +1 -0
  86. package/dist/x402/types.d.ts +60 -0
  87. package/dist/x402/types.d.ts.map +1 -0
  88. package/dist/x402/types.js +13 -0
  89. package/dist/x402/types.js.map +1 -0
  90. package/package.json +64 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may accept and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Joshua Harrington
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,7 @@
1
+ thebuyside-x402-agent
2
+ Copyright 2026 Joshua Harrington
3
+
4
+ This product is licensed under the Apache License, Version 2.0.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
package/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # thebuyside-x402-agent
2
+
3
+ The MCP gateway that lets any AI agent discover and pay x402-priced APIs — without the user wiring payments themselves.
4
+
5
+ `thebuyside-x402-agent` is the canonical buyer-side reference implementation for [x402](https://x402.org), the HTTP 402 payment standard stewarded by the Linux Foundation. Drop it into Claude Code, Claude Desktop, Cursor, or any MCP client, and your agent gains three tools:
6
+
7
+ - **`x402.discover`** — search a curated registry of x402-priced APIs
8
+ - **`x402.fetch`** — call one (the gateway pays the 402 challenge automatically)
9
+ - **`x402.wallet_status`** — show the gateway's wallet, today's spend, and caps
10
+
11
+ The agent never sees the 402, never sees a wallet, never holds a private key.
12
+
13
+ ## Status
14
+
15
+ **v0 shipped 2026-05-10.** First end-to-end paid call from Claude Code:
16
+
17
+ > `$0.005 USDC` settled on Base mainnet — tx [`0xd0917b35d8b778cf8d0249cc1b107a48ff7125b9fcaf7b4b257d823f73cc6aac`](https://basescan.org/tx/0xd0917b35d8b778cf8d0249cc1b107a48ff7125b9fcaf7b4b257d823f73cc6aac)
18
+
19
+ - 65 unit tests + MCP smoke test, all green
20
+ - x402 v1 + v2 dual wire support (handles both transports)
21
+ - Spend caps, host allowlist, receipts log, self-transfer guard, confirm-before-pay (MCP elicitation)
22
+ - Apache 2.0, DCO not CLA
23
+
24
+ ## Quickstart
25
+
26
+ Two steps. You need Node 20+ and a Base-mainnet wallet funded with at least `$0.01 USDC`.
27
+
28
+ ### 1. Set the wallet key in your environment
29
+
30
+ ```bash
31
+ export X402_PAYER_PRIVATE_KEY=0x...
32
+ ```
33
+
34
+ Use a fresh wallet, not your main one. (You can also pass this via your MCP client's `env` block — see below.)
35
+
36
+ ### 2. Register the gateway with your MCP client
37
+
38
+ **Claude Code (CLI):**
39
+
40
+ ```bash
41
+ claude mcp add x402-pay -- npx -y thebuyside-x402-agent
42
+ ```
43
+
44
+ Open a session, type `/mcp` to verify, then ask: *"Use x402.wallet_status to show my wallet."*
45
+
46
+ **Claude Desktop:**
47
+
48
+ See [docs/install-claude-desktop.md](docs/install-claude-desktop.md).
49
+
50
+ > Local stdio MCP servers can only be reached by **local** MCP clients (Claude Code CLI, Claude Desktop). The claude.ai web app's "Code" mode runs in Anthropic's cloud and can't reach a server on your laptop.
51
+
52
+ ## Try it
53
+
54
+ Once connected, ask the model:
55
+
56
+ > *"Use x402.discover to find APIs about news."*
57
+
58
+ > *"Use x402.fetch to get https://news-ep.com/api/v1/stories?market=houston&limit=5"*
59
+
60
+ The first returns the registry. The second pays `$0.005 USDC` and returns Houston news.
61
+
62
+ After a successful call, ask `x402.wallet_status` and you'll see today's spend reflected.
63
+
64
+ ## Configuration
65
+
66
+ Spend controls have safe defaults. Override via env if needed.
67
+
68
+ | Var | Default | What it does |
69
+ | --- | --- | --- |
70
+ | `X402_PAYER_PRIVATE_KEY` | *(required for payment)* | Base-mainnet wallet key (0x-prefixed) |
71
+ | `X402_DAILY_LIMIT` | `1.00` | Max USDC spent per rolling 24h window |
72
+ | `X402_PER_CALL_LIMIT` | `0.05` | Max USDC per single call |
73
+ | `X402_ALLOWLIST` | hosts in `seed.json` | Comma-separated extra allowed hostnames |
74
+ | `X402_ALLOW_UNVERIFIED` | *(off)* | `1` allows any host — dev only |
75
+ | `X402_REQUIRE_CONFIRM` | `always` | `always`, `never`, or a USDC threshold (e.g. `0.01`). Asks the user to approve via MCP elicitation before signing |
76
+ | `X402_CONFIRM_STRICT` | *(off)* | `1` refuses payment when the client doesn't support elicitation (default: log a warning and proceed) |
77
+ | `X402_RECEIPTS_PATH` | `.local/receipts.jsonl` | Where the receipts log is written |
78
+ | `X402_TEST_URL` | news-ep stories | Override target for `pnpm pay-newsep` |
79
+
80
+ Limit values accept either decimal USDC (`0.05`) or atomic units (`50000`). See [docs/configuring-spend-limits.md](docs/configuring-spend-limits.md) for the full guide.
81
+
82
+ ## How it works
83
+
84
+ ```
85
+ Claude Code / Claude Desktop / Cursor
86
+
87
+ │ MCP over stdio
88
+
89
+ ┌─────▼─────┐
90
+ │ Gateway │ ← src/server.ts (this repo)
91
+ └─────┬─────┘
92
+
93
+ │ HTTPS GET → 402 challenge → EIP-3009 sign
94
+ │ → retry with PAYMENT-SIGNATURE
95
+ │ → 200 + body
96
+
97
+ ┌─────▼──────┐ ┌──────────────────┐
98
+ │ x402 server│ ──────→ │ CDP facilitator │ ─→ Base USDC settle
99
+ │ (e.g. │ │ (verify + submit │
100
+ │ news-ep) │ ←────── │ tx) │
101
+ └────────────┘ └──────────────────┘
102
+ ```
103
+
104
+ The gateway holds the wallet, drives the 402 → sign → 200 loop, enforces spend caps, writes a receipts log. The agent stays at the MCP layer and never deals with payment plumbing.
105
+
106
+ ## Run from source
107
+
108
+ For contributors and anyone who wants to hack on the gateway:
109
+
110
+ ```bash
111
+ git clone https://github.com/jaysperspective/thebuyside-x402-agent.git
112
+ cd thebuyside-x402-agent
113
+ pnpm install
114
+ cp .env.example .env # then paste your key into X402_PAYER_PRIVATE_KEY
115
+ ```
116
+
117
+ Useful scripts:
118
+
119
+ - **`pnpm pay-newsep`** — standalone script that pays news-ep `$0.005` end-to-end without MCP. Useful for verifying your wallet + protocol setup.
120
+ - **`pnpm smoke`** — spawns the MCP server in a subprocess and round-trips a few tool calls. CI-safe; no real payments.
121
+ - **`pnpm verify-seed`** — hits each registry entry's example URL and asserts a valid 402 with the advertised price. Run nightly in CI.
122
+ - **`pnpm test`** — the full vitest unit-test suite.
123
+ - **`pnpm build`** — compiles to `dist/`. Used by `npm publish`.
124
+
125
+ To point your MCP client at the local source instead of the published npm package:
126
+
127
+ ```bash
128
+ claude mcp add x402-pay -- "$(pwd)/node_modules/.bin/tsx" "$(pwd)/src/index.ts"
129
+ ```
130
+
131
+ ## Adding an API to the registry
132
+
133
+ The discover tool reads `src/registry/seed.json`. Adding a new x402-priced endpoint is a single PR — see [docs/adding-an-api.md](docs/adding-an-api.md). CI verifies the entry returns a clean 402 with your advertised price before merge.
134
+
135
+ ## What this is *not*
136
+
137
+ - An agent framework. Bring your own.
138
+ - An LLM router. Bring your own.
139
+ - A marketplace. The registry is curated open-source data, not a vendor list.
140
+ - A custodial wallet service. Keys live in your `.env`. A managed-wallet (KMS) seam exists for a future hosted version, but the OSS gateway will always work BYO-key.
141
+
142
+ ## Contributing
143
+
144
+ Apache 2.0. Sign your commits with DCO (`git commit -s`). No CLA. PRs welcome — small, focused, with tests.
145
+
146
+ ## License
147
+
148
+ [Apache License 2.0](./LICENSE).
@@ -0,0 +1,33 @@
1
+ /**
2
+ * ChainAdapter — encapsulates per-chain knowledge: network identifier
3
+ * matching, EIP-712 domain, and how to build the typed-data + authorization
4
+ * payload for an x402 payment.
5
+ *
6
+ * v0 has one implementation: `BaseUsdcAdapter`. Adding Solana (Pay.sh) for
7
+ * v0.2 means writing one new class and registering it; no rewrite.
8
+ *
9
+ * Note: the return type is currently EVM-shaped (EIP-712 typed data). When
10
+ * Solana lands we'll either widen this interface or split it into
11
+ * `EvmChainAdapter` / `SolanaChainAdapter` siblings — both are clean changes
12
+ * from here.
13
+ */
14
+ import type { Address } from 'viem';
15
+ import type { Eip712TypedData } from '../signer/signer.js';
16
+ import type { Authorization, PaymentRequirements } from '../x402/types.js';
17
+ export interface ChainAdapter {
18
+ /** Short identifier used in logs and receipts (e.g. 'base', 'solana'). */
19
+ readonly id: string;
20
+ /** Returns true if this adapter handles the network ID in a 402 challenge. */
21
+ matches(network: string): boolean;
22
+ /**
23
+ * Build the data to sign and the authorization body for a given payment
24
+ * requirement. The gateway then signs `typedData` with its `Signer`,
25
+ * combines the signature with `authorization`, and base64-encodes the
26
+ * whole thing into the `X-PAYMENT` header.
27
+ */
28
+ buildPayment(reqs: PaymentRequirements, payerAddress: Address): {
29
+ typedData: Eip712TypedData;
30
+ authorization: Authorization;
31
+ };
32
+ }
33
+ //# sourceMappingURL=adapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/chains/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAE3E,MAAM,WAAW,YAAY;IAC3B,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB,8EAA8E;IAC9E,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;IAElC;;;;;OAKG;IACH,YAAY,CACV,IAAI,EAAE,mBAAmB,EACzB,YAAY,EAAE,OAAO,GACpB;QAAE,SAAS,EAAE,eAAe,CAAC;QAAC,aAAa,EAAE,aAAa,CAAA;KAAE,CAAC;CACjE"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * ChainAdapter — encapsulates per-chain knowledge: network identifier
3
+ * matching, EIP-712 domain, and how to build the typed-data + authorization
4
+ * payload for an x402 payment.
5
+ *
6
+ * v0 has one implementation: `BaseUsdcAdapter`. Adding Solana (Pay.sh) for
7
+ * v0.2 means writing one new class and registering it; no rewrite.
8
+ *
9
+ * Note: the return type is currently EVM-shaped (EIP-712 typed data). When
10
+ * Solana lands we'll either widen this interface or split it into
11
+ * `EvmChainAdapter` / `SolanaChainAdapter` siblings — both are clean changes
12
+ * from here.
13
+ */
14
+ export {};
15
+ //# sourceMappingURL=adapter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/chains/adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG"}
@@ -0,0 +1,22 @@
1
+ /**
2
+ * BaseUsdcAdapter — Base mainnet, USDC-as-asset, EIP-3009 transferWithAuthorization.
3
+ *
4
+ * Behaviour matches what news-ep.com expects: `network` of either "base" or
5
+ * "eip155:8453"; EIP-712 domain name "USD Coin" / version "2" (provided by
6
+ * the `extra` field of the 402 challenge — we don't hardcode these because
7
+ * different USDC deployments use different domain names, e.g. Base Sepolia
8
+ * uses "USDC").
9
+ */
10
+ import type { Address } from 'viem';
11
+ import type { Eip712TypedData } from '../signer/signer.js';
12
+ import type { Authorization, PaymentRequirements } from '../x402/types.js';
13
+ import type { ChainAdapter } from './adapter.js';
14
+ export declare class BaseUsdcAdapter implements ChainAdapter {
15
+ readonly id = "base";
16
+ matches(network: string): boolean;
17
+ buildPayment(reqs: PaymentRequirements, payerAddress: Address): {
18
+ typedData: Eip712TypedData;
19
+ authorization: Authorization;
20
+ };
21
+ }
22
+ //# sourceMappingURL=base-usdc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-usdc.d.ts","sourceRoot":"","sources":["../../src/chains/base-usdc.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAO,MAAM,MAAM,CAAC;AAEzC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,KAAK,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC3E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAajD,qBAAa,eAAgB,YAAW,YAAY;IAClD,QAAQ,CAAC,EAAE,UAAU;IAErB,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAIjC,YAAY,CACV,IAAI,EAAE,mBAAmB,EACzB,YAAY,EAAE,OAAO,GACpB;QAAE,SAAS,EAAE,eAAe,CAAC;QAAC,aAAa,EAAE,aAAa,CAAA;KAAE;CAqChE"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * BaseUsdcAdapter — Base mainnet, USDC-as-asset, EIP-3009 transferWithAuthorization.
3
+ *
4
+ * Behaviour matches what news-ep.com expects: `network` of either "base" or
5
+ * "eip155:8453"; EIP-712 domain name "USD Coin" / version "2" (provided by
6
+ * the `extra` field of the 402 challenge — we don't hardcode these because
7
+ * different USDC deployments use different domain names, e.g. Base Sepolia
8
+ * uses "USDC").
9
+ */
10
+ import { randomBytes } from 'node:crypto';
11
+ import { base } from 'viem/chains';
12
+ const TRANSFER_WITH_AUTHORIZATION_TYPES = {
13
+ TransferWithAuthorization: [
14
+ { name: 'from', type: 'address' },
15
+ { name: 'to', type: 'address' },
16
+ { name: 'value', type: 'uint256' },
17
+ { name: 'validAfter', type: 'uint256' },
18
+ { name: 'validBefore', type: 'uint256' },
19
+ { name: 'nonce', type: 'bytes32' },
20
+ ],
21
+ };
22
+ export class BaseUsdcAdapter {
23
+ id = 'base';
24
+ matches(network) {
25
+ return network === 'base' || network === 'eip155:8453';
26
+ }
27
+ buildPayment(reqs, payerAddress) {
28
+ const validAfter = 0n;
29
+ const validBefore = BigInt(Math.floor(Date.now() / 1000) + reqs.maxTimeoutSeconds);
30
+ const nonce = `0x${randomBytes(32).toString('hex')}`;
31
+ const typedData = {
32
+ domain: {
33
+ name: reqs.extra.name,
34
+ version: reqs.extra.version,
35
+ chainId: base.id,
36
+ verifyingContract: reqs.asset,
37
+ },
38
+ types: TRANSFER_WITH_AUTHORIZATION_TYPES,
39
+ primaryType: 'TransferWithAuthorization',
40
+ message: {
41
+ from: payerAddress,
42
+ to: reqs.payTo,
43
+ value: BigInt(reqs.maxAmountRequired),
44
+ validAfter,
45
+ validBefore,
46
+ nonce,
47
+ },
48
+ };
49
+ const authorization = {
50
+ from: payerAddress,
51
+ to: reqs.payTo,
52
+ value: reqs.maxAmountRequired,
53
+ validAfter: validAfter.toString(),
54
+ validBefore: validBefore.toString(),
55
+ nonce,
56
+ };
57
+ return { typedData, authorization };
58
+ }
59
+ }
60
+ //# sourceMappingURL=base-usdc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base-usdc.js","sourceRoot":"","sources":["../../src/chains/base-usdc.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAKnC,MAAM,iCAAiC,GAAG;IACxC,yBAAyB,EAAE;QACzB,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;QACjC,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE;QAC/B,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,SAAS,EAAE;QACvC,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE;QACxC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;KACnC;CACO,CAAC;AAEX,MAAM,OAAO,eAAe;IACjB,EAAE,GAAG,MAAM,CAAC;IAErB,OAAO,CAAC,OAAe;QACrB,OAAO,OAAO,KAAK,MAAM,IAAI,OAAO,KAAK,aAAa,CAAC;IACzD,CAAC;IAED,YAAY,CACV,IAAyB,EACzB,YAAqB;QAErB,MAAM,UAAU,GAAG,EAAE,CAAC;QACtB,MAAM,WAAW,GAAG,MAAM,CACxB,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC,iBAAiB,CACvD,CAAC;QACF,MAAM,KAAK,GAAG,KAAK,WAAW,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAS,CAAC;QAE5D,MAAM,SAAS,GAAoB;YACjC,MAAM,EAAE;gBACN,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI;gBACrB,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO;gBAC3B,OAAO,EAAE,IAAI,CAAC,EAAE;gBAChB,iBAAiB,EAAE,IAAI,CAAC,KAAK;aAC9B;YACD,KAAK,EAAE,iCAAiC;YACxC,WAAW,EAAE,2BAA2B;YACxC,OAAO,EAAE;gBACP,IAAI,EAAE,YAAY;gBAClB,EAAE,EAAE,IAAI,CAAC,KAAK;gBACd,KAAK,EAAE,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC;gBACrC,UAAU;gBACV,WAAW;gBACX,KAAK;aACN;SACF,CAAC;QAEF,MAAM,aAAa,GAAkB;YACnC,IAAI,EAAE,YAAY;YAClB,EAAE,EAAE,IAAI,CAAC,KAAK;YACd,KAAK,EAAE,IAAI,CAAC,iBAAiB;YAC7B,UAAU,EAAE,UAAU,CAAC,QAAQ,EAAE;YACjC,WAAW,EAAE,WAAW,CAAC,QAAQ,EAAE;YACnC,KAAK;SACN,CAAC;QAEF,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC;IACtC,CAAC;CACF"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Config loader. Reads `.env` (via dotenv) and validates env vars at startup.
3
+ *
4
+ * The wallet key is intentionally optional at M1 — the gateway boots and lists
5
+ * tools without it; payment-shaped tools just won't have a real address to use.
6
+ *
7
+ * `.env` is loaded by absolute path (relative to this file) so the gateway
8
+ * works correctly when spawned from any cwd — e.g. by Claude Desktop, where
9
+ * the working directory is not guaranteed to be the project root.
10
+ */
11
+ import type { Hex } from 'viem';
12
+ export type Config = {
13
+ /** Hex 0x-prefixed private key, or null if unset / placeholder. */
14
+ payerPrivateKey: Hex | null;
15
+ };
16
+ export declare function loadConfig(): Config;
17
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAKH,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAC;AAMhC,MAAM,MAAM,MAAM,GAAG;IACnB,mEAAmE;IACnE,eAAe,EAAE,GAAG,GAAG,IAAI,CAAC;CAC7B,CAAC;AAaF,wBAAgB,UAAU,IAAI,MAAM,CAInC"}
package/dist/config.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Config loader. Reads `.env` (via dotenv) and validates env vars at startup.
3
+ *
4
+ * The wallet key is intentionally optional at M1 — the gateway boots and lists
5
+ * tools without it; payment-shaped tools just won't have a real address to use.
6
+ *
7
+ * `.env` is loaded by absolute path (relative to this file) so the gateway
8
+ * works correctly when spawned from any cwd — e.g. by Claude Desktop, where
9
+ * the working directory is not guaranteed to be the project root.
10
+ */
11
+ import { dirname, resolve } from 'node:path';
12
+ import { fileURLToPath } from 'node:url';
13
+ import { config as loadDotenv } from 'dotenv';
14
+ const here = dirname(fileURLToPath(import.meta.url));
15
+ const projectRoot = resolve(here, '..');
16
+ loadDotenv({ path: resolve(projectRoot, '.env') });
17
+ function parsePrivateKey(raw) {
18
+ if (!raw)
19
+ return null;
20
+ if (raw === '0xPASTE_YOUR_PRIVATE_KEY_HERE')
21
+ return null;
22
+ if (!raw.startsWith('0x') || raw.length !== 66) {
23
+ throw new Error('X402_PAYER_PRIVATE_KEY is set but malformed. Expected 0x-prefixed 64 hex chars.');
24
+ }
25
+ return raw;
26
+ }
27
+ export function loadConfig() {
28
+ return {
29
+ payerPrivateKey: parsePrivateKey(process.env.X402_PAYER_PRIVATE_KEY),
30
+ };
31
+ }
32
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,QAAQ,CAAC;AAG9C,MAAM,IAAI,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AACrD,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AACxC,UAAU,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,WAAW,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;AAOnD,SAAS,eAAe,CAAC,GAAuB;IAC9C,IAAI,CAAC,GAAG;QAAE,OAAO,IAAI,CAAC;IACtB,IAAI,GAAG,KAAK,+BAA+B;QAAE,OAAO,IAAI,CAAC;IACzD,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,iFAAiF,CAClF,CAAC;IACJ,CAAC;IACD,OAAO,GAAU,CAAC;AACpB,CAAC;AAED,MAAM,UAAU,UAAU;IACxB,OAAO;QACL,eAAe,EAAE,eAAe,CAAC,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC;KACrE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Gateway — the bundle of components every MCP tool needs.
3
+ *
4
+ * Built once at server startup and passed by reference to each tool's
5
+ * register function. Keeps tool signatures stable as we add modules
6
+ * (analytics, hosted-wallet, multi-chain, etc.) over time.
7
+ */
8
+ import type { ChainAdapter } from './chains/adapter.js';
9
+ import type { Config } from './config.js';
10
+ import { Allowlist } from './policy/allowlist.js';
11
+ import { CapPolicy } from './policy/caps.js';
12
+ import { ConfirmPolicy } from './policy/confirm.js';
13
+ import { Receipts } from './policy/receipts.js';
14
+ import { Registry } from './registry/lookup.js';
15
+ import type { Signer } from './signer/signer.js';
16
+ export type Gateway = {
17
+ signer: Signer | null;
18
+ chains: ChainAdapter[];
19
+ allowlist: Allowlist;
20
+ receipts: Receipts;
21
+ caps: CapPolicy;
22
+ confirm: ConfirmPolicy;
23
+ registry: Registry;
24
+ };
25
+ export declare function buildGateway(config: Config): Promise<Gateway>;
26
+ //# sourceMappingURL=gateway.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway.d.ts","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG;IACpB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,EAAE,YAAY,EAAE,CAAC;IACvB,SAAS,EAAE,SAAS,CAAC;IACrB,QAAQ,EAAE,QAAQ,CAAC;IACnB,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,aAAa,CAAC;IACvB,QAAQ,EAAE,QAAQ,CAAC;CACpB,CAAC;AAEF,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAanE"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Gateway — the bundle of components every MCP tool needs.
3
+ *
4
+ * Built once at server startup and passed by reference to each tool's
5
+ * register function. Keeps tool signatures stable as we add modules
6
+ * (analytics, hosted-wallet, multi-chain, etc.) over time.
7
+ */
8
+ import { BaseUsdcAdapter } from './chains/base-usdc.js';
9
+ import { Allowlist } from './policy/allowlist.js';
10
+ import { CapPolicy } from './policy/caps.js';
11
+ import { ConfirmPolicy } from './policy/confirm.js';
12
+ import { Receipts } from './policy/receipts.js';
13
+ import { Registry } from './registry/lookup.js';
14
+ import { EnvKeySigner } from './signer/env-key.js';
15
+ export async function buildGateway(config) {
16
+ const receipts = Receipts.fromEnv();
17
+ const registry = await Registry.load();
18
+ return {
19
+ signer: config.payerPrivateKey ? new EnvKeySigner(config.payerPrivateKey) : null,
20
+ chains: [new BaseUsdcAdapter()],
21
+ allowlist: Allowlist.fromEnv({ defaultHosts: registry.hosts() }),
22
+ receipts,
23
+ caps: CapPolicy.fromEnv(receipts),
24
+ confirm: ConfirmPolicy.fromEnv(),
25
+ registry,
26
+ };
27
+ }
28
+ //# sourceMappingURL=gateway.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gateway.js","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAGxD,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAanD,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,MAAc;IAC/C,MAAM,QAAQ,GAAG,QAAQ,CAAC,OAAO,EAAE,CAAC;IACpC,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;IAEvC,OAAO;QACL,MAAM,EAAE,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,IAAI,YAAY,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI;QAChF,MAAM,EAAE,CAAC,IAAI,eAAe,EAAE,CAAC;QAC/B,SAAS,EAAE,SAAS,CAAC,OAAO,CAAC,EAAE,YAAY,EAAE,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC;QAChE,QAAQ;QACR,IAAI,EAAE,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC;QACjC,OAAO,EAAE,aAAa,CAAC,OAAO,EAAE;QAChC,QAAQ;KACT,CAAC;AACJ,CAAC"}
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * thebuyside-x402-agent — MCP gateway for x402-priced APIs.
4
+ * Entry point. See src/server.ts for the actual server.
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA;;;GAGG"}