Skip to main content
Glama
write-contract.ts1.61 kB
import { FastMCP } from "fastmcp"; import { z } from "zod"; import { writeContract } from "@wagmi/core" import { wagmiConfig } from "../wagmi-config.js"; import { Abi, Address, TransactionExecutionError } from "viem"; import { JSONStringify } from "../utils/json-stringify.js"; 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, } ] } } }, }); };

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