aptos_abi_generate
Generate ABI for Aptos smart contracts in desired formats like TS or JSON. Specify the contract path to streamline blockchain application development.
Instructions
Generate ABI for an Aptos contract.
Args:
contract_path: Path to the contract directory
output_format: Format of the output (ts, json)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contract_path | Yes | ||
| output_format | No | ts |
Input Schema (JSON Schema)
{
"properties": {
"contract_path": {
"title": "Contract Path",
"type": "string"
},
"output_format": {
"default": "ts",
"title": "Output Format",
"type": "string"
}
},
"required": [
"contract_path"
],
"title": "aptos_abi_generateArguments",
"type": "object"
}
Implementation Reference
- aptos_mcp_server.py:1339-1409 (handler)The main execution logic for the 'aptos_abi_generate' tool. Compiles the Aptos Move contract using 'aptos move compile', then either generates a TypeScript SDK using 'aptos move aptos-sdk-generate' or extracts and displays JSON ABI files from the build directory.async def aptos_abi_generate(contract_path: str, output_format: str = "ts") -> str: """ Generate ABI for an Aptos contract. Args: contract_path: Path to the contract directory output_format: Format of the output (ts, json) """ contract_path = os.path.expanduser(contract_path) # Expand ~ in paths if not os.path.exists(contract_path): return f"Contract path not found: {contract_path}" supported_formats = ["ts", "json"] if output_format not in supported_formats: return f"Unsupported output format. Choose from: {', '.join(supported_formats)}" try: # First compile the contract to get the ABI build_cmd = ["aptos", "move", "compile", "--save-metadata", "--path", contract_path] build_result = subprocess.run(build_cmd, capture_output=True, text=True) if build_result.returncode != 0: return f"Failed to compile contract:\n\n{build_result.stderr}" # Find the build directory and ABI files build_dir = os.path.join(contract_path, "build") if not os.path.exists(build_dir): return "Build directory not found after compilation" # Generate TypeScript client if requested if output_format == "ts": output_dir = os.path.join(contract_path, "generated") os.makedirs(output_dir, exist_ok=True) # Use aptos CLI to generate TS SDK gen_cmd = [ "aptos", "move", "aptos-sdk-generate", "--output-dir", output_dir, "--package-dir", contract_path ] gen_result = subprocess.run(gen_cmd, capture_output=True, text=True) if gen_result.returncode != 0: return f"Failed to generate TypeScript SDK:\n\n{gen_result.stderr}" return f"Successfully generated TypeScript SDK at {output_dir}" # If JSON format, just return the ABI files abi_files = [] for root, _, files in os.walk(build_dir): for file in files: if file.endswith("abi.json"): abi_files.append(os.path.join(root, file)) if not abi_files: return "No ABI files found after compilation" # Return the contents of ABI files result = [] for abi_file in abi_files: with open(abi_file, "r") as f: abi_content = f.read() result.append(f"# {os.path.basename(abi_file)}\n```json\n{abi_content}\n```") return "\n\n".join(result) except Exception as e: return f"Error generating ABI: {str(e)}"
- aptos_mcp_server.py:1338-1338 (registration)The @mcp.tool() decorator registers the aptos_abi_generate function as an MCP tool.@mcp.tool()