Mcp L402 Gate

io.github.zekebuilds-lab由 GitHub 帳號 zekebuilds-lab 發布v0.1.4更新於 Oct 27, 2026

L402 Lightning paywall + DoI score gate for MCP tools: challenge, verify, score.

概覽

AI 產生的概覽

為 MCP 工具加上 Lightning L402 付費牆與身分深度評分檢核的函式庫,呼叫方須支付 sats 並達到信譽分數才會執行。

可以做什麼
  • 包裝 MCP 工具,首次呼叫會回傳 Lightning 發票與 macaroon 挑戰,驗證付款後才執行工具邏輯。
  • 依呼叫方的 Nostr 公鑰向身分深度預言機查詢綜合評分,低於設定門檻的付費呼叫會被拒絕。
  • 可設定金額、最低分數、macaroon 範圍與有效期限、預言機失敗時是否拒絕,以及 LNBits 與預言機位址。
  • 同一套閘道可作為 Express 中介軟體,也可作為工具包裝器使用,並提供發票、付款與評分查詢的測試接縫。
適用情境
適合對外提供付費 MCP 工具、希望抑制女巫帳號與一次性錢包濫用的營運者,而不只是收取小額過路費。當工具本身有實際成本或副作用、需要在執行前依公鑰信譽設下門檻時特別有用。
執行需求
以 npm 套件形式在本機執行,需要 Node 18 以上版本。需要 LNBits 錢包位址與發票/唯讀 API 金鑰、用於 macaroon 簽章的 HMAC 金鑰,以及可存取的 PowForge 預言機與閘道位址(ORACLE_URL、POW_GATE_URL 預設指向公開端點)。需要連線至 LNBits 與預言機的網路。
安裝前請注意
需要錢包與簽章密鑰:LNBits 發票/唯讀金鑰(切勿使用管理員金鑰)以及應定期輪替的 HMAC 金鑰。呼叫方每次呼叫都要支付 sats;目前版本的呼叫方公鑰由呼叫方自行宣稱,信譽檢核可能被偽造。呼叫方公鑰與付款資料會傳送給第三方 PowForge 預言機;預設情況下預言機無法連線時呼叫會被拒絕。

安裝

在 SourceWeft 中

  1. 開啟 儀表板中的 Mcp L402 Gate,將其新增到工作區。
  2. 為需要使用其工具的對話啟用該服務。

Desktop only,透過 STDIO。 STDIO 服務會啟動本機處理程序,因此需要 SourceWeft 桌面主機。

其他 MCP 客戶端

參照 儲存庫 中的啟動說明。

README

@powforge/mcp-l402-gate

[npm] [license] [node]

Identity-scored Lightning paywall for MCP server operators.

L402 alone proves the caller paid 10 sats. It does not prove the caller has a reputation, has been around for more than 10 minutes, or that pricing one tool call shifts their economics at all. A fresh wallet pays the same 10 sats as a real user.

This package adds a Depth-of-Identity check on top of the L402 invoice. Drop it in front of an MCP tool and a caller has to (a) settle a Lightning invoice and (b) carry a DoI score above your threshold before the tool body runs. Cheap sybils still pay the toll, but the toll plus the per-pubkey reputation requirement is harder to grind than either piece on its own.

See it in action

A clone-and-run example server lives at github.com/zekebuilds-lab/mcp-l402-gate-example. It exposes one tool, bitcoin_data, that fetches the BTC/USD price plus mempool fees from mempool.space, gated by L402 + DoI. Clone it, fill in your LNBits creds, npm start, and you have a Lightning-gated MCP server running locally.

The Gap

Sats4AI's own documentation states the limitation plainly:

"autonomous agents cannot build reputation or receive preferential treatment across sessions."

@powforge/mcp-l402-gate closes that gap by composing L402 payment gating with the DoI oracle's composite identity score. A paying caller is also a known caller, with a per-pubkey reputation that survives across sessions and that costs irreversible work to fake.

Why not just L402

L402 is great wire format, weak abuse control. Recent MCP billing tools (sats4ai-mcp, invinoveritas, l402-kit, 402-mcp, coinopai-mcp) all ship the same 402 -> macaroon -> paid -> tool body flow, and an attacker can replay the flow from a fresh node every minute. coinopai-mcp's own author put it: "x402 is payment transport only. It doesn't handle agent identity, rate negotiation, multi-agent splits, or reputation."

PowForge has been shipping the missing piece. The DoI oracle at https://identity.powforge.dev returns a Schnorr-signed score for any Nostr pubkey, computed from observable irreversible work across four dimensions (social, access, vouch, economic). This package wires that score into the L402 path so a paying caller is also a costly-to-fake caller.

