setup_context
Export binary analysis context from Ghidra headless decompilation for reverse-engineering functions, pseudocode, structs, and enums.
Instructions
Run Ghidra headless decompilation to export binary context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ghidra_path | No | /Users/tomi/Downloads/ghidra_11.3.1_PUBLIC | |
| binary_path | No | /Users/tomi/Documents/ghidra_mcp/crackme |
Implementation Reference
- main.py:44-66 (handler)The core handler function for the 'setup_context' MCP tool. Decorated with @mcp.tool() for automatic registration and schema inference from type hints and docstring. Validates inputs, executes Ghidra headless analysis using helper functions, loads the resulting context, and updates global state.@mcp.tool() async def setup_context(ghidra_path: str = "/Users/tomi/Downloads/ghidra_11.3.1_PUBLIC", binary_path: str = "/Users/tomi/Documents/ghidra_mcp/crackme") -> str: """Run Ghidra headless decompilation to export binary context.""" global ctx_ready, last_binary if not os.path.isdir(ghidra_path): return f"❌ Ghidra path '{ghidra_path}' is not valid." if not os.path.isfile(binary_path): return f"❌ Binary file '{binary_path}' does not exist." result = run_headless(ghidra_path, binary_path) if result.returncode != 0: return f"❌ Ghidra failed:\n{result.stderr}" if not is_context_ready(): return f"❌ Export script ran but no context was saved." load_context() ctx_ready = True last_binary = os.path.basename(binary_path) return f"✅ Context loaded for '{last_binary}'."
- main.py:18-35 (helper)Key helper function called by setup_context to execute the Ghidra analyzeHeadless command with the export script, environment variable for JSON output path, and proper arguments for importing and analyzing the binary.def run_headless(ghidra_path: str, binary_path: str): analyze_headless = os.path.join(ghidra_path, "support", "analyzeHeadless") project_dir = os.getcwd() project_name = "ghidra_ctx" cmd = [ analyze_headless, project_dir, project_name, "-import", binary_path, "-overwrite", "-scriptPath", os.path.dirname(EXPORT_SCRIPT_PATH), "-postScript", os.path.basename(EXPORT_SCRIPT_PATH), "-deleteProject" ] env = os.environ.copy() env["GHIDRA_CONTEXT_JSON"] = GHIDRA_CONTEXT_JSON return subprocess.run(cmd, capture_output=True, text=True, env=env)
- main.py:36-40 (helper)Helper function to load the exported Ghidra context JSON into the global ctx dictionary, used after successful headless run.def load_context(): global ctx with open(GHIDRA_CONTEXT_JSON) as f: ctx = json.load(f)
- main.py:41-43 (helper)Helper function to check if the context JSON file exists, used to verify successful export.def is_context_ready(): return os.path.exists(GHIDRA_CONTEXT_JSON)