tereno-mcp 0.1.2 → 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.
package/README.md CHANGED
@@ -93,9 +93,21 @@ miss nothing to reuse — this call computes it, and the next asker pays les
93
93
  | 🤝 `demand_pledge` | free | Name a capability you would pay for. Credit only after the same wallet pays for it later |
94
94
 
95
95
  Calling a paid tool returns a `402` carrying the payment challenge in
96
- `error.data.paymentRequired`. Sign it and retry with a `PAYMENT-SIGNATURE`
97
- header. If your agent already has an x402 wallet, that retry is the normal flow
98
- and **this package never touches a private key**.
96
+ `error.data.paymentRequired`, the payable resource URL in `error.data.resource`,
97
+ and the terms (network, asset, amount, `payTo`) in `error.data.offer`.
98
+
99
+ **Over stdio you cannot pay by retrying the tool call.** stdio has no headers in
100
+ either direction, so there is nowhere for a `PAYMENT-SIGNATURE` to travel back
101
+ through. Two ways to settle, both of which leave **this package never touching a
102
+ private key**:
103
+
104
+ - **Pay the resource over HTTP.** Sign `error.data.paymentRequired` with your
105
+ x402 client and send the signed request to `error.data.resource`. It returns
106
+ the same payload the tool would have, receipt included, billed once.
107
+ - **Use the remote server instead of this package.** If your client supports
108
+ remote MCP, point it at `https://www.tereno.xyz/api/mcp`. That transport does
109
+ have headers, so signing the challenge and retrying the same tool call is the
110
+ normal flow and everything stays inside MCP.
99
111
 
100
112
  > 🔒 `transaction_intent_guard` is never cached, shared or resold. An unsigned
101
113
  > transaction is intent, and it stays with the wallet that paid for it.
@@ -107,6 +119,7 @@ and **this package never touches a private key**.
107
119
  | `TERENO_WALLET` | — | Base address announced before payment, so a challenge can be bound to the wallet that will settle it. Optional. |
108
120
  | `TERENO_BASE_URL` | `https://www.tereno.xyz` | Point at another Tereno deployment. |
109
121
  | `TERENO_TIMEOUT_MS` | `45000` | Upstream request timeout. |
122
+ | `TERENO_EVIDENCE_INSTALLATION_ID` | — | Optional stable random id for this MCP installation. It is hashed only when resolving evidence received from another agent; never use a wallet or secret. |
110
123
 
111
124
  ## 🔐 What this package does and does not do
112
125
 
@@ -115,9 +128,13 @@ It is a **stdio transport** in front of the hosted endpoint at
115
128
  the tool list, the prices and the payment challenge all come from the same place
116
129
  an HTTP agent sees — and this package needs no release when a capability changes.
117
130
 
118
- The one thing it translates is payment: over HTTP the challenge arrives in a
119
- response header, and stdio has no headers, so it is folded into the JSON-RPC
120
- error payload where a client can still reach it.
131
+ The one thing it translates is payment, and the translation is lossy in one
132
+ direction. Over HTTP the challenge arrives in a response header and the caller
133
+ retries with a `PAYMENT-SIGNATURE` header. stdio has neither, so the challenge is
134
+ folded into the JSON-RPC error payload where a client can still reach it, while
135
+ the signature has no way back in. That is why a paid call is settled against the
136
+ resource URL rather than by retrying the tool, and why a client that speaks
137
+ remote MCP should prefer the hosted endpoint.
121
138
 
122
139
  **No private key is ever read, stored or transmitted.** There are no
123
140
  dependencies, and the whole thing is one readable file — [read it](./bin/tereno-mcp.mjs)
@@ -7,18 +7,30 @@
7
7
  // the payment challenge all come from the same place an HTTP agent sees, and
8
8
  // this package never has to be redeployed when a capability changes.
9
9
  //
10
- // The one thing it does translate is payment. Over HTTP the 402 challenge
11
- // arrives in a response header; stdio has no headers, so the challenge is
12
- // folded into the JSON-RPC error payload where an MCP client can still reach it.
10
+ // The one thing it does translate is payment, and the translation is lossy in a
11
+ // way worth naming. Over HTTP the 402 challenge arrives in a response header and
12
+ // the caller retries with a PAYMENT-SIGNATURE header. stdio has no headers in
13
+ // either direction: the challenge can still be folded into the JSON-RPC error
14
+ // payload, but the signature has nowhere to come back through. So a paid tool
15
+ // cannot be settled by retrying it here, and saying otherwise sends an agent
16
+ // round a loop with no exit.
17
+ //
18
+ // What it does instead is hand the call off: the challenge names the capability's
19
+ // own invoke URL, which is payable by any x402 HTTP client and returns the same
20
+ // payload this tool would have. The 402 below says that explicitly and points at
21
+ // that URL rather than at this endpoint.
13
22
 
14
23
  import { createInterface } from "node:readline";
15
24
 
16
25
  // Travels in the user-agent, and the demand panel counts installs by it, so it
17
26
  // has to track the published version rather than drift from it.
18
- const VERSION = "0.1.2";
27
+ const VERSION = "0.1.4";
19
28
  const BASE_URL = (process.env.TERENO_BASE_URL || "https://www.tereno.xyz").replace(/\/+$/, "");
