Skip to main content
Glama
stm32-stlink-mcp

stm32-stlink-mcp

by phryniszak

stm32-stlink-mcp

用于通过 ST-LINK 调试 STM32 微控制器的 MCP 服务器,基于 STMicroelectronics 自家的 STM32CubeCLT 工具集构建——ST-LINK_gdbserver、STM32_Programmer_CLI 和 arm-none-eabi-gdb(通过 GDB/MI2 驱动)。不涉及 OpenOCD、J-Link 或 probe-rs。

架构

调试会话由一对子进程组成,精确复刻 ST 官方文档描述的工作流程(UM2576,"STM32CubeIDE ST-LINK GDB server"):

 arm-none-eabi-gdb  --interpreter=mi2  --(TCP, target extended-remote)-->  ST-LINK_gdbserver  --(USB)-->  ST-LINK  --(SWD)-->  STM32

arm-none-eabi-gdb 以 MI2 模式驱动,因此服务器无需自行实现 GDB 远程串行协议,即可免费获得源码级单步、符号断点和符号感知的表达式求值。ST-LINK_gdbserver 在会话生命周期内持有探针的 USB 句柄;通过 gdb 的 load(MI:-target-download)进行的烧录由服务器透明地委托给 STM32CubeProgrammer,因此重新烧录无需拆除会话。独立的一次性烧录(flash_standalone,无需会话)直接调用 STM32_Programmer_CLI,因此会与同一探针上已打开的会话冲突——请参阅工具描述。

Related MCP server: dbgprobe-mcp-server

安装

npm install
npm run build

需要安装 STM32CubeCLT 并确保其 bin/ 目录可访问——要么已经位于 PATH 中(CLT 安装程序默认会这样做),要么通过 STMCP_CUBECLT_PATH / 按工具的覆盖路径指定。运行 npm run doctor 进行检查。

运行

node dist/index.js serve     # starts the MCP server on stdio (default mode)
node dist/index.js doctor    # pre-flight check: tool resolution, connected probes, udev rules
node dist/index.js doctor --json

向 MCP 客户端注册

{
  "mcpServers": {
    "stm32-stlink": {
      "command": "node",
      "args": ["<path-to-this-repo>/stmcp/dist/index.js"]
    }
  }
}

配置(环境变量)

变量

默认值

用途

STMCP_GDBSERVER_PATH / STMCP_PROGRAMMER_CLI_PATH / STMCP_ARM_GDB_PATH

—

单个二进制文件的覆盖路径(优先级最高)

STMCP_CUBECLT_PATH

—

CubeCLT 安装根目录;子路径通过 STM32CubeCLT_metadata.sh -j 解析

STMCP_STLINK_SERIAL

—

默认探针序列号(省略时,若仅连接一个探针则自动选择)

STMCP_DEFAULT_DEVICE

STM32G431CBTx

默认 MCU 设备字符串

STMCP_DEFAULT_INTERFACE

swd

swd 或 jtag

STMCP_DEFAULT_FREQUENCY_KHZ

4000

SWD/JTAG 时钟频率

STMCP_MAX_SESSIONS

1

并发调试会话上限

STMCP_GDBSERVER_READY_TIMEOUT_MS

8000

等待 "Waiting for debugger connection..." 的超时时间

STMCP_LOG_LEVEL

info

error | warn | info | debug

STMCP_LOG_FILE

—

可选日志文件(无论如何都会使用 stderr——stdout 保留给 MCP 帧)

STMCP_ALLOW_FLASH_ERASE

false

启用擦除路径

STMCP_ALLOW_MEMORY_WRITE

true

启用 memory_write

STMCP_ALLOW_FLASH_ADDRESS_WRITE

false

允许 memory_write 以 flash 地址窗口为目标(通常被阻止——请改用 flash 工具)

STMCP_ALLOWED_FILE_PATHS

(不受限制)

用于 ELF/bin 文件参数的逗号分隔白名单根目录

STMCP_MAX_FILE_SIZE_BYTES

16777216

文件参数的最大大小

STMCP_FLASH_RANGE_START / STMCP_FLASH_RANGE_END

0x08000000 / 0x08020000

写入保护的 flash 地址窗口(默认:128KB,STM32G431CB)

工具

领域

工具

用途

探针

list_probes

列出已连接的 ST-LINK 探针

会话

debug_connect

启动 gdbserver+gdb,加载 ELF 符号,连接

会话

debug_disconnect

干净地拆除会话

会话

debug_session_status

会话信息(单个或全部)

烧录

flash_standalone

通过 STM32_Programmer_CLI 进行一次性烧录,无需会话

烧录

flash_load_in_session

在已打开的会话中通过 gdb load 重新烧录

执行

debug_run

恢复/继续

执行

debug_halt

中断

执行

debug_reset

复位(monitor reset [halt])

