Skip to main content
Glama
kukapay

blocknative-mcp

predict_gas_price

Predict gas prices for blockchain networks to estimate transaction costs. Provides base fee and detailed prediction data in a Markdown table for specified chain IDs.

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
NameRequiredDescriptionDefault
chain_idNo
ctxNo

Implementation Reference

  • The handler function for the 'predict_gas_price' tool. It is decorated with @mcp.tool(), which registers it in the MCP server. Fetches gas prices from Blocknative API using the helper function and formats the response as a Markdown table with base fee and confidence-based predictions.
    @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
  • Helper function to fetch raw gas price data from the Blocknative API, which is called by the predict_gas_price handler to retrieve the necessary data.
    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)}"}
  • The @mcp.tool() decorator registers the predict_gas_price function as an MCP tool with the name derived from the function name.
    @mcp.tool()
  • The docstring provides the input schema description for the tool, including parameters chain_id and ctx.
    """
    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.
    """

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/kukapay/blocknative-mcp'

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