AI & Machine Learning in Automation

CODESYS MCP Server and Claude Code: AI Generates PLC Code

MSc. Matthew Kurantowicz 12 min read Updated

The CODESYS MCP server is a free bridge between the CODESYS IDE and an AI assistant - it lets you programmatically create POUs, function blocks, and Structured Text code using natural language. In this tutorial I'll walk you through installing the community toolkit from GitHub, connecting Claude Code, and generating a complete five-station pump control system in a few minutes. No Professional Developer Edition required.

What is the MCP Server in CODESYS and Why It Changes Everything

Model Context Protocol (MCP) is an open standard designed by Anthropic - the company behind Claude models. MCP gives LLMs secure, controlled access to external tools: databases, APIs, file systems, and now industrial automation IDEs. The CODESYS MCP server exposes IDE functions as "tools" for the AI model - Claude can then open projects, create POUs, write variable declarations and Structured Text implementations, trigger compilation, and read back errors.

This is a qualitative difference compared to plain copy-paste from a chat window. The AI doesn't generate code for you to paste - the AI operates directly on your CODESYS project: creates the function block folder, adds declarations, fills in the implementation, closes the project. You just describe what needs to be done.

There are two ways into this world: a paid official CODESYS add-on and a free community toolkit. Let's start with a comparison.

CODESYS AI-Supported Engineering vs MCP Toolkit

CODESYS recently launched its own "AI-supported Engineering" feature as part of the Professional Developer Edition. It's a paid package that integrates a language model with the IDE - generates POUs, analyzes existing code, adds comments, creates test blocks. A mature solution with vendor support, but it requires a subscription.

The second path is a community project on GitHub: codesys-mcp-toolkit by Johannes Pettersson. The toolkit runs a local MCP server that talks to CODESYS project files (.project) and to the IDE itself. Zero fees - you only pay for model tokens (or use a Claude Max subscription, which covers most projects easily).

Functionally both solutions do similar things: create POUs, function blocks, functions, declarations and implementations, handle properties and methods for object-oriented programming, compile the project, and read back errors. The difference lies in stability, ease of setup, and who's responsible for compatibility with new CODESYS versions. The community toolkit has been tested with CODESYS V3.5 SP22 Patch 1 - that's the version I used during recording and everything worked without issues.

If you work in the Siemens ecosystem, check out a similar approach for TIA Portal: TIA Portal MCP Server - AI Assistants Inside Your PLC Engineering.

How the CODESYS Development System MCP server works

Under the hood there are three pieces. The MCP client (Claude Code here, but any MCP-enabled client like Claude Desktop, Cursor or VS Code works the same way) talks to the toolkit via Model Context Protocol. The toolkit is a small Node.js process that publishes a tool bundle: open or create a project, add a program organization unit (POU), set its declaration and implementation, add methods and properties, compile, read messages. And the third piece is the CODESYS Development System itself, the IEC 61131-3 development environment that actually owns the project.

The toolkit does not edit .project files byte by byte. It starts CODESYS and drives it through the CODESYS scripting engine, the same Python scripting interface you can use for your own automation (we covered it in Python scripting engine for CODESYS). That is why the server configuration in .mcp.json has to point to your CODESYS.exe and to the profile of the installed CODESYS version (check the exact argument names in the toolkit README), for example "CODESYS V3.5 SP21" or "CODESYS V3.5 SP22 Patch 1". If you have several versions installed through the CODESYS Installer, the profile decides which one the AI agent works with.

Because everything goes through the real IDE, the result is a normal CODESYS V3 development project. Code generation, code editing and code analysis happen on the same objects you see in the device tree, and the compiler messages the model reads are the ones you would see yourself. Nothing is stored in a proprietary format.

Installing the CODESYS MCP Server Step by Step

The entire installation is done... through Claude Code. You don't need to type commands manually - just tell the model "install this toolkit from GitHub" and Claude follows the README instructions, only asking for confirmation at each command.

Step 1: Prerequisites

Before you run anything, make sure you have:

  • Node.js 18+ (node --version should return v18.x or higher)
  • Git installed globally
  • Claude Code (npm install -g @anthropic-ai/claude-code)
  • CODESYS V3.5 SP22 (or a newer patch - older versions may have path issues)
  • A Claude Pro/Max subscription or an Anthropic API key

Step 2: Clone the Repo and npm install via Claude

Open PowerShell in your working folder, trust the folder in Claude Code, and prompt:

Install the codesys-mcp-toolkit from
https://github.com/johannesPettersson80/codesys-mcp-toolkit
step by step

