Skip to main content
Glama

prepareContractTransaction

Prepare smart contract interaction transactions for signing by encoding function calls and setting transaction parameters like gas and value.

Instructions

Prepare a smart contract interaction transaction for signing. Returns transaction data that can be signed and broadcast.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contractAddressYesThe smart contract address to interact with
dataYesThe contract interaction data (encoded function call) as hex string
valueNoOptional. The amount of ETH to send with the transaction (default: '0')0
fromAddressYesThe Ethereum address sending the transaction
providerNoOptional. Either a network name or custom RPC URL. Use getAllNetworks to see available networks and their details, or getNetwork to get info about a specific network. You can use any network name returned by these tools as a provider value.
chainIdNoOptional. The chain ID to use.
gasLimitNoOptional. The gas limit for the transaction
gasPriceNoOptional. The gas price (in gwei) for legacy transactions
maxFeePerGasNoOptional. The maximum fee per gas (in gwei) for EIP-1559 transactions
maxPriorityFeePerGasNoOptional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions

Implementation Reference

  • Registration of the 'prepareContractTransaction' MCP tool, including input schema and the handler function that prepares the contract transaction using ethersService and returns formatted response.
      // Prepare Contract Transaction tool
      server.tool(
        "prepareContractTransaction", 
        "Prepare a smart contract interaction transaction for signing. Returns transaction data that can be signed and broadcast.",
        {
          contractAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe(
            "The smart contract address to interact with"
          ),
          data: z.string().regex(/^0x[a-fA-F0-9]*$/).describe(
            "The contract interaction data (encoded function call) as hex string"
          ),
          value: z.string().optional().default("0").describe(
            "Optional. The amount of ETH to send with the transaction (default: '0')"
          ),
          fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe(
            "The Ethereum address sending the transaction"
          ),
          provider: z.string().optional().describe(PROVIDER_DESCRIPTION),
          chainId: z.number().optional().describe(
            "Optional. The chain ID to use."
          ),
          gasLimit: z.string().optional().describe(
            "Optional. The gas limit for the transaction"
          ),
          gasPrice: z.string().optional().describe(
            "Optional. The gas price (in gwei) for legacy transactions"
          ),
          maxFeePerGas: z.string().optional().describe(
            "Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions"
          ),
          maxPriorityFeePerGas: z.string().optional().describe(
            "Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions"
          )
        },
        async ({ contractAddress, data, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => {
          try {
            // Prepare gas options
            const options = {
              gasLimit,
              gasPrice,
              maxFeePerGas,
              maxPriorityFeePerGas
            };
            
            const txRequest = await ethersService.prepareContractTransaction(
              contractAddress,
              data,
              value || "0",
              fromAddress,
              provider,
              chainId,
              options
            );
            
            return {
              content: [{ 
                type: "text", 
                text: `Contract Transaction Prepared:
    
    Contract: ${contractAddress}
    From: ${fromAddress}
    Value: ${value || "0"} ETH
    Data: ${data}
    
    Transaction Data:
    ${JSON.stringify({
      to: txRequest.to,
      data: txRequest.data,
      value: txRequest.value?.toString(),
      from: txRequest.from,
      gasLimit: txRequest.gasLimit?.toString(),
      gasPrice: txRequest.gasPrice?.toString(),
      maxFeePerGas: txRequest.maxFeePerGas?.toString(),
      maxPriorityFeePerGas: txRequest.maxPriorityFeePerGas?.toString(),
      chainId: txRequest.chainId
    }, null, 2)}
    
    This transaction is ready to be signed and broadcast.`
              }]
            };
          } catch (error) {
            return {
              isError: true,
              content: [{ 
                type: "text", 
                text: `Error preparing contract transaction: ${error instanceof Error ? error.message : String(error)}`
              }]
            };
          }
        }
      );
  • The MCP tool handler function for 'prepareContractTransaction' that validates inputs, calls ethersService helper, formats the transaction data, and returns a text response.
        async ({ contractAddress, data, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => {
          try {
            // Prepare gas options
            const options = {
              gasLimit,
              gasPrice,
              maxFeePerGas,
              maxPriorityFeePerGas
            };
            
            const txRequest = await ethersService.prepareContractTransaction(
              contractAddress,
              data,
              value || "0",
              fromAddress,
              provider,
              chainId,
              options
            );
            
            return {
              content: [{ 
                type: "text", 
                text: `Contract Transaction Prepared:
    
    Contract: ${contractAddress}
    From: ${fromAddress}
    Value: ${value || "0"} ETH
    Data: ${data}
    
    Transaction Data:
    ${JSON.stringify({
      to: txRequest.to,
      data: txRequest.data,
      value: txRequest.value?.toString(),
      from: txRequest.from,
      gasLimit: txRequest.gasLimit?.toString(),
      gasPrice: txRequest.gasPrice?.toString(),
      maxFeePerGas: txRequest.maxFeePerGas?.toString(),
      maxPriorityFeePerGas: txRequest.maxPriorityFeePerGas?.toString(),
      chainId: txRequest.chainId
    }, null, 2)}
    
    This transaction is ready to be signed and broadcast.`
              }]
            };
          } catch (error) {
            return {
              isError: true,
              content: [{ 
                type: "text", 
                text: `Error preparing contract transaction: ${error instanceof Error ? error.message : String(error)}`
              }]
            };
          }
        }
  • Zod schema for input parameters of the 'prepareContractTransaction' tool.
    {
      contractAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe(
        "The smart contract address to interact with"
      ),
      data: z.string().regex(/^0x[a-fA-F0-9]*$/).describe(
        "The contract interaction data (encoded function call) as hex string"
      ),
      value: z.string().optional().default("0").describe(
        "Optional. The amount of ETH to send with the transaction (default: '0')"
      ),
      fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe(
        "The Ethereum address sending the transaction"
      ),
      provider: z.string().optional().describe(PROVIDER_DESCRIPTION),
      chainId: z.number().optional().describe(
        "Optional. The chain ID to use."
      ),
      gasLimit: z.string().optional().describe(
        "Optional. The gas limit for the transaction"
      ),
      gasPrice: z.string().optional().describe(
        "Optional. The gas price (in gwei) for legacy transactions"
      ),
      maxFeePerGas: z.string().optional().describe(
        "Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions"
      ),
      maxPriorityFeePerGas: z.string().optional().describe(
        "Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions"
      )
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. While it states the tool 'returns transaction data that can be signed and broadcast,' it doesn't address critical behavioral aspects: whether this is a read-only operation (likely, since it only prepares), what happens if parameters are invalid, whether it performs any validation of contract addresses or data, or if there are rate limits. For a tool with 10 parameters and no annotation coverage, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is perfectly concise: two clear sentences that state the action and outcome without unnecessary words. It's front-loaded with the core purpose and wastes no space on repetition or irrelevant details. Every sentence earns its place by conveying essential information efficiently.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a tool with 10 parameters, no annotations, and no output schema, the description is insufficiently complete. It doesn't explain what the returned transaction data contains, how to use it with signing/broadcasting tools, error conditions, or dependencies on other tools (like getAllNetworks for provider values). The context signals indicate high complexity that isn't adequately addressed by the brief description.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds no parameter-specific information beyond what's already in the schema (which has 100% coverage). It doesn't explain relationships between parameters (e.g., gasPrice vs maxFeePerGas for different transaction types), provide examples, or clarify when certain parameters are needed. With complete schema coverage, the baseline is 3, but the description doesn't enhance understanding of parameter usage or interactions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Prepare a smart contract interaction transaction for signing.' It specifies the action (prepare), resource (smart contract interaction transaction), and outcome (returns transaction data for signing/broadcasting). However, it doesn't explicitly differentiate from sibling tools like 'prepareTransaction' or 'prepareERC20Transfer', which appear to serve similar preparation functions for different transaction types.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With multiple 'prepare*' sibling tools (prepareTransaction, prepareERC20Transfer, prepareERC721Transfer, etc.), there's no indication whether this is a general-purpose contract interaction tool or when to choose it over more specific preparation tools. The description mentions the outcome but not the appropriate context or prerequisites.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/crazyrabbitLTC/mcp-ethers-server'

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