@priors/mcp 0.2.4 → 0.2.6
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 +5 -5
- package/package.json +2 -2
- package/src/server.mjs +37 -19
package/README.md
CHANGED
|
@@ -44,7 +44,7 @@ dedicated agent wallet holding only what the agent may spend.
|
|
|
44
44
|
| variable | default | |
|
|
45
45
|
|---|---|---|
|
|
46
46
|
| `PRIORS_KEY` | none | the agent wallet's private key; without it only the read-only tools work |
|
|
47
|
-
| `PRIORS_AGENT_ID` | looked up from the identity registry | the Priors agent id the wallet acts for (the lookup finds only identities the wallet
|
|
47
|
+
| `PRIORS_AGENT_ID` | looked up from the identity registry | the Priors agent id the wallet acts for (the lookup finds only identities minted to the wallet since the v2 deploy, never one transferred to it; set it for an older or a bought one, or when the wallet holds several) |
|
|
48
48
|
| `PRIORS_RPC` | `https://rpc.mainnet.chain.robinhood.com` | JSON-RPC endpoint (a private URL is redacted from every answer) |
|
|
49
49
|
| `PRIORS_FACILITATOR` | `https://facilitator.priors.trade` | where `find_services` lists merchants |
|
|
50
50
|
| `PRIORS_MAX_PRICE_USD` | `1.00` | ceiling on what `pay_url` may be told to pay per call |
|
|
@@ -54,7 +54,7 @@ dedicated agent wallet holding only what the agent may spend.
|
|
|
54
54
|
| `PRIORS_ALLOW_LOCAL` | off | `1` lets `pay_url` reach `http://localhost` and private addresses (local testing only) |
|
|
55
55
|
| `PRIORS_SCORE_V2` | `https://priors.trade/api/score-v2` | where `score_of` reads Priors Score v2; `off` shows the on-chain score only |
|
|
56
56
|
| `PRIORS_STOCK_VAULT` | the bundled deployments file's `stockVault` | the stock vault the stock tools and `credit_status` read |
|
|
57
|
-
| `PRIORS_STATE_DIR` | `~/.local/state/priors-mcp` | where the payments signed and not yet settled are kept (one owner-only file per wallet, never the key, with a short-lived `.lock` beside it while it is written), so a restart, a new session or another session of the same wallet resends them instead of signing again
|
|
57
|
+
| `PRIORS_STATE_DIR` | `~/.local/state/priors-mcp` | where the payments signed and not yet settled are kept (one owner-only file per wallet, never the key, with a short-lived `.lock` beside it while it is written), so a restart, a new session or another session of the same wallet resends them instead of signing again. An absolute path, or one starting with `~/` (a relative one is refused at start). A payment that cannot be written there is not sent. `off` keeps them in memory only |
|
|
58
58
|
|
|
59
59
|
Contract addresses (pool, lens, registry, USDG) come from `deployments/4663.v2.json`, bundled in the package, and the
|
|
60
60
|
accepted stock tokens from `deployments/stock-assets.4663.json`. The stock vault's address (live since 2026-09-28)
|
|
@@ -69,7 +69,7 @@ Settings → Developer → Edit Config (`claude_desktop_config.json`), then rest
|
|
|
69
69
|
"mcpServers": {
|
|
70
70
|
"priors": {
|
|
71
71
|
"command": "npx",
|
|
72
|
-
"args": ["-y", "@priors/mcp@0.2.
|
|
72
|
+
"args": ["-y", "@priors/mcp@0.2.6"],
|
|
73
73
|
"env": {
|
|
74
74
|
"PRIORS_KEY": "0xYOUR_AGENT_WALLET_KEY",
|
|
75
75
|
"PRIORS_AGENT_ID": "1234"
|
|
@@ -91,7 +91,7 @@ A project `.mcp.json` that reads the key from your shell's environment, so the f
|
|
|
91
91
|
"mcpServers": {
|
|
92
92
|
"priors": {
|
|
93
93
|
"command": "npx",
|
|
94
|
-
"args": ["-y", "@priors/mcp@0.2.
|
|
94
|
+
"args": ["-y", "@priors/mcp@0.2.6"],
|
|
95
95
|
"env": {
|
|
96
96
|
"PRIORS_KEY": "${PRIORS_KEY}",
|
|
97
97
|
"PRIORS_AGENT_ID": "${PRIORS_AGENT_ID:-}"
|
|
@@ -101,7 +101,7 @@ A project `.mcp.json` that reads the key from your shell's environment, so the f
|
|
|
101
101
|
}
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
or, for your user only: `claude mcp add priors --scope user -e PRIORS_KEY="$PRIORS_KEY" -- npx -y @priors/mcp@0.2.
|
|
104
|
+
or, for your user only: `claude mcp add priors --scope user -e PRIORS_KEY="$PRIORS_KEY" -- npx -y @priors/mcp@0.2.6`
|
|
105
105
|
(the key is expanded by your shell from the environment; do not paste it on the command line).
|
|
106
106
|
|
|
107
107
|
## Try it
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@priors/mcp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "MCP server for Priors on Robinhood Chain: pay x402 URLs in USDG, read balances, credit lines and scores, borrow and repay. Key from PRIORS_KEY only.",
|
|
6
6
|
"type": "module",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@modelcontextprotocol/sdk": "^1.30.1",
|
|
45
|
-
"@priors/x402": "0.2.
|
|
45
|
+
"@priors/x402": "0.2.6",
|
|
46
46
|
"ethers": "^6.13.4",
|
|
47
47
|
"undici": "6.29.0",
|
|
48
48
|
"zod": "^4.6.5"
|
package/src/server.mjs
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
// wallet (default ~/.local/state/priors-mcp; "off": memory only)
|
|
23
23
|
import { readFileSync, writeFileSync, mkdirSync, renameSync, openSync, closeSync, statSync, rmSync } from "node:fs";
|
|
24
24
|
import { homedir } from "node:os";
|
|
25
|
-
import { join } from "node:path";
|
|
25
|
+
import { isAbsolute, join } from "node:path";
|
|
26
26
|
import { randomBytes } from "node:crypto";
|
|
27
27
|
import { lookup as dnsLookup } from "node:dns/promises";
|
|
28
28
|
import { isIP } from "node:net";
|
|
@@ -143,7 +143,8 @@ const clean = (s, n) => short(String(s ?? "").replace(/[\u0000-\u0008\u000b\u000
|
|
|
143
143
|
function dollars(v, what) {
|
|
144
144
|
if (typeof v !== "number" || !Number.isFinite(v) || v < 0) throw new ToolError(`${what} must be a non-negative number of US dollars`);
|
|
145
145
|
const s = v.toFixed(6);
|
|
146
|
-
|
|
146
|
+
// never rounded up: 0.0999999995 is not a cap of 0.10 (own audit 2026-10-01)
|
|
147
|
+
if (Math.abs(Number(s) - v) > 1e-9 || Number(s) - v > 1e-12) throw new ToolError(`${what} has more than 6 decimals`);
|
|
147
148
|
return X.toAtomicUsdg(`$${s}`, what);
|
|
148
149
|
}
|
|
149
150
|
function envDollars(env, name, dflt) {
|
|
@@ -234,7 +235,8 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
234
235
|
const parts = [env.PRIORS_RPC];
|
|
235
236
|
try {
|
|
236
237
|
const u = new URL(env.PRIORS_RPC);
|
|
237
|
-
|
|
238
|
+
// the hostname alone too: with a port, u.host is "name:port", and a DNS error names the bare hostname
|
|
239
|
+
parts.push(u.href, u.host, u.hostname, u.hostname.replace(/^\[|\]$/g, ""), u.username, u.password);
|
|
238
240
|
for (const seg of u.pathname.split("/")) if (seg.length >= 8) parts.push(seg, decodeURIComponent(seg));
|
|
239
241
|
for (const v of u.searchParams.values()) if (v.length >= 8) parts.push(v);
|
|
240
242
|
} catch (_) { /* not a URL: the literal is still cut */ }
|
|
@@ -252,8 +254,12 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
252
254
|
// The payments this wallet signed and has not seen settle outlive the process (a restart, or the client closing its
|
|
253
255
|
// session, starts a new one): one file per wallet under PRIORS_STATE_DIR (default ~/.local/state/priors-mcp; "off"
|
|
254
256
|
// disables it), owner-only. It holds signed authorizations, each payable only to the merchant it was sent to, never the key.
|
|
257
|
+
// A leading ~ is the home directory (an MCP client's JSON config is not shell-expanded); a relative path would depend
|
|
258
|
+
// on the directory each client starts the server in, so two sessions of one wallet would keep two files (own audit).
|
|
255
259
|
const stateRaw = String(env.PRIORS_STATE_DIR ?? "").trim();
|
|
256
|
-
const
|
|
260
|
+
const stateHome = stateRaw === "~" || stateRaw.startsWith("~/") ? join(homedir(), stateRaw.slice(1)) : stateRaw;
|
|
261
|
+
if (stateHome && !/^(off|none|false|0)$/i.test(stateHome) && !isAbsolute(stateHome)) throw new Error(`PRIORS_STATE_DIR must be an absolute path (or start with ~/), not "${stateRaw}"`);
|
|
262
|
+
const stateDir = /^(off|none|false|0)$/i.test(stateRaw) ? null : stateHome || join(homedir(), ".local", "state", "priors-mcp");
|
|
257
263
|
const stateFile = wallet && stateDir ? join(stateDir, `outstanding-${wallet.address.toLowerCase()}.json`) : null;
|
|
258
264
|
// An entry is { paymentHeaders, validBefore, price (bigint), payTo }: only what this server wrote, never the merchant's
|
|
259
265
|
// own requirement object (its fields are the merchant's to choose). On disk the price is a decimal string; each entry
|
|
@@ -304,13 +310,23 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
304
310
|
}
|
|
305
311
|
/** Entries another server wrote since: merged in before any decision to sign. */
|
|
306
312
|
function refresh() { if (stateFile) for (const [k, e] of readState()) if (!outstanding.has(k)) outstanding.set(k, e); }
|
|
313
|
+
/** Kept on file before the payment leaves; a failure throws, and the payer then does not send it (own audit: a
|
|
314
|
+
* record silently lost made a restart or another session sign again). */
|
|
307
315
|
async function record(key, e) {
|
|
316
|
+
if (stateFile) await locked(() => { const m = readState(); m.set(key, e); writeState(m); });
|
|
308
317
|
outstanding.set(key, e);
|
|
309
|
-
if (stateFile) { try { await locked(() => { const m = readState(); m.set(key, e); writeState(m); }); } catch (_) { /* best effort: memory still holds it */ } }
|
|
310
318
|
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
319
|
+
/** After the payment is sent and cannot be unsent: the file is best effort, memory always holds it. */
|
|
320
|
+
async function recordSent(key, e) {
|
|
321
|
+
outstanding.set(key, e);
|
|
322
|
+
if (stateFile) { try { await locked(() => { const m = readState(); m.set(key, e); writeState(m); }); } catch (_) { /* best effort */ } }
|
|
323
|
+
}
|
|
324
|
+
/** A settled payment is forgotten only where the entry is that same authorization: another session of this wallet may
|
|
325
|
+
* have signed its own for the purchase meanwhile (the P-15 race), and it stays cashable until it expires (own audit). */
|
|
326
|
+
async function forget(key, paymentHeaders) {
|
|
327
|
+
const same = (e) => e && JSON.stringify(e.paymentHeaders) === JSON.stringify(paymentHeaders);
|
|
328
|
+
if (same(outstanding.get(key))) outstanding.delete(key);
|
|
329
|
+
if (stateFile) { try { await locked(() => { const m = readState(); if (same(m.get(key)) && m.delete(key)) writeState(m); }); } catch (_) { /* best effort */ } }
|
|
314
330
|
}
|
|
315
331
|
refresh();
|
|
316
332
|
const contracts = C.creditContracts({ runner: provider, addresses: { pool: addresses.pool, lens: addresses.lens, usdg: addresses.usdg, registry: addresses.registry, stockVault: addresses.stockVault || null } });
|
|
@@ -329,14 +345,15 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
329
345
|
agentsOf: (addr) => discoverAgents(addr),
|
|
330
346
|
};
|
|
331
347
|
|
|
332
|
-
// The registry is not enumerable: find identities minted
|
|
348
|
+
// The registry is not enumerable: find identities minted to `addr` since the v2 deploy, keep what it owns. Only a mint
|
|
349
|
+
// counts: anyone can transfer an identity with an open loan to this wallet, and `repay` would then pay it (own audit).
|
|
333
350
|
async function discoverAgents(addr) {
|
|
334
351
|
const T = ethers.id("Transfer(address,address,uint256)");
|
|
335
352
|
const latest = await provider.getBlockNumber();
|
|
336
353
|
const from = Number(addresses.deployBlock || Math.max(0, latest - 50_000));
|
|
337
354
|
const ids = new Set();
|
|
338
355
|
for (let b = from; b <= latest; b += 50_000) {
|
|
339
|
-
const logs = await provider.getLogs({ address: addresses.registry, topics: [T,
|
|
356
|
+
const logs = await provider.getLogs({ address: addresses.registry, topics: [T, ethers.zeroPadValue(ethers.ZeroAddress, 32), ethers.zeroPadValue(addr, 32)], fromBlock: b, toBlock: Math.min(latest, b + 49_999) });
|
|
340
357
|
for (const l of logs) ids.add(BigInt(l.topics[3]));
|
|
341
358
|
}
|
|
342
359
|
const mine = [];
|
|
@@ -414,15 +431,14 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
414
431
|
// The method, the URL without its fragment (url#a and url#b are the same purchase) and the body: another body at the
|
|
415
432
|
// same URL is another purchase, quoted and priced on its own (GHSA-xqp9).
|
|
416
433
|
const purchase = await X.purchaseKey(new Request(u.href, init));
|
|
417
|
-
const
|
|
418
|
-
target.hash = "";
|
|
419
|
-
const urlOnly = `${method} ${target.href}`; // the whole key without a body, and every key written before the body was in it
|
|
434
|
+
const urlOnly = await X.purchaseKey(new Request(u.href, { method })); // the key without a body, as keys were before the body was in them
|
|
420
435
|
const now = Math.floor(Date.now() / 1000);
|
|
421
436
|
// Kept for the payer's clock-skew margin past validBefore: the chain's clock may be behind this machine's.
|
|
422
437
|
for (const [k, v] of outstanding) if (!live(v, now)) outstanding.delete(k); // the file drops them on its next write
|
|
423
438
|
refresh(); // a payment another server of this wallet (another session) signed for this purchase is resent, not signed again
|
|
424
439
|
// SHORTCUT: two sessions of one wallet paying the same URL in the same instant (between this read and the other's
|
|
425
|
-
// onSigned, about one 402 round trip) can still both sign: onSigned records a payment, it
|
|
440
|
+
// onSigned, about one 402 round trip) can still both sign: onSigned records a payment, it does not look for another
|
|
441
|
+
// session's. Ceiling:
|
|
426
442
|
// one extra payment of at most max_price_usd to the merchant the user chose, counted in each session's cap (the
|
|
427
443
|
// P-15 class, Low). Upgrade trigger: a report of it, or agents running pay_url concurrently on one wallet: reserve
|
|
428
444
|
// the purchase in the file under the lock before the 402 request, or derive the EIP-3009 nonce per purchase.
|
|
@@ -449,27 +465,29 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
449
465
|
let agentId;
|
|
450
466
|
if (maxBorrow > 0n) { agentId = await resolveAgent(); await needController(agentId); }
|
|
451
467
|
const payer = X.createPayer({ signer, maxPrice, maxBorrow, fetchImpl: payFetch, pendingRetries: 2, maxSleepMs: 10_000, timeoutMs: requestTimeoutMs, signal: AbortSignal.timeout(Math.max(1, deadline - Date.now())), ...(sleep ? { sleep } : {}), ...(maxBorrow > 0n ? { pool: addresses.pool, agentId } : {}),
|
|
452
|
-
//
|
|
453
|
-
|
|
468
|
+
// Recorded, then counted, when signed, at the price the payer checked and signed (a v1 requirement's `amount` is the
|
|
469
|
+
// merchant's text). A record that cannot be kept throws, and the payer does not send the payment.
|
|
470
|
+
onSigned: async (s) => { await record(purchase, { paymentHeaders: s.paymentHeaders, validBefore: s.validBefore, price: s.price, payTo: s.requirement.payTo }); signedPrice = s.price; session.spent += s.price; } });
|
|
454
471
|
try { r = await payer.pay(url, init); } catch (e) {
|
|
455
472
|
// What the error carries was done: a loan taken and a payment signed are counted, whatever failed after.
|
|
456
473
|
if (e?.borrowed > 0n) session.borrowed += e.borrowed;
|
|
457
474
|
// A borrow sent whose answer was lost may have mined (GHSA-v9xj): counted above, and said, never "nothing done".
|
|
458
475
|
if (e?.unconfirmed) throw new ToolError(`A borrow of ${usd(e.borrowed)} was sent${e.hash ? ` (tx ${e.hash})` : ""} and its answer was lost (${explain(e.cause ?? e)}), so it may have opened a loan: check credit_status, and repay it with the repay tool before its due date. Nothing was signed or paid.`);
|
|
459
476
|
if (e?.signed) {
|
|
460
|
-
if (signedPrice === null) { signedPrice = maxPrice; session.spent += maxPrice; await
|
|
477
|
+
if (signedPrice === null) { signedPrice = maxPrice; session.spent += maxPrice; await recordSent(purchase, { ...e.signed, price: maxPrice, payTo: e.requirement.payTo }); } // not reached: onSigned ran
|
|
461
478
|
const price = signedPrice;
|
|
462
479
|
const until = when(e.signed.validBefore + X.SKEW_SECONDS);
|
|
463
480
|
throw new ToolError(`A payment of ${usd(price)} to ${e.requirement.payTo} was signed, then the request failed (${explain(e)}). The merchant may still settle it until ${until}, so do NOT call pay_url again for this purchase (the same method, URL and body) before ${until}; check wallet_balance.${e.borrowed > 0n ? ` Borrowed ${usd(e.borrowed)}${e.loanId !== null ? ` as loan #${e.loanId}` : ""}: repay it with the repay tool.` : ""}`);
|
|
464
481
|
}
|
|
465
482
|
const loan = e?.borrowed > 0n ? ` ${usd(e.borrowed)} was borrowed${e.loanId !== null ? ` as loan #${e.loanId}` : ""} before it failed: repay it with the repay tool.` : "";
|
|
483
|
+
if (e?.code === "NOT_RECORDED") throw new ToolError(`A payment was signed but could not be written to PRIORS_STATE_DIR (${explain(e.cause ?? e)}), so it was not sent and nothing can be settled. Fix that directory, or set PRIORS_STATE_DIR=off to keep payments in this process's memory only, then call again.${loan}`);
|
|
466
484
|
if (e?.name === "TimeoutError" || e?.name === "AbortError") throw new ToolError(`${method} ${u.href} did not answer in time. Nothing was signed or paid.${loan}`);
|
|
467
485
|
if (loan) throw new ToolError(`${explain(e)}.${loan}`);
|
|
468
486
|
throw e;
|
|
469
487
|
}
|
|
470
488
|
if (r.signed && signedPrice === null) { // not reached: onSigned counted and recorded it
|
|
471
489
|
signedPrice = maxPrice; session.spent += maxPrice;
|
|
472
|
-
if (r.paid === 0n) await
|
|
490
|
+
if (r.paid === 0n) await recordSent(purchase, { ...r.signed, price: maxPrice, payTo: r.requirement.payTo });
|
|
473
491
|
}
|
|
474
492
|
if (r.borrowed > 0n) { session.borrowed += r.borrowed; lines.push(`Borrowed ${usd(r.borrowed)} from the Priors line for agent #${agentId}${r.loanId !== null ? ` as loan #${r.loanId}` : ""}${r.dueAt ? `, due ${when(r.dueAt)}` : ""}. Repay it with the repay tool before then.`); }
|
|
475
493
|
else if (r.requirement) lines.push("Nothing was borrowed.");
|
|
@@ -477,7 +495,7 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
|
|
|
477
495
|
const status = r.timedOut ? "no answer in time" : r.transportError ? "the connection was lost" : `HTTP ${r.response.status}`;
|
|
478
496
|
if (!r.requirement) lines.unshift(`No payment was asked for: ${method} ${u.href} answered ${status}. Nothing was paid.`);
|
|
479
497
|
else if (r.paid > 0n) {
|
|
480
|
-
await forget(purchase);
|
|
498
|
+
await forget(purchase, r.signed?.paymentHeaders);
|
|
481
499
|
const tx = r.settlement?.transaction;
|
|
482
500
|
lines.unshift(`Paid ${usd(r.paid)}${r.x402Version ? ` (x402 v${r.x402Version})` : ""} to ${r.requirement.payTo} for ${method} ${u.href}: ${status}.${tx ? (TX_RE.test(String(tx)) ? ` Settlement tx ${tx}.` : " (The merchant's settlement id is not a transaction hash; not shown.)") : ""}`);
|
|
483
501
|
} else {
|