DEV Community

Whatsm97
Whatsm97

Posted on

Agentic Coding with PLC

PLC Development with AI: I Integrated a Coding Agent into CODESYS via MCP (Using Open Source Models)

Over the past few years, we have seen LLMs completely revolutionize traditional software development. But what happens when we attempt to bring the power of Coding Agents into industrial automation, specifically within CODESYS?

I wanted to experiment with this frontier first-hand. I configured a Coding Agent leveraging the official CODESYS Development System MCP Server to interface the AI directly with the IDE. For the Agent's "brain," I opted for an open-source workflow using free DeepSeek models via Open Code solutions.

Here is how it works, the tools the AI has at its disposal, the real-world limitations I ran into, and where this technology absolutely shines today.


šŸ› ļø The Architecture: How the Agent Interacts with CODESYS

The secret to making a Coding Agent effective isn't just asking it to spit out code in a chat box; it requires giving it context and the ability to act (Tool Calling). Thanks to the Model Context Protocol (MCP), the LLM can invoke native IDE tools directly inside the development ecosystem.

Here is the exact set of tools the Agent can call autonomously depending on the prompt:

Command Description
codesys-ide_browse_project_tree Explores the entire project tree structure.
codesys-ide_get_active_app_as_path Gets the path of the currently active application.
codesys-ide_get_active_path_and_selection Retrieves the active path and current selection in the IDE.
codesys-ide_get_structured_text_content Reads Structured Text (ST) content, including lines and nested objects.
codesys-ide_create_or_replace_structured_text_object Creates/replaces ST objects (POUs, FBs, Functions, Methods, GVLs, DUTs, etc.).
codesys-ide_replace_text_in_structured_text Replaces specific target text within an ST object.
codesys-ide_create_folder Creates a new folder within the project tree.
codesys-ide_remove_object Removes a specific object and all of its children.
codesys-ide_search_in_files_by_regex Searches using regex across all ST objects in the project.
codesys-ide_search_path_by_glob Searches for project paths using glob patterns.
codesys-ide_check_for_errors Triggers a pre-compile or full compilation and returns diagnostics (errors/warnings).
codesys-ide_add_program_call_to_task Appends a program call to a specific execution task.
codesys-ide_add_library Adds an external library to the project Manager.
codesys-ide_get_libraries_referenced_in_application Lists all libraries currently referenced in the application.
codesys-ide_get_available_libraries_list Lists all available libraries installed on the system.
codesys-ide_search_libraries_for_type Searches for a specific type or function within the libraries.
codesys-ide_get_library_documentation Retrieves general documentation for a specific library.
codesys-ide_get_detailed_library_documentation Fetches deep documentation details for specific functions/blocks.
codesys-ide_get_device_and_io_configuration Retrieves the complete hardware device tree and I/O mapping.

🚫 Current Limitations and Roadblocks with DeepSeek

While utilizing free DeepSeek models on an open infrastructure proved highly capable, real-world testing exposed critical bottlenecks in both the abstraction layers and the nature of automation software.

1. Restricted CODESYS API Capabilities

The interface exposing the IDE functionality still has fundamental structural gaps. For instance, the Agent cannot create Properties with their corresponding Get and Set methods. It is strictly limited to creating Methods that return values, which fundamentally cripples advanced Object-Oriented Programming (OOP) architectures.

2. Hardware Configuration and Advanced Subsystems

While the AI handles pure logic in Structured Text (ST) remarkably well, it falls apart when dealing with specialized IDE components. It has zero ability to manipulate or configure Visualizations (Visu), set up Traces, or handle fine-tuned parameterization of advanced hardware modules.

3. The Physical Machine Obstacle

Industrial automation operates in the physical world. Machinery is highly customized and bound to unique mechanical, electrical, and physical constraints. An LLM cannot inherently grasp physical friction, fluid dynamics, or the specific hardware quirks of a custom-built machine prototype. Because of this, the foundational system architecture must remain firmly guided by human intelligence.

āš ļø Crucial Takeaway: Because of these physical constraints and the potential for LLM hallucinations, you must maintain absolute mental ownership over every single line of code. You cannot treat the Agent as a black box; you must intimately understand what it is doing, review its output meticulously, and ensure it complies with your exact physical machine safety parameters.


šŸ”’ Security: Why the Absence of "Live" Control is a Blessing

One of the most notable aspects of this toolset is that the Agent cannot perform any online actions. It has absolutely no commands to connect to a live CPU, log in, force variables (Forcing), or issue START / STOP commands to the controller.

