write-contract
Execute write functions on blockchain contracts to modify state or trigger transactions using provided ABI, address, and arguments.
Instructions
Execute a write function on a contract
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| abi | Yes | ||
| address | Yes | ||
| functionName | Yes | ||
| args | Yes |
Implementation Reference
- Handler function that executes the write-contract tool: parses ABI, calls wagmi's writeContract, returns transaction hash or error message.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 defining the input parameters for the write-contract tool: abi (string), address (string), functionName (string), 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)Function that registers the write-contract tool on 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 registration function in the main server setup file, adding the tool to the MetaMask MCP server.registerWriteContractTools(server);