get_recent_trades
Retrieve recent trade data for specific trading pairs in a formatted Markdown table, including trade details like price, quantity, and time.
Instructions
Fetch recent trades data from Aster Finance API and return as Markdown table text.
Parameters:
symbol (str): Trading pair symbol (e.g., 'BTCUSDT', 'ETHUSDT'). Case-insensitive.
limit (Optional[int]): Number of trades to return (1 to 1000). If None, defaults to 500.
Returns:
str: Markdown table containing tradeId, price, qty, quoteQty, time, and isBuyerMaker.
Raises:
Exception: If the API request fails or data processing encounters an error.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | ||
| symbol | Yes |
Implementation Reference
- main.py:626-685 (handler)The handler function for the 'get_recent_trades' tool. It fetches recent trades from the Aster Finance API endpoint '/fapi/v1/trades', processes the data using pandas into a formatted Markdown table, and handles errors appropriately.@mcp.tool() async def get_recent_trades( symbol: str, limit: Optional[int] = None ) -> str: """ Fetch recent trades data from Aster Finance API and return as Markdown table text. Parameters: symbol (str): Trading pair symbol (e.g., 'BTCUSDT', 'ETHUSDT'). Case-insensitive. limit (Optional[int]): Number of trades to return (1 to 1000). If None, defaults to 500. Returns: str: Markdown table containing tradeId, price, qty, quoteQty, time, and isBuyerMaker. Raises: Exception: If the API request fails or data processing encounters an error. """ endpoint = "/fapi/v1/trades" # Construct query parameters params = { "symbol": symbol.upper(), # Ensure symbol is uppercase (e.g., BTCUSDT) } if limit is not None: params["limit"] = limit async with httpx.AsyncClient() as client: try: # Make GET request to the API response = await client.get(f"{BASE_URL}{endpoint}", params=params) response.raise_for_status() # Raise exception for 4xx/5xx errors # Parse JSON response trades_data = response.json() # Create pandas DataFrame df = pd.DataFrame(trades_data) # Convert time to readable format df["time"] = pd.to_datetime(df["time"], unit="ms") # Select relevant columns and format numbers df = df[["id", "price", "qty", "quoteQty", "time", "isBuyerMaker"]] df = df.rename(columns={"id": "tradeId"}) # Rename id to tradeId for clarity df["price"] = df["price"].astype(float).round(8) df["qty"] = df["qty"].astype(float).round(8) df["quoteQty"] = df["quoteQty"].astype(float).round(8) # Convert DataFrame to Markdown table markdown_table = df.to_markdown(index=False) return markdown_table except httpx.HTTPStatusError as e: # Handle HTTP errors (e.g., 400, 429) raise Exception(f"API request failed: {e.response.status_code} - {e.response.text}") except Exception as e: # Handle other errors (e.g., network issues, pandas errors) raise Exception(f"Error processing recent trades data: {str(e)}")
- main.py:627-627 (registration)The @mcp.tool() decorator registers the get_recent_trades function as an MCP tool.async def get_recent_trades(