bsv_getPrice
Fetch the current Bitcoin SV (BSV) price in USD to calculate transaction values, monitor market conditions, or convert between BSV and fiat currencies using real-time data from a reliable exchange API.
Instructions
Retrieves the current price of Bitcoin SV (BSV) in USD from a reliable exchange API. This tool provides real-time market data that can be used for calculating transaction values, monitoring market conditions, or converting between BSV and fiat currencies.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| args | No | No parameters required - simply returns the current BSV price in USD |
Implementation Reference
- tools/bsv/getPrice.ts:64-82 (handler)The handler function for the 'bsv_getPrice' tool. It calls getBsvPriceWithCache to fetch the price, formats it, and returns it as text content, handling errors appropriately.async () => { try { const price = await getBsvPriceWithCache(); return { content: [ { type: "text", text: `Current BSV price: $${price.toFixed(2)} USD`, }, ], }; } catch (err) { return { content: [{ type: "text", text: "Error fetching BSV price." }], isError: true, }; } }, );
- tools/bsv/getPrice.ts:57-62 (schema)Zod schema for tool input arguments. Defines an optional empty object, indicating no parameters are required.args: z .object({}) .optional() .describe( "No parameters required - simply returns the current BSV price in USD", ),
- tools/bsv/getPrice.ts:52-83 (registration)The registration function 'registerGetPriceTool' that registers the 'bsv_getPrice' tool on the MCP server, including name, description, schema, and handler.export function registerGetPriceTool(server: McpServer): void { server.tool( "bsv_getPrice", "Retrieves the current price of Bitcoin SV (BSV) in USD from a reliable exchange API. This tool provides real-time market data that can be used for calculating transaction values, monitoring market conditions, or converting between BSV and fiat currencies.", { args: z .object({}) .optional() .describe( "No parameters required - simply returns the current BSV price in USD", ), }, async () => { try { const price = await getBsvPriceWithCache(); return { content: [ { type: "text", text: `Current BSV price: $${price.toFixed(2)} USD`, }, ], }; } catch (err) { return { content: [{ type: "text", text: "Error fetching BSV price." }], isError: true, }; } }, ); }
- tools/bsv/getPrice.ts:14-46 (helper)Helper function that fetches the BSV price from Whatsonchain API with a 5-minute caching mechanism to avoid excessive API calls.async function getBsvPriceWithCache(): Promise<number> { // Return cached price if it's still valid if ( cachedPrice && Date.now() - cachedPrice.timestamp < PRICE_CACHE_DURATION ) { return cachedPrice.value; } // If no valid cache, fetch new price const res = await fetch( "https://api.whatsonchain.com/v1/bsv/main/exchangerate", ); if (!res.ok) throw new Error("Failed to fetch price"); const data = (await res.json()) as { currency: string; rate: string; time: number; }; const price = Number(data.rate); if (Number.isNaN(price) || price <= 0) throw new Error("Invalid price received"); // Update cache cachedPrice = { value: price, timestamp: Date.now(), }; return price; }