串行MCP
MCP服务器允许代理与连接到计算机串行端口的设备进行通信。仅使用MAC atm进行测试。
采用FastMCP框架构建的强大串行通信服务器,为串行端口通信提供可靠的接口,具有消息缓冲、错误处理和连接管理等功能。
安装
- 克隆存储库:
git clone https://github.com/PaDev1/Serial-MCP.git
cd Serial-MCP- 安装依赖项:
pip install -r requirements.txt安装到Claude Desktop或类似设备后,按照以下结构添加服务器
“serial_MCP”:{ “command”:“uv”, “args”:\[ “运行”, “--带有”, “fastmcp,pyserial”, “fastmcp”, “运行”, " /serial_MCP.py” \], “env”:{ “SERIAL_PORT”:", “SERIAL_bud_RATE”:“9600”, “SERIAL_BUFFER_LENGTH”:“100” } }
特性
- 异步串行通信:基于asyncio构建,实现高效的I/O操作
- 消息缓冲:支持时间戳的可配置缓冲区大小
- 连接管理:自动端口检测和连接处理
- 错误处理:全面的错误检测和报告
- 环境配置:支持环境变量和运行时配置
- 日志记录:详细的日志记录,包括时间戳和错误跟踪
可用工具
1.延迟
等待指定的秒数。
参数:
delay(浮点数):等待的秒数(必须为正)
退货:
- 成功状态
- 请求延迟时间
- 实际延迟时间(测量)
- 状态消息
例子:
await delay({
"delay": 2.5 # Wait for 2.5 seconds
})2.初始序列
使用指定参数初始化串行连接。
参数:
port(str):串行端口设备路径(例如,“/dev/tty.usbmodel1101”)baudrate(int):通信速度(波特率)(默认值:9600)buffer_length(int):缓冲区的最大消息数(默认值:100)
退货:
- 初始化状态
- 当前模式(真实/断开)
- 端口和波特率信息
3.发送消息
通过串行连接发送消息,并可选择等待响应。
参数:
message(str):要发送的消息wait_for_response(bool):发送后是否等待响应(默认值:False)response_timeout(float):等待响应的时间(秒)(默认值:0.5)
退货:
success(bool):整体运营成功status(str):操作状态(“成功”、“错误”等)mode(str):当前连接模式(“真实”、“断开”)write_status(str):写入操作状态:
- “成功”:消息已成功写入 - “failed”:写入操作失败 - “timeout”:写入操作超时
connection_state(str):当前连接状态:
- “已连接”:串行端口已连接且可用 - “断开连接”:无活动串行连接 - “error”:连接处于错误状态
response_status(str):响应状态:
- “not_checked”:未请求响应检查 - “已收到”:已收到响应 - “no_response”:超时内未收到响应 - “error”:检查响应时出错
response_messages(list):收到的消息列表(如果有的话)bytes_written(int):成功写入的字节数error_type(str):操作失败时的错误类型message(str):人类可读状态消息error_details(dict):附加错误信息(如适用)
无响应发送检查示例:
await send_message({
"message": "Hello",
"wait_for_response": False
})
# Returns:
{
"success": True,
"status": "success",
"mode": "real",
"write_status": "success",
"connection_state": "connected",
"response_status": "not_checked",
"response_messages": [],
"bytes_written": 6,
"message": "Message sent successfully in real mode"
}带响应检查的发送示例:
await send_message({
"message": "Hello",
"wait_for_response": True,
"response_timeout": 1.0
})
# Returns:
{
"success": True,
"status": "success",
"mode": "real",
"write_status": "success",
"connection_state": "connected",
"response_status": "received",
"response_messages": [
{
"timestamp": "2024-03-14T15:30:45.123456",
"message": "Hello received"
}
],
"bytes_written": 6,
"message": "Message sent successfully in real mode"
}超时内无响应示例:
await send_message({
"message": "Hello",
"wait_for_response": True,
"response_timeout": 0.5
})
# Returns:
{
"success": True,
"status": "success",
"mode": "real",
"write_status": "success",
"connection_state": "connected",
"response_status": "no_response",
"response_messages": [],
"bytes_written": 6,
"message": "Message sent successfully in real mode"
}4.read_message
从缓冲区读取消息。
参数:
wait(bool):如果缓冲区为空,是否等待消息(默认值:False)timeout(float):等待消息的时间(秒)(默认值:1.0)
退货:
- 带有时间戳的消息列表
- 当前模式
- 读取操作状态
5.列表_串行端口
列出系统上所有可用的串行端口。
参数: 无
退货:
- 可用端口列表及详细信息
- 端口名称和描述
- 硬件标识符
6.获取序列状态
获取串行连接的当前状态。
参数: 无
退货:
- 当前模式
- 端口和波特率信息
- 缓冲区长度
- 可用端口
- 最后一条错误消息
- 串行模块可用性
7.配置序列
初始化后配置串行连接。
参数:
port(str,可选):要使用的新端口baudrate(int,可选):要使用的新波特率list_ports(bool):是否只列出端口(默认值:False)
退货:
- 已更新配置
- 当前模式
- 可用端口(如果list_ports为True)
8.近距离试验
关闭当前的串行连接。
参数: 无
退货:
- 关闭状态
- 当前模式
9.帮助
获取有关如何使用串行MCP服务器的详细说明。
参数: 无
退货:
- 详细的帮助信息包括:
- 工具说明和参数 - 每个工具的示例用法 - 常见的响应格式 - 连接状态 - 响应状态 - 重要使用说明
例子:
await help()
# Returns comprehensive help information about all available tools and their usage配置
可以使用环境变量配置服务器:
SERIAL_PORT:串行端口设备路径(默认:“/dev/tty.usb\*”)SERIAL_BAUD_RATE:通信速度(波特率)(默认值:9600)SERIAL_BUFFER_LENGTH:要缓冲的最大消息数(默认值:100)
用法
启动服务器
python serial_MCP.pyAPI终点
初始化串行连接
await init_serial({
"port": "/dev/tty.usbmodem1101",
"baudrate": 9600,
"buffer_length": 100
})发送消息
await send_message({
"message": "Hello, device!"
})读取消息
await read_message({
"wait": False, # Whether to wait for messages
"timeout": 1.0 # Timeout in seconds
})列出可用端口
await list_serial_ports()获取连接状态
await get_serial_status()配置连接
await configure_serial({
"port": "/dev/tty.usbmodem1101",
"baudrate": 9600,
"list_ports": False
})关闭连接
await close_serial()消息格式
缓冲区中的消息与时间戳一起存储:
{
"timestamp": "2024-03-14T15:30:45.123456", # ISO format
"message": "Actual message content"
}错误处理
服务器为各种场景提供详细的错误消息:
- 连接失败
- 无效参数
- 超时条件
- 缓冲区溢出
- 无效消息
日志记录
日志被写入 logs/serial_MCP.log 格式如下:
[timestamp] - [level] - [message]需求
- Python 3.7+
- pyserial>=3.5
- fastmcp>=0.1.0
- 媒染剂>=2.0.0
- 异步>=3.4.3
贡献
- 分叉存储库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
致谢
- 服务器基础架构的FastMCP框架
- pyserial用于串行通信功能