执行

debug_step

单步跳过/进入/跳出

断点

breakpoint_set / breakpoint_clear / breakpoint_list

按 file:line、符号或 *addr

内存

memory_read / memory_write

原始内存访问(写入受保护)

寄存器

register_read / register_write

按名称访问的核心寄存器

寄存器

read_fault_registers

一次调用即可转储 Cortex-M SCB 故障寄存器(CFSR/HFSR/... 已解码)

表达式

evaluate_expression

通过 gdb MI 进行符号感知的求值

推迟到 v2

SVD 外设寄存器工具(memory_read/write + evaluate_expression 已经可以通过地址访问一切)、实时/流式内存轮询、插件系统、按芯片划分的内存区域白名单、任意 gdb monitor 透传,以及选项字节/RDP 工具(可能导致变砖,有意不在范围内)。

RTT

RTT(SEGGER 实时传输——实时、不暂停内核的控制台/变量跟踪)有意不在此服务器中实现。ST-LINK_gdbserver 的 GDB/MI 存根没有非停止模式,因此通过此服务器的 debug_connect 会话读取内存需要先暂停内核——这违背了 RTT 的目的。正确的机制是永不暂停内核的直接 AP 内存访问(通过阅读 ST 自家的 STM32CubeMonitor 源码确认,它正是使用这种方式,STM32_Programmer_CLI 的 -r32fast 也是如此)。

这正是 strtt 已经实现的功能,strtt-mcp 将其封装为独立的 MCP 服务器(strtt_start/strtt_stop/strtt_status/strtt_read/strtt_write)。请将其与此服务器并列注册,而不是通过它注册:

{
  "mcpServers": {
    "stm32-stlink": { "command": "node", "args": ["<...>/mcp-server/dist/index.js"] },
    "strtt": {
      "command": "node",
      "args": ["<path-to-strtt-repo>/mcp/dist/index.js"],
      "env": { "STRTT_BIN": "<path-to-strtt-binary>" }
    }
  }
}

使用 tcp: true 启动 strtt_start,通过共享的 ST-LINK Server 连接,而不是直接占用 USB 设备——这样它就可以与此处打开的 debug_connect 会话并发运行,因为 GdbServerProcess 总是将 -t/--shared 传递给 ST-LINK_gdbserver。如果不使用 tcp: true,strtt 和已打开的调试会话将争用同一个探针。

硬件验证手册

连接好 ST-LINK 和目标板后:

node dist/index.js doctor                     # confirm probe + tools resolve
npx @modelcontextprotocol/inspector node dist/index.js   # interactive tool testing

然后,通过检查器或 MCP 客户端:

  1. list_probes → 探针的序列号会出现。

  2. debug_connect { elfPath, device, interface: "swd", serial } → 返回一个 sessionId。

  3. breakpoint_set { sessionId, location: "main" } → 返回一个断点编号。

  4. debug_run { sessionId } → 以 reason: "breakpoint-hit" 暂停。

  5. register_read { sessionId, registers: ["pc","sp","lr","r0"] }。

  6. evaluate_expression { sessionId, expression: "<a known global>" }。

  7. read_fault_registers { sessionId } → 复位后标志为良性/零值。

  8. debug_disconnect { sessionId } → 确认没有孤儿进程:ps aux | grep -E 'ST-LINK_gdbserver|arm-none-eabi-gdb'。

  9. flash_standalone { file, reset: "hard", run: true },且没有打开的会话。

  10. 负向测试:打开一个会话,然后在同一序列号上调用 flash_standalone → 预期返回 DEVICE_BUSY。

注意: debug_connect 会暂停目标 CPU。如果电路板正在主动驱动执行器/输出,且意外暂停可能不安全,请勿在未事先确认可行的情况下连接。

Available Tools

19 tools
breakpoint_clearClear a breakpointA

Deletes a previously set breakpoint by its number.

Inputs: sessionId (required), breakpointNumber (required).

breakpoint_listList breakpointsA

Lists breakpoints currently tracked on a session (those set via breakpoint_set).

Inputs: sessionId (required).

breakpoint_setSet a breakpointB

Sets a breakpoint by file:line (e.g. main.c:42), symbol name (e.g. main), or address (e.g. *0x08000200).

Inputs: location (required), sessionId (required), condition, temporary.

debug_connectConnect a debug sessionA

Starts a debug session: spawns ST-LINK_gdbserver and arm-none-eabi-gdb (MI2), loads ELF symbols, and connects via target extended-remote.

Inputs: elfPath (required), apId, device, serial, +3 more.

debug_disconnectDisconnect a debug sessionA

Cleanly tears down a debug session's gdb and ST-LINK_gdbserver child processes.

Inputs: sessionId (required).

debug_haltHalt executionB

Interrupts the running target.

Inputs: sessionId (required).

