@tuanjie/mcp_serial 2.3.2 → 2.3.3

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 CHANGED
@@ -1,15 +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.
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,256 +1,256 @@
1
- # 📡 mcp_serial
2
-
3
- ![NPM Version](https://img.shields.io/npm/v/%40tuanjie%2Fmcp_serial?label=%40tuanjie%2Fmcp_serial)
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
- ```
1
+ # 📡 mcp_serial
2
+
3
+ ![NPM Version](https://img.shields.io/npm/v/%40tuanjie%2Fmcp_serial?label=%40tuanjie%2Fmcp_serial)
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.3",
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.3",
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.3",
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.3 \
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
+ - **No external serial tools (e.g. picocom) required** — uses the `serialport` native binding (since v2.2.3)
235
+ - Prebuilt binaries available for Windows, Linux and macOS
236
+ - If the binding fails after install: `npm rebuild @serialport/bindings-cpp` (requires the node-gyp toolchain)
237
+ - Linux: user should be in the `dialout` group (e.g. `sudo usermod -aG dialout "$USER"`)
238
+ - macOS: grant the terminal/app access to the serial device
239
+ - Windows: COM ports (e.g. `COM3`) are supported; administrator privileges may be required for some ports
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.3 --path /dev/ttyUSB0
249
+ ```
250
+
251
+ ### Local Installation
252
+
253
+ ```bash
254
+ npm install -g @tuanjie/mcp_serial@2.3.3
255
+ mcp_serial --path /dev/ttyUSB0
256
+ ```
package/README_CN.md CHANGED
@@ -1,256 +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
- ```
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.3",
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.3",
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.3",
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.3 \
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 等外部串口工具**;使用 `serialport` 原生绑定(v2.2.3 起)
235
+ - Windows / Linux / macOS 均有预编译二进制
236
+ - 若安装后绑定异常:`npm rebuild @serialport/bindings-cpp`(需 node-gyp 工具链)
237
+ - Linux:用户应在 `dialout` 组中(`sudo usermod -aG dialout "$USER"`)
238
+ - macOS:需为终端/应用授予串口设备访问权限
239
+ - Windows:支持 `COM3` 等 COM 端口,部分端口可能需要管理员权限
240
+
241
+ ## 🔧 安装
242
+
243
+ ### 使用 NPX(推荐)
244
+
245
+ 无需安装,直接使用:
246
+
247
+ ```bash
248
+ npx @tuanjie/mcp_serial@2.3.3 --path /dev/ttyUSB0
249
+ ```
250
+
251
+ ### 本地安装
252
+
253
+ ```bash
254
+ npm install -g @tuanjie/mcp_serial@2.3.3
255
+ mcp_serial --path /dev/ttyUSB0
256
+ ```
package/build/index.js CHANGED
File without changes
@@ -40,10 +40,16 @@ export class HistoryService {
40
40
  return HistoryService.instance;
41
41
  }
42
42
  /**
43
- * 串口路径 → 安全文件名(如 /dev/ttyUSB0 → dev_ttyUSB0)
43
+ * 串口路径 → 安全文件名(兼容 Unix /dev/ttyUSB0 与 Windows \\.\COM3)
44
+ * 例:/dev/ttyUSB0 → dev_ttyUSB0;\\.\COM3 → COM3
44
45
  */
45
46
  portPathToSafeName(portPath) {
46
- return portPath.replace(/^\//, "").replace(/\//g, "_");
47
+ return portPath
48
+ .replace(/^\\\\\.\\/, "") // Windows 设备前缀 \\.\
49
+ .replace(/^[a-zA-Z]:[\\/]?/, "") // Windows 盘符 C:\
50
+ .replace(/^[\\/]+/, "") // 开头的路径分隔符
51
+ .replace(/[\\/:]/g, "_") // 路径分隔符/冒号 → 下划线
52
+ .replace(/[^\w.-]/g, "_"); // 其余非法字符兜底清洗
47
53
  }
48
54
  /**
49
55
  * 串口路径 + 日期 → 日志文件路径(如 dev_ttyUSB0_20260709.log)
@@ -3,6 +3,7 @@ import { splitSerialBuffer, sanitizeSerialLogLine } from "../utils/serial-line-s
3
3
  import { detectInteractiveAction, SERIAL_IDLE_MS } from "../utils/serial-collect.js";
4
4
  import { HistoryService } from "./history-service.js";
5
5
  import fs from "fs";
6
+ import path from "path";
6
7
  import { SerialPort } from "serialport";
7
8
  /**
8
9
  * Serial Port Connection Manager
@@ -891,18 +892,25 @@ export class SerialConnectionManager {
891
892
  productId: p.productId,
892
893
  });
893
894
  }
894
- try {
895
- for (const name of fs.readdirSync("/dev")) {
896
- if (/^tty(USB|ACM)\d+$/.test(name) || /^ttyS\d+$/.test(name)) {
897
- const fullPath = `/dev/${name}`;
898
- if (!byPath.has(fullPath)) {
899
- byPath.set(fullPath, { path: fullPath });
895
+ // 兜底扫描:Linux 扫 ttyUSB/ttyACM/ttyS;macOS 扫 tty.*/cu.*;Windows 无 /dev,跳过
896
+ if (process.platform === "linux" || process.platform === "darwin") {
897
+ try {
898
+ const devDir = "/dev";
899
+ const deviceNamePattern = process.platform === "linux"
900
+ ? /^(ttyUSB\d+|ttyACM\d+|ttyS\d+)$/
901
+ : /^(tty\..+|cu\..+)$/;
902
+ for (const name of fs.readdirSync(devDir)) {
903
+ if (deviceNamePattern.test(name)) {
904
+ const fullPath = path.join(devDir, name);
905
+ if (!byPath.has(fullPath)) {
906
+ byPath.set(fullPath, { path: fullPath });
907
+ }
900
908
  }
901
909
  }
902
910
  }
903
- }
904
- catch {
905
- // /dev 不可读时忽略
911
+ catch {
912
+ // /dev 不可读时忽略
913
+ }
906
914
  }
907
915
  return Array.from(byPath.values()).sort((a, b) => a.path.localeCompare(b.path));
908
916
  }
@@ -1,6 +1,7 @@
1
1
  import { formatLogTimestamp } from "./time-format.js";
2
2
  import fs from "fs";
3
3
  import path from "path";
4
+ import os from "node:os";
4
5
  /**
5
6
  * Logger class
6
7
  */
@@ -19,7 +20,9 @@ export class Logger {
19
20
  const day = String(shanghaiTime.getUTCDate()).padStart(2, "0");
20
21
  const hour = String(shanghaiTime.getUTCHours()).padStart(2, "0");
21
22
  const minute = String(shanghaiTime.getUTCMinutes()).padStart(2, "0");
22
- const logDir = "/var/log";
23
+ // 日志目录优先级:环境变量 > Unix /var/log > Windows 临时目录
24
+ const logDir = process.env.MCP_SERIAL_LOG_DIR ||
25
+ (process.platform === "win32" ? os.tmpdir() : "/var/log");
23
26
  if (!fs.existsSync(logDir)) {
24
27
  fs.mkdirSync(logDir, { mode: 0o755, recursive: true });
25
28
  }
package/package.json CHANGED
@@ -1,46 +1,46 @@
1
- {
2
- "name": "@tuanjie/mcp_serial",
3
- "version": "2.3.2",
4
- "description": "Serial Port-based MCP Server (基于串口的 MCP 服务器)",
5
- "main": "build/index.js",
6
- "type": "module",
7
- "bin": {
8
- "mcp_serial": "build/index.js"
9
- },
10
- "scripts": {
11
- "test": "echo \"Error: no test specified\" && exit 1",
12
- "build": "node scripts/build.js",
13
- "prepublishOnly": "npm run build"
14
- },
15
- "keywords": [
16
- "serial",
17
- "uart",
18
- "mcp",
19
- "server",
20
- "cli",
21
- "tty"
22
- ],
23
- "author": "Junki",
24
- "license": "ISC",
25
- "dependencies": {
26
- "@modelcontextprotocol/sdk": "^1.30.0",
27
- "serialport": "^13.0.0",
28
- "zod": "^3.25.76"
29
- },
30
- "devDependencies": {
31
- "@types/node": "^26.2.0",
32
- "typescript": "^5.9.3"
33
- },
34
- "files": [
35
- "build/**/*",
36
- "README.md",
37
- "README_CN.md"
38
- ],
39
- "publishConfig": {
40
- "access": "public",
41
- "registry": "https://registry.npmjs.org"
42
- },
43
- "allowScripts": {
44
- "@serialport/bindings-cpp@13.0.0": true
45
- }
46
- }
1
+ {
2
+ "name": "@tuanjie/mcp_serial",
3
+ "version": "2.3.3",
4
+ "description": "Serial Port-based MCP Server (基于串口的 MCP 服务器)",
5
+ "main": "build/index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "mcp_serial": "build/index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "echo \"Error: no test specified\" && exit 1",
12
+ "build": "node scripts/build.js",
13
+ "prepublishOnly": "npm run build"
14
+ },
15
+ "keywords": [
16
+ "serial",
17
+ "uart",
18
+ "mcp",
19
+ "server",
20
+ "cli",
21
+ "tty"
22
+ ],
23
+ "author": "Junki",
24
+ "license": "ISC",
25
+ "dependencies": {
26
+ "@modelcontextprotocol/sdk": "^1.30.0",
27
+ "serialport": "^13.0.0",
28
+ "zod": "^3.25.76"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^26.2.0",
32
+ "typescript": "^5.9.3"
33
+ },
34
+ "files": [
35
+ "build/**/*",
36
+ "README.md",
37
+ "README_CN.md"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public",
41
+ "registry": "https://registry.npmjs.org"
42
+ },
43
+ "allowScripts": {
44
+ "@serialport/bindings-cpp@13.0.0": true
45
+ }
46
+ }