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);
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden but fails to disclose any behavioral traits. It doesn't mention that this is a state-changing operation requiring authorization, gas fees, or network interaction, nor does it describe potential side effects, error handling, or return values. This is inadequate for a tool with significant implications like contract writes.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise with a single sentence: 'Execute a write function on a contract.' It is front-loaded and wastes no words, though this brevity contributes to its lack of detail. Every word earns its place by stating the core action, but it may be overly terse for such a complex tool.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness1/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 parameters, no annotations, no output schema, and significant behavioral implications like blockchain transactions), the description is severely incomplete. It fails to explain what the tool returns, error conditions, gas or fee implications, or how it differs from siblings, leaving critical gaps for safe and effective use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, providing clear parameter documentation in the schema itself (e.g., 'The contract's ABI,' 'Value in wei sent with this transaction'). The description adds no additional meaning beyond the schema, but the high coverage justifies a baseline score of 3, as the schema does the heavy lifting for parameter understanding.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Execute a write function on a contract' states a basic purpose but is vague about what constitutes a 'write function' in this context. It doesn't distinguish from sibling tools like 'read-contract' or 'send-transaction' beyond the word 'write,' and lacks specificity about the resource being modified or the exact nature of the operation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance is provided on when to use this tool versus alternatives like 'read-contract' for read-only calls, 'send-transaction' for general transactions, or 'deploy-contract' for deployment. The description offers no context, prerequisites, or exclusions, leaving the agent to guess based on tool names alone.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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