In traditional software engineering, lacking an automated deployment pipeline might be seen as a drawback. In the PLC world, this strict boundary is an outstanding safety feature. An AI hallucination on a web app might crash a server; an AI hallucination deploying to a live manufacturing plant could break a multi-million-dollar machine, destroy a production line, or cause catastrophic human injury. Commissioning, physical dry runs, and live testing must remain solely human responsibilities.


šŸ‘ļø The Mandatory Role of Git for Code Verification

Allowing an Agent to autonomously alter your CODESYS project tree demands absolute transparency. Without a clear audit trail, tracking what the AI has manipulated becomes impossible.

Because of CODESYS's historical limitations with rolling back native tree changes, using Git is non-negotiable. By leveraging the Official CODESYS Git Extension, you can monitor what the Agent is doing in real-time. Before staging any changes, running a quick git diff lets you validate the generated Structured Text line-by-line, ensuring no rogue logic slips through to your codebase.


šŸ”„ The Ultimate Superpower: Automating Rigid Engineering Work

Despite the hardware and API limitations, there is one specific area where this Agent completely obliterates traditional, manual workflows: parsing and importing external configuration sheets and high-level behavioral layouts.

In industrial environments, I/O lists, sensor tags, and motor mappings usually originate from electrical engineering offices in the form of CSV files or Excel spreadsheets. Manually copying hundreds or thousands of these rows into the IDE to build Global Variable Lists (GVL) or Data Unit Types (DUT) is an alienating, time-consuming process prone to typos and human error.

Furthermore, it is not just limited to raw I/O mapping. You can feed the Agent high-level state machine specifications, sequence charts, or step-by-step sequential descriptions.

Because the Agent can read configuration files directly from the filesystem, it can seamlessly:

  1. Open and parse the entire plant configuration spreadsheet or functional requirement documentation.
  2. Extract the tags, IO definitions, and physical data types.
  3. Generate complete state machine logic, structural skeletons, and step-by-step execution chains in Structured Text (ST) matching your conceptual design.
  4. Instantly populate perfectly formatted GVL, DUT, or POU files with hundreds of faultless lines of code in seconds.

This turns hours of mind-numbing data entry and raw code scaffolding into a near-instantaneous execution, freeing up the engineer to focus entirely on the actual control logic and edge cases.


šŸ’” Who Benefits Most? The Power of Great Libraries

The engineers who will extract the absolute highest ROI from this AI paradigm are those who possess the best, most structured modular software and clean library frameworks.

If your company relies on chaotic, unorganized, legacy spaghetti code, an LLM will struggle to find patterns and will likely amplify that chaos. However, if you have a highly standardized codebase with robust, clean, well-documented Function Blocks and well-defined library architectures, the Agent becomes an exponential force multiplier. It can read your library documentation, understand your internal APIs, and perfectly assemble your standard modules to fit new machine configurations or execute state machine transitions without breaking a sweat.

Overleaping the Need for Junior Engineers?

This workflow opens up an important discussion on the evolution of team structures in industrial automation. Historically, repetitive tasks like manual data entry from CSV sheets, setting up empty code layouts, mapping hardware nodes, and drafting basic sequence steps were delegated to junior engineers.

With a well-configured MCP Agent, a senior engineer can comfortably bypass the need for junior staff for this kind of engineering grunt work. The senior developer, who already has the required total system architecture mastery to review and validate every single line of code, can leverage the AI as an untiring, lightning-fast assistant. The engineering workflow shifts to a pure supervisory model: the senior architect guides the AI, reviews the output, and refines the physical execution, removing the traditional bottleneck of manual routine typing.


šŸŽÆ Final Thoughts

Integrating an MCP Coding Agent into CODESYS proves that AI isn't going to replace the PLC programmer anytime soon. Instead, it will drastically alter the role. By swallowing up the tedious data-entry tasks, structuring complex state machines from raw inputs, and providing highly structured code skeletons, it allows automation engineers to focus on what they do best: managing the physics of the machine and optimizing the production cycle.

Have you tried hooking your automation IDE up to an LLM yet? What has your experience been with AI-generated Structured Text? Let's discuss in the comments below!

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev

PLC work is exactly where agentic coding needs hard boundaries. The assistant can help generate and inspect logic, but anything touching real equipment needs simulation, diff review, and explicit operator approval. Agent speed is useful only after the safety loop is boring.