predict_gas_price
Estimate gas prices for a specific blockchain network, providing base fee and detailed predictions in a Markdown table. Supports customization by chain ID for accurate fee forecasts.
Instructions
Predict gas prices for a specified chain, including base fee and detailed prediction data in a Markdown table.
Parameters:
- chain_id (int): The ID of the blockchain network (e.g., 1 for Ethereum Mainnet). Default: 1.
- ctx (Optional[Context]): The MCP context object. Default: None.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| chain_id | No | ||
| ctx | No |
Implementation Reference
- src/blocknative_mcp/cli.py:63-97 (handler)The handler function decorated with @mcp.tool(), implementing the logic to fetch and format gas price predictions from Blocknative API into a Markdown table.@mcp.tool() async def predict_gas_price(chain_id: int = 1, ctx: Optional[Context] = None) -> str: """ Predict gas prices for a specified chain, including base fee and detailed prediction data in a Markdown table. Parameters: - chain_id (int): The ID of the blockchain network (e.g., 1 for Ethereum Mainnet). Default: 1. - ctx (Optional[Context]): The MCP context object. Default: None. """ data = await fetch_gas_prices(chain_id) if "error" in data: return data["error"] # Format base fee and metadata base_fee = data["baseFeePerGas"] output = f"Gas Price Predictions for Chain ID {chain_id} ({data['system']}/{data['network']}):\n" output += f"- Base Fee Per Gas: {base_fee} Gwei\n\n" # Format predictions as a Markdown table predictions = data["estimatedPrices"] if not predictions: output += "No prediction data available." else: output += "| Confidence | Price (Gwei) | Max Priority Fee (Gwei) | Max Fee (Gwei) |\n" output += "|------------|--------------|-------------------------|----------------|\n" for price in predictions: confidence = price.get("confidence", 0) price_value = price.get("price", 0) max_priority_fee = price.get("maxPriorityFeePerGas", 0) max_fee = price.get("maxFeePerGas", 0) output += ( f"| {confidence}% | {price_value} | {max_priority_fee} | {max_fee} |\n" ) return output
- src/blocknative_mcp/cli.py:19-48 (helper)Supporting utility function that fetches raw gas price data from the Blocknative API, called by the predict_gas_price handler.async def fetch_gas_prices(chain_id: int = 1) -> Dict: """Fetch gas price predictions from Blocknative API for a given chain.""" try: headers = {"Authorization": BLOCKNATIVE_API_KEY} if BLOCKNATIVE_API_KEY else {} async with httpx.AsyncClient() as client: response = await client.get( BLOCKNATIVE_GAS_API_URL, headers=headers, params={"chainid": chain_id} ) response.raise_for_status() data = response.json() # Extract relevant fields from the response block_prices = data.get("blockPrices", []) if not block_prices: return {"error": "No block prices found in API response"} # Use the first block's data first_block = block_prices[0] return { "baseFeePerGas": first_block.get("baseFeePerGas", 0), "estimatedPrices": first_block.get("estimatedPrices", []), "unit": data.get("unit", "gwei"), "system": data.get("system", "unknown"), "network": data.get("network", "unknown") } except httpx.HTTPError as e: return {"error": f"Failed to fetch gas prices for chain ID {chain_id}: {str(e)}"}
- src/blocknative_mcp/cli.py:65-71 (schema)Docstring defining the input parameters and description, serving as the tool schema for MCP.""" Predict gas prices for a specified chain, including base fee and detailed prediction data in a Markdown table. Parameters: - chain_id (int): The ID of the blockchain network (e.g., 1 for Ethereum Mainnet). Default: 1. - ctx (Optional[Context]): The MCP context object. Default: None. """