Skip to main content

MCP server for L402 Lightning payments - access paid APIs with AI agents

Project description

Lightning Enable MCP Server

An MCP (Model Context Protocol) server that enables AI agents to access L402-protected APIs with automatic Lightning Network payments.

Overview

Lightning Enable MCP provides tools for AI agents (like Claude) to:

  • Access paid APIs - Automatically handle L402 payment challenges
  • Manage Lightning payments - Pay invoices via Nostr Wallet Connect (NWC)
  • Control spending - Set per-request and session budgets
  • Track payments - View payment history and wallet balance

Pricing Tiers

FREE Tier (No License Required)

Use these tools with no subscription required:

Tool Description
pay_invoice Pay any Lightning invoice
check_wallet_balance Check wallet balance
get_payment_history View payment history
get_budget_status View spending limits and status

PAID Tier

L402 Microtransactions features require a license. Two options:

Option Price Best For
AI Agent License 6,000 sats one-time AI agents, bots, automated systems
Human Subscription $299/month Businesses, API providers

AI Agent License: Pay 6,000 sats via Lightning to unlock L402 features instantly. No account needed - just pay and go.

Human Subscription: Subscribe at https://lightningenable.com for full platform access including dashboard, analytics, and support.

Tool Description
access_l402_resource Auto-pay L402 challenges
pay_l402_challenge Manual L402 payment

Set LIGHTNING_ENABLE_API_KEY with your API key to authenticate.

Installation

Using pip

pip install lightning-enable-mcp

Using uvx (recommended for Claude Desktop)

No installation needed - uvx handles it automatically.

Using Docker

docker pull lightningenable/mcp:latest

Configuration

Environment Variables

Variable Required Default Description
NWC_CONNECTION_STRING Yes* - Nostr Wallet Connect URI
OPENNODE_API_KEY Yes* - OpenNode API key (alternative to NWC)
OPENNODE_ENVIRONMENT No production "production" or "dev" for testnet
L402_MAX_SATS_PER_REQUEST No 1000 Maximum sats per single request
L402_MAX_SATS_PER_SESSION No 10000 Maximum sats for entire session

*Either NWC_CONNECTION_STRING or OPENNODE_API_KEY is required. NWC takes precedence if both are set.

Wallet Options

Option 1: OpenNode (Recommended for Testing)

Use your OpenNode account to pay invoices. Great for testing since you likely already have an OpenNode account if you're using Lightning Enable.

  1. Get your API key from https://app.opennode.com (or https://app.dev.opennode.com for testnet)
  2. Ensure the API key has withdrawal permissions
  3. Fund your OpenNode account
export OPENNODE_API_KEY="your-api-key"
export OPENNODE_ENVIRONMENT="dev"  # Use testnet for testing

Option 2: Nostr Wallet Connect (NWC)

NWC allows this server to pay invoices using your Lightning wallet. You can get a connection string from:

  1. Alby Hub - https://getalby.com/alby-hub?ref=magma
  2. Mutiny Wallet - https://mutinywallet.com
  3. Coinos - https://coinos.io
  4. Any NWC-compatible wallet

The connection string looks like:

nostr+walletconnect://pubkey?relay=wss://relay.example.com&secret=hexsecret

Claude Desktop Configuration

Add to your Claude Desktop config (claude_desktop_config.json):

Using OpenNode:

{
  "mcpServers": {
    "lightning-enable": {
      "command": "uvx",
      "args": ["lightning-enable-mcp"],
      "env": {
        "OPENNODE_API_KEY": "your-opennode-api-key",
        "OPENNODE_ENVIRONMENT": "dev"
      }
    }
  }
}

Using NWC:

{
  "mcpServers": {
    "lightning-enable": {
      "command": "uvx",
      "args": ["lightning-enable-mcp"],
      "env": {
        "NWC_CONNECTION_STRING": "nostr+walletconnect://your-pubkey?relay=wss://relay.getalby.com/v1&secret=your-secret"
      }
    }
  }
}

Or if installed via pip, replace "command": "uvx", "args": ["lightning-enable-mcp"] with just "command": "lightning-enable-mcp".

Using Docker:

{
  "mcpServers": {
    "lightning-enable": {
      "command": "docker",
      "args": ["run", "--rm", "-i", "lightningenable/mcp:latest"],
      "env": {
        "NWC_CONNECTION_STRING": "nostr+walletconnect://..."
      }
    }
  }
}

Available Tools

access_l402_resource

Fetch a URL with automatic L402 payment handling.

Parameters:

Name Type Required Default Description
url string Yes - The URL to fetch
method string No GET HTTP method (GET, POST, PUT, DELETE)
headers object No {} Additional request headers
body string No - Request body for POST/PUT
max_sats integer No 1000 Maximum sats to pay for this request

