prepareTransaction
Prepare ETH transfer transactions for signing by specifying sender, recipient, amount, and gas parameters. Generates transaction data ready for blockchain submission.
Instructions
Prepare a basic ETH transfer transaction for signing. Returns transaction data that can be signed and broadcast.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| toAddress | Yes | The Ethereum address to send ETH to | |
| value | Yes | The amount to send in ETH (e.g., '1.0', '0.5') | |
| fromAddress | Yes | The Ethereum address sending the ETH | |
| provider | No | Optional. 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. | |
| chainId | No | Optional. The chain ID to use. | |
| gasLimit | No | Optional. The gas limit for the transaction | |
| gasPrice | No | Optional. The gas price (in gwei) for legacy transactions | |
| maxFeePerGas | No | Optional. The maximum fee per gas (in gwei) for EIP-1559 transactions | |
| maxPriorityFeePerGas | No | Optional. The maximum priority fee per gas (in gwei) for EIP-1559 transactions |
Implementation Reference
- src/tools/core.ts:1085-1169 (registration)Registration of the MCP tool 'prepareTransaction' using server.tool(). This includes the tool name, description, input schema, and the handler function that executes the tool logic by preparing an ETH transfer transaction via ethersService.// Prepare Transaction tool (ETH transfers) server.tool( "prepareTransaction", "Prepare a basic ETH transfer transaction for signing. Returns transaction data that can be signed and broadcast.", { toAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address to send ETH to" ), value: z.string().describe( "The amount to send in ETH (e.g., '1.0', '0.5')" ), fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address sending the ETH" ), 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 ({ toAddress, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => { try { // Prepare gas options const options = { gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }; const txRequest = await ethersService.prepareTransaction( toAddress, value, fromAddress, provider, chainId, options ); return { content: [{ type: "text", text: `ETH Transfer Transaction Prepared: From: ${fromAddress} To: ${toAddress} Amount: ${value} ETH Transaction Data: ${JSON.stringify({ to: txRequest.to, 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 transaction: ${error instanceof Error ? error.message : String(error)}` }] }; } } );
- src/tools/core.ts:1116-1168 (handler)Handler function for the 'prepareTransaction' tool. It constructs gas options and calls ethersService.prepareTransaction to generate the unsigned transaction data, then formats and returns it.async ({ toAddress, value, fromAddress, provider, chainId, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }) => { try { // Prepare gas options const options = { gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas }; const txRequest = await ethersService.prepareTransaction( toAddress, value, fromAddress, provider, chainId, options ); return { content: [{ type: "text", text: `ETH Transfer Transaction Prepared: From: ${fromAddress} To: ${toAddress} Amount: ${value} ETH Transaction Data: ${JSON.stringify({ to: txRequest.to, 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 transaction: ${error instanceof Error ? error.message : String(error)}` }] }; } }
- src/tools/core.ts:1089-1115 (schema)Input schema for the 'prepareTransaction' tool defined using Zod validators for parameters like toAddress, value, fromAddress, provider, chainId, and gas options.{ toAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address to send ETH to" ), value: z.string().describe( "The amount to send in ETH (e.g., '1.0', '0.5')" ), fromAddress: z.string().regex(/^0x[a-fA-F0-9]{40}$/).describe( "The Ethereum address sending the ETH" ), 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" ) },