@priors/mcp 0.18.1 → 0.18.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # @priors/mcp changelog
2
2
 
3
+ ## 0.18.2 (2026-11-06)
4
+
5
+ - **The state file's lock is never taken from a running server by a clock** (GHSA-wqww-vjpx-w8mq, reported by
6
+ @JasmeJun). A lock was judged abandoned by its file time against this process's clock, so after a clock step (NTP, a
7
+ resumed VM), or on a file server whose clock is behind, a lock held for milliseconds was taken, two servers of one
8
+ wallet wrote at once, and an unsettled payment's entry could be lost; another session, or a restart, then signed that
9
+ purchase again. The lock now names its holder and is removed only by it, and is taken over only when that holder, on
10
+ this host, has ended, or once this server has seen it unchanged for 10 s. After 2 s the write still goes ahead
11
+ without it, as before.
12
+ - **`pt_buy` and `pt_sell` hold the trade to the quote the user agreed to** (GHSA-3fwm-95gh-5xh4, reported by
13
+ @SanjilVinsmoke): `min_out_pt` and `min_out_usdg`, such as `pt_quote`'s "at least". A fresh quote under it is refused
14
+ before anything is signed; otherwise the router call names the larger of it and the fresh quote's own minimum, never a
15
+ lower one. Without it, the minimum came from the fresh quote alone, so a trade could fill below the quote the user
16
+ agreed to (never below the oracle's rate at its own block less the slippage).
17
+
3
18
  ## 0.18.1 (2026-11-05)
4
19
 
5
20
  On @priors/x402 0.8.8 (a plan whose AutoRepay epoch could not be read, below).
package/README.md CHANGED
@@ -44,8 +44,8 @@ repaying.
44
44
  | `find_services(query?)` | services registered with the Priors facilitator that accept USDG (`GET /merchants`): the ones Priors approved, and the ones under review whose own endpoint passed the facilitator's 402 check, marked so; hidden or unchecked ones are not listed | no |
45
45
  | `pt_quote(side, amount, slippage_bps?)` | quote buying PT-USDG with USDG, selling it before maturity or redeeming it after: what comes out at the oracle's spot rate, the minimum a trade would name, the days to maturity and the fixed APY to maturity | no |
46
46
  | `pt_position(address?)` | the wallet's (or any address's) PT-USDG: balance, what it is worth now, what it pays at maturity, and whether the Priors stock vault takes PT-USDG behind a line yet | no (with `address`) |
