@getalby/paidmcp
Version:
Charge for your MCP Server tools using Nostr Wallet Connect
32 lines (31 loc) • 1.24 kB
JavaScript
export function paidCallback(cb, charge, wallet, storage) {
return async (args, extra) => {
if (!args.payment_hash ||
!(await storage.isValid(args.payment_hash)) ||
!(await wallet.verifyPayment(args.payment_hash))) {
const chargeArgs = await charge(args);
const { payment_request, payment_hash } = await wallet.requestInvoice({
satoshi: chargeArgs.satoshi,
description: chargeArgs.description,
});
await storage.setValid(payment_hash, true);
const result = {
payment_instructions: "Payment required. Pay the payment_request and try the same request again with the payment_hash set to continue.",
payment_request,
payment_hash,
};
return {
content: [
{
type: "text",
text: JSON.stringify(result),
},
],
structuredContent: result,
};
}
// only allow one request per payment
await storage.setValid(args.payment_hash, false);
return cb(args, extra);
};
}