A bridge connecting physical hardware with AI large language models through the Model Context Protocol (MCP), enabling natural language control of TCP devices.
English | 简体中文
通过自然语言控制硬件,开启物联网新纪元
mcp2tcp 系统架构图
mcp2tcp 工作流程图
mcp2tcp 将TCP设备接入AI大模型的项目,它通过 Model Context Protocol (MCP) 将物理世界与 AI 大模型无缝连接。最终实现:
智能TCP通信
MCP 协议集成
mcp2tcp 支持所有实现了 MCP 协议的客户端,包括:
客户端 | 特性支持 | 说明 |
---|---|---|
Claude Desktop | 完整支持 | 推荐使用,支持所有 MCP 功能 |
Continue | 完整支持 | 优秀的开发工具集成 |
Cline | 资源+工具 | 支持多种 AI 提供商 |
Zed | 基础支持 | 支持提示词命令 |
Sourcegraph Cody | 资源支持 | 通过 OpenCTX 集成 |
Firebase Genkit | 部分支持 | 支持资源列表和工具 |
得益于灵活的客户端支持,mcp2tcp 可以与多种 AI 模型协同工作:
Python3.11 或更高版本 Claude Desktop 或 Cline
下载 install.py
python install.py
# 下载安装脚本 curl -O https://raw.githubusercontent.com/mcp2everything/mcp2tcp/main/install_macos.py # 运行安装脚本 python3 install_macos.py
# 下载安装脚本 curl -O https://raw.githubusercontent.com/mcp2everything/mcp2tcp/main/install_ubuntu.py # 运行安装脚本 python3 install_ubuntu.py
安装脚本会自动完成以下操作:
windows powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex" MacOS curl -LsSf https://astral.sh/uv/install.sh | sh
主要依赖uv工具,所以当python和uv以及Claude或Cline安装好后就可以了。
在你的 MCP 客户端(如 Claude Desktop 或 Cline)配置文件中添加以下内容: 注意:如果使用的自动安装那么会自动配置Calude Desktop无需此步。 使用默认配置文件:
{ "mcpServers": { "mcp2tcp": { "command": "uvx", "args": [ "mcp2tcp" ] } } }
注意:修改配置后需要重启Cline或者Claude客户端软件
配置TCP和命令: 注意下面的配置默认为COM11 需要根据实际进行修改
# config.yaml tcp: # TCP服务器配置 remote_ip: "127.0.0.1" # 远端IP地址 port: 9999 # 端口号 connect_timeout: 3.0 # 连接超时时间,单位为秒 receive_timeout: 2.0 # 接收超时时间,单位为秒 communication_type: "client" # 通信类型,client或server response_start_string: "CMD" # 可选,TCP应答的开始字符串,默认为OK commands: # PWM控制命令 set_pwm: command: "CMD_PWM {frequency}" # frequency为0-100的整数,表示PWM占空比 need_parse: false # 不需要解析响应内容 data_type: "ascii" # 数据类型,ascii或hex parameters: - name: "frequency" type: "integer" description: "PWM frequency value (0-100)" required: true prompts: - "把PWM调到最大 (frequency=100)" - "把PWM调到最小 (frequency=0)" - "请将PWM设置为{frequency} (0-100的整数)" - "关闭PWM (frequency=0)" - "把PWM调到一半 (frequency=50)"
配置文件(config.yaml
)可以放在位置:
用户主目录(推荐个人使用)
# Windows系统 C:\Users\用户名\.mcp2tcp\config.yaml # macOS系统 /Users/用户名/.mcp2tcp/config.yaml # Linux系统 /home/用户名/.mcp2tcp/config.yaml
.mcp2tcp
目录:
# Windows系统(在命令提示符中) mkdir "%USERPROFILE%\.mcp2tcp" # macOS/Linux系统 mkdir -p ~/.mcp2tcp
在 config.yaml
中添加自定义命令:
使用真实TCP
# config.yaml # PICO信息查询命令 get_pico_info: command: "CMD_PICO_INFO" # 实际发送的命令格式,server会自动添加\r need_parse: true # 需要解析响应内容 data_type: "ascii" # 数据类型,ascii或hex prompts: - "查询Pico板信息" - "显示开发板状态"
指定配置文件: 比如指定加载Pico配置文件:Pico_config.yaml
{ "mcpServers": { "mcp2tcp": { "command": "uvx", "args": [ "mcp2tcp", "--config", "Pico" //指定配置文件名,不需要添加_config.yaml后缀 ] } } }
为了能使用多个TCP,我们可以新增多个mcp2tcp的服务 指定不同的配置文件名即可。 如果要接入多个设备,如有要连接第二个设备: 指定加载Pico2配置文件:Pico2_config.yaml
{ "mcpServers": { "mcp2tcp2": { "command": "uvx", "args": [ "mcp2tcp", "--config", "Pico2" //指定配置文件名,不需要添加_config.yaml后缀 ] } } }
在开始使用之前,建议先进行测试以确保一切正常工作。
首先,启动测试目录下的 TCP 服务器来模拟硬件设备:
# 进入项目目录 cd tests # 启动测试服务器 python tcp_server.py
服务器将在本地启动,监听端口 9999。你会看到类似这样的输出:
TCP server started on 127.0.0.1:9999
Waiting for connections...
Example in Cline
# 通过源码安装: git clone https://github.com/mcp2everything/mcp2tcp.git cd mcp2tcp # 创建虚拟环境 uv venv .venv # 激活虚拟环境 # Windows: .venv\Scripts\activate # Linux/macOS: source .venv/bin/activate # 安装开发依赖 uv pip install --editable .
如果使用真实TCP
# config.yaml tcp: # TCP服务器配置 remote_ip: "127.0.0.1" # 远端IP地址 port: 9999 # 端口号 connect_timeout: 3.0 # 连接超时时间,单位为秒 receive_timeout: 2.0 # 接收超时时间,单位为秒 communication_type: "client" # 通信类型,client或server response_start_string: "CMD" # 可选,TCP应答的开始字符串,默认为OK commands: # PWM控制命令 set_pwm: command: "CMD_PWM {frequency}" # frequency为0-100的整数,表示PWM占空比 need_parse: false # 不需要解析响应内容 data_type: "ascii" # 数据类型,ascii或hex parameters: - name: "frequency" type: "integer" description: "PWM frequency value (0-100)" required: true prompts: - "把PWM调到最大 (frequency=100)" - "把PWM调到最小 (frequency=0)" - "请将PWM设置为{frequency} (0-100的整数)" - "关闭PWM (frequency=0)" - "把PWM调到一半 (frequency=50)"
在使用支持MCP协议的客户端(如Claude Desktop或Cline)时,需要在客户端的配置文件中添加以下内容: 直接自动安装的配置方式 源码开发的配置方式
{ "mcpServers": { "mcp2tcp": { "command": "uv", "args": [ "--directory", "你的实际路径/mcp2tcp", // 例如: "C:/Users/Administrator/Documents/develop/my-mcp-server/mcp2tcp" "run", "mcp2tcp" ] } } }
{ "mcpServers": { "mcp2tcp": { "command": "uv", "args": [ "--directory", "你的实际路径/mcp2tcp", // 例如: "C:/Users/Administrator/Documents/develop/my-mcp-server/mcp2tcp" "run", "mcp2tcp", "--config", // 可选参数,指定配置文件名 "Pico" // 可选参数,指定配置文件名,不需要添加_config.yaml后缀 ] } } }
配置文件(config.yaml
)可以放在不同位置,程序会按以下顺序查找:
./config.yaml
C:\Projects
运行程序,它会查找 C:\Projects\config.yaml
# Windows系统 C:\Users\用户名\.mcp2tcp\config.yaml # macOS系统 /Users/用户名/.mcp2tcp/config.yaml # Linux系统 /home/用户名/.mcp2tcp/config.yaml
.mcp2tcp
目录:
# Windows系统(在命令提示符中) mkdir "%USERPROFILE%\.mcp2tcp" # macOS/Linux系统 mkdir -p ~/.mcp2tcp
# Windows系统(需要管理员权限) C:\ProgramData\mcp2tcp\config.yaml # macOS/Linux系统(需要root权限) /etc/mcp2tcp/config.yaml
# Windows系统(以管理员身份运行) mkdir "C:\ProgramData\mcp2tcp" # macOS/Linux系统(以root身份运行) sudo mkdir -p /etc/mcp2tcp sudo chown root:root /etc/mcp2tcp sudo chmod 755 /etc/mcp2tcp
程序会按照上述顺序查找配置文件,使用找到的第一个有效配置文件。根据你的需求选择合适的位置:
# 确保已激活虚拟环境 .venv\Scripts\activate # 运行服务器(使用默认配置config.yaml 案例中用的LOOP_BACK 模拟TCP,无需真实TCP和TCP设备) uv run src/mcp2tcp/server.py 或 uv run mcp2tcp # 运行服务器(使用指定配置Pico_config.yaml) uv run src/mcp2tcp/server.py --config Pico 或 uv run mcp2tcp --config Pico
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!