UNPKG

@chainbound/payflow-sdk

Version:
5 lines 4.51 kB
import{McpServer as g}from"@modelcontextprotocol/sdk/server/mcp.js";import{z as f}from"zod";import P from"debug";import"x402/types";import{useFacilitator as R}from"x402/verify";import{createFacilitatorConfig as v}from"@coinbase/x402";import{exact as w}from"x402/schemes";import{findMatchingPaymentRequirements as b,processPriceToAtomicAmount as T}from"x402/shared";var h=class extends g{log;options;verify;settle;constructor(e,t){super(e,t),this.log=P("payflow-sdk");let n={version:1,keyId:process.env.CDP_API_KEY_ID,keySecret:process.env.CDP_API_KEY_SECRET};this.options={x402:{...n,...t?.x402||{}}};let{verify:i,settle:r}=R(v(this.options.x402?.keyId,this.options.x402?.keySecret));this.verify=i,this.settle=r}generateRequirements(e,t,n,i,r){let a=T(n,r);if("error"in a)throw new Error(a.error);let{maxAmountRequired:c,asset:l}=a,s=[{scheme:"exact",network:r,maxAmountRequired:c,resource:t,description:"",mimeType:"text/plain",payTo:i,maxTimeoutSeconds:60,asset:l.address,outputSchema:void 0,extra:{name:l.eip712.name,version:l.eip712.version}}],p=b(s,e);if(!p)throw new Error("No matching payment requirements found");return p}async verifyPayment(e,t){let n=await this.verify(e,t);if(!n.isValid)throw x(t,n.invalidReason)}async settlePayment(e,t){let n=await this.settle(e,t);if(!n.success)throw new Error(n.errorReason);return n}createPaidCallback(e,t,n,i,r){return async(a,c)=>{this.log("Called paid tool:",e,a);let{payment:l,...s}=a;i.parse(s);let p=w.evm.decodePayment(l);p.x402Version=this.options.x402?.version??1;let m;try{m=this.generateRequirements(p,e,t,n,"base")}catch(o){return this.log("Error generating requirements:",o),{content:[{type:"text",text:o instanceof Error?o.message:String(o),isError:!0}]}}try{await this.verifyPayment(p,m)}catch(o){return this.log("Error verifying payment:",o),{content:[{type:"text",text:o instanceof Error?o.message:String(o),isError:!0}]}}let d;try{r.length>1?d=await r(s,c):d=await r(c)}catch(o){return this.log("Error in tool call:",o),{content:[{type:"text",text:o instanceof Error?o.message:String(o),isError:!0}]}}let u;try{u=await this.settlePayment(p,m)}catch(o){return this.log("Error settling payment:",o),{content:[{type:"text",text:o instanceof Error?o.message:String(o),isError:!0}]}}return d.content.push({type:"text",text:`Payment: ${u.transaction}`}),d}}paidTool(e,...t){let n,i,r,a;if(t.length>=1&&typeof t[0]=="string"&&(n=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])?r=t.shift():typeof t[0]=="object"&&t[0]!==null&&"title"in t[0]&&(a=t.shift())),t.length>=1&&typeof t[0]=="object"&&t[0]!==null&&"title"in t[0]&&(a=t.shift()),t.length>1)throw new Error("Too many arguments to paidTool()");if(!i)throw new Error("PaymentOptions are required for paidTool()");let c=t[0],l=f.object({payment:f.string().describe("The x402 payment header for the query.")}),s;r?s={...r,payment:l.shape.payment}:s=l.shape;let p=r?f.object(r):f.object({}),m=this.createPaidCallback(e,i.price,i.recipient,p,c),u=`${n||`Paid tool ${e}`} IMPORTANT: Payflow payment details: -Price: ${i.price} - Recipient: ${i.recipient}`;return a?this.tool(e,u,s,a,m):this.tool(e,u,s,m)}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 x(y,e){switch(e){case"insufficient_funds":return new Error(`${e}: Insufficient funds for payment. Required: ${y.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 ${y.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: ${y.payTo}`);case"invalid_network":return new Error(`${e}: Invalid network in the payment. Network: ${y.network}`);default:return new Error(`Payment verification failed: ${e||"unknown reason"}`)}}export{h as PayflowMcpServer}; //# sourceMappingURL=index.js.map