README.md (4391B) | Rendered
1 # dbgeng-mcp 2 3 An MCP server for dynamic analysis on Windows. It drives dbgeng, the engine behind WinDbg and cdb, so an MCP client can launch a program, attach to a process, or open a crash dump, then step, read memory and registers, set breakpoints, and run any WinDbg command. 4 5 ## How it is built 6 7 Three layers: 8 9 ``` 10 MCP client <--stdio--> server.py <--JSON over a pipe--> worker.py --ctypes--> dbgeng.dll 11 (MCP) (WSL or Win) (Windows Python) 12 ``` 13 14 - `server.py` holds no debugger state. It exposes the MCP tools and forwards each call to the worker. 15 - `worker.py` owns one dbgeng engine and runs one target at a time. It lives in its own process because dbgeng is stateful and blocking, and because a target under analysis is code that runs. A separate process is one the server can kill and respawn. 16 - `_engine.py` binds dbgeng directly through ctypes. No pykd, no compiled extension. 17 18 ### Why a direct ctypes binding and not pykd 19 20 pykd is the usual way to script dbgeng from Python, but its newest published build targets Python 3.7. On a current Python it is not an option. A direct binding has no such constraint: it runs on whatever Python is installed and needs nothing beyond the Debugging Tools. 21 22 The cost is that the COM vtable layout is written out by hand. To keep that honest, `_abi.py` is generated from the Windows SDK's `DbgEng.h` by `tools/gen_abi.py`, so the method order and interface IDs match the dll that ships with that SDK. Regenerate it against your own header if the SDK moves: 23 24 ```bash 25 python3 tools/gen_abi.py "/mnt/c/Program Files (x86)/Windows Kits/10/Include/<ver>/um/DbgEng.h" 26 ``` 27 28 ## Requirements 29 30 - Windows 10 or 11, 64 bit. 31 - The Debugging Tools for Windows, which provide `dbgeng.dll`. Install the WinDbg package (`winget install Microsoft.WinDbg`) or the Windows SDK's Debugging Tools component. The engine is found under `C:\Program Files (x86)\Windows Kits\10\Debuggers\x64`, or set `DBGENG_DIR`. 32 - Python 3.10 or newer on the Windows side for the worker. Pure standard library, no packages. 33 - The `mcp` package (2.3 or newer) for the server. The server runs under WSL Python or Windows Python. 34 35 The Debugging Tools binaries are not redistributed here. Install them separately. 36 37 ## Install 38 39 ```bash 40 python3 -m venv .venv && . .venv/bin/activate 41 pip install -e . 42 ``` 43 44 ## Run 45 46 As a standalone MCP stdio server: 47 48 ```bash 49 python -m dbgeng_mcp.server 50 ``` 51 52 From WSL the server spawns the worker with `python.exe` (Windows Python) and translates the worker path to its `\\wsl.localhost` form on its own. Override the interpreter or the worker path if needed: 53 54 - `DBGENG_MCP_PYTHON` the Windows Python to run the worker (default `python.exe`). 55 - `DBGENG_MCP_WORKER` the worker script path (default the installed `worker.py`). 56 57 ### Wire it into an MCP client 58 59 Point the client at the server command. Most clients take a command and its arguments, for example: 60 61 ```json 62 { 63 "command": "/home/you/dev/dbgeng-mcp/.venv/bin/python", 64 "args": ["-m", "dbgeng_mcp.server"] 65 } 66 ``` 67 68 ## Tools 69 70 | Tool | What it does | 71 |---|---| 72 | `open_target` | Launch a command line, attach to a pid, or open a dump. Stops at the first instruction | 73 | `execute` | Run any WinDbg command and return its text (`lm`, `k`, `bp`, `dx`, `!peb`, ...) | 74 | `go` | Continue until the next breakpoint, exception, or exit | 75 | `step` | Single step one instruction, into or over | 76 | `read_memory` | Read bytes at an address, returned as hex | 77 | `registers` | The target's register values | 78 | `close` | End the session | 79 80 `execute` is the escape hatch. Anything the structured tools do not cover, a raw command does. 81 82 ## Checking the binding 83 84 `tools/smoke.py` launches a throwaway target and exercises the engine directly. `tools/test_worker.py` drives the worker over the same JSON protocol the server uses. Both run against the live engine: 85 86 ```bash 87 python.exe tools/smoke.py # engine, Windows Python 88 python3 tools/test_worker.py # worker over its pipe 89 ``` 90 91 ## Limits 92 93 - One target at a time. Opening a new one replaces the current. 94 - `registers` returns the engine's register text. Structured per register values are not exposed yet. 95 - Symbols follow `_NT_SYMBOL_PATH`. Set it to a local cache plus the Microsoft symbol server for names. 96 - Running an untrusted target runs its code. For real samples, run the worker in a disposable VM.