Skip to main content
Glama
lienhage
by lienhage

abi-encode

Encode Ethereum smart contract function parameters using ABI encoding. Input parameter types and values to generate encoded data for blockchain transactions.

Instructions

encode function parameters with abi

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
typesYesparameter types array, e.g. ['uint256', 'address', 'bool']
valuesYesparameter values array, corresponding to the types array

Implementation Reference

  • Executes the ABI encoding logic: validates input lengths, processes values, encodes using ethers.AbiCoder, formats output with types, values, encoded hex, and byte length.
    async ({ types, values }) => { try { if (types.length !== values.length) { return { content: [{ type: "text", text: "error: parameter types and values length mismatch" }], isError: true }; } const processedValues = this.processValues(types, values); const encoded = ethers.AbiCoder.defaultAbiCoder().encode(types, processedValues); const typeSignature = `(${types.join(',')})`; const readableParams = types.map((type, index) => ` ${type}: ${this.formatValue(values[index])}` ).join('\n'); return { content: [{ type: "text", text: `abi encode result:\n\n📋 parameter types: ${typeSignature}\n📝 parameter values:\n${readableParams}\n\n🔢 encoded result: ${encoded}\n\n📏 length: ${(encoded.length - 2) / 2} bytes` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } }
  • Registers the 'abi-encode' tool on the MCP server with title, description, Zod input schema for types and values arrays, and references the handler function.
    server.registerTool( "abi-encode", { title: "abi encode", description: "encode function parameters with abi", inputSchema: { types: z.array(z.string()).describe("parameter types array, e.g. ['uint256', 'address', 'bool']"), values: z.array(z.union([z.string(), z.number(), z.boolean()])).describe("parameter values array, corresponding to the types array") } }, async ({ types, values }) => { try { if (types.length !== values.length) { return { content: [{ type: "text", text: "error: parameter types and values length mismatch" }], isError: true }; } const processedValues = this.processValues(types, values); const encoded = ethers.AbiCoder.defaultAbiCoder().encode(types, processedValues); const typeSignature = `(${types.join(',')})`; const readableParams = types.map((type, index) => ` ${type}: ${this.formatValue(values[index])}` ).join('\n'); return { content: [{ type: "text", text: `abi encode result:\n\n📋 parameter types: ${typeSignature}\n📝 parameter values:\n${readableParams}\n\n🔢 encoded result: ${encoded}\n\n📏 length: ${(encoded.length - 2) / 2} bytes` }] }; } catch (error) { return { content: [{ type: "text", text: `error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } } );
  • Helper method to process and convert input values based on their ABI types (e.g., BigInt for integers, address validation, bool parsing, bytes hex prefixing). Called by the handler.
    private processValues(types: string[], values: (string | number | boolean)[]): any[] { return values.map((value, index) => { const type = types[index]; if (type.includes('uint') || type.includes('int')) { return ethers.getBigInt(value.toString()); } if (type === 'address') { if (typeof value !== 'string') { throw new Error(`address type parameter must be a string: ${value}`); } if (!ethers.isAddress(value)) { throw new Error(`invalid ethereum address: ${value}`); } return value; } if (type === 'bool') { if (typeof value === 'boolean') return value; if (typeof value === 'string') { const lowercaseValue = value.toLowerCase(); if (lowercaseValue === 'true') return true; if (lowercaseValue === 'false') return false; throw new Error(`boolean value must be true or false: ${value}`); } throw new Error(`invalid boolean value: ${value}`); } if (type.startsWith('bytes')) { if (typeof value !== 'string') { throw new Error(`bytes type parameter must be a string: ${value}`); } return value.startsWith('0x') ? value : `0x${value}`; } return value; }); }
  • Helper method to format parameter values for readable display in the tool output.
    private formatValue(value: string | number | boolean): string { if (typeof value === 'string') { return `"${value}"`; } return String(value); }
  • CastCommands class registers its AbiEncoder instance (which registers 'abi-encode') with the MCP server.
    registerWithServer(server: McpServer) { this.fourByteService.registerWithServer(server); this.abiEncoder.registerWithServer(server); this.utilsService.registerWithServer(server);

Other Tools

Related 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/lienhage/blockchain-mcp'

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