write-contract
Execute write functions on blockchain contracts using ABI, address, function name, and arguments. Enable secure blockchain interactions without exposing private keys via MetaMask integration.
Instructions
Execute a write function on a contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | ||
| address | Yes | ||
| args | Yes | ||
| functionName | Yes |
Implementation Reference
- The execute handler for the 'write-contract' tool. Parses ABI from string, calls writeContract from wagmi with the provided address, functionName, and args, returns transaction hash or handles errors appropriately.execute: async (_args) => { try { const abi = JSON.parse(_args.abi) as Abi const address = _args.abi as Address const functionName = _args.functionName const args = _args.args const result = await writeContract(wagmiConfig, { abi, address, functionName, 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 for input parameters of the write-contract tool: abi (JSON string), address, functionName, and args (array of strings).parameters: z.object({ abi: z.string(), address: z.string(), functionName: z.string(), args: z.string().array() }),
- packages/metamask-mcp/src/tools/write-contract.ts:8-62 (registration)The registration function that adds the 'write-contract' tool to the FastMCP server, including name, description, schema, and handler.export function registerWriteContractTools(server: FastMCP): void { server.addTool({ name: "write-contract", description: "Execute a write function on a contract", parameters: z.object({ abi: z.string(), address: z.string(), functionName: z.string(), args: z.string().array() }), execute: async (_args) => { try { const abi = JSON.parse(_args.abi) as Abi const address = _args.abi as Address const functionName = _args.functionName const args = _args.args const result = await writeContract(wagmiConfig, { abi, address, functionName, 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, } ] } } }, }); };
- packages/metamask-mcp/src/index.ts:61-61 (registration)Invocation of the registerWriteContractTools function in the main server setup to register the tool.registerWriteContractTools(server);