Skip to main content
Glama

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
NameRequiredDescriptionDefault
abiYes
addressYes
argsYes
functionNameYes

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() }),
  • 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, } ] } } }, }); };
  • Invocation of the registerWriteContractTools function in the main server setup to register the tool.
    registerWriteContractTools(server);

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/mcpilot'

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