Claude will run git clone, navigate to the folder, and execute npm install. You just click "Yes" at each command (or "Yes, and don't ask again" for repeating actions like npm install).

Step 3: Configure .mcp.json

The last step is pointing to the CODESYS executable. Claude generates a .mcp.json file in the project folder and asks you to confirm the path (default: C:\Program Files\CODESYS 3.5.22.0\CODESYS\Common\CODESYS.exe). After restarting Claude Code, you'll see in the terminal:

codesys_local - connected

That means the MCP server is listening and Claude has access to CODESYS tools. From this point on, you can request project operations in natural language.

Creating FB_TwoPumpControl Through AI

Practical test: controlling a dual-pump station with fault logic and duty alternation. We create a function block FB_TwoPumpControl with inputs (bEnable, bDemand, bManualMode, bFaultPump1, bFaultPump2, tAlternateInterval), outputs (bAlarm, bRunPump1, bRunPump2), and logic that switches to the standby pump on fault.

Prompt for Claude Code:

Create a function block called FB_TwoPumpControl in Structured Text
under Application. Declarations: inputs bEnable, bDemand, bManualMode,
bFaultPump1, bFaultPump2, tAlternateInterval TIME; outputs bAlarm,
bRunPump1, bRunPump2. Internal: timers for alternation.
Implementation: if fault on pump1 run pump2; alternate based on
tAlternateInterval when no faults.

Key gotcha: the CODESYS project must be closed in the IDE when the MCP server modifies it. Otherwise you'll get:

> The project is locked because it's already open in the CODESYS IDE (by 'Administrator' on 'LAPTOP'). The MCP server can't access it while the IDE has it locked.

Close the project (File → Close Project) or shut down CODESYS entirely, then re-run the prompt.

After confirmation, Claude creates the POU in about ten seconds, fills in declarations, and writes several dozen lines of ST. You open the project - the function block is already sitting under Application, ready to compile.

With a second prompt you instantiate it in PLC_PRG as an array of five stations:

VAR
   aStation          : ARRAY[0..4] OF FB_TwoPumpControl;
   bSystemEnable     : BOOL;
   bTestDemand       : BOOL;
   nStationIndex     : INT;
   abFaultP1         : ARRAY[0..4] OF BOOL;
   abFaultP2         : ARRAY[0..4] OF BOOL;
   abStationAlarm    : ARRAY[0..4] OF BOOL;
   abRunning1        : ARRAY[0..4] OF BOOL;
   abRunning2        : ARRAY[0..4] OF BOOL;
END_VAR

// Loop through all 5 stations
FOR nStationIndex := 0 TO 4 DO
   // Wire inputs
   aStation[nStationIndex].bEnable            := bSystemEnable;
   aStation[nStationIndex].bDemand            := bTestDemand;
   aStation[nStationIndex].bManualMode        := FALSE;
   aStation[nStationIndex].bFaultPump1        := abFaultP1[nStationIndex];
   aStation[nStationIndex].bFaultPump2        := abFaultP2[nStationIndex];
   aStation[nStationIndex].tAlternateInterval := T#10S;

   // Call station instance
   aStation[nStationIndex]();

   // Copy outputs to monitoring arrays
   abStationAlarm[nStationIndex] := aStation[nStationIndex].bAlarm;
   abRunning1[nStationIndex]     := aStation[nStationIndex].bRunPump1;
   abRunning2[nStationIndex]     := aStation[nStationIndex].bRunPump2;
END_FOR

Compile, deploy to the simulator, set bSystemEnable and bTestDemand to TRUE - pump 1 starts. Force abFaultP1[0] := TRUE - the standby pump kicks in immediately. The logic works without writing a single line by hand.

Use cases: where an AI agent helps in CODESYS

The pump station above is a good demo, but the daily use cases are more down to earth:

  • Boilerplate POUs. Function blocks for motors, valves, alarms and analog scaling follow the same pattern in every project. Describe the interface once, let the AI agent create the POU, then review the Structured Text.
  • Repeating structures. Arrays of stations, axis instances or fieldbus device wrappers are tedious to type and easy for large language models to get right when the naming convention is clear.
  • Code analysis of an existing codebase. Ask the model to open the entire project, list all POUs and explain the call structure before you change anything. This is useful on machines you did not write yourself.
  • Documentation and comments. Generating header comments and variable descriptions for each program organization unit is low risk and saves hours.
  • Interfaces for data exchange. Declaring the structures you publish over OPC UA or exchange with a SCADA or building automation system is repetitive work the model handles well, while you decide what is actually exposed.

What does not belong here: safety functions, anything that changes the machine without review, and online changes on a running industrial control system. The AI writes to the project, never directly to the controller.