Example:

Use access_l402_resource to fetch https://api.example.com/premium-data

Returns: Response body text or error message

pay_l402_challenge

Manually pay an L402 invoice and get the authorization token.

Parameters:

Name Type Required Default Description
invoice string Yes - BOLT11 invoice string
macaroon string Yes - Base64-encoded macaroon from L402 challenge
max_sats integer No 1000 Maximum sats allowed for this payment

Returns: L402 token in format macaroon:preimage for use in Authorization header

check_wallet_balance

Check the connected wallet balance via NWC.

Parameters: None

Returns: Current balance in satoshis

get_payment_history

List recent L402 payments made during this session.

Parameters:

Name Type Required Default Description
limit integer No 10 Maximum number of payments to return
since string No - ISO timestamp to filter payments from

Returns: List of payments with url, amount, timestamp, and status

configure_budget

Set spending limits for the session.

Parameters:

Name Type Required Default Description
per_request integer No 1000 Maximum sats per individual request
per_session integer No 10000 Maximum total sats for the session

Returns: Confirmation of new limits

pay_invoice

Pay a Lightning invoice directly and get the preimage as proof of payment. Use this to pay any BOLT11 Lightning invoice without L402 protocol overhead.

Parameters:

Name Type Required Default Description
invoice string Yes - BOLT11 Lightning invoice string to pay
max_sats integer No 1000 Maximum sats allowed to pay

Example:

Use pay_invoice to pay lnbc100n1pj9npjpp5... with max_sats 500

Returns: JSON with payment result including:

  • success: boolean - Whether payment succeeded
  • preimage: string - Payment preimage (proof of payment)
  • message: string - Status message
  • invoice.paid: string - Truncated invoice that was paid

How L402 Works

L402 (formerly LSAT) is a protocol for API monetization using Lightning Network:

  1. Client requests a resource
  2. Server returns 402 Payment Required with a WWW-Authenticate header containing:
    • A macaroon (authorization token)
    • A BOLT11 Lightning invoice
  3. Client pays the invoice, receiving a preimage
  4. Client retries the request with Authorization: L402 <macaroon>:<preimage>
  5. Server validates and returns the resource

This MCP server handles steps 2-5 automatically when you use access_l402_resource.

Security Considerations

  • Budget Limits: Always set appropriate spending limits for your use case
  • NWC Secret: Keep your NWC connection string secure - it allows payments from your wallet
  • Session Isolation: Each server instance maintains its own budget and payment history
  • Invoice Verification: The server verifies invoice amounts before paying

Development

Setup

git clone https://github.com/refinedelement/lightning-enable-mcp
cd lightning-enable-mcp
pip install -e ".[dev]"

Running Tests

pytest

Type Checking

mypy src/lightning_enable_mcp

Linting

ruff check src/
ruff format src/

Architecture

lightning_enable_mcp/
├── server.py          # Main MCP server and tool registration
├── l402_client.py     # L402 protocol implementation
├── nwc_wallet.py      # Nostr Wallet Connect client
├── budget.py          # Spending limit management
└── tools/
    ├── access_resource.py   # access_l402_resource tool
    ├── pay_challenge.py     # pay_l402_challenge tool
    ├── wallet.py            # check_wallet_balance tool
    └── budget.py            # configure_budget, get_payment_history tools

License

MIT License - see LICENSE for details.

Support

Related Projects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lightning_enable_mcp-1.5.2.tar.gz (50.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lightning_enable_mcp-1.5.2-py3-none-any.whl (57.9 kB view details)

Uploaded Python 3

File details

Details for the file lightning_enable_mcp-1.5.2.tar.gz.

File metadata

  • Download URL: lightning_enable_mcp-1.5.2.tar.gz
  • Upload date:
  • Size: 50.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for lightning_enable_mcp-1.5.2.tar.gz
Algorithm Hash digest
SHA256 8ff604ff72819d9668b6fceb992381667e62ed9dba0fb5c3038ccde05d2cddd1
MD5 0443131e93a9999e0671342d0d54126b
BLAKE2b-256 84252e8346083f93e07876585f87039da864fe5799da77e9e5f2a60b8eb7f67d

See more details on using hashes here.

File details

Details for the file lightning_enable_mcp-1.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for lightning_enable_mcp-1.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f2faf1dc5e81af613f42bc9aeec75c63a2eabd7effce7520fd9dc0a8d26d24a6
MD5 9083010e1078adef976062278448ba05
BLAKE2b-256 dc2586a0621689d68639f45fea7e26b657ba52e936cc37072887da8157099471

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page