get-crypto-price
Retrieve current price and 24-hour market statistics for a cryptocurrency by providing its symbol (e.g., BTC, ETH).
Instructions
Get current price and 24h stats for a cryptocurrency
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| symbol | Yes | Cryptocurrency symbol (e.g., BTC, ETH) |
Implementation Reference
- src/tools/price.ts:5-7 (schema)Zod schema for get-crypto-price input validation: requires a 'symbol' string field
export const GetPriceArgumentsSchema = z.object({ symbol: z.string().min(1), }); - src/tools/price.ts:9-38 (handler)handleGetPrice function: parses args, fetches assets from CoinCap API via getAssets(), finds the asset by symbol, and returns formatted price info
export async function handleGetPrice(args: unknown) { const { symbol } = GetPriceArgumentsSchema.parse(args); const upperSymbol = symbol.toUpperCase(); const assetsData = await getAssets(); if (!assetsData) { return { content: [{ type: "text", text: "Failed to retrieve cryptocurrency data" }], }; } const asset = assetsData.data.find( (a: { symbol: string; }) => a.symbol.toUpperCase() === upperSymbol ); if (!asset) { return { content: [ { type: "text", text: `Could not find cryptocurrency with symbol ${upperSymbol}`, }, ], }; } return { content: [{ type: "text", text: formatPriceInfo(asset) }], }; } - src/index.ts:30-44 (registration)Tool registration for 'get-crypto-price': defines name, description, and inputSchema requiring a 'symbol' string parameter
tools: [ { name: "get-crypto-price", description: "Get current price and 24h stats for a cryptocurrency", inputSchema: { type: "object", properties: { symbol: { type: "string", description: "Cryptocurrency symbol (e.g., BTC, ETH)", }, }, required: ["symbol"], }, }, - src/index.ts:92-94 (registration)Call handler routing: dispatches 'get-crypto-price' tool calls to handleGetPrice
switch (name) { case "get-crypto-price": return await handleGetPrice(args); - src/services/formatters.ts:3-17 (helper)formatPriceInfo helper: formats asset data (price, 24h change, volume, market cap, rank) into a human-readable string
export function formatPriceInfo(asset: CryptoAsset): string { const price = parseFloat(asset.priceUsd).toFixed(2); const change = parseFloat(asset.changePercent24Hr).toFixed(2); const volume = (parseFloat(asset.volumeUsd24Hr) / 1000000).toFixed(2); const marketCap = (parseFloat(asset.marketCapUsd) / 1000000000).toFixed(2); return [ `${asset.name} (${asset.symbol})`, `Price: $${price}`, `24h Change: ${change}%`, `24h Volume: $${volume}M`, `Market Cap: $${marketCap}B`, `Rank: #${asset.rank}`, ].join('\n'); }