Skip to main content
Glama

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
NameRequiredDescriptionDefault
toYesThe transaction recipient or contract address.
dataNoA contract hashed method call with encoded args.
valueNoValue in wei sent with this transaction.
maxFeePerGasNoTotal fee per gas in wei, inclusive of maxPriorityFeePerGas.
maxPriorityFeePerGasNoMax priority fee per gas in wei.
chainIdNoChain ID to validate against before sending transaction.

Implementation Reference

  • 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, }, ], }; } },
  • 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."), }),
  • 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, }, ], }; } }, });
  • Call to registerEstimateGasTools function as part of the central registerTools, which registers the estimate-gas tool.
    registerEstimateGasTools(server, wagmiConfig);

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/Xiawpohr/metamask-mcp'

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