import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import * as exchangeRateService from '../services/exchangeRate.js';
export function registerExchangeRateTools(server: McpServer): void {
// Tool 1: get_exchange_rate
server.tool(
'get_exchange_rate',
'Get the current exchange rate between two currencies',
{
from: z.string().length(3).describe('Source currency code (e.g., EUR, USD, GBP)'),
to: z.string().length(3).describe('Target currency code (e.g., EUR, USD, GBP)')
},
async ({ from, to }) => {
try {
const rate = await exchangeRateService.getRate(from, to);
const result = {
...rate,
message: `1 ${rate.base} = ${rate.rate} ${rate.target}`
};
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
return {
content: [{ type: 'text', text: JSON.stringify({ error: errorMsg }, null, 2) }],
isError: true
};
}
}
);
// Tool 2: convert_currency
server.tool(
'convert_currency',
'Convert an amount from one currency to another',
{
amount: z.number().positive().describe('The amount to convert'),
from: z.string().length(3).describe('Source currency code (e.g., EUR)'),
to: z.string().length(3).describe('Target currency code (e.g., USD)')
},
async ({ amount, from, to }) => {
try {
const conversion = await exchangeRateService.convert(amount, from, to);
const result = {
...conversion,
message: `${amount.toFixed(2)} ${conversion.original.currency} = ${conversion.converted.amount.toFixed(2)} ${conversion.converted.currency}`
};
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
return {
content: [{ type: 'text', text: JSON.stringify({ error: errorMsg }, null, 2) }],
isError: true
};
}
}
);
// Tool 3: get_multiple_rates
server.tool(
'get_multiple_rates',
'Get exchange rates from a base currency to multiple target currencies',
{
base: z.string().length(3).describe('Base currency code (e.g., EUR)'),
targets: z.array(z.string().length(3)).min(1).max(10).describe('Array of target currency codes')
},
async ({ base, targets }) => {
try {
const result = await exchangeRateService.getMultipleRates(base, targets);
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }]
};
} catch (error) {
const errorMsg = error instanceof Error ? error.message : 'Unknown error';
return {
content: [{ type: 'text', text: JSON.stringify({ error: errorMsg }, null, 2) }],
isError: true
};
}
}
);
}