@getalby/mcp
Version:
MCP server for controlling a Lightning wallet using Nostr Wallet Connect
58 lines (57 loc) • 1.91 kB
JavaScript
import { l402 } from "@getalby/lightning-tools";
import { z } from "zod";
export function registerFetchL402Tool(server, webln) {
server.registerTool("fetch_l402", {
title: "Fetch L402",
description: "Fetch a paid resource protected by L402",
inputSchema: {
url: z.string().describe("the URL to fetch"),
method: z
.string()
.nullish()
.describe("HTTP request method. Default GET"),
body: z
.string()
.nullish()
.describe("HTTP request body as a string (either plaintext or stringified JSON)"),
},
outputSchema: {
content: z.string().describe("Response content"),
},
}, async (params) => {
const requestOptions = {
method: params.method || undefined,
};
if (params.method &&
params.method !== "GET" &&
params.method !== "HEAD") {
requestOptions.body = params.body;
requestOptions.headers = {
"Content-Type": "application/json",
};
}
const result = await l402.fetchWithL402(params.url, requestOptions, {
webln,
});
const responseContent = await result.text();
if (!result.ok) {
console.error("L402 fetch returned non-OK status", result.status, responseContent);
throw new Error("fetch returned non-OK status: " +
result.status +
" " +
responseContent);
}
const responseData = {
content: responseContent,
};
return {
content: [
{
type: "text",
text: responseContent,
},
],
structuredContent: responseData,
};
});
}