Requirements

  • Node >= 18
  • An LNBits wallet (URL + invoice/read API key) for the Lightning side
  • An accessible PowForge oracle URL (default: https://identity.powforge.dev)

5-line integration (Express)

js
const express = require('express');const { mcpL402Middleware } = require('@powforge/mcp-l402-gate');
const app = express();app.use('/tools/expensive', mcpL402Middleware({  secret: process.env.GATE_HMAC_SECRET,  lnbitsUrl: process.env.LNBITS_URL,  lnbitsApiKey: process.env.LNBITS_INVOICE_KEY,  satsAmount: 10,  minScore: 10, // composite >= 10 means "emerging" tier on the oracle}));
app.post('/tools/expensive', (req, res) => {  // Reached only when L402 paid AND req.doiScore >= 10  res.json({ ok: true, doiScore: req.doiScore, l402: req.l402Token });});

The caller passes their pubkey via the X-Caller-Pubkey header or ?pubkey= query string. v0.1.0 treats this as caller-asserted; v0.2.0 will bind it cryptographically via NIP-98.

MCP tool wrapping

js
const { mcpL402Tool } = require('@powforge/mcp-l402-gate');
const expensiveTool = mcpL402Tool({  secret: process.env.GATE_HMAC_SECRET,  lnbitsUrl: process.env.LNBITS_URL,  lnbitsApiKey: process.env.LNBITS_INVOICE_KEY,  satsAmount: 10,  minScore: 10,}, {  name: 'image_render',  description: 'Render an image. 10 sats. Requires DoI score >= 10.',  inputSchema: {    type: 'object',    properties: {      prompt: { type: 'string' },      pubkey: { type: 'string' },      auth: { type: 'object', properties: { macaroon: { type: 'string' }, preimage: { type: 'string' } } },    },    required: ['prompt', 'pubkey'],  },}, async (args, ctx) => {  // Runs only when paid AND ctx.doiScore >= 10  return { image_url: `https://example/r/${args.prompt}`, billed_to: ctx.doiScore };});
// Register expensiveTool with your MCP server. On first call without args.auth,// the tool returns { paid: false, challenge: { macaroon, invoice, ... } }.// The MCP client pays the invoice, then re-calls with args.auth set.

Config reference

FieldDefaultNotes
secretrequiredHMAC key for macaroon signing. Rotate periodically.
lnbitsUrlrequiredLNBits base URL.
lnbitsApiKeyrequiredLNBits invoice/read key. NEVER pass admin key.
satsAmount10Invoice amount per call.
minScore10Reject paid callers below this composite score.
failClosedtrueIf oracle errors, reject the call. Set false to fall through with req.doiScoreError.
oracleUrlhttps://identity.powforge.devOverride for self-hosted oracles.
scopemcp-l402-gate:callL402 macaroon scope.
ttlSeconds600Macaroon validity.
scoreFieldcompositeWhich envelope field to compare to minScore.
callerPubkeyHeaderx-caller-pubkeyHTTP header carrying the caller's asserted pubkey.
oracleAuthoptional{macaroon, preimage} if your oracle is itself L402-paywalled.
createInvoiceFnoptionalTest seam. Async (memo) => {payment_hash, bolt11}.
checkPaidFnoptionalTest seam. Async (payment_hash) => boolean.
lookupScoreFnoptionalTest seam. Async (pubkey) => {composite, rank, depth}.

Score thresholds (composite)

Same buckets the oracle reports as rank:

ThresholdRankUse it when
0unknownYou only want pay-to-call. Skip this package and use L402 directly.
10emergingFirst-call abuse hurts. Default for most public MCP tools.
40activeThe tool burns real GPU or has expensive side effects.
100establishedCompliance-sensitive or single-tenant SaaS-style endpoints.
200trustedHigh-trust admin tooling.

Failure modes

StatusBodyMeaning
402{error: "payment required", macaroon, invoice, payment_hash}First call. Pay the invoice, retry with Authorization: L402 <macaroon>:<preimage>.
401{error: "invalid macaroon", reason}Macaroon malformed, expired, wrong scope, or wrong signature.
401{error: "preimage does not match payment hash"}Preimage failed sha256 check against the macaroon's payment hash.
409{error: "macaroon already redeemed"}Replay guard fired. Mint a fresh macaroon.
400{error: "caller_pubkey_required"}No X-Caller-Pubkey header or ?pubkey= query.
403{error: "score_too_low", score, min, rank}Caller paid but DoI score is below threshold.
503{error: "oracle_unavailable", mode: "fail_closed"}Oracle error and failClosed is on (the default).
502{error: "invoice provider unavailable"}LNBits unreachable on first-call mint.

Why this is a separate package

The L402 macaroon mint and verify code, the LNBits client, and the oracle client are all already shipping inside other PowForge packages. The point of @powforge/mcp-l402-gate is to make the composition trivial: one factory, one config object, one middleware OR one tool wrapper. Operators do not have to assemble three packages by hand to get a defended endpoint.

Tests

npm test

16 unit tests, no real network. The macaroon HMAC is real; LNBits and oracle are stubbed.

License

MIT.

Links

來源:README.md,提交 7fc1fe1

工具

0
SourceWeft 尚未在沙箱中啟動過該軟體包,因此其工具未知。

版本歷史

1
  1. v0.1.4最新Sep 16, 2026

評分

已滿足適用檢查項的 40%

該評分衡量此收錄條目的完整程度,依據註冊中心條目、端點與公開來源計算得出。它不是對伺服器本身的評價,也不表示伺服器安全或目前可用:可連通性是帶日期的檢查結果,評分也不包含安裝量或使用者評價。

  • 近 30 天內可連通25 分

    請保持端點能夠回應 MCP 握手;SourceWeft 每 30 天探測一次。

  • 已宣告認證方式10 分

    請在註冊中心條目中宣告憑證請求標頭,或保持端點能夠回應探測,以便判定其認證方式。

  • 已列出工具15 分

    請讓伺服器在無憑證時回應 tools/list,或發布可在沙箱中無憑證啟動的軟體包。

  • 描述必需15 分
  • 圖示10 分
  • 文件15 分
  • 授權條款10 分

    請在儲存庫或註冊中心條目中宣告授權條款。