@aibtc/mcp-server 1.73.1 → 1.74.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.
@@ -1,29 +1,23 @@
1
1
  import { McpServer } from "@modelcontextprotocol/server";
2
2
  /**
3
- * Pre-flight balance check for a non-sponsored sBTC inbox payment.
4
- * Verifies the wallet holds enough sBTC for the message AND enough STX for a
5
- * realistic transfer fee (the sender pays its own gas).
3
+ * Pre-flight balance check for an sBTC inbox payment.
4
+ * Verifies the wallet holds enough sBTC for the message, and — unless the
5
+ * payment is sponsored — enough STX for a realistic transfer fee.
6
6
  */
7
- export declare function checkDirectInboxBalance(address: string, amount: string): Promise<void>;
7
+ export declare function checkDirectInboxBalance(address: string, amount: string, sponsored?: boolean): Promise<void>;
8
8
  /**
9
- * Direct (non-sponsored) x402 inbox messaging.
9
+ * x402 inbox messaging via the `x402-stacks` client interceptor
10
+ * (`wrapAxiosWithPayment`). The transaction is signed but not broadcast; the
11
+ * inbox endpoint settles it through its x402 facilitator.
10
12
  *
11
- * Unlike `send_inbox_message` — which builds a *sponsored* sBTC transfer
12
- * (`sponsored: true`, `fee: 0n`) so the aibtc relay co-signs and pays the STX
13
- * gas — this tool uses the standard `x402-stacks` client interceptor
14
- * (`wrapAxiosWithPayment`). That signs a normal standard-auth sBTC transfer
15
- * where the *sender pays its own STX gas*. The transaction is signed but not
16
- * broadcast; the inbox endpoint settles it via the x402 facilitator's
17
- * `/settle` flow (no relay sponsorship).
13
+ * The inbox decides who pays gas. When its 402 advertises `extra.feePayer`,
14
+ * x402-stacks (>=2.1.0) signs a sponsored sBTC transfer with fee 0 and the
15
+ * facilitator co-signs and pays the STX gas. Without it, the SDK signs a
16
+ * standard transfer and the sender pays its own gas.
18
17
  *
19
18
  * Server side: `lib/inbox/x402-verify.ts` deserializes the payment tx and
20
- * branches on `tx.auth.authType`. A standard-auth tx (what x402-stacks signs)
21
- * takes the non-sponsored `X402PaymentVerifier.settle()` path — the relay never
22
- * adds a sponsor signature or pays gas.
23
- *
24
- * Trade-off vs the sponsored tool: the sender must hold STX to cover the
25
- * transfer fee (the pre-flight check enforces this). In exchange there is no
26
- * relay in the middle of the payment authorization.
19
+ * branches on `tx.auth.authType` (sponsored → relay sponsors; standard →
20
+ * relay `/settle` broadcasts as-is).
27
21
  */
28
22
  export declare function registerInboxX402Tools(server: McpServer): void;
29
23
  //# sourceMappingURL=inbox-x402.tools.d.ts.map
@@ -2,6 +2,7 @@ import { z } from "zod";
2
2
  import axios, { AxiosError } from "axios";
3
3
  import { wrapAxiosWithPayment, decodePaymentRequired, decodePaymentResponse, X402_HEADERS, } from "x402-stacks";
4
4
  import { getAccount, NETWORK } from "../services/x402.service.js";
5
+ import { getSpendLimiter } from "../services/spend-limiter.js";
5
6
  import { getSbtcService } from "../services/sbtc.service.js";
6
7
  import { getHiroApi } from "../services/hiro-api.js";
7
8
  import { getExplorerTxUrl } from "../config/networks.js";
@@ -20,11 +21,11 @@ const INBOX_BASE = "https://aibtc.com/api/inbox";
20
21
  */
21
22
  const MAX_REALISTIC_FEE_USTX = 3000n; // 0.003 STX — the sbtc_transfer fee ceiling
22
23
  /**
23
- * Pre-flight balance check for a non-sponsored sBTC inbox payment.
24
- * Verifies the wallet holds enough sBTC for the message AND enough STX for a
25
- * realistic transfer fee (the sender pays its own gas).
24
+ * Pre-flight balance check for an sBTC inbox payment.
25
+ * Verifies the wallet holds enough sBTC for the message, and — unless the
26
+ * payment is sponsored — enough STX for a realistic transfer fee.
26
27
  */
