mcp-banka
Version:
Model Context Protocol Server for Turkish Currency and Exchange Rate data from the Central Bank of The Republic of Turkey (TCMB) (TCMB)
158 lines (157 loc) • 5.51 kB
JavaScript
import { z } from "zod";
import { getExchangeRateUrl, tcmbApiClient, parseDateParam } from "./helper.js";
/**
* Today's Exchange Rate Tool Schema
*/
export const todayExchangeRateSchema = {
currencyCode: z.string().optional().describe('The currency code to get the exchange rate for (e.g., USD, EUR, GBP). Rates are provided in TRY (Turkish Lira).')
};
/**
* Today's Exchange Rate Tool Handler
*/
export async function todayExchangeRateHandler({ currencyCode }, extra) {
try {
const result = await tcmbApiClient.getTodayExchangeRate(currencyCode);
console.log(result);
return {
content: [{
type: "text",
text: JSON.stringify(result)
}]
};
}
catch (error) {
console.log(error);
return {
content: [{
type: "text",
text: JSON.stringify({
error: true,
message: error instanceof Error ? error.message : String(error)
})
}]
};
}
}
/**
* Exchange Rate History Tool Schema
*/
export const exchangeRateHistorySchema = {
currencyCode: z.string().describe('The currency code to get the exchange rate for (e.g., USD, EUR, GBP). Rates are provided in TRY (Turkish Lira).'),
date: z.string().describe('The date to get the exchange rate for in YYYY-MM-DD format')
};
/**
* Exchange Rate History Tool Handler
*/
export async function exchangeRateHistoryHandler({ currencyCode, date }, extra) {
try {
const result = await tcmbApiClient.getExchangeRateHistory(currencyCode, date);
return {
content: [{
type: "text",
text: JSON.stringify({
...result,
baseCurrency: "TRY",
source: "Central Bank of The Republic of Turkey (TCMB)"
})
}]
};
}
catch (error) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: true,
message: error instanceof Error ? error.message : String(error)
})
}]
};
}
}
/**
* Get All Currencies Tool Schema
*/
export const getAllCurrenciesSchema = {
date: z.string().optional().describe('Optional: The date to get all currencies for in YYYY-MM-DD format. Defaults to today. All rates are in TRY (Turkish Lira).')
};
/**
* Get All Currencies Tool Handler
*/
export async function getAllCurrenciesHandler({ date }, extra) {
try {
const dateObj = parseDateParam(date);
const data = await tcmbApiClient.fetchDataAsJson(dateObj);
// Extract all currencies with basic info
const tarihDate = data['Tarih_Date'];
const currencies = Array.isArray(tarihDate.Currency)
? tarihDate.Currency
: [tarihDate.Currency];
const result = currencies.map((currency) => ({
code: currency['@Kod'],
name: currency['@CurrencyName'] || currency['Isim'] || '',
forexBuying: currency.ForexBuying,
forexSelling: currency.ForexSelling,
baseCurrency: "TRY"
}));
return {
content: [{
type: "text",
text: JSON.stringify({
currencies: result,
baseCurrency: "TRY",
source: "Central Bank of The Republic of Turkey (TCMB)",
date: tarihDate['@Date'] || tarihDate['@Tarih'] || new Date().toISOString().split('T')[0]
})
}]
};
}
catch (error) {
return {
content: [{
type: "text",
text: JSON.stringify({
error: true,
message: error instanceof Error ? error.message : String(error)
})
}]
};
}
}
/**
* Get Exchange Rate URL Tool Schema
*/
export const getExchangeRateUrlSchema = {
date: z.string().optional().describe('Optional: The date to get the URL for in YYYY-MM-DD format. Defaults to today. Returns URL to TCMB (Central Bank of Turkey) data.')
};
/**
* Get Exchange Rate URL Tool Handler
*/
export async function getExchangeRateUrlHandler({ date }, extra) {
try {
const result = await getExchangeRateUrl(date);
return {
content: [{
type: "text",
text: JSON.stringify(result)
}]
};
}
catch (error) {
// Handle the specific "Maximum call stack size exceeded" error
const errorMessage = error instanceof Error ? error.message : String(error);
const isStackOverflow = errorMessage.includes("Maximum call stack size exceeded");
return {
content: [{
type: "text",
text: JSON.stringify({
error: true,
message: errorMessage,
possibleCause: isStackOverflow ?
"There may be an infinite recursion in the XML parsing logic. Please check the parseXmlNode method in the API client." :
undefined
})
}]
};
}
}