小车与边缘设备驱动 MCP Server
将《小车与边缘设备 API 对接文档》中的接口封装为 Python MCP (Model Context Protocol) 服务,并在此基础上提供面向智能体编排的复合工具(启动 → 等待 → 自动停止)。支持本地 stdio 与 K8s/远程 streamable-http 两种运行方式。
说明:本文档中的设备地址、端口均为示例占位,请通过
.env或环境变量替换为实际地址。
一、设备与接口(示例)
| 设备类型 | 地址(示例) | 端口(示例) | 功能 |
|---|---|---|---|
| 边缘设备 | 203.0.113.10 |
5062 |
应用控制 API |
| 小车设备 | 203.0.113.10 |
8085 |
实时视频流 |
| 接口 | 请求地址 | 方式 |
|---|---|---|
| 应用控制(巡线/抓取启停) | POST http://<EDGE_HOST>:<EDGE_PORT>/api/cmd |
POST (JSON) |
| 获取实时视频流 | GET http://<CAR_HOST>:<CAR_PORT>/stream |
GET |
应用控制请求体:
{"cmd":"app","name":"line|grasp","action":"start|stop"}
二、封装的 MCP 工具
| 工具 | 类型 | 说明 |
|---|---|---|
control_app(name, action) |
单步 | 底层控制:name ∈ line(巡线)/grasp(抓取),action ∈ start/stop |
patrol_line(event_id?, wait_seconds?) |
复合 | 启动巡线 → 等待(默认 PATROL_WAIT_SECONDS) → 自动停止 |
grasp_sort(event_id?, wait_seconds?) |
复合 | 启动抓取/分拣 → 等待(默认 GRASP_WAIT_SECONDS) → 自动停止 |
capture_frame(save_path?) |
单步 | 从视频流截取一帧,上传 MinIO 并返回可访问 URL |
list_devices() |
单步 | 返回设备网络信息 |
三、复合工具(面向智能体)
patrol_line / grasp_sort 在服务端内部完成 启动 → 等待 → 自动停止,智能体只需调用一次。
为什么不阻塞等待? 平台侧 MCP 客户端超时通常只有 25 秒,阻塞 30 秒会被判超时。因此:
- 启动后立即返回
status=DISPATCHED,stop.at为计划停止时间; - 停止由服务端后台线程定时执行(默认 30 秒后)。
返回示例:
{
"ok": true,
"status": "DISPATCHED",
"device": "巡查小车",
"start": {"ok": true, "status_code": 200, "response": "..."},
"stop": {"scheduled": true, "at": "2026-11-26T10:30:00+08:00", "error": ""},
"start_time": "...",
"end_time": "...",
"waited_seconds": 30,
"error": ""
}
- 启动失败时:立即尽力补一次 stop,返回
status=FAILED、device_ack=false。 - 等待秒数:不传则用配置;可传 1–600 覆盖。
四、截帧(capture_frame)
capture_frame 按以下顺序尝试:
- ffmpeg(优先):支持 MJPEG / H.264 / RTSP 等任意格式。
- 纯 Python MJPEG 提取:若流是
multipart/x-mixed-replace,直接解析第一帧 JPEG。
自动定位 ffmpeg:FFMPEG_BIN > PATH > ~/.local/bin/ffmpeg > /usr/local/bin/ffmpeg 等。
若配置了 MinIO(MINIO_ENDPOINT/MINIO_ACCESS_KEY/MINIO_SECRET_KEY/MINIO_BUCKET 均非空),截帧后自动上传并返回可访问 URL;未配置则仅保存本地并带 note 提示。
五、本地安装与运行
要求 Python ≥ 3.10(推荐 3.12)。
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python server.py # 默认 stdio
# 或 HTTP 方式
MCP_TRANSPORT=streamable-http MCP_HOST=0.0.0.0 MCP_PORT=8000 python server.py
六、配置
优先级:环境变量 > .env > 内置默认值。
| 环境变量 / .env 键 | 默认值 | 说明 |
|---|---|---|
EDGE_DEVICE_HOST |
203.0.113.10 |
边缘设备主机(示例) |
EDGE_DEVICE_PORT |
5062 |
边缘设备端口(示例) |
CAR_HOST |
203.0.113.10 |
小车主机(示例) |
CAR_PORT |
8085 |
小车端口(示例) |
REQUEST_TIMEOUT |
10 | HTTP 请求超时(秒) |
FRAME_DIR |
captures | 截帧图片保存目录 |
FFMPEG_BIN |
自动查找 | ffmpeg 可执行文件路径 |
PATROL_WAIT_SECONDS |
30 | patrol_line 启动后自动停止的默认等待秒数(1-600) |
GRASP_WAIT_SECONDS |
30 | grasp_sort 启动后自动停止的默认等待秒数(1-600) |
MCP_TRANSPORT |
stdio | stdio / streamable-http / sse |
MCP_HOST |
0.0.0.0 | HTTP 传输监听地址 |
MCP_PORT |
8000 | HTTP 传输监听端口 |
MINIO_ENDPOINT |
空 | MinIO 地址(host:port),留空则不启用上传 |
MINIO_ACCESS_KEY |
空 | MinIO 访问密钥 |
MINIO_SECRET_KEY |
空 | MinIO 私钥 |
MINIO_BUCKET |
空 | 目标 bucket |
MINIO_SECURE |
false | 是否 https |
MINIO_PUBLIC_URL |
空 | 公共访问地址;设置后返回拼接 URL,否则预签名 |
MINIO_OBJECT_PREFIX |
captures | object 名前缀 |
MINIO_URL_EXPIRES |
3600 | 预签名 URL 有效期(秒) |
七、Docker 与 K8s 部署
docker build -t device-driver-mcp-server:latest .
docker run --rm -p 8000:8000 \
-e EDGE_DEVICE_HOST=203.0.113.10 \
-e CAR_HOST=203.0.113.10 \
device-driver-mcp-server:latest
curl http://127.0.0.1:8000/healthz # {"status":"ok"}
kubectl apply -f k8s/deployment.yaml
- 健康检查:
/healthz(Deployment 已配 readiness/liveness 探针)。 - 默认以
streamable-http监听0.0.0.0:8000。 - 设备网络:Pod 需能访问设备地址;若集群与设备内网直连,可改内网地址。
- 多副本:当前为有状态会话,建议先
replicas: 1。
八、接入 MCP 客户端
stdio(Claude Desktop / Cursor)
{
"mcpServers": {
"device-driver-mcp-server": {
"command": "/path/to/.venv/bin/python",
"args": ["/path/to/server.py"],
"env": {
"EDGE_DEVICE_HOST": "203.0.113.10",
"CAR_HOST": "203.0.113.10"
}
}
}
}
streamable-http(远程):客户端配置 http://<host>:8000/mcp。
九、视频流查看
视频流地址通过内部接口 get_stream_url() 获取(不封装为 MCP 工具),供人工查看:
ffplay http://<CAR_HOST>:<CAR_PORT>/stream
# 或 VLC / 浏览器直接打开
十、智能体与平台接入
- 两个物端智能体的提示词:agent-prompts.md
- 现场巡查智能体 →
patrol_line - 应急保障准备智能体 →
grasp_sort
- 现场巡查智能体 →
- 接入 Coze 风格平台的配置与顺序:platform-integration.md
十一、目录结构
device-driver-mcp-server/
├── server.py # MCP 服务主程序
├── requirements.txt # Python 依赖(mcp 2.x)
├── Dockerfile # 容器镜像
├── .dockerignore
├── k8s/
│ └── deployment.yaml # K8s Deployment + Service
├── scripts/
│ ├── start-local.sh # 一键启动本地测试环境
│ ├── stop-local.sh # 一键停止
│ └── mock_device.py # mock 设备(可切 ok/fail/slow)
├── .env.example # 配置示例
├── agent-prompts.md # 两个智能体提示词
├── platform-integration.md # 平台接入说明
└── README.md
十二、本地一键启动/停止
# 启动:Docker 中间件 + 平台前后端 + MCP(:9090)
./scripts/start-local.sh
# 同时启动 mock 设备(:18080) 与 mock 版 MCP(:9091)
./scripts/start-local.sh --with-mock
# 停止(MCP / 平台前后端 / Docker 容器)
./scripts/stop-local.sh
# 连 Docker Desktop 一起退出
QUIT_DOCKER=true ./scripts/stop-local.sh
可覆盖变量:PLATFORM_DIR、DOCKER_BIN、GO_BIN、NPM_BIN、PLATFORM_BACKEND_PORT、PLATFORM_FRONTEND_PORT、MCP_PORT、MOCK_MCP_PORT、MOCK_HTTP_PORT。
mock 模式切换:改
/tmp/mock_mode为ok/fail/slow(即时生效)。
Metadata
Release files for device-driver-mcp-server 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| device_driver_mcp_server-0.1.0.tar.gz | 11.8 kB | Without provenance | Details for device_driver_mcp_server-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| device_driver_mcp_server-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for device_driver_mcp_server-0.1.0-py3-none-any.whl |
Total release size: 24.4 kB
Release files / device_driver_mcp_server-0.1.0.tar.gz
| Download URL | device_driver_mcp_server-0.1.0.tar.gz |
|---|---|
| Size | 11.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6d9bcb555042a2066b88e664aa0c0df89d87b71ddced97932bcdeeb6732889f2
|
|
BLAKE2b-256 checksum How to use checksums |
3c5b729c52ddb8a92d3d797df59b0f43218a35fc7953d2e8306652d897802eb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / device_driver_mcp_server-0.1.0-py3-none-any.whl
| Download URL | device_driver_mcp_server-0.1.0-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
01ac88a6b7199ed8fc3ef8f59a740684ce5daa1e6fa2a918d09be41d44a1ec0e
|
|
BLAKE2b-256 checksum How to use checksums |
040e61e6acd2961ad1b7d3355c38698631b2776d557ae90c49280da46672c4e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for device-driver-mcp-server 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.