debug_resetReset the targetA

Resets the target via ST-LINK_gdbserver's monitor reset command, optionally resuming afterward.

Inputs: sessionId (required), halt.

debug_runResume executionA

Resumes (continues) the target and waits for it to stop again (e.g. a breakpoint), up to a timeout.

Inputs: sessionId (required), waitForStopMs.

debug_session_statusDebug session statusA

Reports the status of one debug session (by id), or lists all active sessions if id is omitted.

Inputs: sessionId.

debug_stepSingle-stepB

Steps the target: 'over' (next line, step over calls), 'into' (step into calls), or 'out' (finish the current function).

Inputs: sessionId (required), mode, waitForStopMs.

evaluate_expressionEvaluate an expressionB

Symbol-aware expression evaluation in the current frame via gdb's -data-evaluate-expression — reads globals/locals by name, struct/array member access, pointer dereference, arithmetic, etc.

Inputs: sessionId (required), expression (required).

flash_load_in_sessionReflash firmware within a debug sessionA

Reflashes the currently loaded ELF via gdb's load (MI: -target-download) inside an already-open debug session. No USB conflict — ST-LINK_gdbserver retains ownership of the probe throughout.

Inputs: sessionId (required), runToMain, resetAfter.

flash_standaloneFlash firmware (standalone)A

One-shot flash via STM32_Programmer_CLI — no debug session required. Fails with DEVICE_BUSY if a debug session is already open on the same probe (it holds exclusive USB access); disconnect first, or use flash_load_in_session instead.

Inputs: file (required), run, reset, serial, +3 more.

list_probesList ST-LINK probesA

Lists connected ST-LINK probes via STM32_Programmer_CLI. Returns an empty list (not an error) if none are attached.

Inputs: no named parameters.

memory_readRead memoryA

Reads raw bytes from target memory at an address or symbol-address expression (e.g. '&my_global').

Inputs: length (required), address (required), sessionId (required).

memory_writeWrite memoryA

Writes raw bytes to target RAM/registers at an address. Blocked for addresses inside the flash window (use flash_standalone / flash_load_in_session for flash) unless STMCP_ALLOW_FLASH_ADDRESS_WRITE is set.

Inputs: address (required), bytesHex (required), sessionId (required).

read_fault_registersRead Cortex-M fault registersA

One-call dump of Cortex-M SCB fault registers (CFSR/HFSR/MMFAR/BFAR/SHCSR/CPUID/ICSR) plus PC/SP/LR, with CFSR/HFSR bits decoded to flag names. Raw evidence only — no root-cause interpretation.

Inputs: sessionId (required).

register_readRead core registersA

Reads named core registers (default: all general-purpose + sp/lr/pc/xpsr) via gdb MI.

Inputs: sessionId (required), registers.

register_writeWrite a core registerB

Writes a value to a named core register via gdb's -gdb-set $<register>=<value>.

Inputs: value (required), register (required), sessionId (required).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    A3.7/5.0

    Scored across 19 tools

    Disambiguation5/5

    Each tool targets a distinct action: session lifecycle, run control, breakpoints, register/memory access, and flashing are clearly separated. The only similar pair, flash_standalone and flash_load_in_session, is explicitly differentiated by requiring or not requiring an active debug session.

    Naming Consistency4/5

    All tool names are lowercase snake_case and mostly follow a predictable domain-prefixed verb pattern such as breakpoint_set, memory_read, and debug_run. Minor exceptions like debug_session_status and flash_standalone break the strict verb_noun pattern but remain easy to anticipate.

    Tool Count4/5

    19 tools is on the heavier side, but the embedded debug workflow legitimately spans session management, execution control, breakpoints, register/memory inspection, fault diagnosis, and flashing. Each tool has a justifiable role, so the count feels slightly over ideal rather than bloated.

    Completeness4/5

    The toolset covers the full core debug loop: connect/disconnect, run/halt/reset/step, breakpoint management, register and memory access, expression evaluation, fault register dumps, and both standalone and in-session flashing. Missing conveniences like stack backtraces or watchpoints are minor gaps, not workflow-breaking omissions.

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      D
      maintenance
      Stateful MCP server for driving debug probes (J-Link) to flash, debug, and inspect embedded targets. Enables AI agents to perform flash, memory, breakpoint, and ELF/SVD-aware operations conversationally.
      41
      23 PyPI
      11
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      MCP server for embedded debugging based on probe-rs, providing 22 tools for ARM Cortex-M and RISC-V microcontrollers, including connection, memory operations, breakpoints, flash programming, and RTT communication.
      2
      MIT
    • A
      license
      Not graded
      quality
      D
      maintenance
      An MCP server that provides comprehensive debugging capabilities for J-Link debuggers, enabling memory, flash, register, and RTT operations through AI assistants.
      43
      MIT