Skip to main content
Glama
Studio5000-MCP

Studio5000-MCP

README.md
# Studio5000-MCP: PLC MCP Server for Rockwell Automation Studio 5000 Logix Designer

[![CI](https://github.com/krusailor44-afk/Studio5000-MCP/actions/workflows/ci.yml/badge.svg)](https://github.com/krusailor44-afk/Studio5000-MCP/actions/workflows/ci.yml)
[![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/)
[![Rockwell Studio 5000](https://img.shields.io/badge/Rockwell-Studio%205000%20(v30--v38)-red.svg)](https://www.rockwellautomation.com/)
[![Protocol: Model Context Protocol](https://img.shields.io/badge/MCP-FastMCP-purple.svg)](https://modelcontextprotocol.io/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Studio5000-MCP is a **Model Context Protocol (MCP)** server that connects MCP-compatible AI assistants (such as Claude Desktop, Cursor, Antigravity, and Cline) to **Rockwell Automation Studio 5000 Logix Designer** workflows for **Allen-Bradley ControlLogix and CompactLogix PLCs**. It supports both live online tag and ladder logic operations over the Logix Designer SDK and offline `.L5X` inspection, cross-referencing, and code generation without requiring a live controller.

*(Independent open-source project; not affiliated with or endorsed by Rockwell Automation.)*

## What You Can Do With Studio5000-MCP

* **Read Live PLC Tags and Run Gated Online Ladder Edits:** Inspect live tag values and Relay Ladder Logic (`RLL`) routines, stage pending rung changes (`propose_edit` via `AcceptEdits`), toggle Test Mode (`test_edits` / `untest_edits`) to verify behavior without overwriting original rungs, and commit to controller memory (`assemble_edits`) only when an explicit `confirmed=True` flag is passed.
* **Inspect Offline `.L5X` Projects and Cross-Reference Tag Usage:** List tags by controller or program scope, inspect hardware I/O modules and slots, read both `RLL` and Structured Text (`ST`) routines, and run `search_logic` to locate every rung or line referencing a tag or member across programs and Add-On Instructions (AOIs).
* **Generate and Validate `.L5X` Imports for Routines, AOIs, and UDTs:** Build ready-to-import `.L5X` files for routines, AOIs, and user-defined types (UDTs, including `BIT` overlay structures) with pre-import checks for `Use="Target"` (Error 25134-80044178), parallel branch syntax (Error 12740-0), Studio 5000 v36+ IEC comparison mnemonics (`LT`, `GT`, `LE`, `GE`, `EQ`, `NE`), and advisory `RSLogix5000_V35.xsd` schema validation.
* **Convert Verbose `.L5X` XML to Compact Logix NeutralText:** Translate XML ladder rungs into readable Logix NeutralText (`XIC(Run) LT(PV, SP) OTE(Motor);`) so AI assistants can review and reason over large routines without wasting context window space, and look up local Studio 5000 HTML Help topics (`search_instruction_help`, `get_instruction_help`) directly from an installed Help directory.
* **Enforce Code-Level Tag Guardrails and Analyze Exports:** Block online edits targeting tags with a `Safety_` prefix, alias tags, consumed tags, `ExternalAccess="None"` tags, non-RLL routines, or AOI calls; categorize Studio 5000 Tag `.CSV` exports by rack/slot I/O; and optionally extract equipment tags (`M001`, `VFD01`, `CV-001`) from CAD/P&ID PDFs when `PyMuPDF` is installed.

---

## Why This Matters

Interacting with programmable logic controllers in live plants requires strict discipline around syntax, tag scope, and controller state. Standard LLMs frequently hallucinate invalid ladder syntax (such as malformed single-leg branches causing Studio 5000 Error 12740-0, obsolete comparison mnemonics on v36+, or missing XML target headers). 

**Studio5000-MCP** addresses this by establishing a schema-enforced and safety-gated bridge:

```mermaid
flowchart LR
    subgraph AI["AI Agents & IDEs"]
        Agent["Claude Desktop / Cursor / Antigravity"]
    end

    subgraph MCP["MCP Server Core"]
        FastMCP["FastMCP stdio Transport"]
        Router{"Adapter Router"}
        Safety["Hardcoded Safety Gates"]
        L5XVal["L5X Schema & Syntax Validator"]
    end

    subgraph Adapters["Modular Adapters"]
        Online["LogixOnlineAdapter\n(Logix Designer SDK + Win32)"]
        Offline["LogixOfflineAdapter\n(XML Parsing + NeutralText)"]
    end

    subgraph Rockwell["Rockwell Ecosystem"]
        PLC[("Physical / Emulated PLC\nControlLogix / CompactLogix")]
        Studio["Studio 5000 Logix Designer\n(v30 - v38.11)"]
        Files[(".L5X Exports & .ACD Projects")]
    end

    Agent <--> FastMCP
    FastMCP --> Router
    Router --> Safety
    Safety --> L5XVal
    L5XVal --> Online
    L5XVal --> Offline
    Online <--> PLC
    Online <--> Studio
    Offline <--> Files
```

---

## Core Features

### 🟢 1. Safe Online Editing Lifecycle (Module A)
Enforces Studio 5000's native online edit zone progression (`N` Normal $\rightarrow$ `I` Insert Pending $\rightarrow$ `R` Running Edits):
* **`read_routine` & `read_tag`**: Safely read live ladder logic, instruction states, and process tag values.
* **`propose_edit`**: Injects new or replaced rungs as **Accepted Pending Edits** (`AcceptEdits`). Logic is buffered in the controller but **not yet executed**.
* **`test_edits`**: Invokes MFC lifecycle verb `32061` to toggle Test Mode. Controller executes proposed edits in RAM while keeping original rungs intact.
* **`untest_edits`**: Reverts controller scan back to original rungs instantly if anomalous process behavior is detected.
* **`assemble_edits`**: Finalizes changes into non-volatile memory. **Strictly guarded by a Human-in-the-Loop requirement (`confirmed=True`).**

```mermaid
stateDiagram-v2
    [*] --> Standby: Controller Online
    Standby --> PendingEdits: propose_edit() [AcceptEdits]
    PendingEdits --> InTest: test_edits() [Verb 32061]
    InTest --> PendingEdits: untest_edits() [Verb 32062]
    InTest --> HumanApproval: Review Process Telemetry
    HumanApproval --> Assembled: assemble_edits(confirmed=True)
    HumanApproval --> PendingEdits: Reject / Revert
    Assembled --> Standby: Logic Active in Non-Volatile Memory
```

### 🟠 2. Offline Engineering, Cross-Reference & Code Synthesis (Module B)
* **`parse_l5x`, `list_tags`, `get_udt`, `get_aoi`, `list_modules`**: Granular offline `.L5X` inspection across Controller & Program scopes, hardware slots, UDTs (skipping hidden internal members), and both **RLL** and **Structured Text (ST)** routines.
* **`search_logic`**: Fast cross-reference engine indexing every tag and dotted member across Programs and AOIs (both RLL and ST).
* **`translate_to_neutral_text`**: Strips verbose XML wrappers into concise Logix NeutralText syntax (e.g., `XIC(Run) LT(PV, SP) OTE(Motor);`), **reducing LLM context window consumption by 85–92%**.
* **`generate_l5x_import`, `build_aoi_l5x`, `build_udt_l5x`**: Generates 100% schema-compliant `.L5X` import fragments for Routines, AOIs, and UDTs (including `BIT` overlay structures).
* **Engineering Intelligence**: Local Studio 5000 HTML Help lookup (`search_instruction_help`, `get_instruction_help`), Tag CSV rack/slot I/O mapping (`parse_tag_csv`), advisory Rockwell V35 XSD validation, and optional AutoCAD/P&ID PDF equipment tag extraction (`parse_pdf_drawing`).

### šŸ›”ļø 3. Unbypassable Industrial Safety Gates
* **Safety Tag Blacklist**: Writes or edits to tags beginning with `Safety_` (e.g. `Safety_Estop_PB`, `Safety_Door_Interlock`) are **hard-blocked** with a `PROHIBITED_SAFETY_TAG` exception.
* **Alias & Consumed Tag Protection**: Writes to alias tags or cyclically overwritten consumed tags are denied to eliminate memory corruption hazards.
* **External Access Enforcement**: Tags marked `ExternalAccess="None"` cannot be written to externally.
* **Routine Restriction**: Edits are restricted to Relay Ladder Logic (`RLL`). Function Block Diagrams (`FBD`) and Sequential Function Charts (`SFC`) are locked out.

### ⚔ 4. Studio 5000 v36+ IEC 61131-3 Modernization
Starting in Studio 5000 v36 (v36, v37, and v38.11), Rockwell standardized comparison instructions:
* `LES` $\rightarrow$ **`LT`** (Less Than)
* `GRT` $\rightarrow$ **`GT`** (Greater Than)
* `LEQ` $\rightarrow$ **`LE`** (Less Than or Equal)
* `GEQ` $\rightarrow$ **`GE`** (Greater Than or Equal)
* `EQU` $\rightarrow$ **`EQ`** (Equal)
* `NEQ` $\rightarrow$ **`NE`** (Not Equal)

The server **automatically modernizes** legacy comparison instructions when compiling or proposing rungs for v36+ controllers, completely eliminating Rockwell compiler errors (`Update instruction to LT/GT/LE/GE`).

### šŸ” 5. Pre-Import Scanner & Error Prevention
* **Error 25134-80044178 Prevention**: Guarantees `<RSLogix5000Content ContainsContext="true">` and target elements contain `Use="Target"`.
* **Error 12740-0 Prevention**: Validates nested parallel branch brackets `[...]`, ensuring every branch contains $\ge 2$ comma-separated legs.
* **AOI Duplicate Backing Tag Prevention**: Enforces linear clamping architectures to eliminate Studio 5000 backing tag warnings across parallel branches.

---

## Directory Structure

```text
Studio5000-MCP/
ā”œā”€ā”€ .github/workflows/ci.yml           # GitHub Actions Automated CI Pipeline
ā”œā”€ā”€ LICENSE                            # MIT License
ā”œā”€ā”€ README.md                          # Enterprise Documentation
ā”œā”€ā”€ CONTRIBUTING.md                    # Contribution Standards
ā”œā”€ā”€ SECURITY.md                        # Industrial Controls Safety Policy
ā”œā”€ā”€ requirements.txt                   # Production Dependencies
ā”œā”€ā”€ pyproject.toml                     # Package Configuration & Pytest settings
ā”œā”€ā”€ examples/                          # Ready-to-Run Engineering Examples
│   ā”œā”€ā”€ generate_powerflex_aoi.py      # Programmatic AOI Synthesis
│   └── optimize_tokens_neutral_text.py# NeutralText Context Optimizer
ā”œā”€ā”€ src/
│   └── studio5000_live_mcp/
│       ā”œā”€ā”€ __init__.py                # Package exports
│       ā”œā”€ā”€ server.py                  # FastMCP transport (stdio/sse/http) & exception shields
│       ā”œā”€ā”€ sdk_client.py              # Rockwell Logix Designer SDK client wrapper
│       ā”œā”€ā”€ safety_validator.py        # Safety gates & mnemonic validator
│       ā”œā”€ā”€ l5x_builder.py             # Rockwell L5X/UDT/AOI builder & XSD/syntax validator
│       ā”œā”€ā”€ xref.py                    # Offline RLL & ST cross-reference search engine
│       ā”œā”€ā”€ help_index.py              # Zero-dependency local Studio 5000 Help indexer
│       ā”œā”€ā”€ csv_analyzer.py            # Studio 5000 Tag CSV & rack/slot I/O analyzer
│       ā”œā”€ā”€ pdf_analyzer.py            # Optional PyMuPDF engineering drawing tag extractor
│       ā”œā”€ā”€ win32_lifecycle.py         # Win32 WM_COMMAND driver (test/untest/assemble)
│       ā”œā”€ā”€ schemas/
│       │   └── RSLogix5000_V35.xsd    # Bundled Rockwell L5X XML Schema
│       └── adapters/
│           ā”œā”€ā”€ __init__.py
│           ā”œā”€ā”€ base.py                # Abstract PLCAdapter interface
│           ā”œā”€ā”€ logix_online.py        # Online Logix SDK & Win32 adapter
│           ā”œā”€ā”€ logix_offline.py       # Offline .L5X parser, xref & NeutralText engine
│           └── router.py              # State manager & dynamic adapter router
└── tests/                             # Comprehensive Unit Test Suite
    ā”œā”€ā”€ test_safety_gates.py           # Safety gate lockout & safeguard tests
    ā”œā”€ā”€ test_l5x_builder.py            # L5X schema, IEC mnemonics & Error 12740-0 tests
    ā”œā”€ā”€ test_offline_adapter.py        # Offline parsing & NeutralText translation tests
    ā”œā”€ā”€ test_phase1_offline_and_xref.py# Granular offline, ST, xref & XSD tests
    ā”œā”€ā”€ test_phase2_intelligence.py    # Studio 5000 Help, Tag CSV & UDT overlay tests
    ā”œā”€ā”€ test_phase3_pdf_and_docs.py    # Optional PDF drawing analyzer tests
    └── test_adapter_router.py         # Router state transitions & error handling tests
```

---

## Quick Start & Installation

### 1. Prerequisites
* Windows 10 / 11 / Server 2022 (64-bit)
* Python 3.12+
* Rockwell Studio 5000 Logix Designer (v30 to v38)
* Rockwell Logix Designer SDK (`logix_designer_sdk` wheel installed from your Rockwell installation directory for online features)

### 2. Installation
```powershell
# Clone the repository
git clone https://github.com/krusailor44-afk/Studio5000-MCP.git
cd Studio5000-MCP

# Create virtual environment
python -m venv .venv
.\.venv\Scripts\Activate.ps1

# Install dependencies
pip install -r requirements.txt
pip install -e .

# Optional: install PyMuPDF for PDF technical drawing tag extraction
pip install -e ".[pdf]"
```

### 3. Verify Test Suite
```powershell
pytest tests/ -v
```

### 4. Transport Modes (`stdio`, `sse`, `streamable-http`)
```powershell
# Default stdio transport
python -m studio5000_live_mcp.server --project-path C:\Projects\Plant.L5X

# SSE network transport
python -m studio5000_live_mcp.server --transport sse --host 127.0.0.1 --port 8080
```

---

## MCP Client Configuration

### Claude Desktop
Add this to your `claude_desktop_config.json` (`%APPDATA%\Claude\claude_desktop_config.json`):

```json
{
  "mcpServers": {
    "Studio5000-MCP": {
      "command": "C:\\path\\to\\Studio5000-MCP\\.venv\\Scripts\\python.exe",
      "args": [
        "-m",
        "studio5000_live_mcp.server",
        "--project-path",
        "C:\\Projects\\Plant_Controller.ACD"
      ]
    }
  }
}
```

### Cursor / Cline / Antigravity
Configure via your IDE's MCP settings:
* **Command**: `C:\path\to\Studio5000-MCP\.venv\Scripts\python.exe`
* **Args**: `["-m", "studio5000_live_mcp.server"]`

---

## Tool Reference

| Tool Name | Scope | Description |
| :--- | :--- | :--- |
| **`set_active_project`** | Universal | Switches active project between live `.ACD` and offline `.L5X` file. |
| **`get_project_status`** | Universal | Returns execution mode (`ONLINE` vs `OFFLINE`), active path, and connection state. |
| **`read_routine`** | Dual | Reads ladder rungs (from live controller scan in online mode; from XML in offline mode). |
| **`read_tag`** | Dual | Reads tag values, data types, and accessibility properties. |
| **`propose_edit`** | Online | Pushes staged ladder edits as pending changes via `AcceptEdits`. |
| **`test_edits`** | Online | Switches live controller into Test Mode (Verb `32061`). |
| **`untest_edits`** | Online | Reverts controller to original rungs (Verb `32062`). |
| **`assemble_edits`** | Online | Permanently commits edits to controller. **Requires `confirmed=True`.** |
| **`parse_l5x`** | Offline | Extracts hardware tree, UDTs, controller tags, and routines from `.L5X`. |
| **`translate_to_neutral_text`** | Offline | Converts XML rungs to compact NeutralText (saves up to 92% tokens). |
| **`generate_l5x_import`** | Offline | Packages NeutralText into a strictly formatted `.L5X` for Studio 5000 import. |
| **`build_aoi_l5x`** | Offline | Generates an Add-On Instruction `.L5X` file adhering 100% to Rockwell import schema. |
| **`build_udt_l5x`** | Offline | Generates a User-Defined DataType `.L5X` file supporting `BIT` overlay members. |
| **`validate_l5x_file`** | Offline | Validates `.L5X` syntax, `Use="Target"`, IEC mnemonics, and optional V35 XSD schema. |
| **`list_tags`** | Offline | Lists tags filtered by scope (`controller` or program) and/or `data_type`. |
| **`get_udt`** | Offline | Lists UDTs or returns full member definitions (skipping hidden internal members). |
| **`get_aoi`** | Offline | Lists AOIs or returns full parameters, local tags, and RLL/ST routines. |
| **`list_modules`** | Offline | Lists hardware I/O modules with catalog numbers, slots, and inhibited status. |
| **`search_logic`** | Offline | Cross-reference search across all RLL and Structured Text (ST) routines. |
| **`search_instruction_help`** | Docs | Searches local Studio 5000 HTML Help topics and instruction mnemonics. |
| **`get_instruction_help`** | Docs | Extracts clean plain-text documentation for a Studio 5000 instruction/topic. |
| **`parse_tag_csv`** | CSV | Categorizes Studio 5000 Tag CSV exports and maps physical rack/slot I/O usage. |
| **`parse_pdf_drawing`** | PDF *(Opt.)* | Extracts equipment tags (`M001`, `VFD01`, `CV-001`) and classifies CAD/P&ID PDF pages. |

---

## Industrial Controls Disclaimer

> [!WARNING]
> This software interacts directly with industrial automation controllers that may govern physical machinery, robotics, pumps, valves, and chemical batching equipment. 
> 
> Always observe fundamental automation safety standards:
> - Never disable or bypass safety gates.
> - Ensure hardwired E-Stop circuits are fully independent of software logic.
> - Verify logic in simulated or offline environments prior to running on physical machines.
> - Always maintain physical access to the controller's key switch in `REM` or `PROG`.

---

## Contributing & License

Contributions, bug reports, and suggestions from both controls engineers and AI practitioners are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) and [SECURITY.md](SECURITY.md) before opening a pull request.

Distributed under the **MIT License**. See [LICENSE](LICENSE) for more information.