47
- | `pt_buy(amount_usdg, slippage_bps?, dry_run?)` | buy PT-USDG with the wallet's USDG through Pendle's router (at most `PRIORS_MAX_PT_USD` per call, `PRIORS_MAX_PT_TOTAL_USD` per run), keeping back what payments signed and not settled yet still need; `dry_run` lists the calls; needs ETH for gas | yes |
48
- | `pt_sell(amount_pt \| "all", slippage_bps?, dry_run?)` | sell the wallet's PT-USDG for USDG at the market, before maturity; needs ETH for gas | yes |
47
+ | `pt_buy(amount_usdg, slippage_bps?, min_out_pt?, dry_run?)` | buy PT-USDG with the wallet's USDG through Pendle's router (at most `PRIORS_MAX_PT_USD` per call, `PRIORS_MAX_PT_TOTAL_USD` per run), keeping back what payments signed and not settled yet still need; `min_out_pt` (`pt_quote`'s "at least", the quote the user agreed to): a fresh quote under it is refused before anything is signed, and the swap names the larger of it and the fresh quote's own minimum; `dry_run` lists the calls; needs ETH for gas | yes |
48
+ | `pt_sell(amount_pt \| "all", slippage_bps?, min_out_usdg?, dry_run?)` | sell the wallet's PT-USDG for USDG at the market, before maturity; `min_out_usdg` holds the sale to the quote the user agreed to, as `min_out_pt` does a buy; needs ETH for gas | yes |
49
49
  | `pt_redeem(amount_pt \| "all", slippage_bps?, dry_run?)` | redeem the wallet's PT-USDG for USDG 1:1, from maturity (2027-03-25) on; needs ETH for gas | yes |
50
50
  | `pt_buy_and_deposit(amount_usdg, agent_id?, include_held?, slippage_bps?, dry_run?)` | only with `PRIORS_PT_ONESTEP=on` (below): buy PT-USDG with the wallet's USDG and deposit it, with the PT-USDG the wallet already holds (unless `include_held` is false), behind the agent's line on the Priors stock vault, which opens the line, in one call: four transactions (the exact USDG approval, the buy with its minimum out, the exact PT-USDG approval to the vault, the vault's open with this wallet's pool consent). The wallet must own the agent, and the agent must have no open stock line; checked before the first is signed. `pt_buy`'s caps apply; `dry_run` lists the calls; needs ETH for gas | yes |
51
51
  | `fund_base(amount_usd, max_borrow_usd?)` | only with Base on (below): move USDG to USDC in the same address on Base through Across, keeping back what Autopay and signed payments need, borrowing the gap only with `max_borrow_usd`; needs ETH for gas | yes |
@@ -388,7 +388,7 @@ Settings → Developer → Edit Config (`claude_desktop_config.json`), then rest
388
388
  "mcpServers": {
389
389
  "priors": {
390
390
  "command": "npx",
391
- "args": ["-y", "@priors/mcp@0.18.1"],
391
+ "args": ["-y", "@priors/mcp@0.18.2"],
392
392
  "env": {
393
393
  "PRIORS_KEY": "0xYOUR_AGENT_WALLET_KEY",
394
394
  "PRIORS_AGENT_ID": "1234"
@@ -410,7 +410,7 @@ A project `.mcp.json` that reads the key from your shell's environment, so the f
410
410
  "mcpServers": {
411
411
  "priors": {
412
412
  "command": "npx",
413
- "args": ["-y", "@priors/mcp@0.18.1"],
413
+ "args": ["-y", "@priors/mcp@0.18.2"],
414
414
  "env": {
415
415
  "PRIORS_KEY": "${PRIORS_KEY}",
416
416
  "PRIORS_AGENT_ID": "${PRIORS_AGENT_ID:-}"
@@ -423,7 +423,7 @@ A project `.mcp.json` that reads the key from your shell's environment, so the f
423
423
  or, for your user only, from a macOS or Linux terminal:
424
424
 
425
425
  ```sh
426
- (read -rs PRIORS_KEY && umask 077 && mkdir -p ~/.config/priors && rm -f ~/.config/priors/agent.key && printf '%s\n' "$PRIORS_KEY" > ~/.config/priors/agent.key) && claude mcp add priors --scope user -- sh -c 'read -r PRIORS_KEY < "$1" && export PRIORS_KEY && exec npx -y @priors/mcp@0.18.1' priors-mcp ~/.config/priors/agent.key
426
+ (read -rs PRIORS_KEY && umask 077 && mkdir -p ~/.config/priors && rm -f ~/.config/priors/agent.key && printf '%s\n' "$PRIORS_KEY" > ~/.config/priors/agent.key) && claude mcp add priors --scope user -- sh -c 'read -r PRIORS_KEY < "$1" && export PRIORS_KEY && exec npx -y @priors/mcp@0.18.2' priors-mcp ~/.config/priors/agent.key
427
427
  ```
428
428
 
429
429
  It waits for the key without showing it (paste it, then Enter), saves it in a file only you can read, and gives Claude
@@ -504,7 +504,7 @@ to approve a borrow; this package, with the key, pays for APIs. With borrowing o
504
504
  approves each loan in Priors' Go mode. ElizaOS (`@elizaos/plugin-mcp`):
505
505
 
506
506
  ```ts
507
- "priors-wallet": { type: "stdio", command: "npx", args: ["-y", "@priors/mcp@0.18.1"],
507
+ "priors-wallet": { type: "stdio", command: "npx", args: ["-y", "@priors/mcp@0.18.2"],
508
508
  env: { PRIORS_KEY_FILE: "/home/agent/.config/priors/agent-7311.key", PRIORS_AGENT_ID: "7311",
509
509
  PRIORS_MAX_BORROW_USD: "0", PRIORS_MAX_BORROW_DAY_USD: "0", PRIORS_MAX_SPEND_DAY_USD: "1", PRIORS_MAX_PRICE_USD: "0.10",
510
510
  PRIORS_PAY_HOSTS: "api.example.com" } }
@@ -516,7 +516,7 @@ OpenClaw (`mcp.servers`, JSON5) takes the same `command`, `args` and `env`; Herm
516
516
  mcp_servers:
517
517
  priors-wallet:
518
518
  command: npx
519
- args: ["-y", "@priors/mcp@0.18.1"]
519
+ args: ["-y", "@priors/mcp@0.18.2"]
520
520
  env:
521
521
  PRIORS_KEY_FILE: /home/agent/.config/priors/agent-7311.key
522
522
  PRIORS_AGENT_ID: "7311"
@@ -543,7 +543,7 @@ key proves it is there: it signs the ERC-8004 identity registry's `AgentWalletSe
543
543
  agent's).
544
544
 
545
545
  ```sh
546
- PRIORS_KEY_FILE=~/.config/priors/agent-7311.key npx -y @priors/mcp@0.18.1 link-wallet --agent 7311
546
+ PRIORS_KEY_FILE=~/.config/priors/agent-7311.key npx -y @priors/mcp@0.18.2 link-wallet --agent 7311
547
547
  ```
548
548
 
549
549
  It reads the registry's EIP-712 domain and refuses to sign for any other, takes the owner from the chain (`--owner`
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@priors/mcp",
3
3
  "mcpName": "io.github.priors-agents/priors",
4
- "version": "0.18.1",
4
+ "version": "0.18.2",
5
5
  "private": false,
6
6
  "description": "MCP server for Priors on Robinhood Chain: pay x402 URLs in USDG, read balances, credit lines and scores, borrow and repay, and buy, sell and redeem PT-USDG. Key from PRIORS_KEY_FILE (a file only you can read) or PRIORS_KEY, never the command line.",
7
7
  "type": "module",
package/src/server.mjs CHANGED
@@ -121,8 +121,8 @@
121
121
  // or http for localhost only, no credentials, query or fragment
122
122
  // With both on, PRIORS_AGENT_ID and PRIORS_STATE_DIR are required: a buy leaves what that agent's open loans owe, and
123
123
  // the zap's 5 buys and 5 sales a UTC day are counted there for the agent and the wallet.
124
- import { readFileSync, writeFileSync, mkdirSync, renameSync, openSync, closeSync, statSync, rmSync, existsSync, realpathSync, lstatSync, readlinkSync } from "node:fs";
125
- import { homedir } from "node:os";
124
+ import { readFileSync, writeFileSync, mkdirSync, renameSync, statSync, rmSync, existsSync, realpathSync, lstatSync, readlinkSync, linkSync } from "node:fs";
125
+ import { homedir, hostname } from "node:os";
126
126
  import { isAbsolute, join } from "node:path";
127
127
  import { randomBytes } from "node:crypto";
128
128
  import { createRequire } from "node:module";
@@ -674,23 +674,58 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
674
674
  }
675
675
  // Several servers can share one wallet (two sessions, Claude Desktop and Claude Code): every change is a
676
676
  // read-modify-write of the file under an exclusive lock file, so no server rewrites it from its own map and erases
677
- // another's entries. Best effort: a lock older than 10 s is stale, and after 2 s the write goes ahead without it.
677
+ // another's entries. Best effort: after 2 s the write goes ahead without it.
678
678
  // Waiting for it delays this call only (an async wait, never a blocking one): the server keeps answering meanwhile.
679
679
  // Inside one process the lock is held only across synchronous code, so two calls of this server never contend.
680
+ // The lock names its holder (this host, its pid, a random token), written as it is made, and only that holder removes
681
+ // it. No clock says a lock was abandoned (GHSA-wqww-vjpx-w8mq): its file time against this process's clock read a lock
682
+ // milliseconds old as one over 10 s old after a clock step (NTP, a resumed VM) or on a file server whose clock is
683
+ // behind, so a second server took it and wrote at the same time, one erasing the other's entry, and the first then
684
+ // removed the second's lock. A lock is taken over only when its holder is a process of this host no longer running,
685
+ // or once this process has seen that same lock for 10 s of its own monotonic clock, over its calls (a holder stopped
686
+ // that long, a pid reused, another host's lock, the empty lock of a server before this rule).
687
+ const lockHost = hostname();
688
+ let lockSeen = null; // the lock this process last found held, and when (performance.now()) it first found it
689
+ const running = (pid) => { try { process.kill(pid, 0); return true; } catch (e) { return e?.code === "EPERM"; } };
690
+ /** The lock at `path`: which one it is (its file and its token, never its time against a clock), and whether its
691
+ * holder is a process of this host no longer running (in this process a lock never outlives a call). Throws if none. */
692
+ function lockAt(path) {
693
+ const st = statSync(path);
694
+ let h = null;
695
+ try { h = JSON.parse(readFileSync(path, "utf8")); } catch (_) { /* empty: being written, or an older server's */ }
696
+ const pid = Number(h?.pid);
697
+ return { id: `${st.dev}:${st.ino}:${st.mtimeMs}:${h?.token ?? ""}`, gone: h?.host === lockHost && Number.isSafeInteger(pid) && pid > 0 && (pid === process.pid || !running(pid)) };
698
+ }
699
+ /** Moves the lock `id` aside and removes it. If what moved is another lock by then (a server that took it over first
700
+ * and made its own), it is put back, unless a third took the name meanwhile: as sdk/tx-record.mjs's lockKey. */
701
+ function takeOver(path, id) {
702
+ const aside = `${path}.${process.pid}.${randomBytes(4).toString("hex")}.stale`;
703
+ try { renameSync(path, aside); } catch (e) { return e?.code === "ENOENT"; }
704
+ try { if (lockAt(aside).id !== id) linkSync(aside, path); } catch (_) { /* a third took the name */ }
705
+ rmSync(aside, { force: true });
706
+ return true;
707
+ }
680
708
  async function locked(fn) {
681
709
  const lock = `${stateFile}.lock`;
682
- const giveUp = Date.now() + 2000;
710
+ const token = randomBytes(12).toString("hex");
711
+ const start = performance.now();
683
712
  let held = false;
684
713
  for (;;) {
685
- try { closeSync(openSync(lock, "wx", 0o600)); held = true; break; } catch (e) {
714
+ try { writeFileSync(lock, JSON.stringify({ host: lockHost, pid: process.pid, token }), { flag: "wx", mode: 0o600 }); held = true; break; } catch (e) {
686
715
  if (e?.code === "ENOENT") { try { mkdirSync(stateDir, { recursive: true, mode: 0o700 }); continue; } catch (_) { break; } }
687
716
  if (e?.code !== "EEXIST") break;
688
- try { if (Date.now() - statSync(lock).mtimeMs > 10_000) { rmSync(lock, { force: true }); continue; } } catch (_) { continue; }
689
- if (Date.now() > giveUp) break;
717
+ let now;
718
+ try { now = lockAt(lock); } catch (_) { continue; } // released meanwhile
719
+ const t = performance.now();
720
+ if (lockSeen?.id !== now.id) lockSeen = { id: now.id, at: t };
721
+ if ((now.gone || t - lockSeen.at >= 10_000) && takeOver(lock, now.id)) continue;
722
+ if (t - start > 2000) break;
690
723
  await new Promise((res) => setTimeout(res, 10));
691
724
  }
692
725
  }
693
- try { return fn(); } finally { if (held) rmSync(lock, { force: true }); }
726
+ try { return fn(); } finally {
727
+ if (held) { try { if (JSON.parse(readFileSync(lock, "utf8")).token === token) rmSync(lock, { force: true }); } catch (_) { /* gone, or not ours */ } }
728
+ }
694
729
  }
695
730
  /** Entries another server of this wallet wrote since: merged in before any decision to sign. */
696
731
  function refresh() { if (stateFile) for (const [k, e] of readState()) if (!outstanding.has(k)) outstanding.set(k, e); }
@@ -2055,7 +2090,7 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
2055
2090
 
2056
2091
  /** One trade from the configured wallet: the SDK's calls, sent in order (each simulated, then waited for). `count` is
2057
2092
  * serial's: a buy's amount counts against PRIORS_MAX_PT_TOTAL_USD from its check until the call ends. */
2058
- async function ptTrade(kind, amount, slippage_bps, dry_run, market, count) {
2093
+ async function ptTrade(kind, amount, slippage_bps, dry_run, market, count, floor) {
2059
2094
  const signer = needWallet(`pt_${kind}`);
2060
2095
  let units = null;
2061
2096
  if (kind === "buy") {
@@ -2068,6 +2103,17 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
2068
2103
  if (!dry_run) count("ptBought", units);
2069
2104
  }
2070
2105
  const o = { account: signer.address, amount: units ?? amount, slippageBps: slippage_bps, ...(market ? { market } : {}) };
2106
+ // the least the caller holds the trade to (pt_quote's or a dry_run's "at least", the quote the user agreed to): a
2107
+ // fresh quote under it is refused before anything is signed, and the router call names the larger of it and the
2108
+ // fresh quote's own minimum, never a lower one (GHSA-3fwm-95gh-5xh4, as stock_buy's min_out)
2109
+ if (floor !== undefined) {
2110
+ const what = kind === "buy" ? "min_out_pt" : "min_out_usdg";
2111
+ const least = dollars(floor, what);
2112
+ let b;
2113
+ try { b = await pt.trade(kind, o); } catch (e) { throw asked(e); }
2114
+ if (b.quote.expectedOut < least) throw new ToolError(`pt_${kind} refused: the price moved: a fresh quote gives ${nf(b.quote.expectedOutText)} ${b.quote.tokenOut}, under the ${nf(ethers.formatUnits(least, 6))} ${b.quote.tokenOut} asked for at least (${what}). Nothing was signed or sent: quote it again (pt_quote) and ask the user.`);
2115
+ o.minOut = b.quote.minOut > least ? b.quote.minOut : least;
2116
+ }
2071
2117
  if (dry_run) {
2072
2118
  let b;
2073
2119
  try { b = await pt.trade(kind, o); } catch (e) { throw asked(e); }
@@ -2112,16 +2158,16 @@ export async function createPriorsMcpServer({ env = process.env, fetchImpl = glo
2112
2158
  tool("pt_buy", {
2113
2159
  title: "Buy PT-USDG with USDG",
2114
2160
  description: `Buy PT-USDG (Pendle's principal token for USDG, redeemable 1:1 for USDG on ${P.PT_USDG.maturity.slice(0, 10)}) with the configured wallet's USDG, through Pendle's router: an approval of exactly that USDG, then the swap, paying the PT-USDG to this wallet with a minimum out. Held to maturity it pays a fixed amount of USDG; sold earlier, it gets the market's rate then, which can be lower. Moves real money: quote it (pt_quote), state the amount and what it buys to the user and get their go-ahead first; dry_run: true lists the calls without sending. Needs ETH for gas.`,
2115
- inputSchema: { amount_usdg: z.number().positive().describe("USDG to spend, in US dollars (required, no default)."), slippage_bps: slippage, dry_run: dryRun },
2161
+ inputSchema: { amount_usdg: z.number().positive().describe("USDG to spend, in US dollars (required, no default)."), slippage_bps: slippage, min_out_pt: z.number().positive().optional().describe("The least PT-USDG to accept, e.g. pt_quote's \"at least\": a fresh quote under it is refused before anything is signed."), dry_run: dryRun },
2116
2162
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
2117
- }, serial(async ({ amount_usdg, slippage_bps, dry_run }, _deadline, count) => ptTrade("buy", amount_usdg, slippage_bps, dry_run, undefined, count)));
2163
+ }, serial(async ({ amount_usdg, slippage_bps, min_out_pt, dry_run }, _deadline, count) => ptTrade("buy", amount_usdg, slippage_bps, dry_run, undefined, count, min_out_pt)));
2118
2164
 
2119
2165
  tool("pt_sell", {
2120
2166
  title: "Sell PT-USDG for USDG",
2121
2167
  description: "Sell the configured wallet's PT-USDG for USDG at the market, before maturity, through Pendle's router: an approval of exactly that PT-USDG, then the swap, paying the USDG to this wallet with a minimum out. Moves real money: quote it (pt_quote), state the amount to the user and get their go-ahead first; dry_run: true lists the calls without sending. Needs ETH for gas.",
2122
- inputSchema: { amount_pt: ptAmount, slippage_bps: slippage, dry_run: dryRun },
2168
+ inputSchema: { amount_pt: ptAmount, slippage_bps: slippage, min_out_usdg: z.number().positive().optional().describe("The least USDG to accept, e.g. pt_quote's \"at least\": a fresh quote under it is refused before anything is signed."), dry_run: dryRun },
2123
2169
  annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: false, openWorldHint: true },
2124
- }, serial(async ({ amount_pt, slippage_bps, dry_run }) => ptTrade("sell", amount_pt, slippage_bps, dry_run)));
2170
+ }, serial(async ({ amount_pt, slippage_bps, min_out_usdg, dry_run }) => ptTrade("sell", amount_pt, slippage_bps, dry_run, undefined, undefined, min_out_usdg)));
2125
2171
 
2126
2172
  tool("pt_redeem", {
2127
2173
  title: "Redeem PT-USDG for USDG",