Skip to main content

An MCP server that standardizes and contextualizes Modbus data, enabling seamless integration of AI agents with industrial IoT systems.e

Project description

Modbus MCP Server

An MCP server that standardizes and contextualizes Modbus data, enabling seamless integration of AI agents with industrial IoT systems.

GitHub License Python Version Status

Features

  • Modbus Tools:
    • Read/write holding registers (read_register, write_register).
    • Read/write coils (read_coils, write_coil).
    • Read input registers (read_input_registers).
    • Read multiple holding registers (read_multiple_holding_registers).
  • Prompt: Analyze Modbus register values with a customizable prompt (analyze_register).
  • Flexible Connections: Supports Modbus over TCP, UDP, or serial, configured via environment variables.

Requirements

  • Python: 3.10
  • uv for dependency and virtual environment management.

Installation

  1. Install uv:

    curl -LsSf https://astral.sh/uv/install.sh | sh
    
  2. Clone the Repository:

    git clone https://github.com/kukapay/modbus-mcp.git
    cd modbus-mcp
    
  3. Install Dependencies:

    uv sync
    

Configuration

The server connects to a Modbus device using parameters specified via environment variables. Set these variables in a .env file or your shell environment.

Environment Variables

Variable Description Default Required
MODBUS_TYPE Connection type: tcp, udp, or serial tcp Yes
MODBUS_HOST Host address for TCP/UDP 127.0.0.1 For TCP/UDP
MODBUS_PORT Port for TCP/UDP 502 For TCP/UDP
MODBUS_DEFAULT_SLAVE_ID Slave ID 1 For TCP/UDP
MODBUS_SERIAL_PORT Serial port (e.g., /dev/ttyUSB0, COM1) /dev/ttyUSB0 For serial
MODBUS_BAUDRATE Serial baud rate 9600 For serial
MODBUS_PARITY Serial parity: N (none), E (even), O (odd) N For serial
MODBUS_STOPBITS Serial stop bits 1 For serial
MODBUS_BYTESIZE Serial byte size 8 For serial
MODBUS_TIMEOUT Serial timeout (seconds) 1 For serial

Example .env File

For TCP:

MODBUS_TYPE=tcp
MODBUS_HOST=192.168.1.100
MODBUS_PORT=502
MODBUS_SLAVE_ID=1

For Serial:

MODBUS_TYPE=serial
MODBUS_SERIAL_PORT=/dev/ttyUSB0
MODBUS_BAUDRATE=9600
MODBUS_PARITY=N
MODBUS_STOPBITS=1
MODBUS_BYTESIZE=8
MODBUS_TIMEOUT=1

Usage

Installing for Claude Desktop

The configuration file:

{
   "mcpServers": {
       "Modbus MCP Server": {
           "command": "uv",
           "args": [ "--directory", "/path/to/modbus-mcp", "run", "modbus-mcp" ],
           "env": { "MODBUS_TYPE": "tcp", "MODBUS_HOST": "127.0.0.1", "MODBUS_PORT": 502 },
       }
   }
}

Using Tools

Note: Natural language support depends on the client’s ability to parse and map prompts to tools. The MCP Inspector requires structured JSON, but the examples below show how conversational inputs translate.

  1. Read a Holding Register:

    • Prompt:
      Please read the value of Modbus holding register 0.
      
    • MCP Inspector JSON:
      {
        "tool": "read_register",
        "parameters": {"address": 0, "slave_id": 1}
      }
      
    • Expected Output: Value: <register_value>
  2. Write to a Holding Register:

    • Prompt:
      Set Modbus holding register 10 to the value 100.
      
    • MCP Inspector JSON:
      {
        "tool": "write_register",
        "parameters": {"address": 10, "value": 100, "slave_id": 1}
      }
      
    • Expected Output: Successfully wrote 100 to register 10
  3. Read Coils:

    • Prompt:
      Check the status of the first 5 Modbus coils starting at address 0.
      
    • MCP Inspector JSON:
      {
        "tool": "read_coils",
        "parameters": {"address": 0, "count": 5, "slave_id": 1}
      }
      
    • Expected Output: Coils 0 to 4: [False, False, False, False, False]
  4. Write to a Coil:

    • Prompt:
      Turn on Modbus coil 5.
      
    • MCP Inspector JSON:
      {
        "tool": "write_coil",
        "parameters": {"address": 5, "value": true, "slave_id": 1}
      }
      
    • Expected Output: Successfully wrote True to coil 5
  5. Read Input Registers:

    • Prompt:
      Read the values of 3 Modbus input registers starting from address 2.
      
    • MCP Inspector JSON:
      {
        "tool": "read_input_registers",
        "parameters": {"address": 2, "count": 3, "slave_id": 1}
      }
      
    • Expected Output: Input Registers 2 to 4: [<value1>, <value2>, <value3>]
  6. Read Multiple Holding Registers:

    • Prompt:
      Get the values of Modbus holding registers 0 through 2.
      
    • MCP Inspector JSON:
      {
        "tool": "read_multiple_holding_registers",
        "parameters": {"address": 0, "count": 3, "slave_id": 1}
      }
      
    • Expected Output: Holding Registers 0 to 2: [<value1>, <value2>, <value3>]

License

This project is licensed under the MIT License. See LICENSE for details.

Project details


Download files

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

Source Distribution

iflow_mcp_kukapay_modbus_mcp-0.1.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

iflow_mcp_kukapay_modbus_mcp-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file iflow_mcp_kukapay_modbus_mcp-0.1.0.tar.gz.

File metadata

  • Download URL: iflow_mcp_kukapay_modbus_mcp-0.1.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_kukapay_modbus_mcp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0479008a2981e952d24b9a46745d830a9f356d3c3b805b79c8db83a28cb9efe2
MD5 87a5abf96d3fc030592edd6b56a74593
BLAKE2b-256 635641735072c8b41cce48c6ca840ed0960a016868a954f0df91b3850c4d1f9a

See more details on using hashes here.

File details

Details for the file iflow_mcp_kukapay_modbus_mcp-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iflow_mcp_kukapay_modbus_mcp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for iflow_mcp_kukapay_modbus_mcp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 137289c6915305807f26a7177ee405f8abaa5b40ba095c5bbd2bafb0847bae31
MD5 94992e05d68a0c347c0c11fbf0cafcbf
BLAKE2b-256 0e5b49ee00ea592b5e4e8ad20b1936e409c9d7c9b323a600876c7710f2ba8ea1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page