27
- export async function checkDirectInboxBalance(address, amount) {
28
+ export async function checkDirectInboxBalance(address, amount, sponsored = false) {
28
29
  const sbtcService = getSbtcService(NETWORK);
29
30
  const sbtcBalance = BigInt((await sbtcService.getBalance(address)).balance);
30
31
  const required = BigInt(amount);
@@ -33,6 +34,9 @@ export async function checkDirectInboxBalance(address, amount) {
33
34
  throw new InsufficientBalanceError(`Insufficient sBTC balance: need ${formatSbtc(amount)}, have ${formatSbtc(sbtcBalance.toString())} (shortfall: ${formatSbtc(shortfall.toString())}). ` +
34
35
  `Deposit more sBTC via the bridge at https://bridge.stx.eco or use a different wallet.`, "sBTC", sbtcBalance.toString(), amount, shortfall.toString());
35
36
  }
37
+ // Sponsored: the facilitator pays the gas, so no STX is needed.
38
+ if (sponsored)
39
+ return;
36
40
  const hiro = getHiroApi(NETWORK);
37
41
  const stxBalance = BigInt((await hiro.getStxBalance(address)).balance);
38
42
  const fees = await hiro.getMempoolFees();
@@ -53,33 +57,28 @@ export async function checkDirectInboxBalance(address, amount) {
53
57
  }
54
58
  }
55
59
  /**
56
- * Direct (non-sponsored) x402 inbox messaging.
60
+ * x402 inbox messaging via the `x402-stacks` client interceptor
61
+ * (`wrapAxiosWithPayment`). The transaction is signed but not broadcast; the
62
+ * inbox endpoint settles it through its x402 facilitator.
57
63
  *
58
- * Unlike `send_inbox_message` — which builds a *sponsored* sBTC transfer
59
- * (`sponsored: true`, `fee: 0n`) so the aibtc relay co-signs and pays the STX
60
- * gas — this tool uses the standard `x402-stacks` client interceptor
61
- * (`wrapAxiosWithPayment`). That signs a normal standard-auth sBTC transfer
62
- * where the *sender pays its own STX gas*. The transaction is signed but not
63
- * broadcast; the inbox endpoint settles it via the x402 facilitator's
64
- * `/settle` flow (no relay sponsorship).
64
+ * The inbox decides who pays gas. When its 402 advertises `extra.feePayer`,
65
+ * x402-stacks (>=2.1.0) signs a sponsored sBTC transfer with fee 0 and the
66
+ * facilitator co-signs and pays the STX gas. Without it, the SDK signs a
67
+ * standard transfer and the sender pays its own gas.
65
68
  *
66
69
  * Server side: `lib/inbox/x402-verify.ts` deserializes the payment tx and
67
- * branches on `tx.auth.authType`. A standard-auth tx (what x402-stacks signs)
68
- * takes the non-sponsored `X402PaymentVerifier.settle()` path — the relay never
69
- * adds a sponsor signature or pays gas.
70
- *
71
- * Trade-off vs the sponsored tool: the sender must hold STX to cover the
72
- * transfer fee (the pre-flight check enforces this). In exchange there is no
73
- * relay in the middle of the payment authorization.
70
+ * branches on `tx.auth.authType` (sponsored → relay sponsors; standard →
71
+ * relay `/settle` broadcasts as-is).
74
72
  */
75
73
  export function registerInboxX402Tools(server) {
76
74
  server.registerTool("send_inbox_message_direct", {
77
75
  description: "Send a paid x402 message to another agent's inbox on aibtc.com. This is the canonical inbox " +
78
76
  "send tool (the older sponsored send_inbox_message is deprecated).\n\n" +
79
- "It signs a standard sBTC transfer with the x402-stacks client interceptor — you pay BOTH the sBTC " +
80
- "message cost AND your own STX gas fee. No relay sits in the middle of the payment; the inbox settles " +
81
- "the signed transaction via the x402 facilitator.\n\n" +
82
- "Requires an unlocked wallet holding sBTC (message cost) and STX (gas). Mainnet only.",
77
+ "It signs an sBTC transfer with the x402-stacks client interceptor and the inbox settles it via its " +
78
+ "x402 facilitator. When the inbox offers sponsorship (402 `extra.feePayer`), the transfer is gasless — " +
79
+ "you pay only the sBTC message cost. Otherwise you also pay your own STX gas fee.\n\n" +
80
+ "Requires an unlocked wallet holding sBTC (message cost), plus STX for gas when the inbox does not " +
81
+ "sponsor. Mainnet only.",
83
82
  inputSchema: z.object({
84
83
  recipientBtcAddress: z
85
84
  .string()
@@ -135,13 +134,19 @@ export function registerInboxX402Tools(server) {
135
134
  throw new Error("402 response missing or empty payment-required header");
136
135
  }
137
136
  const accept = paymentRequired.accepts[0];
138
- // Step 2: Pre-flight balance check — sBTC for the message AND a
139
- // realistic STX gas budget (sender pays its own fee).
140
- await checkDirectInboxBalance(account.address, accept.amount);
137
+ // The SDK signs a sponsored (fee 0) transfer exactly when the
138
+ // requirement names a fee payer — mirror its check.
139
+ const sponsored = typeof accept.extra?.feePayer === "string";
140
+ // Step 2: Pre-flight balance check — sBTC for the message, plus a
141
+ // realistic STX gas budget when the sender pays its own fee.
142
+ await checkDirectInboxBalance(account.address, accept.amount, sponsored);
143
+ // The message cost is a signed sBTC spend: meter it before signing.
144
+ const spendLimiter = getSpendLimiter();
145
+ await spendLimiter.check("sats", BigInt(accept.amount), account.address);
141
146
  // Step 3: Build a payment-enabled axios client. wrapAxiosWithPayment
142
- // installs the x402-stacks interceptor: on a 402 it signs a standard
143
- // (non-sponsored) sBTC transfer and retries with the payment-signature
144
- // header. The sender's wallet pays the gas.
147
+ // installs the x402-stacks interceptor: on a 402 it signs the sBTC
148
+ // transfer (sponsored or standard, per `extra.feePayer`) and retries
149
+ // with the payment-signature header.
145
150
  const stacksAccount = {
146
151
  address: account.address,
147
152
  privateKey: account.privateKey,
@@ -152,13 +157,16 @@ export function registerInboxX402Tools(server) {
152
157
  const response = await api.post(inboxUrl, body, {
153
158
  headers: { "Content-Type": "application/json" },
154
159
  });
160
+ await spendLimiter.record("sats", BigInt(accept.amount), account.address);
155
161
  // Step 5: Decode the settlement (txid + payer) from the response header.
156
162
  const settlement = decodePaymentResponse(response.headers[X402_HEADERS.PAYMENT_RESPONSE]);
157
163
  const txid = settlement?.transaction;
158
164
  const payer = settlement?.payer;
159
165
  return createJsonResponse({
160
166
  success: true,
161
- message: "Message delivered (direct non-sponsored x402)",
167
+ message: sponsored
168
+ ? "Message delivered (x402, gas sponsored)"
169
+ : "Message delivered (x402, sender paid gas)",
162
170
  recipient: {
163
171
  btcAddress: recipientBtcAddress,
164
172
  stxAddress: recipientStxAddress,
@@ -166,14 +174,16 @@ export function registerInboxX402Tools(server) {
166
174
  contentLength: content.length,
167
175
  inbox: response.data,
168
176
  payment: {
169
- mode: "direct-x402-nonsponsored",
177
+ mode: sponsored ? "x402-sponsored" : "direct-x402-nonsponsored",
170
178
  amount: accept.amount + " sats sBTC",
171
179
  ...(payer && { payer }),
172
180
  ...(txid && {
173
181
  txid,
174
182
  explorer: getExplorerTxUrl(txid, NETWORK),
175
183
  }),
176
- note: "Sender paid its own STX gas — no relay sponsorship.",
184
+ note: sponsored
185
+ ? `STX gas paid by the inbox facilitator (${accept.extra?.feePayer}).`
186
+ : "Sender paid its own STX gas — no relay sponsorship.",
177
187
  },
178
188
  });
179
189
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aibtc/mcp-server",
3
- "version": "1.73.1",
3
+ "version": "1.74.0",
4
4
  "description": "Bitcoin-native MCP server for AI agents: BTC/STX wallets, DeFi yield, sBTC peg, NFTs, and x402 payments.",
5
5
  "mcpName": "io.github.aibtcdev/mcp-server",
6
6
  "type": "module",
@@ -74,7 +74,7 @@
74
74
  "sbtc": "^0.3.2",
75
75
  "tslib": "^2.8.1",
76
76
  "ws": "^8.21.0",
77
- "x402-stacks": "^2.0.3",
77
+ "x402-stacks": "^2.1.0",
78
78
  "zod": "^4.6.5"
79
79
  },
80
80
  "devDependencies": {
package/skill/SKILL.md CHANGED
@@ -4,7 +4,7 @@ description: Bitcoin L1 wallet for agents - check balances, send BTC, manage UTX
4
4
  license: MIT
5
5
  metadata:
6
6
  author: aibtcdev
7
- version: 1.73.1 # x-release-please-version
7
+ version: 1.74.0 # x-release-please-version
8
8
  npm: "@aibtc/mcp-server"
9
9
  github: https://github.com/aibtcdev/aibtc-mcp-server
10
10
  ---