Skip to main content

Spectrum MCP Server for Stravision

Project description

Spectrum MCP Server

这个项目将来自 ESP32 上 AS7341 光谱传感器的数据通过 MQTT 发布,并在本地运行一个 FastMCP 服务器将最新光谱封装为 MCP 资源与工具接口,方便其它程序/服务调用。

目录结构(相关文件):

  • server.py — 本地 FastMCP 服务,订阅 MQTT 主题并暴露 spectrum://latest 资源。
  • esp32/main.txt — ESP32 上的 MicroPython 主程序(请在设备上保存为 main.py 并按需修改 WiFi/MQTT 配置)。
  • esp32/as7341.txt — AS7341 驱动(MicroPython)。

注意:ESP32 代码以 .txt 附件形式保存在仓库以便在 PC 上编辑;部署到设备时应保存为 main.pyas7341.py

快速开始(在开发机器)

  1. 创建并激活虚拟环境(可选但推荐):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
  1. 安装依赖:
python -m pip install "mcp[cli]>=1.26.0" paho-mqtt>=1.6.1
  1. 运行 MCP 服务器:
python server.py

服务器启动后会连接到配置的 MQTT Broker(默认:meimefarm/Spectrum),并在收到最新光谱消息时保存到内存中。

MCP 接口

  • 资源 spectrum://latest — 返回最新的光谱数据(字典,键为 channel1..channel11)。
  • 工具 get_channel(name) — 传入通道名(例如 channel1)返回整数值。

示例:使用 Python 客户端调用(示例使用 mcp 包内的客户端 API):

from mcp.client import MCPClient

c = MCPClient.as_stdio()  # 依据运行方式选择合适 transport
print(c.resource('spectrum://latest')())
print(c.tool('get_channel')('channel1'))

注:如果使用 mcp[cli] 提供的命令行工具,参阅 mcp 文档以通过 CLI 调用资源/工具。

在 ESP32 上部署

  1. 修改 esp32/main.txt 中的 WiFi 和 MQTT 配置(如 WIFI_SSID, WIFI_PASS, MQTT_BROKER, MQTT_USER, MQTT_PASSWORD)。
  2. esp32/as7341.txt 重命名为 as7341.py,将 esp32/main.txt 重命名为 main.py
  3. 使用 mpremote / ampy / rshell 等工具上传文件到设备并重启:
# 使用 mpremote 示例
mpremote connect auto fs cp esp32/as7341.py :
mpremote connect auto fs cp esp32/main.py :
mpremote connect auto run :

设备上运行的 main.py 会连接 WiFi、读取 AS7341 光谱并通过 MQTT 向 meimefarm/Spectrum 主题发布 JSON 消息,例如:

{
  "channel1": 123,
  "channel2": 456,
  ...
}

自定义与调试

  • 若要改为异步 MQTT 客户端,可在 server.py 中替换为 asyncio-mqtt
  • 若 MQTT 需要自定义 CA 或证书,请在 server.pystart_mqtt() 中调用 client.tls_set(ca_certs=..., certfile=..., keyfile=...) 并根据需要设置 tls_insecure_set()
  • 若无法收到消息,检查设备是否能连上 WiFi,并确认 Broker/Topic 与 server.py 中的一致。

常见问题

  • 为什么 ESP32 文件以 .txt 結尾?

    • 为了方便在桌面环境编辑并避免直接覆盖开发环境,发布库内保存为 .txt;部署到设备时请改名为 .py
  • 如何保证数据持久化?

    • 当前实现仅保存在内存 latest_spectrum,若需要持久化可把收到的消息写入数据库或文件。

后续建议

  • 添加一个小型 web 前端或 HTTP API 以便浏览器/其他服务直接读取最新光谱。
  • 将 MCP 服务改为使用持久后端(Redis/SQLite)以支持历史查询与持久化。

如果你愿意,我可以:

  • esp32/main.txt 转为 esp32/main.py(直接写文件),并生成一个 requirements.txtpyproject 更新示例;
  • 或把 server.py 改写为异步版本并使用 asyncio-mqtt

告诉我你下一步想要哪个,我会继续实现。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcp_server_spectrum_stravision-2.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcp_server_spectrum_stravision-2.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file mcp_server_spectrum_stravision-2.0.1.tar.gz.

File metadata

File hashes

Hashes for mcp_server_spectrum_stravision-2.0.1.tar.gz
Algorithm Hash digest
SHA256 9eef3a2f0a769c1c0e596cba818ef0533c0e0015ff0453d429433a802ce33824
MD5 79bfcf299c4e55e66967a38abe858f22
BLAKE2b-256 3d17071f42b521d7118241d357ad177a99a590b2d2abb111a3a8c89470745130

See more details on using hashes here.

File details

Details for the file mcp_server_spectrum_stravision-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mcp_server_spectrum_stravision-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7c9736d7011dc6783de6572e8d73d2607b120ddbc46cb6a10f084ba563c529f5
MD5 c69ae14c0e6f1c08dcf124bf8f1947bb
BLAKE2b-256 9b7f24545567ff1bc2066a65e6c325e44ae0eed9245feb983bb9c87b329bfd23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page