estimate-gas
Calculate transaction gas costs before execution to optimize fees and prevent failures in MetaMask MCP blockchain operations.
Instructions
Estimate the gas necessary to complete a transaction without submitting it to the network.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| to | Yes | The transaction recipient or contract address. | |
| data | No | A contract hashed method call with encoded args. | |
| value | No | Value in wei sent with this transaction. | |
| maxFeePerGas | No | Total fee per gas in wei, inclusive of maxPriorityFeePerGas. | |
| maxPriorityFeePerGas | No | Max priority fee per gas in wei. | |
| chainId | No | Chain ID to validate against before sending transaction. |
Implementation Reference
- src/tools/estimate-gas.ts:21-55 (handler)Handler function that executes the gas estimation logic using wagmi's estimateGas, returns the estimated gas as JSON, and handles TransactionExecutionError or other errors by returning error messages in MCP content format.execute: async (args) => { try { const result = await estimateGas(wagmiConfig, args); return { content: [ { type: "text", text: JSONStringify({ hash: result, }), }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } },
- src/tools/estimate-gas.ts:13-20 (schema)Zod schema defining the input parameters for the estimate-gas tool including to, data, value, fee parameters, and chainId.parameters: z.object({ to: Address.describe("The transaction recipient or contract address."), data: Address.optional().describe("A contract hashed method call with encoded args."), value: z.coerce.bigint().optional().describe("Value in wei sent with this transaction."), maxFeePerGas: z.coerce.bigint().optional().describe("Total fee per gas in wei, inclusive of maxPriorityFeePerGas."), maxPriorityFeePerGas: z.coerce.bigint().optional().describe("Max priority fee per gas in wei."), chainId: z.coerce.number().optional().describe("Chain ID to validate against before sending transaction."), }),
- src/tools/estimate-gas.ts:10-56 (registration)The server.addTool registration of the 'estimate-gas' tool, defining name, description, input schema, and execute handler.server.addTool({ name: "estimate-gas", description: "Estimate the gas necessary to complete a transaction without submitting it to the network.", parameters: z.object({ to: Address.describe("The transaction recipient or contract address."), data: Address.optional().describe("A contract hashed method call with encoded args."), value: z.coerce.bigint().optional().describe("Value in wei sent with this transaction."), maxFeePerGas: z.coerce.bigint().optional().describe("Total fee per gas in wei, inclusive of maxPriorityFeePerGas."), maxPriorityFeePerGas: z.coerce.bigint().optional().describe("Max priority fee per gas in wei."), chainId: z.coerce.number().optional().describe("Chain ID to validate against before sending transaction."), }), execute: async (args) => { try { const result = await estimateGas(wagmiConfig, args); return { content: [ { type: "text", text: JSONStringify({ hash: result, }), }, ], }; } catch (error) { if (error instanceof TransactionExecutionError) { return { content: [ { type: "text", text: error.cause.message, }, ], }; } return { content: [ { type: "text", text: (error as Error).message, }, ], }; } }, });
- src/tools/register-tools.ts:39-39 (registration)Call to registerEstimateGasTools function as part of the central registerTools, which registers the estimate-gas tool.registerEstimateGasTools(server, wagmiConfig);