write_contract
Interact with smart contracts on BNB Chain by calling state-changing functions. Specify contract address, ABI, function name, arguments, and network to execute write operations securely.
Instructions
Write data to a smart contract by calling a state-changing function
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | The ABI of the smart contract function, as a JSON array | |
| args | Yes | The arguments to pass to the function | |
| contractAddress | Yes | The address of the smart contract to interact with | |
| functionName | Yes | The name of the function to call on the contract | |
| network | No | Network name (e.g. 'bsc', 'opbnb', 'ethereum', 'base', etc.) or chain ID. Supports others main popular networks. Defaults to BSC mainnet. | bsc |
| privateKey | No | Private key of the sending account. Used only for transaction signing. | 0x5a2b7e4d9c8f1a3e6b0d2c5f4e3d2a1b0c9f8e7d6a5b4c3d2e1f0a9b8c7d6e5f4 |
Implementation Reference
- Complete registration of the 'write_contract' MCP tool, including description, Zod input schema, and the async handler function that parses ABI, invokes the underlying writeContract service using the provided private key, and returns the transaction hash or formatted error.server.tool( "write_contract", "Write data to a smart contract by calling a state-changing function", { contractAddress: z .string() .describe("The address of the smart contract to interact with"), abi: z .array(z.any()) .describe("The ABI of the smart contract function, as a JSON array"), functionName: z .string() .describe("The name of the function to call on the contract"), args: z.array(z.any()).describe("The arguments to pass to the function"), privateKey: z .string() .describe( "Private key of the sending account. Used only for transaction signing." ) .default(process.env.PRIVATE_KEY as string), network: defaultNetworkParam }, async ({ contractAddress, abi, functionName, args, privateKey, network = "bsc" }) => { try { // Parse ABI if it's a string const parsedAbi = typeof abi === "string" ? JSON.parse(abi) : abi const contractParams: Record<string, any> = { address: contractAddress as Address, abi: parsedAbi, functionName, args } const txHash = await services.writeContract( privateKey as Hex, contractParams, network ) return mcpToolRes.success({ contractAddress, functionName, args, transactionHash: txHash, message: "Contract write transaction sent successfully" }) } catch (error) { return mcpToolRes.error(error, "writing to contract") } } )
- src/evm/services/contracts.ts:26-33 (helper)Helper service function 'writeContract' that creates a wallet client from private key and network, then calls viem's writeContract to execute the transaction and return the hash.export async function writeContract( privateKey: Hex, params: Record<string, any>, network = "ethereum" ): Promise<Hash> { const client = getWalletClient(privateKey, network) return await client.writeContract(params as any) }
- src/evm/modules/contracts/index.ts:6-9 (registration)Module-level registration function that calls registerContractTools(server), thereby registering the 'write_contract' tool among others.export function registerContracts(server: McpServer) { registerContractPrompts(server) registerContractTools(server) }
- src/evm/index.ts:13-13 (registration)EVM module registration that includes the contracts module, invoking the chain of registrations for 'write_contract' tool.registerContracts(server)