Skip to main content

Modbus MCP Server

test codecov PyPI - Version

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.

Getting Started

Use uv to add and manage the Modbus MCP server as a dependency in your project, or install it directly via uv pip install or pip install. See the Installation section of the documentation for full installation instructions and more details.

uv add modbus-mcp

The server can be embedded in and run directly from your application. By default, it exposes a Streamable HTTP endpoint at http://127.0.0.1:8000/mcp/.

# app.py
from modbus_mcp import ModbusMCP

mcp = ModbusMCP()

if __name__ == "__main__":
    mcp.run(transport="http")

It can also be launched from the command line using the provided CLI without modifying the source code.

modbus-mcp

Or in an ephemeral, isolated environment using uvx. Check out the Using tools guide for more details.

uvx modbus-mcp

Configuration

For the use cases where most operations target a specific device, such as a Programmable Logic Controller (PLC) or Modbus gateway, its connection settings (host, port, and unit) can be specified at runtime using environment variables so that all prompts that omit explicit connection parameters will be routed to this device.

export MODBUS_MCP_MODBUS__HOST=10.0.0.1
export MODBUS_MCP_MODBUS__PORT=502
export MODBUS_MCP_MODBUS__UNIT=1

These settings can also be specified in a .env file in the working directory.

# .env
modbus_mcp_modbus__host=10.0.0.1
modbus_mcp_modbus__port=502
modbus_mcp_modbus__unit=1

When interacting with multiple devices, each device’s connection parameters (host, port, unit) can be defined with a unique name in a devices.json file in the working directory. Prompts can then refer to devices by name.

{
  "devices": [
    {"name": "Boiler", "host": "10.0.0.3", "port": 503, "unit": 3},
    {"name": "Valve", "host": "10.0.0.4", "port": 504, "unit": 4}
  ]
}

MCP Inspector

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 is built with FastMCP 2.0 and leverages its 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 authentication using the built-in AuthKit provider for the Streamable HTTP transport, provide the AuthKit domain and redirect URL in the .env file. Check out the AuthKit Provider 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 as follows:

docker run -d \
  --name modbus-mcp \
  --restart=always \
  -p 8080:8000 \
  --env-file .env \
  ghcr.io/ezhuk/modbus-mcp:latest

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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

modbus_mcp-0.3.3.tar.gz (276.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

modbus_mcp-0.3.3-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file modbus_mcp-0.3.3.tar.gz.

File metadata

  • Download URL: modbus_mcp-0.3.3.tar.gz
  • Upload date:
  • Size: 276.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_mcp-0.3.3.tar.gz
Algorithm Hash digest
SHA256 695640197651c1da7dd5e365d1a97537892bea9970eed348472bfa8f989c07f0
MD5 194b21e77fdcce09832dc4b3283225b5
BLAKE2b-256 70a659ab855928d66c7a6a5ba95a8a36b9eb081c4f0ea33bf27b6d2e2e6e6acc

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_mcp-0.3.3.tar.gz:

Publisher: pypi.yml on ezhuk/modbus-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file modbus_mcp-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: modbus_mcp-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for modbus_mcp-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 50630fe9d013f9525208a5d34fe1f240e4d99ee62251ffcac09a936e42fe2903
MD5 82f97812a93bec4ad292b72c3fdfea05
BLAKE2b-256 5bd1a1e3582afb0906fafa235d9f85f5cb293bbae98fd5d449df52fe5854d18d

See more details on using hashes here.

Provenance

The following attestation bundles were made for modbus_mcp-0.3.3-py3-none-any.whl:

Publisher: pypi.yml on ezhuk/modbus-mcp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

2 files

0.2.14

2 files

0.2.13

2 files

0.2.12

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

2 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.28

2 files

0.1.27

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page