Sponsored by Deepsite.site

Modbus Mcp

Created By
ezhuk6 months ago
A Model Context Protocol (MCP) server for Modbus.
Content

Modbus MCP Server

A lightweight Model Context Protocol (MCP) server that connects LLM agents to Modbus devices in a secure, standardized way, enabling seamless integration of AI-driven workflows with Building Automation (BAS) and Industrial Control (ICS) systems, allowing agents to monitor real-time sensor data, actuate devices, and orchestrate complex automation tasks.

test

Getting Started

The server is built with FastMCP 2.0 and uses uv for project and dependency management. Simply run the following command to install uv or check out the installation guide for more details and alternative installation methods.

curl -LsSf https://astral.sh/uv/install.sh | sh

Clone the repository, then use uv to install project dependencies and create a virtual environment.

git clone https://github.com/ezhuk/modbus-mcp.git
cd modbus-mcp
uv sync

Start the Modbus MCP server by running the following command in your terminal. It defaults to using the Streamable HTTP transport on port 8000.

uv run modbus-mcp

To confirm the server is up and running and explore available resources and tools, run the MCP Inspector and connect it to the Modbus MCP server at http://127.0.0.1:8000/mcp/. Make sure to set the transport to Streamable HTTP.

npx @modelcontextprotocol/inspector

s01

Core Concepts

The Modbus MCP server leverages FastMCP 2.0's core building blocks - resource templates, tools, and prompts - to streamline Modbus read and write operations with minimal boilerplate and a clean, Pythonic interface.

Read Registers

Each register on a device is mapped to a resource (and exposed as a tool) and resource templates are used to specify connection details (host, port, unit) and read parameters (address, count).

@mcp.resource("tcp://{host}:{port}/{address}?count={count}&unit={unit}")
@mcp.tool(
    annotations={"title": "Read Registers", "readOnlyHint": True, "openWorldHint": True}
)
async def read_registers(
    host: str = settings.modbus.host,
    port: int = settings.modbus.port,
    address: int = 40001,
    count: int = 1,
    unit: int = settings.modbus.unit,
) -> int | list[int]:
    """Reads the contents of one or more registers on a remote unit."""
    ...

Write Registers

Write operations are exposed as a tool, accepting the same connection details (host, port, unit) and allowing to set the contents of one or more holding registers or coils in a single, atomic call.

@mcp.tool(
    annotations={
        "title": "Write Registers",
        "readOnlyHint": False,
        "openWorldHint": True,
    }
)
async def write_registers(
    data: list[int],
    host: str = settings.modbus.host,
    port: int = settings.modbus.port,
    address: int = 40001,
    unit: int = settings.modbus.unit,
) -> str:
    """Writes data to one or more registers on a remote unit."""
    ...

Authentication

To enable Bearer Token authentication for the Streamable HTTP transport, provide the RSA public key in PEM format in the .env file. Check out the Bearer Token Authentication section for more details.

Interactive Prompts

Structured response messages are implemented using prompts that help guide the interaction, clarify missing parameters, and handle errors gracefully.

@mcp.prompt(name="modbus_help", tags={"modbus", "help"})
def modbus_help() -> list[Message]:
    """Provides examples of how to use the Modbus MCP server."""
    ...

Here are some example text inputs that can be used to interact with the server.

Please read the value of register 40001 on 127.0.0.1:502.
Set register 40005 to 123 on host 192.168.1.10, unit 3.
Write [1, 2, 3] to holding registers starting at address 40010.
What is the status of input register 30010 on 10.0.0.5?

Examples

The examples folder contains sample projects showing how to integrate with the Modbus MCP server using various client APIs to provide tools and context to LLMs.

Docker

The Modbus MCP server can be deployed as a Docker container. Build the image using the docker build command:

docker build -t modbus-mcp .

Then run the container as follows:

docker run -dit \
  --name modbus-mcp \
  --restart=always \
  -p 8080:8000 \
  --env-file .env \
  modbus-mcp

This maps port 8080 on the host to the MCP server's port 8000 inside the container and loads settings from the .env file, if present.

License

The server is licensed under the MIT License.

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Playwright McpPlaywright MCP server
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
WindsurfThe new purpose-built IDE to harness magic
Amap Maps高德地图官方 MCP Server
CursorThe AI Code Editor
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Tavily Mcp
DeepChatYour AI Partner on Desktop
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
Serper MCP ServerA Serper MCP Server
ChatWiseThe second fastest AI chatbot™
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题; Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"