Skip to main content
Glama

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

Implementation Reference

  • 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, }, ], }; } },
  • 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."),
  • 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, }, ], }; } }, });
  • Invocation of registerWriteContractTools in the main registerTools function, which registers all tools including 'write-contract'.
    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