estimate_gas
Calculate gas fees for transactions on supported blockchain networks like BSC, Ethereum, and more using recipient address, value, and data inputs. Defaults to BSC mainnet.
Instructions
Estimate the gas cost for a transaction
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | No | The transaction data as a hex string | |
| network | No | Network name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet. | bsc |
| to | Yes | The recipient address | |
| value | No | The amount of ETH to send in ether (e.g., '0.1') |
Implementation Reference
- The main handler function for the 'estimate_gas' MCP tool. It processes input parameters, constructs the transaction params, calls the services.estimateGas helper, and returns the estimated gas in a standardized MCP response format.async ({ to, value, data, network }) => { try { const params: any = { to: to as Address } if (value) { params.value = services.helpers.parseEther(value) } if (data) { params.data = data as `0x${string}` } const gas = await services.estimateGas(params, network) return mcpToolRes.success({ network, estimatedGas: gas.toString() }) } catch (error) { return mcpToolRes.error(error, "estimating gas") } }
- Zod schema defining the input parameters for the 'estimate_gas' tool: 'to' (required address), 'value' (optional ETH amount), 'data' (optional hex data), 'network' (chain identifier).{ to: z.string().describe("The recipient address"), value: z .string() .optional() .describe("The amount of ETH to send in ether (e.g., '0.1')"), data: z .string() .optional() .describe("The transaction data as a hex string"), network: defaultNetworkParam },
- src/evm/modules/transactions/tools.ts:31-68 (registration)The registration of the 'estimate_gas' tool on the MCP server within registerTransactionTools, including name, description, input schema, and handler function.server.tool( "estimate_gas", "Estimate the gas cost for a transaction", { to: z.string().describe("The recipient address"), value: z .string() .optional() .describe("The amount of ETH to send in ether (e.g., '0.1')"), data: z .string() .optional() .describe("The transaction data as a hex string"), network: defaultNetworkParam }, async ({ to, value, data, network }) => { try { const params: any = { to: to as Address } if (value) { params.value = services.helpers.parseEther(value) } if (data) { params.data = data as `0x${string}` } const gas = await services.estimateGas(params, network) return mcpToolRes.success({ network, estimatedGas: gas.toString() }) } catch (error) { return mcpToolRes.error(error, "estimating gas") } } )
- Supporting helper function that performs the actual gas estimation using the viem public client for the specified network and transaction parameters.export async function estimateGas( params: EstimateGasParameters, network = "ethereum" ): Promise<bigint> { const client = getPublicClient(network) return await client.estimateGas(params) }