Skip to main content
Glama

write-contract

Execute write functions on smart contracts to modify blockchain state, enabling token transfers, contract interactions, and state updates through MetaMask.

Instructions

Execute a write function on a contract.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
abiYesThe contract's ABI.
addressYesThe contract's address.
functionNameYesFunction to call on the contract.
argsNoArguments to pass when calling the contract.
valueNoValue in wei sent with this transaction.
maxFeePerGasNoTotal fee per gas in wei, inclusive of maxPriorityFeePerGas.
maxPriorityFeePerGasNoMax priority fee per gas in wei.
chainIdNoChain ID to validate against before sending transaction.

Implementation Reference

  • The execute handler for the 'write-contract' tool. Simulates the transaction with simulateContract, executes it with writeContract, returns the tx hash or formatted error.
    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, }, ], }; } },
  • Input schema using Zod for validating parameters like ABI, address, functionName, args, and gas options.
    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."), }),
  • The primary registration function that defines and adds the 'write-contract' tool to the MCP server using server.addTool, including name, description, schema, and handler.
    export function registerWriteContractTools(server: FastMCP, wagmiConfig: Config): void { 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, }, ], }; } }, }); };
  • Top-level registration invocation within the overall registerTools function.
    registerWriteContractTools(server, wagmiConfig);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/Xiawpohr/metamask-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server