@codesys/mcp-toolkit 2.1.2 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/README.md +190 -10
  3. package/README.zh-CN.md +254 -0
  4. package/dist/actions.d.ts +9 -4
  5. package/dist/actions.js +1 -1
  6. package/dist/actions.js.map +1 -1
  7. package/dist/bridge/bridge.d.ts +12 -1
  8. package/dist/bridge/bridge.js +77 -11
  9. package/dist/bridge/bridge.js.map +1 -1
  10. package/dist/cli.js +7 -1
  11. package/dist/cli.js.map +1 -1
  12. package/dist/codesys/debug.d.ts +5 -0
  13. package/dist/codesys/debug.js +89 -0
  14. package/dist/codesys/debug.js.map +1 -0
  15. package/dist/codesys/device-tools.d.ts +3 -0
  16. package/dist/codesys/device-tools.js +38 -0
  17. package/dist/codesys/device-tools.js.map +1 -0
  18. package/dist/codesys/graphical.d.ts +76 -0
  19. package/dist/codesys/graphical.js +226 -0
  20. package/dist/codesys/graphical.js.map +1 -0
  21. package/dist/codesys/lang.d.ts +39 -0
  22. package/dist/codesys/lang.js +177 -0
  23. package/dist/codesys/lang.js.map +1 -0
  24. package/dist/codesys/simtest.d.ts +48 -0
  25. package/dist/codesys/simtest.js +103 -0
  26. package/dist/codesys/simtest.js.map +1 -0
  27. package/dist/codesys/st.d.ts +1 -1
  28. package/dist/codesys/st.js.map +1 -1
  29. package/dist/codesys/trace.d.ts +19 -0
  30. package/dist/codesys/trace.js +139 -0
  31. package/dist/codesys/trace.js.map +1 -0
  32. package/dist/hmi/native.d.ts +89 -0
  33. package/dist/hmi/native.js +447 -0
  34. package/dist/hmi/native.js.map +1 -0
  35. package/dist/hmi/render.d.ts +90 -0
  36. package/dist/hmi/render.js +307 -0
  37. package/dist/hmi/render.js.map +1 -0
  38. package/dist/hmi/route.d.ts +32 -0
  39. package/dist/hmi/route.js +184 -0
  40. package/dist/hmi/route.js.map +1 -0
  41. package/dist/hmi/service.d.ts +45 -0
  42. package/dist/hmi/service.js +182 -0
  43. package/dist/hmi/service.js.map +1 -0
  44. package/dist/hmi/spec.d.ts +821 -0
  45. package/dist/hmi/spec.js +191 -0
  46. package/dist/hmi/spec.js.map +1 -0
  47. package/dist/hmi/templates.d.ts +11 -0
  48. package/dist/hmi/templates.js +92 -0
  49. package/dist/hmi/templates.js.map +1 -0
  50. package/dist/hmi/tools.d.ts +4 -0
  51. package/dist/hmi/tools.js +222 -0
  52. package/dist/hmi/tools.js.map +1 -0
  53. package/dist/hmi/value.d.ts +3 -0
  54. package/dist/hmi/value.js +26 -0
  55. package/dist/hmi/value.js.map +1 -0
  56. package/dist/panel/hmi.js +138 -0
  57. package/dist/panel.d.ts +3 -0
  58. package/dist/panel.js +17 -0
  59. package/dist/panel.js.map +1 -1
  60. package/dist/server.d.ts +1 -1
  61. package/dist/server.js +7 -2
  62. package/dist/server.js.map +1 -1
  63. package/dist/tools.d.ts +5 -1
  64. package/dist/tools.js +19 -3
  65. package/dist/tools.js.map +1 -1
  66. package/dist/viewer.d.ts +1 -1
  67. package/dist/viewer.js +13 -2
  68. package/dist/viewer.js.map +1 -1
  69. package/package.json +1 -1
  70. package/watcher/mcp_alarms.py +134 -0
  71. package/watcher/mcp_debug.py +161 -0
  72. package/watcher/mcp_devices.py +83 -0
  73. package/watcher/mcp_lang.py +179 -0
  74. package/watcher/mcp_ops.py +3 -1
  75. package/watcher/mcp_trace.py +90 -0
  76. package/watcher/mcp_ui.py +54 -0
  77. package/watcher/mcp_visu.py +422 -0
  78. package/watcher/watcher.py +52 -10
