pyghidra-mcp

repository·main·Indexed 18 days ago

https://github.com/clearbluejar/pyghidra-mcp

An MCP server that integrates the Ghidra reverse engineering suite with LLM-based agents. It provides tools for decompilation, semantic search via ChromaDB, and program manipulation. The system consists of the pyghidra-mcp server, which manages the Ghidra lifecycle and projects, and the pyghidra-mcp-cli, a client that interacts with the server via HTTP to perform tasks such as searching symbols, generating call graphs, and modifying binaries.

Tokens
25.4K
Snippets
93
Records
126
Agent score
56%

What's inside pyghidra-mcp

  1. Overview of PyGhidra-MCP

    main

    PyGhidra-MCP

    pyghidra-mcp is a Model Context Protocol (MCP) server that exposes Ghidra's reverse engineering capabilities to AI agents and LLM-based tools. It bridges Ghidra's ProgramAPI and FlatProgramAPI to Python via pyghidra and jpype.

    Key Features:

    • Headless-first, GUI-capable: Run via CLI for automation or use --gui to launch a live Ghidra instance that shares state with the MCP server.
    • Agent-optimized: Uses compact tool descriptions and structured data to minimize token usage for LLM agents.
    • Semantic Search: Integrates ChromaDB for vector-based fuzzy lookup across decompiled code, comments, and symbols.
    • Automation-ready: Designed for CI/CD, repeatable workflows, and concurrent analysis of multiple binaries within a Ghidra project.
  2. Use GUI mode for live Ghidra interaction

    main

    GUI mode allows MCP actions to operate against the same live program objects currently displayed in the Ghidra CodeBrowser. This is ideal for navigating the listing, renaming functions, or adding comments and seeing the changes reflected immediately in the Ghidra UI.

    Requirements and Behavior:

    • Requires --gui and --transport streamable-http (or --transport http).
    • Ghidra is launched by pyghidra-mcp, keeping both GUI and MCP transactions in the same JVM.
    • GUI-only tools are only exposed when running with the --gui flag.

    Example:

    pyghidra-mcp \
      --gui \
      --transport streamable-http \
      --project-path /absolute/path/to/my_research.gpr
    pyghidra-mcp \
      --gui \
      --transport streamable-http \
      --project-path /absolute/path/to/my_research.gpr
  3. Agent-optimized MCP design

    main

    The server is designed to be efficient for LLM agents by:

    • Minimizing Tool Surface: MCP tool docstrings are kept compact to keep FastMCP tool schemas small and cheap for models to process.
    • Context Discipline: Tools return focused, structured data rather than dumping entire program contexts, supporting iterative analysis.
    • Conditional Tool Exposure: GUI-specific tools (like goto or set_current_program) are only exposed when the server is explicitly started with the --gui flag, preventing unnecessary noise in headless agent sessions.
  4. How the PyGhidra MCP CLI and Server architecture works

    main

    The pyghidra-mcp-cli is a client that connects to the pyghidra-mcp server exclusively via HTTP.

    Why HTTP?

    • No startup overhead: Avoids spawning a new Java/Ghidra process for every command (which can take 10-60 seconds).
    • Lifecycle management: The server stays running, allowing multiple commands to use a single Ghidra instance.
    • Resource efficiency: One Ghidra instance serves multiple CLI calls.

    Connection Model

    1. The Server (pyghidra-mcp) manages the Ghidra lifecycle, projects, and binaries.
    2. The CLI (pyghidra-mcp-cli) sends commands to the server via HTTP.
    3. GUI Commands: If the server is started with --gui, the CLI can interact with the Ghidra GUI (e.g., open, goto).
  5. How to choose a PyGhidra-MCP operating mode

    main

    PyGhidra-MCP operates in different modes depending on your use case:

    1. Headless MCP: Best for local MCP hosts (like Claude Desktop) or automation. Use the stdio transport for local hosts, or streamable-http if multiple clients need to connect to the same long-running Ghidra project.
    2. GUI Mode: Use this when you need live Ghidra GUI control. When started with --gui, the server launches Ghidra and exposes additional tools to steer the CodeBrowser (e.g., goto, open_program_in_gui).
    3. CLI Client Mode: Use pyghidra-mcp-cli to interact with a running streamable-http server via a terminal. This requires starting the server with --transport streamable-http first.
  6. Manage analysis and indexing readiness

    main

    By default, pyghidra-mcp starts the server without waiting for full analysis (--no-wait-for-analysis). This allows the server to start quickly even for large projects, while analysis and MCP-side indexing continue in the background.

    Key considerations:

    • Ghidra analysis vs. MCP indexing: These are separate states. A binary might be fully analyzed in Ghidra, but semantic search features (like search_strings or search_code) might still be waiting on MCP-side indexing.
    • When to use --wait-for-analysis: Use this flag if you require a fully analyzed project before the server begins serving requests. This increases startup time but ensures immediate readiness for all features.
    • Default behavior: Decompilation, navigation, renaming, and comments typically work even while indexing-heavy search features are still catching up.
  7. Configure SSE (Server-Sent Events) transport

    main

    SSE transport enables server-to-client streaming.

    WARNING

    SSE is considered a legacy transport protocol. Use streamable-http instead.

    By default, the server listens on http://127.0.0.1:8000/sse.

    Python:

    pyghidra-mcp -t sse

    Docker:

    docker run -p 8000:8000 ghcr.io/clearbluejar/pyghidra-mcp -t sse
  8. Configure streamable-http transport

    main

    Streamable HTTP enables streaming responses over JSON RPC via HTTP POST requests. This is the recommended transport protocol.

    By default, the server listens on http://127.0.0.1:8000/mcp. You can change the bind address using --host/--port or the MCP_HOST/MCP_PORT environment variables.

    Python:

    pyghidra-mcp -t streamable-http

    GUI Mode (Python): To launch the Ghidra GUI in-process and serve MCP against GUI-open programs:

    pyghidra-mcp \
      --gui \
      --transport streamable-http \
      --project-path /absolute/path/to/my_project.gpr

    Docker:

    docker run -p 8000:8000 ghcr.io/clearbluejar/pyghidra-mcp
  9. Configure stdio transport

    main

    The stdio transport enables communication through standard input and output streams. This is the default mode for the Python package and is useful for local integrations.

    Python:

    pyghidra-mcp

    Note: The tool may appear to hang without output; this is expected behavior for stdio mode.

    Docker:

    docker run -i --rm ghcr.io/clearbluejar/pyghidra-mcp -t stdio
  10. Integrate pyghidra-mcp with Claude Desktop

    main

    To use pyghidra-mcp with Claude Desktop, add the following configuration to your claude_desktop_config.json file. Ensure you set the GHIDRA_INSTALL_DIR environment variable to your local Ghidra installation path.

    {
        "mcpServers": {
            "pyghidra-mcp": {
                "command": "uvx",
                "args": [
                    "--from",
                    "git+https://github.com/clearbluejar/pyghidra-mcp",
                    "pyghidra-mcp",
                    "--project-path",
                    "/tmp/pyghidra",
                    "/bin/ls"
                ],
                "env": {
                    "GHIDRA_INSTALL_DIR": "/path/to/ghidra/ghidra_12.0_PUBLIC"
                }
            }
        }
    }
    {
        "mcpServers": {
            "pyghidra-mcp": {
                "command": "uvx",
                "args": [
                    "--from",
                    "git+https://github.com/clearbluejar/pyghidra-mcp",
                    "pyghidra-mcp",
                    "--project-path",
                    "/tmp/pyghidra", // or path to writeable directory
                    "/bin/ls" //
                ],
                "env": {
                    "GHIDRA_INSTALL_DIR": "/path/to/ghidra/ghidra_12.0_PUBLIC"
                }
            }
        }
    }
  11. Set up the pyghidra-mcp-cli development environment

    main

    To develop on the CLI package, use the provided Makefile or manual venv setup to install the package in editable mode.

    cd cli
    make dev-setup      # or create venv manually
    pip install -e .
    
    # Run tests
    pytest
    
    # Lint
    ruff check .
  12. Quick Start: Running the CLI

    main

    To use the CLI, you must first start the server in a separate terminal session.

    1. Start the Server

    Choose one of the following modes:

    Option A: Open an existing Ghidra project

    pyghidra-mcp --transport streamable-http --project-path /path/to/project.gpr

    Option B: Import and analyze a single binary

    pyghidra-mcp --transport streamable-http --wait-for-analysis /bin/ls

    Option C: Import multiple binaries

    pyghidra-mcp --transport streamable-http --wait-for-analysis ./binary1 ./binary2

    2. Use the CLI

    Once the server is running, execute commands via pyghidra-mcp-cli:

    # List available binaries
    pyghidra-mcp-cli list binaries
    
    # Decompile a function
    pyghidra-mcp-cli decompile --binary my_binary.dylib main
    
    # Search for symbols
    pyghidra-mcp-cli search symbols --binary my_binary.so malloc -l 20
    # Start server
    pyghidra-mcp --transport streamable-http --wait-for-analysis /bin/ls
    
    # Use CLI
    pyghidra-mcp-cli list binaries