Convert currency
convertConverts an amount from one currency to another using up-to-date exchange rates. Accepts ISO 4217 codes for 170+ fiat currencies and major cryptocurrencies.
Instructions
Convert an amount from one currency to another using the latest exchange rate. Codes are ISO 4217 (e.g. USD, EUR, GBP). Supports 170+ fiat currencies and major cryptocurrencies.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from | Yes | Source currency code, e.g. 'USD' | |
| to | Yes | Target currency code, e.g. 'EUR' | |
| amount | Yes | Amount in the source currency |
Implementation Reference
- src/index.ts:60-71 (handler)The tool handler function for 'convert'. Executes the conversion logic by calling client.convert() and returns the formatted result.
async ({ from, to, amount }) => { try { const result = await client.convert(from, to, amount); return ok( `${amount} ${from.toUpperCase()} = ${result} ${to.toUpperCase()}`, { from: from.toUpperCase(), to: to.toUpperCase(), amount, result }, ); } catch (err) { return fail(err); } }, ); - src/index.ts:48-58 (schema)Input schema and metadata for the 'convert' tool, defining 'from' (source currency), 'to' (target currency), and 'amount' parameters with Zod validation.
{ title: "Convert currency", description: "Convert an amount from one currency to another using the latest exchange rate. " + "Codes are ISO 4217 (e.g. USD, EUR, GBP). Supports 170+ fiat currencies and major cryptocurrencies.", inputSchema: { from: z.string().min(3).max(10).describe("Source currency code, e.g. 'USD'"), to: z.string().min(3).max(10).describe("Target currency code, e.g. 'EUR'"), amount: z.number().positive().describe("Amount in the source currency"), }, annotations: { readOnlyHint: true, openWorldHint: true }, - src/index.ts:46-71 (registration)Registration of the 'convert' tool via server.registerTool(), binding the name 'convert', the input schema, and the handler function together.
server.registerTool( "convert", { title: "Convert currency", description: "Convert an amount from one currency to another using the latest exchange rate. " + "Codes are ISO 4217 (e.g. USD, EUR, GBP). Supports 170+ fiat currencies and major cryptocurrencies.", inputSchema: { from: z.string().min(3).max(10).describe("Source currency code, e.g. 'USD'"), to: z.string().min(3).max(10).describe("Target currency code, e.g. 'EUR'"), amount: z.number().positive().describe("Amount in the source currency"), }, annotations: { readOnlyHint: true, openWorldHint: true }, }, async ({ from, to, amount }) => { try { const result = await client.convert(from, to, amount); return ok( `${amount} ${from.toUpperCase()} = ${result} ${to.toUpperCase()}`, { from: from.toUpperCase(), to: to.toUpperCase(), amount, result }, ); } catch (err) { return fail(err); } }, ); - src/client.ts:173-180 (helper)The client.convert() method that performs the actual API call to /api/convert with the from, to, and amount parameters, returning the parsed numeric result.
async convert(from: string, to: string, amount: number): Promise<number> { const data = await this.request<{ result: string }>("/api/convert", { from: from.toUpperCase(), to: to.toUpperCase(), amount, }); return parseFloat(data.result); }