package/CHANGELOG.md CHANGED
@@ -5,6 +5,106 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [Unreleased]
9
+
10
+ ## [2.3.0] - 2026-11-14
11
+
12
+ Several of the new actions use CODESYS's own services inside the IDE, beyond
13
+ what its scripting API documents: `get_signature`, `find_references`,
14
+ `rename_symbol`, `trace_setup`, `trace_record`, the debug actions,
15
+ `hmi_export_codesys` and `--show-window`. They are experimental and checked on
16
+ the versions noted below.
17
+
18
+ ### Added
19
+ - HMI screens: Claude designs live operator screens for the program
20
+ (`hmi_save_screen`): a process diagram with tanks, pumps, fans, valves and
21
+ animated pipes, plus gauges, values with sparklines, bars, status lights,
22
+ buttons, switches, setpoints, trends and alarms, bound to PLC variables, in a
23
+ dark or light theme. `hmi_screenshot` returns the screen as an image so the
24
+ model can check and polish its layout. `open_hmi` opens it in the browser
25
+ (127.0.0.1 with a token), live while the simulation runs; buttons, switches
26
+ and setpoints write to the simulation only. Screens are stored next to the
27
+ project as `<project>.hmi.json`.
28
+ - `hmi_export_codesys`: the same screen as a native CODESYS visualization,
29
+ with cards, gradients, SVG pump, fan and valve symbols in an embedded image
30
+ pool, live values, bars, tank levels, colour states, buttons, switches,
31
+ setpoints and alarm rows; a generated program scales values for bars and
32
+ levels. Built through CODESYS's visualization scripting and its element
33
+ creators. Returns a picture of the visualization. Checked on SP21.
34
+ - HMI projects with several pages: a shared header with links to every page and
35
+ the count of active alarms across all pages; page templates (`hmi_template`:
36
+ overview, unit, alarms, trends, settings); faceplate popups on pumps, fans
37
+ and valves; panel sizes 1280x720, 1920x1080, 1024x768 and 800x480; warnings
38
+ for overlapping symbols. Trend history survives page changes.
39
+ - HMI pipes connect to named ports on tanks, pumps, fans, valves and junctions
40
+ and are routed with right angles, so they meet the symbols exactly in the
41
+ browser and in CODESYS.
42
+ - `get_signature`: the interface of any POU or type from CODESYS's compiler,
43
+ including FBs in compiled libraries (TON, CTU, MC_...), with doc comments.
44
+ - `find_references`: where a variable, POU or type is declared, read, written
45
+ or called, with line and column. Both checked on SP21 and SP22.
46
+ - `trace_setup` and `trace_record`: CODESYS Trace in the simulation, returned
47
+ as a summary (BOOL edges and extremes with their times), a table and a
48
+ chart image. Checked on SP22 without a window.
49
+ - `create_graphical`: ladder, FBD and SFC programs and function blocks from a
50
+ structured description, generated as PLCopenXML. Checked on SP22: a ladder
51
+ latch, an AND into a TON and a three-step SFC compile and behave correctly
52
+ in the simulation.
53
+ - Debugging in the simulation: `breakpoint_set` (by line), `debug_status`,
54
+ `debug_step` (over, into, out), `debug_continue`, `breakpoint_clear`.
55
+ Checked on SP22 without a window.
56
+ - `rename_symbol`: rename a variable or POU everywhere it is used, from
57
+ CODESYS's cross-references (works without a window; CODESYS's own
58
+ refactoring needs one). Checked on SP22: 0 build errors after.
59
+ - `codesys_sim_test`: test cases in the simulation (write or force, cycles or
60
+ real time, expected values with comparisons and tolerances), reported as a
61
+ pass/fail table. Checked on SP22.
62
+ - `find_device` and `add_device`: search CODESYS's device repository and add
63
+ devices to the tree. Checked on SP22: Ethernet, a Modbus TCP Client and a
64
+ Modbus TCP Server device compile with 0 errors.
65
+ - `create_object` creates persistent variable lists (`persistent_vars`).
66
+ - `hmi_export_codesys` builds every page: page buttons in each header
67
+ (change shown visualization), the start page and client size of
68
+ TargetVisu and WebVisu, turning pumps and fans (8 frames chosen by the
69
+ clock), faceplates as CODESYS dialog visualizations, the pages' alarms in
70
+ CODESYS alarm management (an alarm group with Error/Warning/Info classes
71
+ and message texts), and visualization user groups with a Login button.
72
+ Checked on SP21: three pages and two dialogs compile with 0 errors; the
73
+ alarms show in CODESYS's alarm group editor with their messages after the
74
+ project is reopened, and in the simulation CODESYS's alarm manager reports
75
+ the raised fault as an active alarm.
76
+ - Input actions only go on shapes: CODESYS raises an assertion dialog when
77
+ an image element gets one.
78
+ - The browser viewer closes open connections when it stops, so an open HMI
79
+ tab no longer keeps the server from exiting. Screenshots retry once when
80
+ the headless browser hangs at start.
81
+ - Actions can return images.
82
+ - `--show-window` opens every object the model creates or writes in its editor
83
+ tab, so the CODESYS window shows the code as it is written, and, while the
84
+ simulation runs, its live values. CODESYS's scripting API has no call for
85
+ this, so it uses CODESYS's own frame (checked on SP21 and SP22); where that
86
+ isn't available, nothing opens and the call works as before.
87
+
88
+ ### Changed
89
+ - The Chinese README states model support directly and lists the models
90
+ tested with OpenCode, as suggested by Zhen Yang (Alfabete-spec), who
91
+ reviews the Chinese documentation.
92
+
93
+ ## [2.2.0] - 2026-11-14
94
+
95
+ ### Added
96
+ - `--show-window`: start CODESYS with its window instead of headless, to watch
97
+ the work in clients without the live panel (such as OpenCode). Prompts are
98
+ answered as when headless, and the toolkit never closes the window: not on a
99
+ hang, not when the server stops.
100
+ - README: setup for OpenCode and other MCP clients, and the models tested with
101
+ OpenCode 1.19.5 on CODESYS SP22 (DeepSeek V4.1 Flash, Qwen 3.6 Plus, GLM 5.3,
102
+ MiniMax M3: all passed). New screenshots: Claude building and testing a
103
+ tank level controller next to the workspace (top of the README), an OpenCode
104
+ session, the browser workspace and the CODESYS window.
105
+ - A Simplified Chinese README (`README.zh-CN.md`): setup for OpenCode and other
106
+ clients that work in China, an npmmirror install line, and the tested models.
107
+
8
108
  ## [2.1.2] - 2026-11-13
9
109
 
10
110
  ### Fixed
package/README.md CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  # CODESYS MCP Toolkit
4
4
 
5
- **Let Claude read, write, compile and simulate Structured Text in your CODESYS V3.5 projects.**
5
+ **Let Claude, or any MCP client, read, write, compile and simulate Structured Text in your CODESYS V3.5 projects.**
6
+
7
+ English | [简体中文](README.zh-CN.md)
6
8
 
