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.py 和 as7341.py。
快速开始(在开发机器)
- 创建并激活虚拟环境(可选但推荐):
python -m venv .venv
.\.venv\Scripts\Activate.ps1
- 安装依赖:
python -m pip install "mcp[cli]>=1.26.0" paho-mqtt>=1.6.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 上部署
- 修改
esp32/main.txt中的 WiFi 和 MQTT 配置(如WIFI_SSID,WIFI_PASS,MQTT_BROKER,MQTT_USER,MQTT_PASSWORD)。 - 将
esp32/as7341.txt重命名为as7341.py,将esp32/main.txt重命名为main.py。 - 使用
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.py的start_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.txt或pyproject更新示例; - 或把
server.py改写为异步版本并使用asyncio-mqtt。
告诉我你下一步想要哪个,我会继续实现。
光谱MCPServer
介绍
{以下是 Gitee 平台说明,您可以替换此简介 Gitee 是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 无论是个人、团队、或是企业,都能够用 Gitee 实现代码托管、项目管理、协作开发。企业项目请看 https://gitee.com/enterprises}
软件架构
软件架构说明
安装教程
- xxxx
- xxxx
- xxxx
使用说明
- xxxx
- xxxx
- xxxx
参与贡献
- Fork 本仓库
- 新建 Feat_xxx 分支
- 提交代码
- 新建 Pull Request
特技
- 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
- Gitee 官方博客 blog.gitee.com
- 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
- GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
- Gitee 官方提供的使用手册 https://gitee.com/help
- Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/
d3c9dbc219465b6368443b1c82e6e9d7c75533e9
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mcp_server_spectrum_stravision-0.1.10-py3-none-any.whl.
File metadata
- Download URL: mcp_server_spectrum_stravision-0.1.10-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85618906723b751a55f54693cc19a1eea8b66040462f5be083a5373bc1bfe8cc
|
|
| MD5 |
027d8105bf2540ea0d660fc00e8e44bc
|
|
| BLAKE2b-256 |
b328332d15d342557f7be277c996f87056ec8c51ee76bf66fe96803346bad1ac
|