get_order_book
Retrieve order book data for specified trading pairs from Aster Finance API, formatted as a Markdown table. Input the symbol and optional limit to display bid/ask prices and quantities.
Instructions
Fetch order book 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 order book entries to return (5, 10, 20, 50, 100, 500, 1000, 5000).
If None, defaults to 100.
Returns:
str: Markdown table containing side (bid or ask), price, and quantity.
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:554-623 (handler)The handler function for the 'get_order_book' tool. It is decorated with @mcp.tool() which registers it in the FastMCP server. Fetches order book depth data from the Aster Finance API, processes bids and asks into a pandas DataFrame sorted by side and price, formats it, and returns a Markdown table.@mcp.tool() async def get_order_book( symbol: str, limit: Optional[int] = None ) -> str: """ Fetch order book 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 order book entries to return (5, 10, 20, 50, 100, 500, 1000, 5000). If None, defaults to 100. Returns: str: Markdown table containing side (bid or ask), price, and quantity. Raises: Exception: If the API request fails or data processing encounters an error. """ endpoint = "/fapi/v1/depth" # 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 order_book_data = response.json() # Extract bids and asks bids = order_book_data.get("bids", []) asks = order_book_data.get("asks", []) # Create DataFrames for bids and asks bids_df = pd.DataFrame(bids, columns=["price", "quantity"]) bids_df["side"] = "bid" asks_df = pd.DataFrame(asks, columns=["price", "quantity"]) asks_df["side"] = "ask" # Combine bids and asks, sorting by price (descending for bids, ascending for asks) df = pd.concat([bids_df, asks_df], ignore_index=True) df["price"] = df["price"].astype(float) df = df.sort_values(by=["side", "price"], ascending=[True, False]) # Format numbers df["price"] = df["price"].round(8) df["quantity"] = df["quantity"].astype(float).round(8) # Reorder columns df = df[["side", "price", "quantity"]] # 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 order book data: {str(e)}")
- main.py:555-572 (schema)The function signature and docstring define the input schema (symbol: str required, limit: Optional[int]=None) and output (str: Markdown table). FastMCP uses this for tool schema generation.async def get_order_book( symbol: str, limit: Optional[int] = None ) -> str: """ Fetch order book 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 order book entries to return (5, 10, 20, 50, 100, 500, 1000, 5000). If None, defaults to 100. Returns: str: Markdown table containing side (bid or ask), price, and quantity. Raises: Exception: If the API request fails or data processing encounters an error. """
- main.py:554-554 (registration)The @mcp.tool() decorator registers the get_order_book function as an MCP tool with the FastMCP server instance 'mcp'.@mcp.tool()