7
9
  [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
8
10
  [![npm](https://img.shields.io/npm/v/@codesys/mcp-toolkit)](https://www.npmjs.com/package/@codesys/mcp-toolkit)
@@ -18,7 +20,7 @@ open in the background and gives Claude, or any MCP client, direct access to
18
20
  it. Claude works on your real project with the real CODESYS compiler, and you
19
21
  can watch the code change as it happens.
20
22
 
21
- ![The live workspace, showing a sample project with a change waiting for review](docs/images/workspace.png)
23
+ ![Claude builds a tank level controller in CODESYS: the live panel in the chat, the code compiled with 0 errors and five simulation tests passed, and the same function block in the CODESYS-style workspace, ready to review](docs/images/claude-and-codesys.png)
22
24
 
23
25
  ## Highlights
24
26
 
@@ -32,6 +34,9 @@ can watch the code change as it happens.
32
34
  interfaces, without reading every object.
33
35
  - **Shows its work.** A live panel with the project tree, the code as it is
34
36
  written, compile results, and Accept or Revert for each change.
37
+ - **Designs operator screens.** Claude builds a live HMI for your program,
38
+ with tanks, pumps, valves, gauges, trends and alarms, looks at it, and
39
+ polishes it. You operate it in the browser against the simulation.
35
40
  - **Never touches a real controller.** The only online connection is to
36
41
  CODESYS's own simulation, and every online call checks for it first.
37
42
 
@@ -79,6 +84,9 @@ Claude Desktop:
79
84
  `--codesys-path` and `--codesys-profile` are optional. Without them the server
80
85
  uses the newest CODESYS install under Program Files and its newest profile.
81
86
 
87
+ Using OpenCode, Cursor, Cline or another client, or a model such as DeepSeek or
88
+ Qwen? See [Other clients and models](#other-clients-and-models).
89
+
82
90
  ### Try it
83
91
 
84
92
  > Create a project called `BlinkDemo.project`. Add a function block that toggles
@@ -101,19 +109,33 @@ operation. [The reference](docs/reference.md) describes each one.
101
109
  | --- | --- |
102
110
  | **Projects** | `open_project` `create_project` `save_project` `close_project` `codesys_status` |
103
111
  | **Read code** | `get_project_tree` `read_objects` `search_code` `get_outline` |
104
- | **Change code** | `write_object` `create_object` `delete_object` `rename_object` |
112
+ | **Change code** | `write_object` `create_object` `create_graphical` `delete_object` `rename_object` |
105
113
  | **Build** | `compile` |
106
- | **Libraries** | `list_libraries` `add_library` `remove_library` `get_library_api` |
107
- | **Tasks and I/O** | `list_tasks` `configure_task` `get_io_list` `get_devices` |
114
+ | **Libraries** | `list_libraries` `add_library` `remove_library` `get_library_api` `get_signature` |
115
+ | **Navigate code** | `find_references` `rename_symbol` |
116
+ | **Tasks and I/O** | `list_tasks` `configure_task` `get_io_list` `get_devices` `find_device` `add_device` |
108
117
  | **Change tracking** | `checkpoint` `get_changes` `revert_object` |
109
- | **Simulation** | `codesys_sim_start` `codesys_sim_stop` `codesys_sim_status` `codesys_sim_control` `codesys_sim_cycle` `codesys_sim_read` `codesys_sim_write` `codesys_sim_force` `codesys_sim_unforce` |
118
+ | **Simulation** | `codesys_sim_start` `codesys_sim_stop` `codesys_sim_status` `codesys_sim_control` `codesys_sim_cycle` `codesys_sim_read` `codesys_sim_write` `codesys_sim_force` `codesys_sim_unforce` `codesys_sim_test` |
119
+ | **Trace** | `trace_setup` `trace_record` |
120
+ | **Debug** | `breakpoint_set` `breakpoint_clear` `debug_status` `debug_step` `debug_continue` |
121
+ | **HMI screens** | `hmi_save_screen` `hmi_template` `hmi_screenshot` `hmi_export_codesys` `hmi_list` `hmi_delete_screen` `open_hmi` |
110
122
  | **Helpers** | `batch` `wait` `help` `open_workspace` |
111
123
 
112
124
  `batch` runs up to 50 actions in one call, which is much faster than separate
113
125
  calls.
114
126
 
115
- Version 2 edits Structured Text only. Objects with graphical bodies (SFC, CFC,
116
- LD, FBD) appear in the tree and can be read as PLCopenXML, but not edited.
127
+ Some actions go beyond what CODESYS's scripting API documents and use CODESYS's
128
+ own services inside the IDE: `get_signature`, `find_references`,
129
+ `rename_symbol`, `trace_setup`, `trace_record`, the debug actions,
130
+ `hmi_export_codesys` (with its alarms and user groups) and `--show-window`.
131
+ They are **experimental**: checked on CODESYS V3.5 SP21 and SP22 as noted in
132
+ the [changelog](CHANGELOG.md). On another version they may report that the
133
+ feature isn't available, or not work.
134
+
135
+ Claude writes Structured Text, and creates ladder (LD), FBD and SFC programs
136
+ and function blocks with `create_graphical` from a simple description of rungs,
137
+ networks or steps; CODESYS draws them. Existing graphical bodies can be read as
138
+ PLCopenXML but not edited in place yet. CFC isn't supported.
117
139
 
118
140
  ## Live panel
119
141
 
@@ -125,6 +147,8 @@ the project tree, editor tabs, compile problems and a review list.
125
147
  From the panel you can **Accept** or **Revert** each change, compare it with
126
148
  the version you last reviewed, and **Pause Claude** while you look around.
127
149
 
150
+ ![The workspace: a PI controller function block with properties and an interface, changed by Claude and waiting for review](docs/images/workspace.png)
151
+
128
152
  The card needs a client that supports
129
153
  [MCP Apps](https://modelcontextprotocol.io/extensions/apps). In our testing
130
154
  Claude Desktop draws it in the Code tab but not in a normal chat
@@ -134,6 +158,128 @@ the browser never opens by itself.
134
158
 
135
159
  [More about the panel](docs/reference.md#live-panel)
136
160
 
161
+ ## HMI screens
162
+
163
+ Ask Claude for an operator screen and it designs one for your program: a
164
+ process diagram with tanks, pumps, fans, valves and flowing pipes, and gauges,
165
+ values, trends, status lights, alarms, buttons, switches and setpoints, all
166
+ bound to your PLC variables.
167
+
168
+ ![The live HMI Claude designed for a pump station, running against CODESYS's simulation: the pump's faceplate switched to manual and started, the pump turning and the pipes flowing, a fault raised on the alarm page, the trend page, and the alarm on the overview](docs/images/hmi-live.gif)
169
+
170
+ - **A whole HMI, not one screen.** Pages (overview, unit, alarms, trends,
171
+ settings) share a header that links to every page and counts the active
172
+ alarms of all pages. `hmi_template` gives Claude a good layout for each kind
173
+ of page to adapt. Panels: 1280x720, 1920x1080, 1024x768 or 800x480.
174
+ - **Faceplates.** Clicking a pump, fan or valve opens its popup: mode switch,
175
+ Start/Stop or Open/Close, Reset, live values and a setpoint.
176
+ - **Pipes connect themselves.** Tanks, pumps, fans, valves and junctions have
177
+ named ports (`T-101.right`, `P-101.in`, `V-102.out`). Claude says what a pipe
178
+ connects, and the toolkit routes it with right angles to meet each symbol
179
+ exactly, in the browser and in CODESYS alike. Unknown symbols or ports are
180
+ refused with the list of valid ones.
181
+ - **Claude checks its own work.** `hmi_screenshot` returns the screen as an
182
+ image, so Claude can see overlaps, empty space or unclear labels and fix
183
+ them before you look.
184
+ - **Live in your browser.** Ask Claude to open the HMI (`open_hmi`). The page
185
+ is served by the toolkit on 127.0.0.1 with a token, like the workspace. While
186
+ CODESYS's simulation runs, it shows live values, pumps spin, pipes flow and
187
+ trends draw. Buttons, switches and setpoints write to the simulation, so you
188
+ can operate your program before any hardware exists.
189
+ - **Stored with the project.** Screens are saved next to the project file as
190
+ `<project>.hmi.json`, so they travel with it and can go into version control.
191
+ - **Dark and light themes,** on a 1280x720 canvas that scales to any window.
192
+
193
+ The HMI writes only into CODESYS's own simulation, like the rest of the
194
+ toolkit. Screenshots use Microsoft Edge or Google Chrome, whichever is
195
+ installed.
196
+
197
+ ### As a native CODESYS visualization
198
+
199
+ `hmi_export_codesys` builds the same screen as a real CODESYS visualization in
200
+ your project. It is an ordinary visualization object, so it goes with the
201
+ application like one you drew yourself (we checked it in CODESYS's simulation,
202
+ not on a controller). It doesn't use CODESYS's stock lamps: cards with gradients, SVG
203
+ pump, fan and valve symbols in an embedded image pool, live values, bars and
204
+ tank levels, colour states, buttons, switches, setpoints and alarm rows that
205
+ appear only while active. A generated program, `PRG_HMI_<screen>`, scales
206
+ values for the bars and levels. Claude gets a picture of the result.
207
+
208
+ A multi-page HMI comes out as one visualization per page with page buttons in
209
+ the header, the first page as the start page of TargetVisu and WebVisu at the
210
+ panel size, pumps and fans that turn while running, faceplates as CODESYS
211
+ dialogs, the pages' alarms in CODESYS's alarm management (Alarm Configuration,
212
+ with timestamps and acknowledge in the PLC), and user groups (Operator,
213
+ Maintenance, Admin) behind a Login button. Users and passwords are yours to add
214
+ in the Visualization Manager.
215
+
216
+ ![The same pump station as a native CODESYS visualization, running in CODESYS's simulation](docs/images/hmi-codesys.png)
217
+
218
+ The alarms are CODESYS's own, not a copy in the HMI: an alarm group with its
219
+ observations, classes and messages, which CODESYS's alarm manager evaluates in
220
+ the PLC. In the simulation, raising the fault made the group report an active
221
+ alarm, and it stayed pending after the fault cleared, until acknowledged, as an
222
+ Error-class alarm should.
223
+
224
+ ![The exported alarms in CODESYS's alarm group editor: a digital observation on PLC_PRG.xHighLevel with class Warning and message "High level T-101 (above 90 %)", and one on PLC_PRG.xFault with class Error and message "Pump P-101 fault"](docs/images/codesys-alarms.png)
225
+
226
+ CODESYS's scripting can't create trend, meter or alarm-table elements, so
227
+ trends show their current values, gauges show a bar, and the alarm page shows a
228
+ row per alarm while it is active (acknowledge it in CODESYS's own alarm table,
229
+ which you can add from the toolbox). Checked on CODESYS V3.5 SP21 in the
230
+ simulation. A visualization needs the `FloatingPointUtils` library in CODESYS's
231
+ library repository; if it's missing, the export says so.
232
+
233
+ ## Other clients and models
234
+
235
+ The server works with any MCP client, not only Claude. For
236
+ [OpenCode](https://opencode.ai), put this in `opencode.json` in your project
237
+ folder (or in `~/.config/opencode/opencode.json`):
238
+
239
+ ```json
240
+ {
241
+ "$schema": "https://opencode.ai/config.json",
242
+ "mcp": {
243
+ "codesys": {
244
+ "type": "local",
245
+ "command": [
246
+ "codesys-mcp-tool",
247
+ "--codesys-path", "C:\\Program Files\\CODESYS 3.5.22.40\\CODESYS\\Common\\CODESYS.exe",
248
+ "--codesys-profile", "CODESYS V3.5 SP22 Patch 4",
249
+ "--workspace", "C:\\PLC\\Projects"
250
+ ],
251
+ "enabled": true
252
+ }
253
+ }
254
+ }
255
+ ```
256
+
257
+ `opencode mcp list` should then show `codesys` as connected. Clients that read
258
+ an `mcpServers` file (Cursor, Cline, VS Code and others) take the same
259
+ `command` and `args` as the Claude Desktop example above.
260
+
261
+ Tested on 13 November 2026 with OpenCode 1.19.5 and CODESYS V3.5 SP22: each of
262
+ these models created a project, read and changed `PLC_PRG`, compiled, listed
263
+ libraries and devices, and ran the simulation, without help.
264
+
265
+ | Model | Result |
266
+ | --- | --- |
267
+ | DeepSeek V4.1 Flash | All steps passed |
268
+ | Qwen 3.6 Plus | All steps passed |
269
+ | GLM 5.3 | All steps passed |
270
+ | MiniMax M3 | All steps passed |
271
+
272
+ ![OpenCode with DeepSeek V4.1 Flash: a pump function block written, compiled and tested in CODESYS's simulation](docs/images/opencode-session.png)
273
+
274
+ The live panel needs a client that shows MCP Apps cards (see
275
+ [Live panel](#live-panel)). In other clients `open_sim_panel` does nothing
276
+ visible; ask the model to open the workspace (`open_workspace`) to get the same
277
+ view in your browser, or start the server with
278
+ [`--show-window`](#watch-it-in-the-codesys-window) to watch the CODESYS window
279
+ itself.
280
+
281
+ ![The workspace in the browser after the OpenCode session: PLC_PRG's changes, and the review list with Accept and Revert for each object](docs/images/workspace-review.png)
282
+
137
283
  ## Safety
138
284
 
139
285
  - **No real controller.** The toolkit doesn't log in or download to a
@@ -166,6 +312,36 @@ the browser never opens by itself.
166
312
  | `--no-viewer` | Don't serve the browser workspace |
167
313
  | `--no-shared` | Run CODESYS in this process instead of the shared session |
168
314
  | `--attach <dir>` | Experimental: work in a CODESYS / Machine Expert window you already have open, instead of starting one. See [Working in an open window](#work-in-a-window-you-already-have-open-experimental) |
315
+ | `--show-window` | Start CODESYS with its window instead of headless. See [Watch it in the CODESYS window](#watch-it-in-the-codesys-window) |
316
+
317
+ ### Watch it in the CODESYS window
318
+
319
+ By default CODESYS runs headless. With `--show-window` the server starts it with
320
+ its normal window, so you can watch the work in any MCP client: every object the
321
+ model creates or writes opens in its editor tab, with the new code in it.
322
+
323
+ ![CODESYS started with --show-window: the model just created FB_Blink, and it opened in its editor](docs/images/codesys-window.png)
324
+
325
+ While the simulation runs, the open editors show live values, as in CODESYS's own
326
+ online mode:
327
+
328
+ ![The same window during the simulation: PLC_PRG shows xEnable and xOn as TRUE](docs/images/codesys-window-sim.png)
329
+
330
+ CODESYS's scripting API has no call for opening an editor, so the toolkit uses
331
+ CODESYS's own (undocumented) frame for it, checked on SP21 and SP22. If a CODESYS
332
+ version doesn't have it, nothing opens and everything else works as before.
333
+
334
+ While the server is connected, the window is for watching: the toolkit's script
335
+ keeps running in it (that is what lets it answer CODESYS's prompts, such as a
336
+ storage-format upgrade, the same way as headless), so CODESYS treats it as one
337
+ long operation. The status bar says "Executing script"; its **Click here to
338
+ CANCEL** link stops the toolkit's script, so leave it alone while you work with
339
+ the model. When the server stops, the script ends and the window is yours to
340
+ work in.
341
+
342
+ The toolkit never closes the window: not when a call hangs or a dialog waits
343
+ for an answer (answer it in the window), and not when the server stops. Close it
344
+ yourself when you're done; the next call then starts a new one.
169
345
 
170
346
  ### Several CODESYS versions
171
347
 
@@ -221,6 +397,8 @@ MCP client ── stdio ──▶ codesys-mcp-tool (Node.js)
221
397
  └─ fixed operation handlers (IronPython 2.7)
222
398
  ```
223
399
 
400
+ With `--show-window`, CODESYS starts without `--noUI` and runs the same watcher.
401
+
224
402
  - **No generated code.** Requests are an operation name plus JSON arguments,
225
403
  handled by fixed functions in `watcher/mcp_ops.py`.
226
404
  - **Hang detection.** The watcher reports `busy` with a deadline. If CODESYS
@@ -238,8 +416,8 @@ MCP client ── stdio ──▶ codesys-mcp-tool (Node.js)
238
416
  | **CODESYS is still starting** | The first start after boot can take minutes. Check `codesys_status`, then try again. |
239
417
  | **`startup_failed`** or **`startup_timeout`** | Check the profile name and that CODESYS starts by hand. The error includes the last lines of `watcher.log`. |
240
418
  | **Library errors after `create_project`** ([#4](https://github.com/johannesPettersson80/codesys-mcp-toolkit/issues/4)) | The default template is brought up to the running CODESYS, so its libraries should resolve. A template you pass yourself is used as it is. If libraries are still missing, install them with the CODESYS Library Repository. |
241
- | **`hung`** or **`timeout`** | CODESYS was restarted. Re-open the project. |
242
- | **`dialog`** | CODESYS opened a dialog, named in the error, that a session without a window can't answer. CODESYS was stopped and restarts on the next call. Do the same step once in the CODESYS window, answer the dialog there, and try again. |
419
+ | **`hung`** or **`timeout`** | CODESYS was restarted. Re-open the project. With `--show-window` nothing is closed: check the window. |
420
+ | **`dialog`** | CODESYS opened a dialog, named in the error, that a session without a window can't answer. CODESYS was stopped and restarts on the next call. Do the same step once in the CODESYS window, answer the dialog there, and try again. With `--show-window`, answer it in the window. |
243
421
 
244
422
  Each session folder keeps a `watcher.log`.
245
423
 
@@ -285,6 +463,8 @@ The persistent-watcher design borrows ideas from
285
463
  [phobicdotno/Codesys-MCP-Master](https://github.com/phobicdotno/Codesys-MCP-Master)
286
464
  (file-queue IPC, version-safe opening). Both are MIT-licensed.
287
465
 
466
+ The [Chinese README](README.zh-CN.md) was reviewed by Zhen Yang (Alfabete-spec).
467
+
288
468
  ## License
289
469
 
290
470
  [MIT](LICENSE)
@@ -0,0 +1,254 @@
1
+ <div align="center">
2
+
3
+ # CODESYS MCP 工具包
4
+
5
+ **让 Claude 或任何 MCP 客户端,在你的 CODESYS V3.5 项目中读取、编写、编译并仿真结构化文本(ST)代码。**
6
+
7
+ [English](README.md) | 简体中文
8
+
9
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)
10
+ [![npm](https://img.shields.io/npm/v/@codesys/mcp-toolkit)](https://www.npmjs.com/package/@codesys/mcp-toolkit)
11
+ ![CODESYS V3.5](https://img.shields.io/badge/CODESYS-V3.5-c8102e)
12
+ ![Node.js 20 or later](https://img.shields.io/badge/node-%E2%89%A520-339933)
13
+ ![Platform: Windows](https://img.shields.io/badge/platform-Windows-0078d4)
14
+ [![Model Context Protocol](https://img.shields.io/badge/MCP-server-6e56cf)](https://modelcontextprotocol.io)
15
+
16
+ </div>
17
+
18
+ 这是一个 [MCP](https://modelcontextprotocol.io)(模型上下文协议)服务器。它在后台保持一个
19
+ CODESYS 会话,让 AI 助手直接操作你的真实项目:用真正的 CODESYS 编译器编译,在 CODESYS
20
+ 自带的仿真中测试,而你可以实时看到代码的变化。
21
+
22
+ 工具包支持任何能够通过 MCP 客户端调用工具的 AI 模型。我们用 OpenCode 测试了 DeepSeek V4.1 Flash、
23
+ 通义千问 Qwen 3.6 Plus、GLM 5.3 和 MiniMax M3,均完成了全部步骤(见[已测试的模型](#已测试的模型))。
24
+
25
+ ![Claude 在 CODESYS 中编写液位控制功能块:对话中的实时面板、0 错误编译和 5 项仿真测试全部通过,右侧是同一功能块在工作区中的代码](docs/images/claude-and-codesys.png)
26
+
27
+ ## 主要功能
28
+
29
+ - **直接修改真实项目。** 以结构化文本编写 POU、方法、属性、接口、DUT 和 GVL。
30
+ - **用真正的编译器编译。** 错误会带上对象、行号和错误代码返回,包括还没有被任何任务调用的代码。
31
+ - **在仿真中测试。** 在 CODESYS 自带的仿真中运行、按周期单步执行、读取、写入和强制变量,
32
+ 无需安装其他软件,也不需要 PLC 硬件。
33
+ - **理解整个项目。** 提供项目大纲、I/O 列表、设备树和库接口,无需逐个读取对象。
34
+ - **过程可见。** 实时面板显示项目树、正在编写的代码和编译结果,每个改动都可以接受或还原。
35
+ - **绝不连接真实控制器。** 唯一的在线连接是 CODESYS 自带的仿真,每次在线操作前都会先检查。
36
+
37
+ ## 快速开始
38
+
39
+ ### 环境要求
40
+
41
+ - Windows,安装 **CODESYS V3.5** 及其 **Scripting**(脚本)插件。已在 SP21 和 SP22
42
+ 上测试,SP16 及以上版本应该都可以使用。
43
+ - **Node.js 20** 或更高版本。
44
+
45
+ 施耐德 EcoStruxure Machine Expert V2.3(`LogicBuilder.exe`)同样可用:只设置
46
+ `--codesys-path`,不要设置 `--codesys-profile`,因为 Machine Expert 没有配置文件。
47
+ 详见 [Machine Expert 说明](docs/machine-expert-testing.md)(英文)。
48
+
49
+ ### 安装
50
+
51
+ ```bash
52
+ npm install -g @codesys/mcp-toolkit
53
+ ```
54
+
55
+ 如果从 npm 下载很慢,可以使用 npmmirror 镜像:
56
+
57
+ ```bash
58
+ npm install -g @codesys/mcp-toolkit --registry=https://registry.npmmirror.com
59
+ ```
60
+
61
+ ### 在 OpenCode 中使用
62
+
63
+ 在项目文件夹的 `opencode.json` 中(或 `~/.config/opencode/opencode.json`)加入:
64
+
65
+ ```json
66
+ {
67
+ "$schema": "https://opencode.ai/config.json",
68
+ "mcp": {
69
+ "codesys": {
70
+ "type": "local",
71
+ "command": [
72
+ "codesys-mcp-tool",
73
+ "--codesys-path", "C:\\Program Files\\CODESYS 3.5.22.40\\CODESYS\\Common\\CODESYS.exe",
74
+ "--codesys-profile", "CODESYS V3.5 SP22 Patch 4",
75
+ "--workspace", "C:\\PLC\\Projects"
76
+ ],
77
+ "enabled": true
78
+ }
79
+ }
80
+ }
81
+ ```
82
+
83
+ 运行 `opencode mcp list`,应能看到 `codesys` 显示为已连接(connected)。
84
+
85
+ ### 其他客户端
86
+
87
+ Cline、Trae、通义灵码、Cursor、Claude Desktop 等使用 `mcpServers` 配置的客户端,
88
+ 一般都采用下面的格式(具体位置见各客户端的 MCP 设置):
89
+
90
+ ```json
91
+ {
92
+ "mcpServers": {
93
+ "codesys": {
94
+ "command": "codesys-mcp-tool",
95
+ "args": [
96
+ "--codesys-path", "C:\\Program Files\\CODESYS 3.5.21.0\\CODESYS\\Common\\CODESYS.exe",
97
+ "--codesys-profile", "CODESYS V3.5 SP21",
98
+ "--workspace", "C:\\PLC\\Projects"
99
+ ]
100
+ }
101
+ }
102
+ }
103
+ ```
104
+
105
+ `--codesys-path` 和 `--codesys-profile` 可以省略,省略时服务器会使用 Program Files 下
106
+ 最新安装的 CODESYS 及其最新的配置文件。
107
+
108
+ ### 试一试
109
+
110
+ > 创建一个名为 `BlinkDemo.project` 的项目。添加一个每秒翻转一次输出的功能块,在 `PLC_PRG`
111
+ > 中调用它,编译,并在仿真中证明输出确实在翻转。
112
+
113
+ 服务器启动时,CODESYS 会在后台启动。首次冷启动需要 1 到 3 分钟,之后大约 10 秒。
114
+
115
+ ## 已测试的模型
116
+
117
+ 2026 年 11 月 13 日,使用 OpenCode 1.19.5 和 CODESYS V3.5 SP22 测试:每个模型都在没有人工
118
+ 帮助的情况下完成了创建项目、读取并修改 `PLC_PRG`、编译、列出库和设备,以及运行仿真。
119
+
120
+ | 模型 | 结果 |
121
+ | --- | --- |
122
+ | DeepSeek V4.1 Flash | 全部通过 |
123
+ | Qwen 3.6 Plus | 全部通过 |
124
+ | GLM 5.3 | 全部通过 |
125
+ | MiniMax M3 | 全部通过 |
126
+
127
+ ![OpenCode 与 DeepSeek V4.1 Flash:编写水泵功能块,编译,并在 CODESYS 仿真中测试](docs/images/opencode-session.png)
128
+
129
+ ## 实时查看 AI 的工作
130
+
131
+ 有三种方式:
132
+
133
+ - **实时面板(MCP Apps 卡片)。** 在支持 [MCP Apps](https://modelcontextprotocol.io/extensions/apps)
134
+ 的客户端中,打开或创建项目后,面板会以卡片形式显示在对话中:项目树、正在编写的代码、
135
+ 编译问题,以及每个改动的“接受”和“还原”。
136
+ - **浏览器中的工作区。** 在其他客户端中,让模型“打开 CODESYS 工作区”(`open_workspace`),
137
+ 同样的界面会在浏览器中打开,只监听本机 127.0.0.1,并带有访问令牌。浏览器不会自动打开。
138
+ - **CODESYS 窗口。** 启动参数加上 `--show-window`,CODESYS 会以正常窗口运行,而不是在后台
139
+ 无界面运行。模型创建或修改的每个对象都会在编辑器标签页中打开,可以直接看到新写的代码;
140
+ 仿真运行时,打开的编辑器会显示变量的实时值。工具包不会自动关闭这个窗口。
141
+
142
+ ![使用 --show-window 启动的 CODESYS:模型刚创建的 FB_Blink 已在编辑器中打开](docs/images/codesys-window.png)
143
+
144
+ ![OpenCode 会话结束后在浏览器中打开的工作区:PLC_PRG 的改动,以及每个对象的接受和还原按钮](docs/images/workspace-review.png)
145
+
146
+ ## HMI 操作画面
147
+
148
+ 让 Claude 为你的程序设计操作画面:带储罐、泵、风机、阀门和流动管道的工艺流程图,以及仪表盘、
149
+ 数值、趋势图、状态灯、报警、按钮、开关和设定值,全部绑定到 PLC 变量。
150
+
151
+ ![Claude 为泵站设计的实时 HMI,连接 CODESYS 仿真运行:打开泵的面板切换到手动并启动,泵转动、管道流动,在报警页触发故障,趋势页,以及总览页上的报警](docs/images/hmi-live.gif)
152
+
153
+ - **完整的 HMI,而不只是一个画面。** 多个页面(总览、单元、报警、趋势、设置)共用一个页眉,
154
+ 页眉链接到每个页面并统计所有页面的活动报警。`hmi_template` 为每种页面提供良好的布局。
155
+ 面板尺寸:1280x720、1920x1080、1024x768 或 800x480。
156
+ - **操作面板(faceplate)。** 点击泵、风机或阀门会弹出面板:模式开关、启动/停止或开/关、复位、
157
+ 实时数值和设定值。
158
+ - **Claude 会检查自己的设计。** `hmi_screenshot` 把画面作为图片返回给 Claude,它能发现重叠、
159
+ 空白或不清楚的标签并自行修正。
160
+ - **在浏览器中实时运行。** 让 Claude 打开 HMI(`open_hmi`)。页面由工具包在 127.0.0.1 上提供,
161
+ 并带有访问令牌。CODESYS 仿真运行时显示实时数值,泵会转动,管道会流动;按钮、开关和设定值
162
+ 写入仿真,在没有硬件之前就能操作你的程序。
163
+ - **管道自动连接。** 储罐、泵、风机、阀门和三通都有命名接口(如 `T-101.right`、`P-101.in`),Claude 只需说明管道连接哪两个接口,
164
+ 工具包会以直角自动布线,在浏览器和 CODESYS 中都能精确对接。
165
+ - **与项目一起保存。** 画面保存在项目文件旁边的 `<项目名>.hmi.json` 中。
166
+
167
+ HMI 只写入 CODESYS 自带的仿真,不会连接真实控制器。
168
+
169
+ `hmi_export_codesys` 还能把同一个画面生成为项目中的原生 CODESYS 可视化(不用 CODESYS 自带的灯和按钮样式):
170
+ 渐变卡片、嵌入图片池的 SVG 泵/风机/阀门符号、实时数值、液位和棒图、按钮、开关、设定值和报警行,
171
+ 它是普通的可视化对象,随应用程序一起使用(我们在 CODESYS 仿真中验证过,未在真实控制器上测试)。
172
+
173
+ 多页 HMI 会生成为每页一个可视化,页眉中有页面按钮;第一页设为 TargetVisu 和 WebVisu 的起始页;
174
+ 运行中的泵和风机会转动;操作面板生成为 CODESYS 对话框;用户组(Operator、Maintenance、Admin)
175
+ 配有登录按钮(用户和密码由你在 Visualization Manager 中添加)。
176
+
177
+ ![同一个泵站画面,作为原生 CODESYS 可视化在仿真中运行](docs/images/hmi-codesys.png)
178
+
179
+ 报警是 CODESYS 自己的报警,而不是 HMI 中的副本:导出会创建报警配置(Alarm Configuration)中的
180
+ 报警组,包括观察条件、报警类别和报警消息,由 CODESYS 的报警管理器在 PLC 中计算。在仿真中触发
181
+ 故障后,报警组报告一个活动报警;故障消失后它仍保持待确认状态,直到被确认,这正是 Error 类报警
182
+ 应有的行为。
183
+
184
+ ![导出的报警在 CODESYS 报警组编辑器中:PLC_PRG.xHighLevel 的数字观察,类别 Warning,消息 "High level T-101 (above 90 %)";PLC_PRG.xFault 的数字观察,类别 Error,消息 "Pump P-101 fault"](docs/images/codesys-alarms.png)
185
+
186
+ CODESYS 的脚本无法创建趋势、仪表或报警表元素,所以趋势显示当前值,仪表显示为棒图,报警页在
187
+ 报警活动时显示一行(可在 CODESYS 自带的报警表中确认报警,报警表可从工具箱添加)。已在 CODESYS
188
+ V3.5 SP21 的仿真中验证。可视化需要 CODESYS 库存储库中有 `FloatingPointUtils` 库;如果缺少,导出会提示。
189
+
190
+ ## 可用操作
191
+
192
+ 所有操作都通过一个 MCP 工具 `codesys` 完成,每个操作对应一个 action。
193
+ [参考文档](docs/reference.md)(英文)详细说明了每一个操作。
194
+
195
+ | 类别 | 操作 |
196
+ | --- | --- |
197
+ | **项目** | `open_project` `create_project` `save_project` `close_project` `codesys_status` |
198
+ | **读取代码** | `get_project_tree` `read_objects` `search_code` `get_outline` |
199
+ | **修改代码** | `write_object` `create_object` `create_graphical` `delete_object` `rename_object` |
200
+ | **编译** | `compile` |
201
+ | **库** | `list_libraries` `add_library` `remove_library` `get_library_api` `get_signature` |
202
+ | **代码导航** | `find_references` `rename_symbol` |
203
+ | **任务与 I/O** | `list_tasks` `configure_task` `get_io_list` `get_devices` `find_device` `add_device` |
204
+ | **改动跟踪** | `checkpoint` `get_changes` `revert_object` |
205
+ | **仿真** | `codesys_sim_start` `codesys_sim_stop` `codesys_sim_status` `codesys_sim_control` `codesys_sim_cycle` `codesys_sim_read` `codesys_sim_write` `codesys_sim_force` `codesys_sim_unforce` `codesys_sim_test` |
206
+ | **跟踪(Trace)** | `trace_setup` `trace_record` |
207
+ | **调试** | `breakpoint_set` `breakpoint_clear` `debug_status` `debug_step` `debug_continue` |
208
+ | **HMI 画面** | `hmi_save_screen` `hmi_template` `hmi_screenshot` `hmi_export_codesys` `hmi_list` `hmi_delete_screen` `open_hmi` |
209
+ | **辅助** | `batch` `wait` `help` `open_workspace` |
210
+
211
+ `batch` 可以在一次调用中执行最多 50 个操作,比逐个调用快得多。
212
+
213
+ 有些操作超出了 CODESYS 脚本 API 文档的范围,使用 IDE 内部的 CODESYS 服务:`get_signature`、
214
+ `find_references`、`rename_symbol`、`trace_setup`、`trace_record`、调试操作、`hmi_export_codesys`
215
+ (包括报警和用户组)以及 `--show-window`。它们属于**实验性功能**:已在 CODESYS V3.5 SP21 和 SP22 上
216
+ 验证(见[更新日志](CHANGELOG.md)),在其他版本上可能提示功能不可用,或无法正常工作。
217
+
218
+ Claude 编写结构化文本,并可用 `create_graphical` 根据梯级、网络或步骤的简单描述创建梯形图(LD)、
219
+ FBD 和 SFC 程序及功能块,由 CODESYS 绘制。已有的图形化代码可以以 PLCopenXML 形式读取,但还不能
220
+ 就地编辑。不支持 CFC。
221
+
222
+ ## 安全
223
+
224
+ - **不连接真实控制器。** 工具包不会登录或下载到控制器,只会连接 CODESYS 自带的仿真,
225
+ 每次在线操作前都会检查仿真模式是否开启。
226
+ - **一次授权涵盖所有操作,** 包括修改或删除项目代码的操作。请在副本上工作,或使用版本控制。
227
+ - **改动可以撤销。** 打开项目时会自动创建检查点,`revert_object` 可以把对象(包括其方法和
228
+ 属性)恢复到检查点时的状态。
229
+ - **自动保存。** 默认每次改动后都会保存项目;使用 `--no-autosave` 只在需要时保存。
230
+ - **仿真不会改动项目。** 仿真运行时禁止编辑,设备上的仿真设置也不会被保存。
231
+
232
+ ## 更多内容(英文)
233
+
234
+ - [全部启动参数](README.md#configuration)
235
+ - [故障排除](README.md#troubleshooting)
236
+ - [工作原理](README.md#how-it-works)
237
+ - [开发与测试](README.md#development)
238
+ - [参与贡献](CONTRIBUTING.md)
239
+
240
+ 欢迎提交 Issue 和 Pull Request,中文或英文均可。
241
+
242
+ ## 致谢
243
+
244
+ 感谢 Zhen Yang(Alfabete-spec)审阅本中文文档。
245
+
246
+ ## 许可证
247
+
248
+ [MIT](LICENSE)
249
+
250
+ CODESYS 是 CODESYS GmbH 的商标。本项目为独立项目,与 CODESYS GmbH 无关联,也未获其认可。
251
+
252
+ ---
253
+
254
+ 本文档对应 2026 年 11 月 14 日的英文 README。如有出入,以[英文版](README.md)为准。
package/dist/actions.d.ts CHANGED
@@ -8,11 +8,16 @@
8
8
  import type { McpServer } from "@modelcontextprotocol/server";
9
9
  import * as z from "zod/v4";
10
10
  export declare const TOOL_NAME = "codesys";
11
+ export type ActionContent = {
12
+ type: "text";
13
+ text: string;
14
+ } | {
15
+ type: "image";
16
+ data: string;
17
+ mimeType: string;
18
+ };
11
19
  export type ActionResult = {
12
- content: {
13
- type: "text";
14
- text: string;
15
- }[];
20
+ content: ActionContent[];
16
21
  isError?: boolean;
17
22
  };
18
23
  export interface ActionConfig<S extends z.ZodObject = z.ZodObject> {