write-contract
Execute write functions on smart contracts by specifying the ABI, address, function name, and arguments. Securely interact with blockchain via MetaMask MCP for transaction management.
Instructions
Execute a write function on a contract.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | The contract's ABI. | |
| address | Yes | The contract's address. | |
| args | No | Arguments to pass when calling the contract. | |
| chainId | No | Chain ID to validate against before sending transaction. | |
| functionName | Yes | Function to call on the contract. | |
| maxFeePerGas | No | Total fee per gas in wei, inclusive of maxPriorityFeePerGas. | |
| maxPriorityFeePerGas | No | Max priority fee per gas in wei. | |
| value | No | Value in wei sent with this transaction. |
Implementation Reference
- src/tools/write-contract.ts:23-58 (handler)The main handler function for the 'write-contract' tool. It simulates the contract transaction using simulateContract, then executes writeContract, and returns the transaction hash or error message.execute: async (args) => { try { const { request } = await simulateContract(wagmiConfig, args); const result = await writeContract(wagmiConfig, request); 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/write-contract.ts:12-21 (schema)Zod schema defining the input parameters for the 'write-contract' tool, including ABI, address, function name, args, and gas options.description: "Execute a write function on a contract.", parameters: z.object({ abi: Abi.describe("The contract's ABI."), address: Address.describe("The contract's address."), functionName: z.string().describe("Function to call on the contract."), args: z.unknown().array().optional().describe("Arguments to pass when calling the contract."), 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/write-contract.ts:10-59 (registration)The server.addTool call within registerWriteContractTools that defines and registers the 'write-contract' tool.server.addTool({ name: "write-contract", description: "Execute a write function on a contract.", parameters: z.object({ abi: Abi.describe("The contract's ABI."), address: Address.describe("The contract's address."), functionName: z.string().describe("Function to call on the contract."), args: z.unknown().array().optional().describe("Arguments to pass when calling the contract."), 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 { request } = await simulateContract(wagmiConfig, args); const result = await writeContract(wagmiConfig, request); 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:58-58 (registration)Invocation of registerWriteContractTools in the main registerTools function, which registers all tools including 'write-contract'.registerWriteContractTools(server, wagmiConfig);