query_exchange_rates
Queries the exchange rate between two currencies using three-letter codes, such as CNY to USD. Returns the conversion rate to convert one currency to another.
Instructions
根据货币的三位字母代码查询两者之间的兑换汇率
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_code | Yes | 您希望转换的货币的三位字母货币代码。如:CNY | |
| to_code | Yes | 您希望转换为目标货币的三位字母货币代码。如:USD |
Implementation Reference
- src/jexchange_mcp_server/server.py:17-47 (handler)The main handler function for the 'query_exchange_rates' tool. It takes from_code and to_code (3-letter currency codes), calls the JUHE exchange API via HTTP POST, and returns the exchange rate result or error message as text content.
@mcp.tool(name="query_exchange_rates", description="根据货币的三位字母代码查询两者之间的兑换汇率") async def query_exchange_rates( from_code: str = Field(description="您希望转换的货币的三位字母货币代码。如:CNY"), to_code : str = Field(description="您希望转换为目标货币的三位字母货币代码。如:USD") ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: """根据城市、地区、区县名称查询当地实时天气预报情况""" url = f"{JUHE_EXCHANGE_API_BASE}/currency" params = { "version": "2", "from": from_code, "to": to_code, "key": JUHE_EXCHANGE_API_KEY } async with httpx.AsyncClient() as client: response = await client.post(url, params=params) data = response.json() if data["error_code"] == 0: result = data["result"] return [ types.TextContent( type="text", text=f"{result}" ) ] else: return [ types.TextContent( type="text", text=f"Error: {data['reason']}" ) ] - src/jexchange_mcp_server/server.py:17-17 (registration)Registration of the tool via @mcp.tool decorator with name='query_exchange_rates' and a description in Chinese.
@mcp.tool(name="query_exchange_rates", description="根据货币的三位字母代码查询两者之间的兑换汇率") - Function signature and pydantic Field descriptions defining the input schema: from_code and to_code are both required strings (3-letter currency codes). The return type is list of TextContent | ImageContent | EmbeddedResource.
async def query_exchange_rates( from_code: str = Field(description="您希望转换的货币的三位字母货币代码。如:CNY"), to_code : str = Field(description="您希望转换为目标货币的三位字母货币代码。如:USD") ) -> list[types.TextContent | types.ImageContent | types.EmbeddedResource]: - Helper constants: API base URL and API key loaded from environment variable JUHE_EXCHANGE_API_KEY.
JUHE_EXCHANGE_API_BASE = "http://op.juhe.cn/onebox/exchange/" JUHE_EXCHANGE_API_KEY = os.environ.get("JUHE_EXCHANGE_API_KEY")