mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-01 18:50:34 +03:00
feat(tools): mcp-server added monitor-device tool
Add a `monitor_device` MCP tool that lets an AI agent run a scripted, non-interactive `esp-idf-monitor` session against a flashed device and get back a short status plus a log file path, instead of raw serial output inline. Under the hood: - The agent supplies a plain-text command body (expect/send/sleep/reset/ exit/comments). `assemble_monitor_script_from_agent_commands()` frames it into a script the monitor's non-interactive command mode can consume via stdin: it appends `exit` if the agent didn't already end with one, and rewrites every bare `expect <regex>` into `expect --timeout <timeout_sec> <regex>` via `_monitor_normalize_expect_line()` (an already-bounded `expect --timeout ...` line is left untouched so the monitor itself reports a bad value). A leading `reset` is not prepended - the monitor already resets the chip when it opens the port - and any `reset` the agent wrote is left in place. `_monitor_parse_sleep_duration()` extracts each `sleep <n>` duration. The effective timeout is the sum of every bounded expect duration plus every sleep duration. Scripts whose sum exceeds `MONITOR_MAX_SCRIPT_SEC` are rejected. If the script has neither expect nor sleep (for example only `send`), `timeout_sec` is used so the process still has a kill bound. - `monitor_device()` runs `python -m esp_idf_monitor` via `subprocess.run(..., input=script, timeout=2 * effective_timeout)`. `no_reset` is forwarded as `--no-reset` so the connection reset can be skipped; an explicit `-p` is forwarded when a port is given. Extra arguments match `idf.py monitor` where a build exists: baud (`baud` tool arg, else `monitor_baud` from `project_description.json`), toolchain prefix, `--target`/`--revision`, coredump/panic decode, and ELF files with the app ELF first. The 2x hard timeout is a safety net independent of the script's own `expect --timeout`/`exit` logic; on `TimeoutExpired` the process is killed but any output already captured is preserved and logged. `decode_stream()` normalizes that captured output, which can be `bytes` on the timeout path even though the process otherwise runs in text mode. - The monitor's exit code drives the reported status via `_monitor_status()`, using `EXIT_EXPECT_TIMEOUT` and `EXIT_SCRIPT_ERROR` from `esp_idf_monitor.base.constants`: 0 is success, 110 means an `expect` pattern never showed up before its `--timeout` elapsed, 2 means the monitor rejected the script (bad syntax/timeout/regex), anything else is reported generically. - Serial output and the monitor's own messages share one pipe (`stderr=STDOUT`) so decoded panic backtraces stay next to the lines that triggered them. `_save_monitor_output()` writes the full merge to `<tempdir>/esp_idf_mcp_log/action_monitor/monitor_<timestamp>.log` and reports a dedicated `Log file:` line. On non-zero exit or process kill, a short tail of that same merge is also returned inline so the agent has some failure context without a second file read. If the log file can't be written, it falls back to inlining a truncated tail. Closes https://github.com/espressif/esp-idf/issues/18757 Closes https://github.com/espressif/esp-idf/pull/18385 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -337,8 +337,9 @@ The MCP server provides the following tools:
|
||||
- ``flash project``: Flash the built project to a connected device. Specify it by port name
|
||||
- ``clean project``: Clean build artifacts
|
||||
- ``create project``: Create a new ESP-IDF project from the sample template. Can be used before any project exists
|
||||
- ``monitor device``: Run a scripted serial monitor session against a flashed device and wait for expected output. See :ref:`mcp-monitor-device`
|
||||
|
||||
All tools accept an optional ``project_dir`` argument. When omitted, the tool operates on the directory configured at startup (``-C`` flag or ``IDF_MCP_WORKSPACE_FOLDER``). You can instruct the AI model to use a specific project directory explicitly, for example when working with multiple projects or when no default project was configured at startup.
|
||||
The project tools accept an optional ``project_dir`` argument. When omitted, the tool operates on the directory configured at startup (``-C`` flag or ``IDF_MCP_WORKSPACE_FOLDER``). You can instruct the AI model to use a specific project directory explicitly, for example when working with multiple projects or when no default project was configured at startup. The ``create project`` and ``monitor device`` tools are the exception: the former takes the parent ``path`` for the new project, and the latter talks to a device rather than to a project directory.
|
||||
|
||||
The MCP server also provides these resources:
|
||||
|
||||
@@ -346,6 +347,15 @@ The MCP server also provides these resources:
|
||||
- ``project://status``: Get current project build status and artifacts
|
||||
- ``project://devices``: Get list of connected devices
|
||||
|
||||
.. _mcp-monitor-device:
|
||||
|
||||
Monitoring a Device
|
||||
^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The ``monitor device`` tool lets an AI assistant observe what a flashed device prints. Ask in ordinary language, for example "flash it and check that the device prints ``Minimum free heap size`` within 30 seconds". The assistant waits for that output and then tells you whether it appeared.
|
||||
|
||||
The full serial log is kept in a file. Ask the assistant to search that log if you want more detail than the short status it reports.
|
||||
|
||||
Adding ESP-IDF MCP Server to IDEs and AI agents
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
|
||||
Reference in New Issue
Block a user