Keep AI edits under version control

When AI systems edit your project, version control stops being optional. Commit the project before each AI session, so every change the model makes can be reviewed and reverted. CODESYS Git, available in the CODESYS Store, brings Git (a distributed version control system) into the CODESYS Development System, so you can diff POUs instead of binary .project files. If you are new to Git, start with our guide GitHub for PLC programmers. It is the same DevOps habit modern software development uses: small steps, every change reviewed.

Common Problems and How to Work Around Them

Project lock. The most frequent error - the CODESYS IDE holds the project open. Close the project before every prompt that modifies the structure. Alternatively: kill the CODESYS process in Task Manager if a previous session left a lock file behind.

Token burn on large projects. A single function block is a few thousand tokens in input + output. Dozens of POUs + compilation + error readback can eat through the Claude Pro limit in one session. A Max subscription or API billing solves the problem.

Claude sometimes "forgets" the project path. In long sessions it helps to remind it every few prompts: "Work in folder C:\Users\Administrator\Documents\MCP server codesys v02". Without this, the model occasionally creates POUs in the wrong location.

Incomplete type declarations. Claude doesn't always guess whether you want TIME or TON, UINT or DINT. Be specific in your prompts, or ask for a review after generation: "List all VAR declarations with their types and comments."

For a refresher on classic blocks that AI frequently inserts into generated code: Triggers in CODESYS: R_TRIG and F_TRIG for edge detection and Complete guide to CODESYS counters (CTU, CTD, CTUD).

Summary: the CODESYS MCP server in 2026

The CODESYS MCP server changes how you program PLCs - from typing ST line by line to having a conversation with an AI assistant that operates directly on your project. The community toolkit from GitHub gives you the full workflow for free: installation through Claude Code, function block creation, instantiation in PLC_PRG, simulation. The paid CODESYS Professional Developer Edition offers the same with official vendor support.

The key rule: close the IDE before every MCP operation, refine your prompts, use naming conventions consistent with your project. The rest - compilation, testing, fine-tuning - is classic automation engineer work.

CODESYS MCP server - frequently asked questions

Does the CODESYS MCP server work with versions older than V3.5 SP22?

The community toolkit has been tested on V3.5 SP22 Patch 1. Older versions (SP19, SP20) may require path and parameter modifications in .mcp.json. The safest approach is to update CODESYS to the latest patch before installing the toolkit.

Do I need a Claude Max subscription for this to work?

No. It works with Claude Pro and pay-as-you-go through the Anthropic API. A Max subscription simplifies things for large projects because you don't worry about limits - but a single function block fits comfortably within Pro.

Will AI generate Safety-certified code compliant with ISO 13849?

Not without additional verification. An LLM can sketch the structure, but certified Safety code must be written and verified by an engineer with the proper qualifications - in accordance with EN ISO 13849-1 and IEC 61508. Treat AI like a junior engineer: it generates the skeleton, you do the review and validation testing.

What's the difference between the MCP toolkit and CODESYS Professional Developer Edition?

The community toolkit is free, open source, and community-maintained. PDE is an official CODESYS product with support, guaranteed compatibility with new versions, and integration with other CODESYS tools (e.g., CODESYS Automation Server). Functionally they do the same thing; the difference is price and SLA.

Does Claude Code understand prompts in languages other than English?

Yes. Claude is multilingual and handles prompts in Polish, German, French, and many other languages very well. Technical terminology (function block, POU, Structured Text) stays in the original English, but the rest of your prompt can be in whatever language you're most comfortable with.

Can I use the CODESYS MCP server with Cursor, VS Code or ChatGPT?

Any MCP client that can start a local server process works: Claude Code, Claude Desktop, Cursor and VS Code with GitHub Copilot in agent mode. The server has to run on the same Windows machine as the CODESYS Development System. ChatGPT and other browser-based tools cannot start a local process, so they are not a practical fit for this toolkit.

How is this different from the TIA Portal MCP server?

The idea is the same, the engineering API is different. In CODESYS the toolkit works through the scripting engine and creates POUs directly. In Siemens TIA Portal the server goes through the Openness API and imports SCL sources and tags. The Siemens version is described step by step in our TIA Portal MCP server guide linked above.

---

Want to learn PLC programming from scratch so you can fully leverage tools like the CODESYS MCP server? Check out our PLC Structured Text programming course in CODESYS - we teach on real industrial projects, from variable declarations to complete process stations. AI will help you write faster, but only if you know what to write.

Online courses

Want to program PLCs in practice?

Siemens, CODESYS, communication and safety courses with a certificate and instructor support. Start with a free mini-course.

See courses
Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare