@chainbound/payflow-sdk
Version:
5 lines • 5.16 kB
JavaScript
;var S=Object.create;var h=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var A=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty;var q=(o,e)=>{for(var t in e)h(o,t,{get:e[t],enumerable:!0})},R=(o,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of k(e))!_.call(o,i)&&i!==t&&h(o,i,{get:()=>e[i],enumerable:!(r=E(e,i))||r.enumerable});return o};var O=(o,e,t)=>(t=o!=null?S(A(o)):{},R(e||!o||!o.__esModule?h(t,"default",{value:o,enumerable:!0}):t,o)),C=o=>R(h({},"__esModule",{value:!0}),o);var Z={};q(Z,{PayflowMcpServer:()=>P});module.exports=C(Z);var v=require("@modelcontextprotocol/sdk/server/mcp.js"),f=require("zod"),w=O(require("debug"),1),I=require("x402/types"),b=require("x402/verify"),T=require("@coinbase/x402"),x=require("x402/schemes"),g=require("x402/shared"),P=class extends v.McpServer{log;options;verify;settle;constructor(e,t){super(e,t),this.log=(0,w.default)("payflow-sdk");let r={version:1,keyId:process.env.CDP_API_KEY_ID,keySecret:process.env.CDP_API_KEY_SECRET};this.options={x402:{...r,...t?.x402||{}}};let{verify:i,settle:a}=(0,b.useFacilitator)((0,T.createFacilitatorConfig)(this.options.x402?.keyId,this.options.x402?.keySecret));this.verify=i,this.settle=a}generateRequirements(e,t,r,i,a){let s=(0,g.processPriceToAtomicAmount)(r,a);if("error"in s)throw new Error(s.error);let{maxAmountRequired:m,asset:c}=s,p=[{scheme:"exact",network:a,maxAmountRequired:m,resource:t,description:"",mimeType:"text/plain",payTo:i,maxTimeoutSeconds:60,asset:c.address,outputSchema:void 0,extra:{name:c.eip712.name,version:c.eip712.version}}],l=(0,g.findMatchingPaymentRequirements)(p,e);if(!l)throw new Error("No matching payment requirements found");return l}async verifyPayment(e,t){let r=await this.verify(e,t);if(!r.isValid)throw $(t,r.invalidReason)}async settlePayment(e,t){let r=await this.settle(e,t);if(!r.success)throw new Error(r.errorReason);return r}createPaidCallback(e,t,r,i,a){return async(s,m)=>{this.log("Called paid tool:",e,s);let{payment:c,...p}=s;i.parse(p);let l=x.exact.evm.decodePayment(c);l.x402Version=this.options.x402?.version??1;let y;try{y=this.generateRequirements(l,e,t,r,"base")}catch(n){return this.log("Error generating requirements:",n),{content:[{type:"text",text:n instanceof Error?n.message:String(n),isError:!0}]}}try{await this.verifyPayment(l,y)}catch(n){return this.log("Error verifying payment:",n),{content:[{type:"text",text:n instanceof Error?n.message:String(n),isError:!0}]}}let d;try{a.length>1?d=await a(p,m):d=await a(m)}catch(n){return this.log("Error in tool call:",n),{content:[{type:"text",text:n instanceof Error?n.message:String(n),isError:!0}]}}let u;try{u=await this.settlePayment(l,y)}catch(n){return this.log("Error settling payment:",n),{content:[{type:"text",text:n instanceof Error?n.message:String(n),isError:!0}]}}return d.content.push({type:"text",text:`Payment: ${u.transaction}`}),d}}paidTool(e,...t){let r,i,a,s;if(t.length>=1&&typeof t[0]=="string"&&(r=t.shift()),t.length>=1&&typeof t[0]=="object"&&t[0]!==null&&"price"in t[0]&&"recipient"in t[0]&&(i=t.shift()),t.length>=1&&(this.isZodRawShape(t[0])?a=t.shift():typeof t[0]=="object"&&t[0]!==null&&"title"in t[0]&&(s=t.shift())),t.length>=1&&typeof t[0]=="object"&&t[0]!==null&&"title"in t[0]&&(s=t.shift()),t.length>1)throw new Error("Too many arguments to paidTool()");if(!i)throw new Error("PaymentOptions are required for paidTool()");let m=t[0],c=f.z.object({payment:f.z.string().describe("The x402 payment header for the query.")}),p;a?p={...a,payment:c.shape.payment}:p=c.shape;let l=a?f.z.object(a):f.z.object({}),y=this.createPaidCallback(e,i.price,i.recipient,l,m),u=`${r||`Paid tool ${e}`}
IMPORTANT: Payflow payment details:
-Price: ${i.price}
- Recipient: ${i.recipient}`;return s?this.tool(e,u,p,s,y):this.tool(e,u,p,y)}isZodRawShape(e){return typeof e!="object"||e===null?!1:Object.keys(e).length===0||Object.values(e).some(this.isZodTypeLike)}isZodTypeLike(e){return e!==null&&typeof e=="object"&&"parse"in e&&typeof e.parse=="function"&&"safeParse"in e&&typeof e.safeParse=="function"}};function $(o,e){switch(e){case"insufficient_funds":return new Error(`${e}: Insufficient funds for payment. Required: ${o.maxAmountRequired}`);case"invalid_exact_evm_payload_authorization_valid_after":return new Error(`${e}: Invalid validAfter value in the payment`);case"invalid_exact_evm_payload_authorization_valid_before":return new Error(`${e}: Invalid validBefore value in the payment`);case"invalid_exact_evm_payload_authorization_value":return new Error(`${e}: The value of the payment is incorrect, it should be ${o.maxAmountRequired}`);case"invalid_exact_evm_payload_signature":return new Error(`${e}: Invalid signature in the payment`);case"invalid_exact_evm_payload_recipient_mismatch":return new Error(`${e}: Recipient mismatch in the payment. Pay to: ${o.payTo}`);case"invalid_network":return new Error(`${e}: Invalid network in the payment. Network: ${o.network}`);default:return new Error(`Payment verification failed: ${e||"unknown reason"}`)}}0&&(module.exports={PayflowMcpServer});
//# sourceMappingURL=index.cjs.map