Skip to main content
Glama

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

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()
    }),
  • 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,
                }
              ]
            }
          }
        },
      });
    };
  • Invocation of the registration function in the main server setup file, adding the tool to the MetaMask MCP server.
    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