Skip to main content
Glama

decode_invoice

Decode BOLT11 Lightning invoices to extract payment details and verify transaction information for Bitcoin network interactions.

Instructions

Decode a Lightning invoice

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
invoiceYesBOLT11 Lightning invoice

Implementation Reference

  • The primary MCP tool handler for 'decode_invoice'. Validates the input args for a required 'invoice' string, delegates decoding to BitcoinService.decodeInvoice, formats the result into MCP TextContent response, and handles errors appropriately.
    export async function handleDecodeInvoice(
      bitcoinService: BitcoinService,
      args: unknown
    ) {
      if (
        !args ||
        typeof args !== "object" ||
        !("invoice" in args) ||
        typeof args.invoice !== "string"
      ) {
        throw new McpError(
          ErrorCode.InvalidParams,
          "Invalid parameters: invoice is required"
        );
      }
    
      try {
        const invoice = bitcoinService.decodeInvoice(args.invoice);
        return {
          content: [
            {
              type: "text",
              text: `Decoded Lightning invoice:\nNetwork: ${invoice.network}\nAmount: ${invoice.amount} satoshis\nDescription: ${invoice.description}\nExpiry: ${invoice.expiryDate}\nStatus: ${invoice.status}`,
            },
          ] as TextContent[],
        };
      } catch (error: any) {
        throw new McpError(
          ErrorCode.InternalError,
          error.message || "Failed to decode invoice"
        );
      }
    }
  • Registration of the 'decode_invoice' tool in the ListToolsRequestHandler. Defines the tool name, description, and input schema requiring a 'invoice' string parameter.
      name: "decode_invoice",
      description: "Decode a Lightning invoice",
      inputSchema: {
        type: "object",
        properties: {
          invoice: {
            type: "string",
            description: "BOLT11 Lightning invoice",
          },
        },
        required: ["invoice"],
      },
    } as Tool,
  • Dispatching logic in the CallToolRequestHandler switch statement that routes 'decode_invoice' calls to the handleDecodeInvoice function with BitcoinService instance.
    case "decode_invoice": {
      return handleDecodeInvoice(this.bitcoinService, args);
    }
  • BitcoinService.decodeInvoice method: Checks if LNBitsClient is configured, then delegates the actual BOLT11 decoding to LNBitsClient.toHumanFriendlyInvoice, with error handling and logging.
    decodeInvoice(bolt11: string): HumanFriendlyInvoice {
      if (!this.lnbitsClient) {
        throw new LightningError(
          "LNBits not configured. Please add lnbitsUrl, lnbitsAdminKey, and lnbitsReadKey to configuration.",
          LightningErrorCode.NOT_CONNECTED
        );
      }
    
      try {
        return this.lnbitsClient.toHumanFriendlyInvoice(bolt11);
      } catch (error) {
        logger.error({ error, bolt11 }, "Failed to decode invoice");
        throw new LightningError(
          "Failed to decode Lightning invoice",
          LightningErrorCode.PAYMENT_ERROR
        );
      }
    }
  • LNBitsClient helper methods for decoding BOLT11 invoices. 'toHumanFriendlyInvoice' uses the 'bolt11-decoder' library to parse the invoice string, then 'humanFriendlyInvoice' extracts and formats key fields like network, amount, description, expiry, and status into HumanFriendlyInvoice.
    humanFriendlyInvoice(decoded: DecodedInvoice): HumanFriendlyInvoice {
      const network = decoded.prefix.startsWith("lnbc") ? "mainnet" : "testnet";
      const descriptionTag = decoded.tags.find(
        (t) => t.tagName === "description"
      );
      const description = descriptionTag
        ? String(descriptionTag.data)
        : "No description provided";
      return {
        network: network,
        amount: decoded.satoshis,
        description: description,
        expiryDate: new Date(decoded.timestamp * 1000).toLocaleString(),
        status: "VALID",
      };
    }
    
    toHumanFriendlyInvoice(bolt11Invoice: string): HumanFriendlyInvoice {
      const decoded = bolt11.decode(bolt11Invoice) as DecodedInvoice;
      return this.humanFriendlyInvoice(decoded);
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/AbdelStark/bitcoin-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server