mcp-stm32-server

mcp-stm32-server

Enables AI assistants to control an STM32 microcontroller over serial by reading/writing GPIO, setting LED brightness, reading ADC channels, and displaying text on an OLED screen via natural language.

Category
访问服务器

README

🔌 MCP STM32 Controller Server / MCP STM32 控制器服务器

Python MCP License

Control an STM32 microcontroller through the MCP (Model Context Protocol). AI assistants can read/write GPIO, control an LED, read ADC, and drive an OLED — all through natural language.

通过 MCP(Model Context Protocol) 协议控制 STM32 微控制器。AI 助手可以调用本服务提供的工具来读写 STM32 的 GPIO、控制 LED、读取 ADC、操作 OLED 屏幕等。


<!-- ═══════════════════════════════════════════════════════════════ --> <!-- English --> <!-- ═══════════════════════════════════════════════════════════════ -->

Architecture

AI Assistant (Claude) ←→ MCP Server (this program) ←→ UART ←→ STM32 firmware

Communication uses a JSON-line protocol: one JSON object per line, terminated by \n.


Quick Start

1. Install dependencies

pip install -r requirements.txt

2. Connect the hardware

  • STM32 USART1: PA9 (TX), PA10 (RX)
  • Connect to the PC through a USB-TTL module
  • Flash the matching firmware on the device (implement the JSON-line protocol described below)

3. Start the server

# auto-detect the serial port
python main.py

# specify a serial port
python main.py --port COM3

# SSE mode (HTTP service)
python main.py --sse --port COM3

Tools

Tool Description Arguments
ping check whether the STM32 is online —
set_led set the LED brightness brightness: 0-100
get_led read the current LED brightness —
read_pin read a GPIO level pin: "PA0", "PC13", ...
write_pin set a GPIO level pin + value: 0/1
read_adc read an ADC value channel: 0-2
oled_clear clear the OLED screen —
oled_print print text on the OLED x + y + text

Resources

URI Description
stm32://status connection status, port info, uptime

Configuration

Via environment variables or command-line arguments:

Argument Env var Default Description
--port STM32_PORT auto-detect serial port
--baud — 115200 baud rate
--sse — false SSE mode
--sse-port — 8000 SSE port

Integrate with Claude Desktop

Edit claude_desktop_config.json:

{
  "mcpServers": {
    "stm32": {
      "command": "python",
      "args": ["C:/path/to/mcp-stm32-server/main.py", "--port", "COM3"],
      "env": {}
    }
  }
}

Wire Protocol (JSON-lines)

Each line is an independent JSON object. The firmware simply echoes a response with the matching id.

Request:

{"id":1,"cmd":"set_led","args":{"brightness":75}}

Success response:

{"id":1,"status":"ok","data":{"brightness":75}}

Error response:

{"id":1,"status":"error","msg":"brightness must be 0-100"}

Firmware contract: on every received request, reply with one JSON line carrying the same id, plus status ("ok" / "error") and either data or msg. Boot-log lines that are not valid JSON are skipped by the client automatically.


License

MIT License.


<!-- ═══════════════════════════════════════════════════════════════ --> <!-- 中文 --> <!-- ═══════════════════════════════════════════════════════════════ -->

架构

AI 助手 (Claude) ←→ MCP Server (本程序) ←→ 串口 ←→ STM32 固件

通信使用 JSON 行协议,每行一个 JSON 对象,以 \n 结尾。


快速开始

1. 安装依赖

pip install -r requirements.txt

2. 连接硬件

  • STM32 的 USART1:PA9(TX),PA10(RX)
  • 通过 USB-TTL 模块连接电脑
  • 在设备端烧录实现下述 JSON 行协议的配套固件

3. 启动服务

# 自动检测串口
python main.py

# 指定串口
python main.py --port COM3

# SSE 模式(HTTP 服务)
python main.py --sse --port COM3

工具列表

工具 说明 参数
ping 检测 STM32 在线状态 无
set_led 设置 LED 亮度 brightness: 0-100
get_led 读取 LED 亮度 无
read_pin 读取 GPIO 电平 pin: "PA0", "PC13" 等
write_pin 设置 GPIO 电平 pin + value: 0/1
read_adc 读取 ADC 值 channel: 0-2
oled_clear 清空 OLED 屏幕 无
oled_print 在 OLED 上显示文字 x + y + text

资源

URI 说明
stm32://status 连接状态、串口信息、运行时间

配置

通过环境变量或命令行参数:

参数 环境变量 默认值 说明
--port STM32_PORT 自动检测 串口号
--baud - 115200 波特率
--sse - false SSE 模式
--sse-port - 8000 SSE 端口

与 Claude Desktop 集成

编辑 claude_desktop_config.json:

{
  "mcpServers": {
    "stm32": {
      "command": "python",
      "args": ["C:/path/to/mcp-stm32-server/main.py", "--port", "COM3"],
      "env": {}
    }
  }
}

协议格式(JSON 行)

每一行是一个独立的 JSON 对象。固件只需回一个 id 相同的 JSON 行即可。

请求:

{"id":1,"cmd":"set_led","args":{"brightness":75}}

成功响应:

{"id":1,"status":"ok","data":{"brightness":75}}

错误响应:

{"id":1,"status":"error","msg":"brightness must be 0-100"}

固件约定:收到每条请求后,回一行包含相同 id 的 JSON,含 status("ok" / "error")以及 data 或 msg。启动时的非 JSON 日志行会被客户端自动跳过。


许可证

MIT License。

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选