@tuanjie/mcp_serial 0.0.0-stage → 2.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +255 -2
- package/README_CN.md +256 -0
- package/build/cli/command-line-parser.js +99 -0
- package/build/config/server.js +7 -0
- package/build/core/mcp-server.js +191 -0
- package/build/index.js +52 -0
- package/build/models/types.js +1 -0
- package/build/services/history-service.js +377 -0
- package/build/services/serial-connection-manager.js +928 -0
- package/build/tools/connect.js +69 -0
- package/build/tools/connection-status.js +27 -0
- package/build/tools/disconnect.js +46 -0
- package/build/tools/export-log.js +81 -0
- package/build/tools/index.js +21 -0
- package/build/tools/log-between.js +71 -0
- package/build/tools/realtime-speed.js +24 -0
- package/build/tools/send-command.js +114 -0
- package/build/tools/serial-detect.js +30 -0
- package/build/utils/logger.js +81 -0
- package/build/utils/serial-collect.js +22 -0
- package/build/utils/serial-line-split.js +47 -0
- package/build/utils/time-format.js +142 -0
- package/package.json +44 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 junki.cn
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
7
|
+
copyright notice and this permission notice appear in all copies.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
10
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
11
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
12
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
13
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
14
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,256 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 📡 mcp_serial
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
Serial Port-based MCP (Model Context Protocol) server that allows communication with serial devices via the MCP protocol.
|
|
6
|
+
|
|
7
|
+
English Document | [中文文档](README_CN.md)
|
|
8
|
+
|
|
9
|
+
## 📝 Project Overview
|
|
10
|
+
|
|
11
|
+
mcp_serial is a bridging tool that enables AI assistants and other applications supporting the MCP protocol to communicate with serial port devices through a standardized interface. This allows AI assistants to read from and write to serial devices, configure serial port settings, and monitor connection status without direct serial port handling.
|
|
12
|
+
|
|
13
|
+
## ✨ Key Features
|
|
14
|
+
|
|
15
|
+
- **🔌 Serial Port Support**: Connect to multiple serial ports simultaneously
|
|
16
|
+
- **📖 Read/Write Operations**: Read data from and write data to serial ports with timeout and filtering options
|
|
17
|
+
- **⚙️ Flexible Configuration**: Configure baud rate, data bits, stop bits, parity, and flow control
|
|
18
|
+
- **🔄 Standardized Interface**: Complies with MCP protocol specifications for seamless integration with AI assistants
|
|
19
|
+
- **📊 Status Monitoring**: Monitor connection status, bytes transferred, and port information
|
|
20
|
+
- **🚀 Ready to Use**: Can be run directly using NPX without global installation
|
|
21
|
+
|
|
22
|
+
## 🛠️ Tools List
|
|
23
|
+
|
|
24
|
+
| Tool | Name | Description |
|
|
25
|
+
|---------|-----------|----------|
|
|
26
|
+
| list-ports | List Ports Tool | List all available serial ports on the system |
|
|
27
|
+
| open-port | Open Port Tool | Open a serial port connection |
|
|
28
|
+
| execute-command | Execute Command Tool | Execute a command on serial port and get output (waits for prompt or timeout) |
|
|
29
|
+
| list-connections | List Connections Tool | List all configured serial port connections |
|
|
30
|
+
| add-connection | Add Connection Tool | Add a new serial port connection configuration |
|
|
31
|
+
| list-history | List History Tool | List serial port operation history records |
|
|
32
|
+
|
|
33
|
+
## 📚 Usage
|
|
34
|
+
|
|
35
|
+
### 🔧 MCP Configuration Examples
|
|
36
|
+
|
|
37
|
+
> **⚠️ Important**: In MCP configuration files, each command line argument and its value must be separate elements in the `args` array. Do NOT combine them with spaces. For example, use `"--path", "/dev/ttyUSB0"` instead of `"--path /dev/ttyUSB0"`.
|
|
38
|
+
|
|
39
|
+
#### ⚙️ Command Line Options
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
Options:
|
|
43
|
+
-p, --path Serial port path (e.g., /dev/ttyUSB0, COM3)
|
|
44
|
+
-b, --baudRate Baud rate (default: 9600)
|
|
45
|
+
--dataBits Data bits: 5, 6, 7, or 8 (default: 8)
|
|
46
|
+
--stopBits Stop bits: 1 or 2 (default: 1)
|
|
47
|
+
--parity Parity: none, even, odd, mark, or space (default: none)
|
|
48
|
+
--rtscts Enable RTS/CTS flow control (default: false)
|
|
49
|
+
--xon Enable XON/XOFF flow control (default: false)
|
|
50
|
+
--xoff Enable XOFF flow control (default: false)
|
|
51
|
+
--xany Enable XANY flow control (default: false)
|
|
52
|
+
--serial Multiple serial connections: "name=port1,path=/dev/ttyUSB0,baudRate=9600"
|
|
53
|
+
--transport Transport mode: stdio or http (default: stdio)
|
|
54
|
+
--httpPort HTTP server port (default: 8800, only for http transport)
|
|
55
|
+
--httpHost HTTP server host (default: 0.0.0.0, only for http transport)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### 🔌 Basic Serial Port Connection
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"mcpServers": {
|
|
63
|
+
"mcp_serial": {
|
|
64
|
+
"command": "npx",
|
|
65
|
+
"args": [
|
|
66
|
+
"-y",
|
|
67
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
68
|
+
"--path", "/dev/ttyUSB0",
|
|
69
|
+
"--baudRate", "115200"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### ⚙️ Advanced Configuration
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"mcpServers": {
|
|
81
|
+
"mcp_serial": {
|
|
82
|
+
"command": "npx",
|
|
83
|
+
"args": [
|
|
84
|
+
"-y",
|
|
85
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
86
|
+
"--path", "/dev/ttyUSB0",
|
|
87
|
+
"--baudRate", "115200",
|
|
88
|
+
"--dataBits", "8",
|
|
89
|
+
"--stopBits", "1",
|
|
90
|
+
"--parity", "none",
|
|
91
|
+
"--rtscts"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### 🌐 HTTP Transport Mode
|
|
99
|
+
|
|
100
|
+
For HTTP transport mode, use the `--transport` option:
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"mcpServers": {
|
|
105
|
+
"mcp_serial": {
|
|
106
|
+
"command": "npx",
|
|
107
|
+
"args": [
|
|
108
|
+
"-y",
|
|
109
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
110
|
+
"--path", "/dev/ttyUSB0",
|
|
111
|
+
"--baudRate", "115200",
|
|
112
|
+
"--transport", "http",
|
|
113
|
+
"--httpPort", "9813",
|
|
114
|
+
"--httpHost", "0.0.0.0"
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 🔗 Multiple Serial Port Connections
|
|
122
|
+
|
|
123
|
+
You can specify multiple serial port connections by passing multiple --serial parameters:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npx @tuanjie/mcp_serial@2.3.2 \
|
|
127
|
+
--serial "name=port1,path=/dev/ttyUSB0,baudRate=9600" \
|
|
128
|
+
--serial "name=port2,path=/dev/ttyUSB1,baudRate=115200"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
In MCP tool calls, specify the connection name via the `connectionName` parameter. If omitted, the default connection is used.
|
|
132
|
+
|
|
133
|
+
Example (execute command on 'port1' connection):
|
|
134
|
+
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"tool": "execute-command",
|
|
138
|
+
"params": {
|
|
139
|
+
"command": "show version",
|
|
140
|
+
"connectionName": "port1",
|
|
141
|
+
"timeout": 5000,
|
|
142
|
+
"prompt": "#"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 📖 Tool Usage Examples
|
|
148
|
+
|
|
149
|
+
#### List Available Ports
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"tool": "list-ports",
|
|
154
|
+
"params": {}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### Open a Serial Port
|
|
159
|
+
|
|
160
|
+
```json
|
|
161
|
+
{
|
|
162
|
+
"tool": "open-port",
|
|
163
|
+
"params": {
|
|
164
|
+
"connectionName": "default"
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### Execute Command on Serial Port
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{
|
|
173
|
+
"tool": "execute-command",
|
|
174
|
+
"params": {
|
|
175
|
+
"command": "show version",
|
|
176
|
+
"connectionName": "default",
|
|
177
|
+
"timeout": 5000,
|
|
178
|
+
"prompt": "#",
|
|
179
|
+
"skipEcho": true
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Add a New Connection
|
|
185
|
+
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"tool": "add-connection",
|
|
189
|
+
"params": {
|
|
190
|
+
"name": "device1",
|
|
191
|
+
"path": "/dev/ttyUSB0",
|
|
192
|
+
"baudRate": 115200
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### List Operation History
|
|
198
|
+
|
|
199
|
+
```json
|
|
200
|
+
{
|
|
201
|
+
"tool": "list-history",
|
|
202
|
+
"params": {
|
|
203
|
+
"connectionName": "default",
|
|
204
|
+
"operation": "read",
|
|
205
|
+
"limit": 10,
|
|
206
|
+
"offset": 0
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### Get Specific History Record by ID
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"tool": "list-history",
|
|
216
|
+
"params": {
|
|
217
|
+
"id": "record-id-here"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## 🛡️ Security Considerations
|
|
223
|
+
|
|
224
|
+
This server provides capabilities to communicate with serial port devices. To ensure it is used securely, please consider the following:
|
|
225
|
+
|
|
226
|
+
- **Port Permissions**: Ensure proper permissions are set for serial port access (e.g., add user to `dialout` group on Linux)
|
|
227
|
+
- **Device Access**: Only connect to trusted serial devices
|
|
228
|
+
- **Data Handling**: Be cautious when reading/writing sensitive data through serial ports
|
|
229
|
+
|
|
230
|
+
## 📋 Requirements
|
|
231
|
+
|
|
232
|
+
- Node.js 18+
|
|
233
|
+
- Serial port access permissions
|
|
234
|
+
- **picocom** command-line tool (required for serial communication)
|
|
235
|
+
- Linux: `apt-get install picocom` or `yum install picocom`
|
|
236
|
+
- macOS: `brew install picocom`
|
|
237
|
+
- Windows: Not supported (picocom is Linux/Unix only)
|
|
238
|
+
- On Linux: User should be in `dialout` group
|
|
239
|
+
- On Windows: Administrator privileges may be required for some COM ports (Note: Windows is not fully supported due to picocom dependency)
|
|
240
|
+
|
|
241
|
+
## 🔧 Installation
|
|
242
|
+
|
|
243
|
+
### Using NPX (Recommended)
|
|
244
|
+
|
|
245
|
+
No installation needed, use directly:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
npx @tuanjie/mcp_serial@2.3.2 --path /dev/ttyUSB0
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Local Installation
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
npm install -g @tuanjie/mcp_serial@2.3.2
|
|
255
|
+
mcp_serial --path /dev/ttyUSB0
|
|
256
|
+
```
|
package/README_CN.md
ADDED
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
# 📡 mcp_serial
|
|
2
|
+
|
|
3
|
+
基于串口的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议与串口设备通信。
|
|
4
|
+
|
|
5
|
+
[English Document](README.md) | 中文文档
|
|
6
|
+
|
|
7
|
+
## 📝 项目介绍
|
|
8
|
+
|
|
9
|
+
mcp_serial 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口与串口设备通信。这使得 AI 助手能够读写串口数据、配置串口参数、监控连接状态,而无需直接处理串口操作。
|
|
10
|
+
|
|
11
|
+
## ✨ 功能亮点
|
|
12
|
+
|
|
13
|
+
- **🔌 串口支持**:同时连接多个串口设备
|
|
14
|
+
- **📖 读写操作**:支持带超时和过滤选项的串口数据读写
|
|
15
|
+
- **⚙️ 灵活配置**:可配置波特率、数据位、停止位、校验位和流控
|
|
16
|
+
- **🔄 标准化接口**:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
|
|
17
|
+
- **📊 状态监控**:监控连接状态、传输字节数和端口信息
|
|
18
|
+
- **🚀 即用即走**:使用 NPX 可直接运行,无需全局安装
|
|
19
|
+
|
|
20
|
+
## 🛠️ 工具列表
|
|
21
|
+
|
|
22
|
+
| 工具 | 名称 | 描述 |
|
|
23
|
+
|---------|-----------|----------|
|
|
24
|
+
| list-ports | 列出端口工具 | 列出系统中所有可用的串口 |
|
|
25
|
+
| open-port | 打开端口工具 | 打开串口连接 |
|
|
26
|
+
| execute-command | 执行命令工具 | 在串口上执行命令并获取输出(等待提示符或超时) |
|
|
27
|
+
| list-connections | 列出连接工具 | 列出所有配置的串口连接 |
|
|
28
|
+
| add-connection | 添加连接工具 | 添加新的串口连接配置 |
|
|
29
|
+
| list-history | 列出历史工具 | 列出串口操作历史记录 |
|
|
30
|
+
|
|
31
|
+
设计与契约(含操作审计)见 [`docs/`](docs/README.md);工具入参出参见 [接口契约](docs/002_contract-interface_01_interface-contract.md),历史追溯见 [日志与审计](docs/006_operations-observability_03_logging.md)。
|
|
32
|
+
|
|
33
|
+
## 📚 使用方法
|
|
34
|
+
|
|
35
|
+
### 🔧 MCP 配置示例
|
|
36
|
+
|
|
37
|
+
> **⚠️ 重要提示**: 在 MCP 配置文件中,每个命令行参数和其值必须是 `args` 数组中的独立元素。不要用空格将它们连接在一起。例如,使用 `"--path", "/dev/ttyUSB0"` 而不是 `"--path /dev/ttyUSB0"`。
|
|
38
|
+
|
|
39
|
+
#### ⚙️ 命令行选项
|
|
40
|
+
|
|
41
|
+
```text
|
|
42
|
+
选项:
|
|
43
|
+
-p, --path 串口路径 (例如: /dev/ttyUSB0, COM3)
|
|
44
|
+
-b, --baudRate 波特率 (默认: 9600)
|
|
45
|
+
--dataBits 数据位: 5, 6, 7, 或 8 (默认: 8)
|
|
46
|
+
--stopBits 停止位: 1 或 2 (默认: 1)
|
|
47
|
+
--parity 校验位: none, even, odd, mark, 或 space (默认: none)
|
|
48
|
+
--rtscts 启用 RTS/CTS 流控 (默认: false)
|
|
49
|
+
--xon 启用 XON/XOFF 流控 (默认: false)
|
|
50
|
+
--xoff 启用 XOFF 流控 (默认: false)
|
|
51
|
+
--xany 启用 XANY 流控 (默认: false)
|
|
52
|
+
--serial 多个串口连接: "name=port1,path=/dev/ttyUSB0,baudRate=9600"
|
|
53
|
+
--transport 传输模式: stdio 或 http (默认: stdio)
|
|
54
|
+
--httpPort HTTP 服务器端口 (默认: 8800, 仅用于 http 传输)
|
|
55
|
+
--httpHost HTTP 服务器主机 (默认: 0.0.0.0, 仅用于 http 传输)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### 🔌 基本串口连接
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"mcpServers": {
|
|
63
|
+
"mcp_serial": {
|
|
64
|
+
"command": "npx",
|
|
65
|
+
"args": [
|
|
66
|
+
"-y",
|
|
67
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
68
|
+
"--path", "/dev/ttyUSB0",
|
|
69
|
+
"--baudRate", "115200"
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### ⚙️ 高级配置
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{
|
|
80
|
+
"mcpServers": {
|
|
81
|
+
"mcp_serial": {
|
|
82
|
+
"command": "npx",
|
|
83
|
+
"args": [
|
|
84
|
+
"-y",
|
|
85
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
86
|
+
"--path", "/dev/ttyUSB0",
|
|
87
|
+
"--baudRate", "115200",
|
|
88
|
+
"--dataBits", "8",
|
|
89
|
+
"--stopBits", "1",
|
|
90
|
+
"--parity", "none",
|
|
91
|
+
"--rtscts"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### 🌐 HTTP 传输模式
|
|
99
|
+
|
|
100
|
+
使用 HTTP 传输模式时,使用 `--transport` 选项:
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{
|
|
104
|
+
"mcpServers": {
|
|
105
|
+
"mcp_serial": {
|
|
106
|
+
"command": "npx",
|
|
107
|
+
"args": [
|
|
108
|
+
"-y",
|
|
109
|
+
"@tuanjie/mcp_serial@2.3.2",
|
|
110
|
+
"--path", "/dev/ttyUSB0",
|
|
111
|
+
"--baudRate", "115200",
|
|
112
|
+
"--transport", "http",
|
|
113
|
+
"--httpPort", "9813",
|
|
114
|
+
"--httpHost", "0.0.0.0"
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 🔗 多串口连接
|
|
122
|
+
|
|
123
|
+
可以通过传递多个 --serial 参数来指定多个串口连接:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npx @tuanjie/mcp_serial@2.3.2 \
|
|
127
|
+
--serial "name=port1,path=/dev/ttyUSB0,baudRate=9600" \
|
|
128
|
+
--serial "name=port2,path=/dev/ttyUSB1,baudRate=115200"
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
在 MCP 工具调用中,通过 `connectionName` 参数指定连接名称。如果省略,则使用默认连接。
|
|
132
|
+
|
|
133
|
+
示例(在 'port1' 连接上执行命令):
|
|
134
|
+
|
|
135
|
+
```json
|
|
136
|
+
{
|
|
137
|
+
"tool": "execute-command",
|
|
138
|
+
"params": {
|
|
139
|
+
"command": "show version",
|
|
140
|
+
"connectionName": "port1",
|
|
141
|
+
"timeout": 5000,
|
|
142
|
+
"prompt": "#"
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
### 📖 工具使用示例
|
|
148
|
+
|
|
149
|
+
#### 列出可用端口
|
|
150
|
+
|
|
151
|
+
```json
|
|
152
|
+
{
|
|
153
|
+
"tool": "list-ports",
|
|
154
|
+
"params": {}
|
|
155
|
+
}
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### 打开串口
|
|
159
|
+
|
|
160
|
+
```json
|
|
161
|
+
{
|
|
162
|
+
"tool": "open-port",
|
|
163
|
+
"params": {
|
|
164
|
+
"connectionName": "default"
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### 在串口上执行命令
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{
|
|
173
|
+
"tool": "execute-command",
|
|
174
|
+
"params": {
|
|
175
|
+
"command": "show version",
|
|
176
|
+
"connectionName": "default",
|
|
177
|
+
"timeout": 5000,
|
|
178
|
+
"prompt": "#",
|
|
179
|
+
"skipEcho": true
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### 添加新连接
|
|
185
|
+
|
|
186
|
+
```json
|
|
187
|
+
{
|
|
188
|
+
"tool": "add-connection",
|
|
189
|
+
"params": {
|
|
190
|
+
"name": "device1",
|
|
191
|
+
"path": "/dev/ttyUSB0",
|
|
192
|
+
"baudRate": 115200
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
#### 列出操作历史
|
|
198
|
+
|
|
199
|
+
```json
|
|
200
|
+
{
|
|
201
|
+
"tool": "list-history",
|
|
202
|
+
"params": {
|
|
203
|
+
"connectionName": "default",
|
|
204
|
+
"operation": "read",
|
|
205
|
+
"limit": 10,
|
|
206
|
+
"offset": 0
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### 根据ID获取特定历史记录
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"tool": "list-history",
|
|
216
|
+
"params": {
|
|
217
|
+
"id": "record-id-here"
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## 🛡️ 安全考虑
|
|
223
|
+
|
|
224
|
+
本服务器提供与串口设备通信的功能。为确保安全使用,请注意以下事项:
|
|
225
|
+
|
|
226
|
+
- **端口权限**:确保正确设置串口访问权限(例如,在 Linux 上将用户添加到 `dialout` 组)
|
|
227
|
+
- **设备访问**:仅连接到可信的串口设备
|
|
228
|
+
- **数据处理**:通过串口读写敏感数据时要小心
|
|
229
|
+
|
|
230
|
+
## 📋 系统要求
|
|
231
|
+
|
|
232
|
+
- Node.js 18+
|
|
233
|
+
- 串口访问权限
|
|
234
|
+
- **picocom** 命令行工具(串口通信必需)
|
|
235
|
+
- Linux: `apt-get install picocom` 或 `yum install picocom`
|
|
236
|
+
- macOS: `brew install picocom`
|
|
237
|
+
- Windows: 不支持(picocom 仅支持 Linux/Unix)
|
|
238
|
+
- 在 Linux 上:用户应在 `dialout` 组中
|
|
239
|
+
- 在 Windows 上:某些 COM 端口可能需要管理员权限(注意:由于 picocom 依赖,Windows 不完全支持)
|
|
240
|
+
|
|
241
|
+
## 🔧 安装
|
|
242
|
+
|
|
243
|
+
### 使用 NPX(推荐)
|
|
244
|
+
|
|
245
|
+
无需安装,直接使用:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
npx @tuanjie/mcp_serial@2.3.2 --path /dev/ttyUSB0
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### 本地安装
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
npm install -g @tuanjie/mcp_serial@2.3.2
|
|
255
|
+
mcp_serial --path /dev/ttyUSB0
|
|
256
|
+
```
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
export class CommandLineParser {
|
|
3
|
+
/**
|
|
4
|
+
* Parse command line arguments
|
|
5
|
+
*/
|
|
6
|
+
static parseArgs() {
|
|
7
|
+
const { values, positionals } = parseArgs({
|
|
8
|
+
args: process.argv.slice(2),
|
|
9
|
+
options: {
|
|
10
|
+
serial: { type: "string", multiple: true },
|
|
11
|
+
path: { type: "string", short: "p" },
|
|
12
|
+
baudRate: { type: "string", short: "b" },
|
|
13
|
+
dataBits: { type: "string" },
|
|
14
|
+
stopBits: { type: "string" },
|
|
15
|
+
parity: { type: "string" },
|
|
16
|
+
rtscts: { type: "boolean" },
|
|
17
|
+
xon: { type: "boolean" },
|
|
18
|
+
xoff: { type: "boolean" },
|
|
19
|
+
xany: { type: "boolean" },
|
|
20
|
+
transport: { type: "string" },
|
|
21
|
+
httpPort: { type: "string" },
|
|
22
|
+
httpHost: { type: "string" },
|
|
23
|
+
"pre-connect": { type: "boolean" },
|
|
24
|
+
},
|
|
25
|
+
allowPositionals: true,
|
|
26
|
+
});
|
|
27
|
+
const transport = values.transport === "http" ? "http" : "stdio";
|
|
28
|
+
const serverOptions = {
|
|
29
|
+
transport,
|
|
30
|
+
...(transport === "http" && {
|
|
31
|
+
httpPort: values.httpPort ? parseInt(values.httpPort, 10) : 8800,
|
|
32
|
+
httpHost: values.httpHost || "0.0.0.0",
|
|
33
|
+
}),
|
|
34
|
+
};
|
|
35
|
+
const configs = {};
|
|
36
|
+
const serialParams = Array.isArray(values.serial)
|
|
37
|
+
? values.serial
|
|
38
|
+
: values.serial
|
|
39
|
+
? [values.serial]
|
|
40
|
+
: [];
|
|
41
|
+
for (const serialStr of serialParams) {
|
|
42
|
+
const conf = {};
|
|
43
|
+
for (const part of serialStr.split(",")) {
|
|
44
|
+
const [k, v] = part.split("=");
|
|
45
|
+
if (k && v)
|
|
46
|
+
conf[k.trim()] = v.trim();
|
|
47
|
+
}
|
|
48
|
+
if (!conf.name || !conf.path) {
|
|
49
|
+
throw new Error("Each --serial must include name and path");
|
|
50
|
+
}
|
|
51
|
+
configs[conf.name] = {
|
|
52
|
+
name: conf.name,
|
|
53
|
+
path: conf.path,
|
|
54
|
+
baudRate: conf.baudRate ? parseInt(conf.baudRate, 10) : undefined,
|
|
55
|
+
dataBits: conf.dataBits
|
|
56
|
+
? parseInt(conf.dataBits, 10)
|
|
57
|
+
: undefined,
|
|
58
|
+
stopBits: conf.stopBits
|
|
59
|
+
? parseInt(conf.stopBits, 10)
|
|
60
|
+
: undefined,
|
|
61
|
+
parity: conf.parity,
|
|
62
|
+
rtscts: conf.rtscts === "true" || conf.rtscts === "1",
|
|
63
|
+
xon: conf.xon === "true" || conf.xon === "1",
|
|
64
|
+
xoff: conf.xoff === "true" || conf.xoff === "1",
|
|
65
|
+
xany: conf.xany === "true" || conf.xany === "1",
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (Object.keys(configs).length === 0) {
|
|
69
|
+
const path = (values.path || positionals[0]);
|
|
70
|
+
if (path) {
|
|
71
|
+
const baudRate = values.baudRate
|
|
72
|
+
? parseInt(values.baudRate, 10)
|
|
73
|
+
: undefined;
|
|
74
|
+
configs["default"] = {
|
|
75
|
+
name: "default",
|
|
76
|
+
path,
|
|
77
|
+
baudRate: baudRate || 9600,
|
|
78
|
+
dataBits: values.dataBits
|
|
79
|
+
? parseInt(values.dataBits, 10)
|
|
80
|
+
: undefined,
|
|
81
|
+
stopBits: values.stopBits
|
|
82
|
+
? parseInt(values.stopBits, 10)
|
|
83
|
+
: undefined,
|
|
84
|
+
parity: values.parity,
|
|
85
|
+
rtscts: values.rtscts,
|
|
86
|
+
xon: values.xon,
|
|
87
|
+
xoff: values.xoff,
|
|
88
|
+
xany: values.xany,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// 无启动参数时允许空配置,由 connect 工具运行时连接
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
configs,
|
|
95
|
+
preConnect: values["pre-connect"] === true,
|
|
96
|
+
serverOptions,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}
|