Skip to main content
Glama
twelvedata

Twelve Data MCP Server

Official
by twelvedata

GetExchangeRate

Retrieve real-time or historical exchange rates for currency pairs including forex and cryptocurrencies. Specify date, format, and timezone for customized data.

Instructions

This API call returns real-time exchange rate for currency pair. Works with forex and cryptocurrency.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
paramsYes

Implementation Reference

  • Core handler function used by all generated tools, including GetExchangeRate, to call the Twelve Data API endpoint /exchange_rate.
    async def _call_endpoint( endpoint: str, params: P, response_model: Type[R], ctx: Context ) -> R: params.apikey = extract_twelve_data_apikey( twelve_data_apikey=twelve_data_apikey, transport=transport, ctx=ctx ) params_dict = params.model_dump(exclude_none=True) resolved_endpoint = resolve_path_params(endpoint, params_dict) async with httpx.AsyncClient( trust_env=False, headers={ "accept": "application/json", "user-agent": "python-httpx/0.24.0" }, ) as client: resp = await client.get( f"{api_base}/{resolved_endpoint}", params=params_dict ) resp.raise_for_status() resp_json = resp.json() if isinstance(resp_json, dict): status = resp_json.get("status") if status == "error": code = resp_json.get('code') raise HTTPException( status_code=code, detail=f"Failed to perform request," f" code = {code}, message = {resp_json.get('message')}" ) return response_model.model_validate(resp_json)
  • Calls register_all_tools which registers the GetExchangeRate tool among others.
    if vector_db_exists(): register_all_tools(server=server, _call_endpoint=_call_endpoint) u_tool = register_u_tool(
  • Alternative registration path without u_tool, registers and limits the number of tools including GetExchangeRate.
    register_all_tools(server=server, _call_endpoint=_call_endpoint) all_tools = server._tool_manager._tools
  • Script that generates the tools.py file containing the specific handler, schema imports, and registration for GetExchangeRate based on OpenAPI spec.
    def main(): spec = load_openapi_spec(OPENAPI_PATH) csv_paths = load_csv_paths(ENDPOINTS_PATH) all_spec_paths = list(spec.get("paths", {}).keys()) extra_paths = sorted(set(all_spec_paths) - set(csv_paths)) final_paths = csv_paths + extra_paths ops = collect_operations(final_paths, spec) total = len(ops) from_csv = len([op for op in ops if '/' + op[2] in csv_paths]) from_extra = total - from_csv print(f"[INFO] Loaded {len(csv_paths)} paths from CSV.") print(f"[INFO] Found {len(all_spec_paths)} paths in OpenAPI spec.") print(f"[INFO] Added {from_extra} additional paths not listed in CSV.") print(f"[INFO] Generated {total} tools in total.") code = '# AUTOGENERATED FILE - DO NOT EDIT MANUALLY\n\n' + generate_code(ops) Path(OUTPUT_PATH).write_text(code, encoding='utf-8') print(f"[SUCCESS] File written to: {OUTPUT_PATH}") if __name__ == '__main__': main()
  • Defines the /exchange_rate endpoint in the core_data group used for splitting OpenAPI spec for tool generation.
    "core_data": [ "/time_series", "/time_series/cross", "/exchange_rate", "/currency_conversion",

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/twelvedata/mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server