convert_currency
Convert currencies in real-time using ISO currency codes. Enter source and target currencies along with the amount to get accurate exchange rates and conversion results.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | Amount to convert | |
| from | Yes | Source currency code (e.g., USD, EUR) | |
| to | Yes | Target currency code (e.g., USD, EUR) |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"amount": {
"description": "Amount to convert",
"exclusiveMinimum": 0,
"type": "number"
},
"from": {
"description": "Source currency code (e.g., USD, EUR)",
"maxLength": 3,
"minLength": 3,
"type": "string"
},
"to": {
"description": "Target currency code (e.g., USD, EUR)",
"maxLength": 3,
"minLength": 3,
"type": "string"
}
},
"required": [
"from",
"to",
"amount"
],
"type": "object"
}
Implementation Reference
- src/tools/convert_currency.ts:33-98 (handler)The handler function that fetches exchange rates from Frankfurter API, performs conversion, and returns the result as ToolResponse.export async function convertCurrency( input: ConvertCurrencyInput, ): Promise<ToolResponse> { const { from, to, amount } = input; try { const url = `${FRANKFURTER_API_BASE}/latest?base=${from.toUpperCase()}&symbols=${to.toUpperCase()}`; const response = await fetch(url); if (!response.ok) { return { content: [ { type: "text", text: `Error: Unable to fetch exchange rate. Status: ${response.status}`, }, ], }; } const data = (await response.json()) as ExchangeRateResponse; if (!data.rates || !data.rates[to.toUpperCase()]) { return { content: [ { type: "text", text: `Error: Exchange rate not available for ${from.toUpperCase()} to ${to.toUpperCase()}`, }, ], }; } const rate = data.rates[to.toUpperCase()]; const convertedAmount = Math.round(amount * rate * 100) / 100; const result: ConvertCurrencyOutput = { ...data, conversion: { from: from.toUpperCase(), to: to.toUpperCase(), amount: amount, result: convertedAmount, rate: rate, }, }; return { content: [ { type: "text", text: JSON.stringify(result), }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error: Failed to convert currency - ${error instanceof Error ? error.message : "Unknown error"}`, }, ], }; } }
- src/tools/convert_currency.ts:8-15 (schema)Zod schema defining the input parameters for the convert_currency tool: from (3-letter code), to (3-letter code), amount (positive number).export const convertCurrencySchema = { from: z .string() .length(3) .describe("Source currency code (e.g., USD, EUR)"), to: z.string().length(3).describe("Target currency code (e.g., USD, EUR)"), amount: z.number().positive().describe("Amount to convert"), };
- src/index.ts:25-27 (registration)Registration of the 'convert_currency' tool on the MCP server, linking the schema and handler function.this.server.tool("convert_currency", convertCurrencySchema, async (input) => convertCurrency(input), );