ROS2 MCP Server

ROS2 MCP Server

A Python-based server that enables AI assistants to control robots by integrating the Model Context Protocol (MCP) with ROS 2, allowing for natural language commands that translate into robot movement via the /cmd\_vel topic.

Category
访问服务器

README

ros2-mcp-server

ros2-mcp-server is a Python-based server that integrates the Model Context Protocol (MCP) with ROS 2, enabling AI assistants to control robots via ROS 2 topics. It processes commands through FastMCP and runs as a ROS 2 node, publishing geometry_msgs/Twist messages to the /cmd_vel topic to control robot movement.

This implementation supports commands like "move forward at 0.2 m/s for 5 seconds and stop," with the /cmd_vel publisher named pub_cmd_vel.

Features

  • MCP Integration: Uses FastMCP to handle commands from MCP clients (e.g., Claude).
  • ROS 2 Native: Operates as a ROS 2 node, directly publishing to /cmd_vel.
  • Time-Based Control: Supports duration-based movement commands (e.g., move for a specified time and stop).
  • Asynchronous Processing: Combines FastMCP's asyncio with ROS 2's event loop for efficient operation.

Prerequisites

  • ROS 2: Humble distribution installed and sourced.
  • Python: Version 3.10 (required for compatibility with ROS 2 Humble).
  • uv: Python package manager for dependency management.
  • Dependencies:
    • rclpy: ROS 2 Python client library (installed with ROS 2).
    • fastmcp: FastMCP framework for MCP server implementation.
    • numpy: Required by ROS 2 message types.

Installation

  1. Clone the Repository:

    git clone https://github.com/kakimochi/ros2-mcp-server.git
    cd ros2-mcp-server
    
  2. Python Version Configuration: This project uses Python 3.10 as required by ROS 2 Humble. The .python-version file is already configured:

    # .python-version content
    3.10
    
  3. Project Dependencies: The pyproject.toml file is configured with the necessary dependencies:

    # pyproject.toml content
    [project]
    name = "ros2-mcp-server"
    version = "0.1.0"
    description = "ROS 2 MCP Server"
    readme = "README.md"
    requires-python = ">=3.10"
    dependencies = [
        "fastmcp",
        "numpy",
    ]
    
  4. Create uv Environment:

    uv venv --python /usr/bin/python3.10
    
  5. Activate the Virtual Environment:

    source .venv/bin/activate
    

    You'll see (.venv) appear at the beginning of your command prompt, indicating that the virtual environment is active.

  6. Install Dependencies:

    uv pip install -e .
    

MCP Server Configuration

To use this server with Claude or other MCP clients, you need to configure it as an MCP server. Here's how to set it up:

For Claude Desktop

  1. Open Claude Desktop settings and navigate to the MCP servers section.

  2. Add a new MCP server with the following configuration:

    "ros2-mcp-server": {
      "autoApprove": [],
      "disabled": false,
      "timeout": 60,
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/ros2-mcp-server",
        "run",
        "bash",
        "-c",
        "export ROS_LOG_DIR=/tmp && source /opt/ros/humble/setup.bash && python3 /path/to/ros2-mcp-server/ros2-mcp-server.py"
      ],
      "transportType": "stdio"
    }
    

    Important: Replace /path/to/ros2-mcp-server with the actual path to your repository. For example, if you cloned the repository to /home/user/projects/ros2-mcp-server, you would use that path instead.

  3. Save the configuration and restart Claude.

For Cline (VSCode Extension)

  1. In VSCode, open the Cline extension settings by clicking on the Cline icon in the sidebar.

  2. Navigate to the MCP servers configuration section.

  3. Add a new MCP server with the following configuration:

    "ros2-mcp-server": {
      "autoApprove": [],
      "disabled": false,
      "timeout": 60,
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/ros2-mcp-server",
        "run",
        "bash",
        "-c",
        "export ROS_LOG_DIR=/tmp && source /opt/ros/humble/setup.bash && python3 /path/to/ros2-mcp-server/ros2-mcp-server.py"
      ],
      "transportType": "stdio"
    }
    

    Important: Replace /path/to/ros2-mcp-server with the actual path to your repository, as in the Claude Desktop example.

  4. You can immediately toggle the server on/off and verify the connection directly from the Cline MCP settings interface without needing to restart VSCode or reload the extension.

Usage

Once the MCP server is configured, you can use Claude to send commands to the robot:

  1. Example Command: Ask Claude to move the robot forward at 0.2 m/s for 5 seconds:

    Please make the robot move forward at 0.2 m/s for 5 seconds.
    
  2. Direct Tool Usage: You can also use the move_robot tool directly:

    {
      "linear": [0.2, 0.0, 0.0],
      "angular": [0.0, 0.0, 0.0],
      "duration": 5.0
    }
    
  3. Monitor ROS 2 Topics: Verify the /cmd_vel topic output:

    ros2 topic echo /cmd_vel
    

Testing

  1. With a Simulator:

    • Launch a ROS 2-compatible simulator (e.g., Gazebo with TurtleBot3):
      export TURTLEBOT3_MODEL=burger
      ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py
      
    • Use Claude to send movement commands.
    • Observe the robot moving in Gazebo.
  2. With a Real Robot:

    • Ensure your robot is properly set up to subscribe to the /cmd_vel topic.
    • Use Claude to send movement commands.
    • The robot should move according to the commands.
  3. Expected Output:

    • The server logs movement commands and stop commands.
    • Claude receives a response like: "Successfully moved for 5.0 seconds and stopped".

Troubleshooting

  • ROS 2 Logging Errors: If you encounter logging directory errors, ensure the ROS_LOG_DIR environment variable is set to a writable directory (e.g., /tmp).
  • Python Version Mismatch: Ensure you're using Python 3.10, as ROS 2 Humble is built for this version.
  • Connection Errors: If Claude reports "Connection closed" errors, check that the MCP server configuration is correct and that all dependencies are installed.

Directory Structure

ros2-mcp-server/
├── ros2-mcp-server.py  # Main server script integrating FastMCP and ROS 2
├── pyproject.toml      # Project dependencies and metadata
├── .python-version     # Python version specification
├── .gitignore          # Git ignore file
└── README.md           # This file

Limitations

  • Single Topic: Currently supports /cmd_vel with Twist messages. Extend ros2-mcp-server.py for other topics or services.
  • Basic Commands: Currently supports simple movement commands. More complex behaviors would require additional implementation.

License

MIT License

Copyright (c) 2025 kakimochi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Note that this project uses FastMCP, which is licensed under the Apache License 2.0. The terms of that license also apply to the use of FastMCP components.

Acknowledgments

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选