Pay Invoice
pay_invoicePay a Lightning Network invoice by supplying the invoice string. For zero-amount invoices, you can set the amount and attach metadata.
Instructions
Pay a lightning invoice
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| invoice | Yes | The lightning invoice to pay | |
| amount_in_sats | No | ||
| metadata | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| preimage | Yes | Payment preimage | |
| fees_paid_in_sats | No | Fees paid in sats |
Implementation Reference
- src/tools/nwc/pay_invoice.ts:1-63 (handler)Main handler function `registerPayInvoiceTool` that registers and implements the 'pay_invoice' tool. It wraps `client.payInvoice()`, converts sats to millisats for NWC, converts millisats back to sats in response, and returns structured content with preimage and fees_paid_in_sats.
import { nwc } from "@getalby/sdk"; import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { z } from "zod"; export function registerPayInvoiceTool( server: McpServer, client: nwc.NWCClient ) { server.registerTool( "pay_invoice", { title: "Pay Invoice", description: "Pay a lightning invoice", inputSchema: { invoice: z.string().describe("The lightning invoice to pay"), amount_in_sats: z .number() .describe( "Optional amount in sats, only provide if paying a zero-amount invoice" ) .nullish(), metadata: z .object({}) .passthrough() .describe("Optional metadata to include with the payment") .nullish(), }, outputSchema: { preimage: z.string().describe("Payment preimage"), fees_paid_in_sats: z.number().nullish().describe("Fees paid in sats"), // TODO: remove nullish once Primal supports it }, }, async (params) => { const { fees_paid, preimage, ...result } = await client.payInvoice({ invoice: params.invoice, amount: params.amount_in_sats ? params.amount_in_sats * 1000 : undefined, // Convert sats to millisats for NWC metadata: params.metadata || undefined, }); // Convert millisats back to sats in the response const convertedResult = { ...result, preimage: preimage || "", // TODO: once Primal supports preimage, remove this fees_paid_in_sats: typeof fees_paid === "number" ? Math.ceil(fees_paid / 1000) // Round up fees when converting millisats to sats : undefined, }; return { content: [ { type: "text", text: JSON.stringify(convertedResult, null, 2), }, ], structuredContent: convertedResult, }; } ); } - src/tools/nwc/pay_invoice.ts:9-32 (registration)Registration of the 'pay_invoice' tool on the MCP server, including input schema (invoice, amount_in_sats, metadata) and output schema (preimage, fees_paid_in_sats).
server.registerTool( "pay_invoice", { title: "Pay Invoice", description: "Pay a lightning invoice", inputSchema: { invoice: z.string().describe("The lightning invoice to pay"), amount_in_sats: z .number() .describe( "Optional amount in sats, only provide if paying a zero-amount invoice" ) .nullish(), metadata: z .object({}) .passthrough() .describe("Optional metadata to include with the payment") .nullish(), }, outputSchema: { preimage: z.string().describe("Payment preimage"), fees_paid_in_sats: z.number().nullish().describe("Fees paid in sats"), // TODO: remove nullish once Primal supports it }, }, - src/mcp_server.ts:7-7 (registration)Import of `registerPayInvoiceTool` from the pay_invoice module.
import { registerPayInvoiceTool } from "./tools/nwc/pay_invoice.js"; - src/mcp_server.ts:26-26 (registration)Registration call: `registerPayInvoiceTool(server, client)` inside `createMCPServer`.
registerPayInvoiceTool(server, client);