setup_context
Export binary context using Ghidra in headless mode. Extracts functions, pseudocode, structs, and enums for reverse-engineering analysis.
Instructions
Run Ghidra headless decompilation to export binary context.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| binary_path | No | /Users/tomi/Documents/ghidra_mcp/crackme | |
| ghidra_path | No | /Users/tomi/Downloads/ghidra_11.3.1_PUBLIC |
Implementation Reference
- main.py:44-66 (handler)The handler function for the setup_context tool, decorated with @mcp.tool() for registration. Handles input validation, invokes Ghidra headless via helper, loads context, and manages 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 custom export script.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 to load the exported Ghidra context JSON into global variable.def load_context(): global ctx with open(GHIDRA_CONTEXT_JSON) as f: ctx = json.load(f)
- main.py:41-43 (helper)Helper to check if context JSON file exists.def is_context_ready(): return os.path.exists(GHIDRA_CONTEXT_JSON)