aptos_abi_generate
Generate ABI files for Aptos smart contracts to enable contract interaction and integration in TypeScript or JSON formats.
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
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contract_path | Yes | ||
| output_format | No | ts |
Implementation Reference
- aptos_mcp_server.py:1338-1409 (handler)The handler function for the 'aptos_abi_generate' tool. It compiles an Aptos Move contract, generates ABI in JSON or TypeScript SDK format using aptos CLI tools, and returns the result or error messages. Registered via @mcp.tool() decorator.@mcp.tool() 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)}"