20
29
  const ENDPOINT = BASE_URL + "/api/mcp";
21
30
  const WALLET = process.env.TERENO_WALLET?.trim();
31
+ // Stable opaque id, supplied by the operator. The hosted server hashes it only
32
+ // when evidence received from another agent is resolved; it is never a wallet.
33
+ const EVIDENCE_INSTALLATION_ID = process.env.TERENO_EVIDENCE_INSTALLATION_ID?.trim();
22
34
  const TIMEOUT_MS = Number(process.env.TERENO_TIMEOUT_MS || 45_000);
23
35
  const PAYMENT_REQUIRED_HEADER = "payment-required";
24
36
 
@@ -30,6 +42,33 @@ function errorFor(id, code, message, data) {
30
42
  return { jsonrpc: "2.0", id: id ?? null, error: data === undefined ? { code, message } : { code, message, data } };
31
43
  }
32
44
 
45
+ // Reads the offer out of the base64 x402 challenge. This is not reimplementing
46
+ // payment: it is the only way to tell the agent *where* to pay, because the
47
+ // payable resource is the capability's invoke URL and never this JSON-RPC
48
+ // endpoint. Everything is optional on purpose, so a challenge whose shape we do
49
+ // not recognise degrades to "the challenge carries it" instead of throwing.
50
+ function readOffer(challenge) {
51
+ if (!challenge) return null;
52
+ try {
53
+ const decoded = JSON.parse(Buffer.from(challenge, "base64").toString("utf8"));
54
+ const accepts = Array.isArray(decoded?.accepts) ? decoded.accepts[0] : null;
55
+ const url = typeof decoded?.resource?.url === "string" ? decoded.resource.url : null;
56
+ if (!url && !accepts) return null;
57
+ return {
58
+ url,
59
+ network: accepts?.network ?? null,
60
+ asset: accepts?.asset ?? null,
61
+ // Smallest unit of `asset`, not USD. Naming it loosely is how an agent
62
+ // ends up signing six orders of magnitude more than it meant to.
63
+ amountInAssetUnits: accepts?.amount ?? null,
64
+ payTo: accepts?.payTo ?? null,
65
+ maxTimeoutSeconds: accepts?.maxTimeoutSeconds ?? null,
66
+ };
67
+ } catch {
68
+ return null;
69
+ }
70
+ }
71
+
33
72
  async function forward(request) {
34
73
  const controller = new AbortController();
35
74
  const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
@@ -42,6 +81,7 @@ async function forward(request) {
42
81
  // Identifies the caller before payment, so a challenge can be bound to the
43
82
  // wallet that will settle it. Optional: everything free works without it.
44
83
  if (WALLET) headers["x-agent-wallet"] = WALLET;
84
+ if (EVIDENCE_INSTALLATION_ID) headers["x-tereno-evidence-installation"] = EVIDENCE_INSTALLATION_ID;
45
85
 
46
86
  const response = await fetch(ENDPOINT, {
47
87
  method: "POST", headers, body: JSON.stringify(request), signal: controller.signal,
@@ -52,12 +92,28 @@ async function forward(request) {
52
92
 
53
93
  if (response.status === 402) {
54
94
  const challenge = response.headers.get(PAYMENT_REQUIRED_HEADER);
95
+ const offer = readOffer(challenge);
96
+ const where = offer?.url
97
+ ? `send the signed request to data.resource (${offer.url})`
98
+ : "send the signed request to the resource URL named inside that challenge";
55
99
  return errorFor(request.id, 402,
56
- "Payment required. This tool is paid per call with x402 in USDC on Base. "
57
- + "Sign the challenge in data.paymentRequired and retry with a PAYMENT-SIGNATURE header, "
58
- + "or call tereno_price first to see what it costs. The free tools (tereno_catalog, "
59
- + "tereno_price, tereno_receipt) need no payment and no wallet.",
60
- { paymentRequired: challenge, resource: ENDPOINT });
100
+ "Payment required, and this transport cannot carry the payment. stdio has no "
101
+ + "headers, so retrying this tool call with a PAYMENT-SIGNATURE is not possible "
102
+ + "and no argument on this tool accepts one. Settle it over HTTP instead: sign "
103
+ + `data.paymentRequired with an x402 client and ${where}. That returns the same `
104
+ + "payload this tool would have returned, including the receipt, and it is billed "
105
+ + "once. Free here, needing no wallet and no payment: tereno_catalog, tereno_price, "
106
+ + "tereno_receipt, tereno_evidence_reference and demand_pledge. Call tereno_price "
107
+ + "first if you only wanted to know the cost.",
108
+ {
109
+ paymentRequired: challenge,
110
+ // The payable resource, which is the capability's invoke URL. This
111
+ // used to carry the MCP endpoint, which is not payable and is not
112
+ // what an x402 client should be pointed at.
113
+ resource: offer?.url ?? null,
114
+ offer,
115
+ mcpEndpoint: ENDPOINT,
116
+ });
61
117
  }
62
118
 
63
119
  const text = await response.text();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tereno-mcp",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "mcpName": "xyz.tereno/tereno",
5
5
  "description": "MCP server for Tereno on Base: check a contract, a transaction or a web page before you act on it. Free catalog, pricing and receipt tools; paid answers settle per call with x402 in USDC.",
6
6
  "type": "module",