Skip to main content

Spectrum MCP Server

Project description

<<<<<<< HEAD

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

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

光谱MCPServer

介绍

{以下是 Gitee 平台说明,您可以替换此简介 Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 https://gitee.com/enterprises}

软件架构

软件架构说明

安装教程

  1. xxxx
  2. xxxx
  3. xxxx

使用说明

  1. xxxx
  2. xxxx
  3. xxxx

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request

特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

d3c9dbc219465b6368443b1c82e6e9d7c75533e9

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-0.1.9.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

File details

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

File metadata

File hashes

Hashes for mcp_server_spectrum_stravision-0.1.9.tar.gz
Algorithm Hash digest
SHA256 3965fb1d9018bfa44d56e1aada2ed234b2897459bf20405cca4681e898c69c2a
MD5 38b9d06ff9a8223c58e125d5b15ce0bc
BLAKE2b-256 789168906800c2b08e7815dc387921446507f455aab6c35eea4578de12786233

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mcp_server_spectrum_stravision-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 1804ace02c888366dcc8560538f22b9729dbf6fc0fe7d3ea8743bde01d53227a
MD5 b202acbc31dc9cddd76a979c45fb55b1
BLAKE2b-256 0fb9c00435bb43a2507a134250175b8c91e025b262c7c780c5806031ea9ebe68

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