@codesys/mcp-toolkit 1.1.3 → 1.1.4
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.
- package/dist/scriptLoader.js +158 -0
- package/dist/server.js +250 -1037
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*
|
|
7
7
|
* IMPORTANT: This server receives configuration as parameters from bin.ts,
|
|
8
8
|
* which helps avoid issues with command-line argument passing in different execution environments.
|
|
9
|
-
* (Incorporates script templates from v1.6.9 and improved tool descriptions)
|
|
10
9
|
*/
|
|
11
10
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
12
11
|
if (k2 === undefined) k2 = k;
|
|
@@ -64,9 +63,10 @@ console.error(`>>> Initial CWD: ${process.cwd()}`);
|
|
|
64
63
|
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
|
|
65
64
|
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
|
|
66
65
|
const zod_1 = require("zod");
|
|
67
|
-
const codesys_interop_1 = require("./codesys_interop");
|
|
66
|
+
const codesys_interop_1 = require("./codesys_interop");
|
|
68
67
|
const path = __importStar(require("path"));
|
|
69
|
-
const promises_1 = require("fs/promises");
|
|
68
|
+
const promises_1 = require("fs/promises");
|
|
69
|
+
const scriptLoader_1 = require("./scriptLoader");
|
|
70
70
|
// --- Wrap server logic in an exported function ---
|
|
71
71
|
function startMcpServer(config) {
|
|
72
72
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -80,16 +80,13 @@ function startMcpServer(config) {
|
|
|
80
80
|
console.error(`SERVER.TS: Using CODESYS Path: ${codesysExePath}`);
|
|
81
81
|
console.error(`SERVER.TS: Using CODESYS Profile: ${codesysProfileName}`);
|
|
82
82
|
// --- Sanity check - confirm the path exists if possible ---
|
|
83
|
-
// This helps catch configuration issues early and prevents runtime failures
|
|
84
83
|
console.error(`SERVER.TS: Checking existence of CODESYS executable: ${codesysExePath}`);
|
|
85
84
|
try {
|
|
86
85
|
// Using sync check here as it's part of initial setup before async operations start
|
|
87
86
|
const fsChecker = require('fs');
|
|
88
87
|
if (!fsChecker.existsSync(codesysExePath)) {
|
|
89
88
|
console.error(`SERVER.TS ERROR: Determined CODESYS executable path does not exist: ${codesysExePath}`);
|
|
90
|
-
// Consider throwing an error instead of exiting if bin.ts handles the catch
|
|
91
89
|
throw new Error(`CODESYS executable not found at specified path: ${codesysExePath}`);
|
|
92
|
-
// process.exit(1); // Avoid process.exit inside library functions if possible
|
|
93
90
|
}
|
|
94
91
|
else {
|
|
95
92
|
console.error(`SERVER.TS: Confirmed CODESYS executable exists.`);
|
|
@@ -98,7 +95,6 @@ function startMcpServer(config) {
|
|
|
98
95
|
catch (err) {
|
|
99
96
|
console.error(`SERVER.TS ERROR: Error checking CODESYS path existence: ${err.message}`);
|
|
100
97
|
throw err; // Rethrow the error to be caught by the caller (bin.ts)
|
|
101
|
-
// process.exit(1);
|
|
102
98
|
}
|
|
103
99
|
// --- End Configuration Handling ---
|
|
104
100
|
// --- Helper Function (fileExists - async version) ---
|
|
@@ -117,914 +113,27 @@ function startMcpServer(config) {
|
|
|
117
113
|
});
|
|
118
114
|
}
|
|
119
115
|
// --- End Helper Function ---
|
|
116
|
+
// --- Preload script templates ---
|
|
117
|
+
try {
|
|
118
|
+
yield (0, scriptLoader_1.preloadAllScriptTemplates)();
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
console.error(`SERVER.TS ERROR: Error preloading script templates: ${err.message}`);
|
|
122
|
+
// Continue execution, as script templates will be loaded on demand as needed
|
|
123
|
+
}
|
|
120
124
|
// --- MCP Server Initialization ---
|
|
121
125
|
console.error("SERVER.TS: Initializing McpServer...");
|
|
122
126
|
const server = new mcp_js_1.McpServer({
|
|
123
127
|
name: "CODESYS Control MCP Server",
|
|
124
|
-
version: "1.
|
|
128
|
+
version: "1.8.0", // Updated version for refactored code
|
|
125
129
|
capabilities: {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
tools: { listChanged: true } // Assuming you might want to notify if tools change dynamically (less common)
|
|
130
|
+
resources: { listChanged: true },
|
|
131
|
+
tools: { listChanged: true }
|
|
129
132
|
}
|
|
130
133
|
});
|
|
131
134
|
console.error("SERVER.TS: McpServer instance created.");
|
|
132
135
|
// --- End MCP Server Initialization ---
|
|
133
|
-
// ---
|
|
134
|
-
const ENSURE_PROJECT_OPEN_PYTHON_SNIPPET = `
|
|
135
|
-
import sys
|
|
136
|
-
import scriptengine as script_engine
|
|
137
|
-
import os
|
|
138
|
-
import time
|
|
139
|
-
import traceback
|
|
140
|
-
|
|
141
|
-
# --- Function to ensure the correct project is open ---
|
|
142
|
-
MAX_RETRIES = 3
|
|
143
|
-
RETRY_DELAY = 2.0 # seconds (use float for time.sleep)
|
|
144
|
-
|
|
145
|
-
def ensure_project_open(target_project_path):
|
|
146
|
-
print("DEBUG: Ensuring project is open: %s" % target_project_path)
|
|
147
|
-
# Normalize target path once
|
|
148
|
-
normalized_target_path = os.path.normcase(os.path.abspath(target_project_path))
|
|
149
|
-
|
|
150
|
-
for attempt in range(MAX_RETRIES):
|
|
151
|
-
print("DEBUG: Ensure project attempt %d/%d for %s" % (attempt + 1, MAX_RETRIES, normalized_target_path))
|
|
152
|
-
primary_project = None
|
|
153
|
-
try:
|
|
154
|
-
# Getting primary project might fail if CODESYS instance is unstable
|
|
155
|
-
primary_project = script_engine.projects.primary
|
|
156
|
-
except Exception as primary_err:
|
|
157
|
-
print("WARN: Error getting primary project: %s. Assuming none." % primary_err)
|
|
158
|
-
# traceback.print_exc() # Optional: Print stack trace for this error
|
|
159
|
-
primary_project = None
|
|
160
|
-
|
|
161
|
-
current_project_path = ""
|
|
162
|
-
project_ok = False # Flag to check if target is confirmed primary and accessible
|
|
163
|
-
|
|
164
|
-
if primary_project:
|
|
165
|
-
try:
|
|
166
|
-
# Getting path should be relatively safe if primary_project object exists
|
|
167
|
-
current_project_path = os.path.normcase(os.path.abspath(primary_project.path))
|
|
168
|
-
print("DEBUG: Current primary project path: %s" % current_project_path)
|
|
169
|
-
if current_project_path == normalized_target_path:
|
|
170
|
-
# Found the right project as primary, now check if it's usable
|
|
171
|
-
print("DEBUG: Target project path matches primary. Checking access...")
|
|
172
|
-
try:
|
|
173
|
-
# Try a relatively safe operation to confirm object usability
|
|
174
|
-
# Getting children count is a reasonable check
|
|
175
|
-
_ = len(primary_project.get_children(False))
|
|
176
|
-
print("DEBUG: Target project '%s' is primary and accessible." % target_project_path)
|
|
177
|
-
project_ok = True
|
|
178
|
-
return primary_project # SUCCESS CASE 1: Already open and accessible
|
|
179
|
-
except Exception as access_err:
|
|
180
|
-
# Project found, but accessing it failed. Might be unstable.
|
|
181
|
-
print("WARN: Primary project access check failed for '%s': %s. Will attempt reopen." % (current_project_path, access_err))
|
|
182
|
-
# traceback.print_exc() # Optional: Print stack trace
|
|
183
|
-
primary_project = None # Force reopen by falling through
|
|
184
|
-
else:
|
|
185
|
-
# A *different* project is primary
|
|
186
|
-
print("DEBUG: Primary project is '%s', not the target '%s'." % (current_project_path, normalized_target_path))
|
|
187
|
-
# Consider closing the wrong project if causing issues, but for now, just open target
|
|
188
|
-
# try:
|
|
189
|
-
# print("DEBUG: Closing incorrect primary project '%s'..." % current_project_path)
|
|
190
|
-
# primary_project.close() # Be careful with unsaved changes
|
|
191
|
-
# except Exception as close_err:
|
|
192
|
-
# print("WARN: Failed to close incorrect primary project: %s" % close_err)
|
|
193
|
-
primary_project = None # Force open target project
|
|
194
|
-
|
|
195
|
-
except Exception as path_err:
|
|
196
|
-
# Failed even to get the path of the supposed primary project
|
|
197
|
-
print("WARN: Could not get path of current primary project: %s. Assuming not the target." % path_err)
|
|
198
|
-
# traceback.print_exc() # Optional: Print stack trace
|
|
199
|
-
primary_project = None # Force open target project
|
|
200
|
-
|
|
201
|
-
# If target project not confirmed as primary and accessible, attempt to open/reopen
|
|
202
|
-
if not project_ok:
|
|
203
|
-
# Log clearly whether we are opening initially or reopening
|
|
204
|
-
if primary_project is None and current_project_path == "":
|
|
205
|
-
print("DEBUG: No primary project detected. Attempting to open target: %s" % target_project_path)
|
|
206
|
-
elif primary_project is None and current_project_path != "":
|
|
207
|
-
print("DEBUG: Primary project was '%s' but failed access check or needed close. Attempting to open target: %s" % (current_project_path, target_project_path))
|
|
208
|
-
else: # Includes cases where wrong project was open
|
|
209
|
-
print("DEBUG: Target project not primary or initial check failed. Attempting to open/reopen: %s" % target_project_path)
|
|
210
|
-
|
|
211
|
-
try:
|
|
212
|
-
# Set flags for silent opening, handle potential attribute errors
|
|
213
|
-
update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
214
|
-
# try:
|
|
215
|
-
# update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
216
|
-
# except AttributeError:
|
|
217
|
-
# print("WARN: VersionUpdateFlags not found, using integer flags for open (1 | 2 = 3).")
|
|
218
|
-
# update_mode = 3 # 1=NoUpdates, 2=SilentMode
|
|
219
|
-
|
|
220
|
-
opened_project = None
|
|
221
|
-
try:
|
|
222
|
-
# The actual open call
|
|
223
|
-
print("DEBUG: Calling script_engine.projects.open('%s', update_flags=%s)..." % (target_project_path, update_mode))
|
|
224
|
-
opened_project = script_engine.projects.open(target_project_path, update_flags=update_mode)
|
|
225
|
-
|
|
226
|
-
if not opened_project:
|
|
227
|
-
# This is a critical failure if open returns None without exception
|
|
228
|
-
print("ERROR: projects.open returned None for %s on attempt %d" % (target_project_path, attempt + 1))
|
|
229
|
-
# Allow retry loop to continue
|
|
230
|
-
else:
|
|
231
|
-
# Open call returned *something*, let's verify
|
|
232
|
-
print("DEBUG: projects.open call returned an object for: %s" % target_project_path)
|
|
233
|
-
print("DEBUG: Pausing for stabilization after open...")
|
|
234
|
-
time.sleep(RETRY_DELAY) # Give CODESYS time
|
|
235
|
-
# Re-verify: Is the project now primary and accessible?
|
|
236
|
-
recheck_primary = None
|
|
237
|
-
try: recheck_primary = script_engine.projects.primary
|
|
238
|
-
except Exception as recheck_primary_err: print("WARN: Error getting primary project after reopen: %s" % recheck_primary_err)
|
|
239
|
-
|
|
240
|
-
if recheck_primary:
|
|
241
|
-
recheck_path = ""
|
|
242
|
-
try: # Getting path might fail
|
|
243
|
-
recheck_path = os.path.normcase(os.path.abspath(recheck_primary.path))
|
|
244
|
-
except Exception as recheck_path_err:
|
|
245
|
-
print("WARN: Failed to get path after reopen: %s" % recheck_path_err)
|
|
246
|
-
|
|
247
|
-
if recheck_path == normalized_target_path:
|
|
248
|
-
print("DEBUG: Target project confirmed as primary after reopening.")
|
|
249
|
-
try: # Final sanity check
|
|
250
|
-
_ = len(recheck_primary.get_children(False))
|
|
251
|
-
print("DEBUG: Reopened project basic access confirmed.")
|
|
252
|
-
return recheck_primary # SUCCESS CASE 2: Successfully opened/reopened
|
|
253
|
-
except Exception as access_err_reopen:
|
|
254
|
-
print("WARN: Reopened project (%s) basic access check failed: %s." % (normalized_target_path, access_err_reopen))
|
|
255
|
-
# traceback.print_exc() # Optional
|
|
256
|
-
# Allow retry loop to continue
|
|
257
|
-
else:
|
|
258
|
-
print("WARN: Different project is primary after reopening! Expected '%s', got '%s'." % (normalized_target_path, recheck_path))
|
|
259
|
-
# Allow retry loop to continue, maybe it fixes itself
|
|
260
|
-
else:
|
|
261
|
-
print("WARN: No primary project found after reopening attempt %d!" % (attempt+1))
|
|
262
|
-
# Allow retry loop to continue
|
|
263
|
-
|
|
264
|
-
except Exception as open_err:
|
|
265
|
-
# Catch errors during the open call itself
|
|
266
|
-
print("ERROR: Exception during projects.open call on attempt %d: %s" % (attempt + 1, open_err))
|
|
267
|
-
traceback.print_exc() # Crucial for diagnosing open failures
|
|
268
|
-
# Allow retry loop to continue
|
|
269
|
-
|
|
270
|
-
except Exception as outer_open_err:
|
|
271
|
-
# Catch errors in the flag setup etc.
|
|
272
|
-
print("ERROR: Unexpected error during open setup/logic attempt %d: %s" % (attempt + 1, outer_open_err))
|
|
273
|
-
traceback.print_exc()
|
|
274
|
-
|
|
275
|
-
# If we didn't return successfully in this attempt, wait before retrying
|
|
276
|
-
if attempt < MAX_RETRIES - 1:
|
|
277
|
-
print("DEBUG: Ensure project attempt %d did not succeed. Waiting %f seconds..." % (attempt + 1, RETRY_DELAY))
|
|
278
|
-
time.sleep(RETRY_DELAY)
|
|
279
|
-
else: # Last attempt failed
|
|
280
|
-
print("ERROR: Failed all ensure_project_open attempts for %s." % normalized_target_path)
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
# If all retries fail after the loop
|
|
284
|
-
raise RuntimeError("Failed to ensure project '%s' is open and accessible after %d attempts." % (target_project_path, MAX_RETRIES))
|
|
285
|
-
# --- End of function ---
|
|
286
|
-
|
|
287
|
-
# Placeholder for the project file path (must be set in scripts using this snippet)
|
|
288
|
-
PROJECT_FILE_PATH = r"{PROJECT_FILE_PATH}"
|
|
289
|
-
`;
|
|
290
|
-
const CHECK_STATUS_SCRIPT = `
|
|
291
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
292
|
-
project_open = False; project_name = "No project open"; project_path = "N/A"; scripting_ok = False
|
|
293
|
-
try:
|
|
294
|
-
scripting_ok = True; primary_project = script_engine.projects.primary
|
|
295
|
-
if primary_project:
|
|
296
|
-
project_open = True
|
|
297
|
-
try:
|
|
298
|
-
project_path = os.path.normcase(os.path.abspath(primary_project.path))
|
|
299
|
-
try:
|
|
300
|
-
project_name = primary_project.get_name() # Might fail
|
|
301
|
-
if not project_name: project_name = "Unnamed (path: %s)" % os.path.basename(project_path)
|
|
302
|
-
except: project_name = "Unnamed (path: %s)" % os.path.basename(project_path)
|
|
303
|
-
except Exception as e_path: project_path = "N/A (Error: %s)" % e_path; project_name = "Unnamed (Path Error)"
|
|
304
|
-
print("Project Open: %s" % project_open); print("Project Name: %s" % project_name)
|
|
305
|
-
print("Project Path: %s" % project_path); print("Scripting OK: %s" % scripting_ok)
|
|
306
|
-
print("SCRIPT_SUCCESS: Status check complete."); sys.exit(0)
|
|
307
|
-
except Exception as e:
|
|
308
|
-
error_message = "Error during status check: %s" % e
|
|
309
|
-
print(error_message); print("Scripting OK: False")
|
|
310
|
-
# traceback.print_exc() # Optional traceback
|
|
311
|
-
print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
312
|
-
`;
|
|
313
|
-
const OPEN_PROJECT_SCRIPT_TEMPLATE = `
|
|
314
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
315
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
316
|
-
try:
|
|
317
|
-
project = ensure_project_open(PROJECT_FILE_PATH)
|
|
318
|
-
# Get name from object if possible, otherwise use path basename
|
|
319
|
-
proj_name = "Unknown"
|
|
320
|
-
try:
|
|
321
|
-
if project: proj_name = project.get_name() or os.path.basename(PROJECT_FILE_PATH)
|
|
322
|
-
else: proj_name = os.path.basename(PROJECT_FILE_PATH) + " (ensure_project_open returned None?)"
|
|
323
|
-
except Exception:
|
|
324
|
-
proj_name = os.path.basename(PROJECT_FILE_PATH) + " (name retrieval failed)"
|
|
325
|
-
print("Project Opened: %s" % proj_name)
|
|
326
|
-
print("SCRIPT_SUCCESS: Project opened successfully.")
|
|
327
|
-
sys.exit(0)
|
|
328
|
-
except Exception as e:
|
|
329
|
-
error_message = "Error opening project %s: %s" % (PROJECT_FILE_PATH, e)
|
|
330
|
-
print(error_message)
|
|
331
|
-
traceback.print_exc()
|
|
332
|
-
print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
333
|
-
`;
|
|
334
|
-
const CREATE_PROJECT_SCRIPT_TEMPLATE = `
|
|
335
|
-
import sys, scriptengine as script_engine, os, shutil, time, traceback
|
|
336
|
-
# Placeholders
|
|
337
|
-
TEMPLATE_PROJECT_PATH = r'{TEMPLATE_PROJECT_PATH}' # Path to Standard.project
|
|
338
|
-
PROJECT_FILE_PATH = r'{PROJECT_FILE_PATH}' # Path for the new project (Target Path)
|
|
339
|
-
try:
|
|
340
|
-
print("DEBUG: Python script create_project (copy from template):")
|
|
341
|
-
print("DEBUG: Template Source = %s" % TEMPLATE_PROJECT_PATH)
|
|
342
|
-
print("DEBUG: Target Path = %s" % PROJECT_FILE_PATH)
|
|
343
|
-
if not PROJECT_FILE_PATH: raise ValueError("Target project file path empty.")
|
|
344
|
-
if not TEMPLATE_PROJECT_PATH: raise ValueError("Template project file path empty.")
|
|
345
|
-
if not os.path.exists(TEMPLATE_PROJECT_PATH): raise IOError("Template project file not found: %s" % TEMPLATE_PROJECT_PATH)
|
|
346
|
-
|
|
347
|
-
# 1. Copy the template project file to the new location
|
|
348
|
-
target_dir = os.path.dirname(PROJECT_FILE_PATH)
|
|
349
|
-
if not os.path.exists(target_dir): print("DEBUG: Creating target directory: %s" % target_dir); os.makedirs(target_dir)
|
|
350
|
-
# Check if target file already exists
|
|
351
|
-
if os.path.exists(PROJECT_FILE_PATH): print("WARN: Target project file already exists, overwriting: %s" % PROJECT_FILE_PATH)
|
|
352
|
-
|
|
353
|
-
print("DEBUG: Copying '%s' to '%s'..." % (TEMPLATE_PROJECT_PATH, PROJECT_FILE_PATH))
|
|
354
|
-
shutil.copy2(TEMPLATE_PROJECT_PATH, PROJECT_FILE_PATH) # copy2 preserves metadata
|
|
355
|
-
print("DEBUG: File copy complete.")
|
|
356
|
-
|
|
357
|
-
# 2. Open the newly copied project file
|
|
358
|
-
print("DEBUG: Opening the copied project: %s" % PROJECT_FILE_PATH)
|
|
359
|
-
# Set flags for silent opening
|
|
360
|
-
update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
361
|
-
# try:
|
|
362
|
-
# update_mode = script_engine.VersionUpdateFlags.NoUpdates | script_engine.VersionUpdateFlags.SilentMode
|
|
363
|
-
# except AttributeError:
|
|
364
|
-
# print("WARN: VersionUpdateFlags not found, using integer flags for open (1 | 2 = 3).")
|
|
365
|
-
# update_mode = 3
|
|
366
|
-
|
|
367
|
-
project = script_engine.projects.open(PROJECT_FILE_PATH, update_flags=update_mode)
|
|
368
|
-
print("DEBUG: script_engine.projects.open returned: %s" % project)
|
|
369
|
-
if project:
|
|
370
|
-
print("DEBUG: Pausing briefly after open...")
|
|
371
|
-
time.sleep(1.0) # Allow CODESYS to potentially initialize things
|
|
372
|
-
try:
|
|
373
|
-
print("DEBUG: Explicitly saving project after opening copy...")
|
|
374
|
-
project.save();
|
|
375
|
-
print("DEBUG: Project save after opening copy succeeded.")
|
|
376
|
-
except Exception as save_err:
|
|
377
|
-
print("WARN: Explicit save after opening copy failed: %s" % save_err)
|
|
378
|
-
# Decide if this is critical - maybe not, but good to know.
|
|
379
|
-
print("Project Created from Template Copy at: %s" % PROJECT_FILE_PATH)
|
|
380
|
-
print("SCRIPT_SUCCESS: Project copied from template and opened successfully.")
|
|
381
|
-
sys.exit(0)
|
|
382
|
-
else:
|
|
383
|
-
error_message = "Failed to open project copy %s after copying template %s. projects.open returned None." % (PROJECT_FILE_PATH, TEMPLATE_PROJECT_PATH)
|
|
384
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
385
|
-
except Exception as e:
|
|
386
|
-
detailed_error = traceback.format_exc()
|
|
387
|
-
error_message = "Error creating project '%s' from template '%s': %s\\n%s" % (PROJECT_FILE_PATH, TEMPLATE_PROJECT_PATH, e, detailed_error)
|
|
388
|
-
print(error_message); print("SCRIPT_ERROR: Error copying/opening template: %s" % e); sys.exit(1)
|
|
389
|
-
`;
|
|
390
|
-
const SAVE_PROJECT_SCRIPT_TEMPLATE = `
|
|
391
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
392
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
393
|
-
try:
|
|
394
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
395
|
-
# Get name from object if possible, otherwise use path basename
|
|
396
|
-
project_name = "Unknown"
|
|
397
|
-
try:
|
|
398
|
-
if primary_project: project_name = primary_project.get_name() or os.path.basename(PROJECT_FILE_PATH)
|
|
399
|
-
else: project_name = os.path.basename(PROJECT_FILE_PATH) + " (ensure_project_open returned None?)"
|
|
400
|
-
except Exception:
|
|
401
|
-
project_name = os.path.basename(PROJECT_FILE_PATH) + " (name retrieval failed)"
|
|
402
|
-
|
|
403
|
-
print("DEBUG: Saving project: %s (%s)" % (project_name, PROJECT_FILE_PATH))
|
|
404
|
-
primary_project.save()
|
|
405
|
-
print("DEBUG: project.save() executed.")
|
|
406
|
-
print("Project Saved: %s" % project_name)
|
|
407
|
-
print("SCRIPT_SUCCESS: Project saved successfully.")
|
|
408
|
-
sys.exit(0)
|
|
409
|
-
except Exception as e:
|
|
410
|
-
error_message = "Error saving project %s: %s" % (PROJECT_FILE_PATH, e)
|
|
411
|
-
print(error_message)
|
|
412
|
-
traceback.print_exc()
|
|
413
|
-
print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
414
|
-
`;
|
|
415
|
-
const FIND_OBJECT_BY_PATH_PYTHON_SNIPPET = `
|
|
416
|
-
import traceback
|
|
417
|
-
# --- Find object by path function ---
|
|
418
|
-
def find_object_by_path_robust(start_node, full_path, target_type_name="object"):
|
|
419
|
-
print("DEBUG: Finding %s by path: '%s'" % (target_type_name, full_path))
|
|
420
|
-
normalized_path = full_path.replace('\\\\', '/').strip('/')
|
|
421
|
-
path_parts = normalized_path.split('/')
|
|
422
|
-
if not path_parts:
|
|
423
|
-
print("ERROR: Path is empty.")
|
|
424
|
-
return None
|
|
425
|
-
|
|
426
|
-
# Determine the actual starting node (project or application)
|
|
427
|
-
project = start_node # Assume start_node is project initially
|
|
428
|
-
if not hasattr(start_node, 'active_application') and hasattr(start_node, 'project'):
|
|
429
|
-
# If start_node is not project but has project ref (e.g., an application), get the project
|
|
430
|
-
try: project = start_node.project
|
|
431
|
-
except Exception as proj_ref_err:
|
|
432
|
-
print("WARN: Could not get project reference from start_node: %s" % proj_ref_err)
|
|
433
|
-
# Proceed assuming start_node might be the project anyway or search fails
|
|
434
|
-
|
|
435
|
-
# Try to get the application object robustly if we think we have the project
|
|
436
|
-
app = None
|
|
437
|
-
if hasattr(project, 'active_application'):
|
|
438
|
-
try: app = project.active_application
|
|
439
|
-
except Exception: pass # Ignore errors getting active app
|
|
440
|
-
if not app:
|
|
441
|
-
try:
|
|
442
|
-
apps = project.find("Application", True) # Search recursively
|
|
443
|
-
if apps: app = apps[0]
|
|
444
|
-
except Exception: pass
|
|
445
|
-
|
|
446
|
-
# Check if the first path part matches the application name
|
|
447
|
-
app_name_lower = ""
|
|
448
|
-
if app:
|
|
449
|
-
try: app_name_lower = (app.get_name() or "application").lower()
|
|
450
|
-
except Exception: app_name_lower = "application" # Fallback
|
|
451
|
-
|
|
452
|
-
# Decide where to start the traversal
|
|
453
|
-
current_obj = start_node # Default to the node passed in
|
|
454
|
-
if hasattr(project, 'active_application'): # Only adjust if start_node was likely the project
|
|
455
|
-
if app and path_parts[0].lower() == app_name_lower:
|
|
456
|
-
print("DEBUG: Path starts with Application name '%s'. Beginning search there." % path_parts[0])
|
|
457
|
-
current_obj = app
|
|
458
|
-
path_parts = path_parts[1:] # Consume the app name part
|
|
459
|
-
# If path was *only* the application name
|
|
460
|
-
if not path_parts:
|
|
461
|
-
print("DEBUG: Target path is the Application object itself.")
|
|
462
|
-
return current_obj
|
|
463
|
-
else:
|
|
464
|
-
print("DEBUG: Path does not start with Application name. Starting search from project root.")
|
|
465
|
-
current_obj = project # Start search from the project root
|
|
466
|
-
else:
|
|
467
|
-
print("DEBUG: Starting search from originally provided node.")
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
# Traverse the remaining path parts
|
|
471
|
-
parent_path_str = getattr(current_obj, 'get_name', lambda: str(current_obj))() # Safer name getting
|
|
472
|
-
|
|
473
|
-
for i, part_name in enumerate(path_parts):
|
|
474
|
-
is_last_part = (i == len(path_parts) - 1)
|
|
475
|
-
print("DEBUG: Searching for part [%d/%d]: '%s' under '%s'" % (i+1, len(path_parts), part_name, parent_path_str))
|
|
476
|
-
found_in_parent = None
|
|
477
|
-
try:
|
|
478
|
-
# Prioritize non-recursive find for direct children
|
|
479
|
-
children_of_current = current_obj.get_children(False)
|
|
480
|
-
print("DEBUG: Found %d direct children under '%s'." % (len(children_of_current), parent_path_str))
|
|
481
|
-
for child in children_of_current:
|
|
482
|
-
child_name = getattr(child, 'get_name', lambda: None)() # Safer name getting
|
|
483
|
-
# print("DEBUG: Checking child: '%s'" % child_name) # Verbose
|
|
484
|
-
if child_name == part_name:
|
|
485
|
-
found_in_parent = child
|
|
486
|
-
print("DEBUG: Found direct child matching '%s'." % part_name)
|
|
487
|
-
break # Found direct child, stop searching children
|
|
488
|
-
|
|
489
|
-
# If not found directly, AND it's the last part, try recursive find from current parent
|
|
490
|
-
if not found_in_parent and is_last_part:
|
|
491
|
-
print("DEBUG: Direct find failed for last part '%s'. Trying recursive find under '%s'." % (part_name, parent_path_str))
|
|
492
|
-
found_recursive_list = current_obj.find(part_name, True) # Recursive find
|
|
493
|
-
if found_recursive_list:
|
|
494
|
-
# Maybe add a check here if multiple are found?
|
|
495
|
-
found_in_parent = found_recursive_list[0] # Take the first match
|
|
496
|
-
print("DEBUG: Found last part '%s' recursively." % part_name)
|
|
497
|
-
else:
|
|
498
|
-
print("DEBUG: Recursive find also failed for last part '%s'." % part_name)
|
|
499
|
-
|
|
500
|
-
# Update current object if found
|
|
501
|
-
if found_in_parent:
|
|
502
|
-
current_obj = found_in_parent
|
|
503
|
-
parent_path_str = getattr(current_obj, 'get_name', lambda: part_name)() # Safer name getting
|
|
504
|
-
print("DEBUG: Stepped into '%s'." % parent_path_str)
|
|
505
|
-
else:
|
|
506
|
-
# If not found at any point, the path is invalid from this parent
|
|
507
|
-
print("ERROR: Path part '%s' not found under '%s'." % (part_name, parent_path_str))
|
|
508
|
-
return None # Path broken
|
|
509
|
-
|
|
510
|
-
except Exception as find_err:
|
|
511
|
-
print("ERROR: Exception while searching for '%s' under '%s': %s" % (part_name, parent_path_str, find_err))
|
|
512
|
-
traceback.print_exc()
|
|
513
|
-
return None # Error during search
|
|
514
|
-
|
|
515
|
-
# Final verification (optional but recommended): Check if the found object's name matches the last part
|
|
516
|
-
final_expected_name = full_path.split('/')[-1]
|
|
517
|
-
found_final_name = getattr(current_obj, 'get_name', lambda: None)() # Safer name getting
|
|
518
|
-
|
|
519
|
-
if found_final_name == final_expected_name:
|
|
520
|
-
print("DEBUG: Final %s found and name verified for path '%s': %s" % (target_type_name, full_path, found_final_name))
|
|
521
|
-
return current_obj
|
|
522
|
-
else:
|
|
523
|
-
print("ERROR: Traversal ended on object '%s' but expected final name was '%s'." % (found_final_name, final_expected_name))
|
|
524
|
-
return None # Name mismatch implies target not found as expected
|
|
525
|
-
|
|
526
|
-
# --- End of find object function ---
|
|
527
|
-
`;
|
|
528
|
-
const CREATE_POU_SCRIPT_TEMPLATE = `
|
|
529
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
530
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
531
|
-
${FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}
|
|
532
|
-
POU_NAME = "{POU_NAME}"; POU_TYPE_STR = "{POU_TYPE_STR}"; IMPL_LANGUAGE_STR = "{IMPL_LANGUAGE_STR}"; PARENT_PATH_REL = "{PARENT_PATH}"
|
|
533
|
-
pou_type_map = { "Program": script_engine.PouType.Program, "FunctionBlock": script_engine.PouType.FunctionBlock, "Function": script_engine.PouType.Function }
|
|
534
|
-
# Map common language names to ImplementationLanguages attributes if needed (optional, None usually works)
|
|
535
|
-
# lang_map = { "ST": script_engine.ImplementationLanguage.st, ... }
|
|
536
|
-
|
|
537
|
-
try:
|
|
538
|
-
print("DEBUG: create_pou script: Name='%s', Type='%s', Lang='%s', ParentPath='%s', Project='%s'" % (POU_NAME, POU_TYPE_STR, IMPL_LANGUAGE_STR, PARENT_PATH_REL, PROJECT_FILE_PATH))
|
|
539
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
540
|
-
if not POU_NAME: raise ValueError("POU name empty.")
|
|
541
|
-
if not PARENT_PATH_REL: raise ValueError("Parent path empty.")
|
|
542
|
-
|
|
543
|
-
# Resolve POU Type Enum
|
|
544
|
-
pou_type_enum = pou_type_map.get(POU_TYPE_STR)
|
|
545
|
-
if not pou_type_enum: raise ValueError("Invalid POU type string: %s. Use Program, FunctionBlock, or Function." % POU_TYPE_STR)
|
|
546
|
-
|
|
547
|
-
# Find parent object using the robust function
|
|
548
|
-
parent_object = find_object_by_path_robust(primary_project, PARENT_PATH_REL, "parent container")
|
|
549
|
-
if not parent_object: raise ValueError("Parent object not found for path: %s" % PARENT_PATH_REL)
|
|
550
|
-
|
|
551
|
-
parent_name = getattr(parent_object, 'get_name', lambda: str(parent_object))()
|
|
552
|
-
print("DEBUG: Using parent object: %s (Type: %s)" % (parent_name, type(parent_object).__name__))
|
|
553
|
-
|
|
554
|
-
# Check if parent object supports creating POUs (should implement ScriptIecLanguageObjectContainer)
|
|
555
|
-
if not hasattr(parent_object, 'create_pou'):
|
|
556
|
-
raise TypeError("Parent object '%s' of type %s does not support create_pou." % (parent_name, type(parent_object).__name__))
|
|
557
|
-
|
|
558
|
-
# Set language GUID to None (let CODESYS default based on parent/settings)
|
|
559
|
-
lang_guid = None
|
|
560
|
-
print("DEBUG: Setting language to None (will use default).")
|
|
561
|
-
# Example if mapping language string: lang_guid = lang_map.get(IMPL_LANGUAGE_STR, None)
|
|
562
|
-
|
|
563
|
-
print("DEBUG: Calling parent_object.create_pou: Name='%s', Type=%s, Lang=%s" % (POU_NAME, pou_type_enum, lang_guid))
|
|
564
|
-
|
|
565
|
-
# Call create_pou using keyword arguments
|
|
566
|
-
new_pou = parent_object.create_pou(
|
|
567
|
-
name=POU_NAME,
|
|
568
|
-
type=pou_type_enum,
|
|
569
|
-
language=lang_guid # Pass None
|
|
570
|
-
)
|
|
571
|
-
|
|
572
|
-
print("DEBUG: parent_object.create_pou returned: %s" % new_pou)
|
|
573
|
-
if new_pou:
|
|
574
|
-
new_pou_name = getattr(new_pou, 'get_name', lambda: POU_NAME)()
|
|
575
|
-
print("DEBUG: POU object created: %s" % new_pou_name)
|
|
576
|
-
|
|
577
|
-
# --- SAVE THE PROJECT TO PERSIST THE NEW POU ---
|
|
578
|
-
try:
|
|
579
|
-
print("DEBUG: Saving Project...")
|
|
580
|
-
primary_project.save() # Save the overall project file
|
|
581
|
-
print("DEBUG: Project saved successfully after POU creation.")
|
|
582
|
-
except Exception as save_err:
|
|
583
|
-
print("ERROR: Failed to save Project after POU creation: %s" % save_err)
|
|
584
|
-
detailed_error = traceback.format_exc()
|
|
585
|
-
error_message = "Error saving Project after creating POU '%s': %s\\n%s" % (new_pou_name, save_err, detailed_error)
|
|
586
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
587
|
-
# --- END SAVING ---
|
|
588
|
-
|
|
589
|
-
print("POU Created: %s" % new_pou_name); print("Type: %s" % POU_TYPE_STR); print("Language: %s (Defaulted)" % IMPL_LANGUAGE_STR); print("Parent Path: %s" % PARENT_PATH_REL)
|
|
590
|
-
print("SCRIPT_SUCCESS: POU created successfully."); sys.exit(0)
|
|
591
|
-
else:
|
|
592
|
-
error_message = "Failed to create POU '%s'. create_pou returned None." % POU_NAME; print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
593
|
-
except Exception as e:
|
|
594
|
-
detailed_error = traceback.format_exc()
|
|
595
|
-
error_message = "Error creating POU '%s' in project '%s': %s\\n%s" % (POU_NAME, PROJECT_FILE_PATH, e, detailed_error)
|
|
596
|
-
print(error_message); print("SCRIPT_ERROR: Error creating POU '%s': %s" % (POU_NAME, e)); sys.exit(1)
|
|
597
|
-
`;
|
|
598
|
-
const SET_POU_CODE_SCRIPT_TEMPLATE = `
|
|
599
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
600
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
601
|
-
${FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}
|
|
602
|
-
POU_FULL_PATH = "{POU_FULL_PATH}" # Expecting format like "Application/MyPOU" or "Folder/SubFolder/MyPOU"
|
|
603
|
-
DECLARATION_CONTENT = """{DECLARATION_CONTENT}"""
|
|
604
|
-
IMPLEMENTATION_CONTENT = """{IMPLEMENTATION_CONTENT}"""
|
|
605
|
-
|
|
606
|
-
try:
|
|
607
|
-
print("DEBUG: set_pou_code script: POU_FULL_PATH='%s', Project='%s'" % (POU_FULL_PATH, PROJECT_FILE_PATH))
|
|
608
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
609
|
-
if not POU_FULL_PATH: raise ValueError("POU full path empty.")
|
|
610
|
-
|
|
611
|
-
# Find the target POU/Method/Property object
|
|
612
|
-
target_object = find_object_by_path_robust(primary_project, POU_FULL_PATH, "target object")
|
|
613
|
-
if not target_object: raise ValueError("Target object not found using path: %s" % POU_FULL_PATH)
|
|
614
|
-
|
|
615
|
-
target_name = getattr(target_object, 'get_name', lambda: POU_FULL_PATH)()
|
|
616
|
-
print("DEBUG: Found target object: %s" % target_name)
|
|
617
|
-
|
|
618
|
-
# --- Set Declaration Part ---
|
|
619
|
-
declaration_updated = False
|
|
620
|
-
# Check if the content is actually provided (might be None/empty if only impl is set)
|
|
621
|
-
has_declaration_content = 'DECLARATION_CONTENT' in locals() or 'DECLARATION_CONTENT' in globals()
|
|
622
|
-
if has_declaration_content and DECLARATION_CONTENT is not None: # Check not None
|
|
623
|
-
if hasattr(target_object, 'textual_declaration'):
|
|
624
|
-
decl_obj = target_object.textual_declaration
|
|
625
|
-
if decl_obj and hasattr(decl_obj, 'replace'):
|
|
626
|
-
try:
|
|
627
|
-
print("DEBUG: Accessing textual_declaration...")
|
|
628
|
-
decl_obj.replace(DECLARATION_CONTENT)
|
|
629
|
-
print("DEBUG: Set declaration text using replace().")
|
|
630
|
-
declaration_updated = True
|
|
631
|
-
except Exception as decl_err:
|
|
632
|
-
print("ERROR: Failed to set declaration text: %s" % decl_err)
|
|
633
|
-
traceback.print_exc() # Print stack trace for detailed error
|
|
634
|
-
else:
|
|
635
|
-
print("WARN: Target '%s' textual_declaration attribute is None or does not have replace(). Skipping declaration update." % target_name)
|
|
636
|
-
else:
|
|
637
|
-
print("WARN: Target '%s' does not have textual_declaration attribute. Skipping declaration update." % target_name)
|
|
638
|
-
else:
|
|
639
|
-
print("DEBUG: Declaration content not provided or is None. Skipping declaration update.")
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
# --- Set Implementation Part ---
|
|
643
|
-
implementation_updated = False
|
|
644
|
-
has_implementation_content = 'IMPLEMENTATION_CONTENT' in locals() or 'IMPLEMENTATION_CONTENT' in globals()
|
|
645
|
-
if has_implementation_content and IMPLEMENTATION_CONTENT is not None: # Check not None
|
|
646
|
-
if hasattr(target_object, 'textual_implementation'):
|
|
647
|
-
impl_obj = target_object.textual_implementation
|
|
648
|
-
if impl_obj and hasattr(impl_obj, 'replace'):
|
|
649
|
-
try:
|
|
650
|
-
print("DEBUG: Accessing textual_implementation...")
|
|
651
|
-
impl_obj.replace(IMPLEMENTATION_CONTENT)
|
|
652
|
-
print("DEBUG: Set implementation text using replace().")
|
|
653
|
-
implementation_updated = True
|
|
654
|
-
except Exception as impl_err:
|
|
655
|
-
print("ERROR: Failed to set implementation text: %s" % impl_err)
|
|
656
|
-
traceback.print_exc() # Print stack trace for detailed error
|
|
657
|
-
else:
|
|
658
|
-
print("WARN: Target '%s' textual_implementation attribute is None or does not have replace(). Skipping implementation update." % target_name)
|
|
659
|
-
else:
|
|
660
|
-
print("WARN: Target '%s' does not have textual_implementation attribute. Skipping implementation update." % target_name)
|
|
661
|
-
else:
|
|
662
|
-
print("DEBUG: Implementation content not provided or is None. Skipping implementation update.")
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
# --- SAVE THE PROJECT TO PERSIST THE CODE CHANGE ---
|
|
666
|
-
# Only save if something was actually updated to avoid unnecessary saves
|
|
667
|
-
if declaration_updated or implementation_updated:
|
|
668
|
-
try:
|
|
669
|
-
print("DEBUG: Saving Project (after code change)...")
|
|
670
|
-
primary_project.save() # Save the overall project file
|
|
671
|
-
print("DEBUG: Project saved successfully after code change.")
|
|
672
|
-
except Exception as save_err:
|
|
673
|
-
print("ERROR: Failed to save Project after setting code: %s" % save_err)
|
|
674
|
-
detailed_error = traceback.format_exc()
|
|
675
|
-
error_message = "Error saving Project after code change for '%s': %s\\n%s" % (target_name, save_err, detailed_error)
|
|
676
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
677
|
-
else:
|
|
678
|
-
print("DEBUG: No code parts were updated, skipping project save.")
|
|
679
|
-
# --- END SAVING ---
|
|
680
|
-
|
|
681
|
-
print("Code Set For: %s" % target_name)
|
|
682
|
-
print("Path: %s" % POU_FULL_PATH)
|
|
683
|
-
print("SCRIPT_SUCCESS: Declaration and/or implementation set successfully."); sys.exit(0)
|
|
684
|
-
|
|
685
|
-
except Exception as e:
|
|
686
|
-
detailed_error = traceback.format_exc()
|
|
687
|
-
error_message = "Error setting code for object '%s' in project '%s': %s\\n%s" % (POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
688
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
689
|
-
`;
|
|
690
|
-
const CREATE_PROPERTY_SCRIPT_TEMPLATE = `
|
|
691
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
692
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
693
|
-
${FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}
|
|
694
|
-
PARENT_POU_FULL_PATH = "{PARENT_POU_FULL_PATH}" # e.g., "Application/MyFB"
|
|
695
|
-
PROPERTY_NAME = "{PROPERTY_NAME}"
|
|
696
|
-
PROPERTY_TYPE = "{PROPERTY_TYPE}"
|
|
697
|
-
# Optional: Language for Getter/Setter (usually defaults to ST)
|
|
698
|
-
# LANG_GUID_STR = "{LANG_GUID_STR}" # Example if needed
|
|
699
|
-
|
|
700
|
-
try:
|
|
701
|
-
print("DEBUG: create_property script: ParentPOU='%s', Name='%s', Type='%s', Project='%s'" % (PARENT_POU_FULL_PATH, PROPERTY_NAME, PROPERTY_TYPE, PROJECT_FILE_PATH))
|
|
702
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
703
|
-
if not PARENT_POU_FULL_PATH: raise ValueError("Parent POU full path empty.")
|
|
704
|
-
if not PROPERTY_NAME: raise ValueError("Property name empty.")
|
|
705
|
-
if not PROPERTY_TYPE: raise ValueError("Property type empty.")
|
|
706
|
-
|
|
707
|
-
# Find the parent POU object
|
|
708
|
-
parent_pou_object = find_object_by_path_robust(primary_project, PARENT_POU_FULL_PATH, "parent POU")
|
|
709
|
-
if not parent_pou_object: raise ValueError("Parent POU object not found: %s" % PARENT_POU_FULL_PATH)
|
|
710
|
-
|
|
711
|
-
parent_pou_name = getattr(parent_pou_object, 'get_name', lambda: PARENT_POU_FULL_PATH)()
|
|
712
|
-
print("DEBUG: Found Parent POU object: %s" % parent_pou_name)
|
|
713
|
-
|
|
714
|
-
# Check if parent object supports creating properties (should implement ScriptIecLanguageMemberContainer)
|
|
715
|
-
if not hasattr(parent_pou_object, 'create_property'):
|
|
716
|
-
raise TypeError("Parent object '%s' of type %s does not support create_property." % (parent_pou_name, type(parent_pou_object).__name__))
|
|
717
|
-
|
|
718
|
-
# Default language to None (usually ST)
|
|
719
|
-
lang_guid = None
|
|
720
|
-
print("DEBUG: Calling create_property: Name='%s', Type='%s', Lang=%s" % (PROPERTY_NAME, PROPERTY_TYPE, lang_guid))
|
|
721
|
-
|
|
722
|
-
# Call the create_property method ON THE PARENT POU
|
|
723
|
-
new_property_object = parent_pou_object.create_property(
|
|
724
|
-
name=PROPERTY_NAME,
|
|
725
|
-
return_type=PROPERTY_TYPE,
|
|
726
|
-
language=lang_guid # Pass None to use default
|
|
727
|
-
)
|
|
728
|
-
|
|
729
|
-
if new_property_object:
|
|
730
|
-
new_prop_name = getattr(new_property_object, 'get_name', lambda: PROPERTY_NAME)()
|
|
731
|
-
print("DEBUG: Property object created: %s" % new_prop_name)
|
|
732
|
-
|
|
733
|
-
# --- SAVE THE PROJECT TO PERSIST THE NEW PROPERTY OBJECT ---
|
|
734
|
-
try:
|
|
735
|
-
print("DEBUG: Saving Project (after property creation)...")
|
|
736
|
-
primary_project.save()
|
|
737
|
-
print("DEBUG: Project saved successfully after property creation.")
|
|
738
|
-
except Exception as save_err:
|
|
739
|
-
print("ERROR: Failed to save Project after creating property: %s" % save_err)
|
|
740
|
-
detailed_error = traceback.format_exc()
|
|
741
|
-
error_message = "Error saving Project after creating property '%s': %s\\n%s" % (PROPERTY_NAME, save_err, detailed_error)
|
|
742
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
743
|
-
# --- END SAVING ---
|
|
744
|
-
|
|
745
|
-
print("Property Created: %s" % new_prop_name)
|
|
746
|
-
print("Parent POU: %s" % PARENT_POU_FULL_PATH)
|
|
747
|
-
print("Type: %s" % PROPERTY_TYPE)
|
|
748
|
-
print("SCRIPT_SUCCESS: Property created successfully."); sys.exit(0)
|
|
749
|
-
else:
|
|
750
|
-
error_message = "Failed to create property '%s' under '%s'. create_property returned None." % (PROPERTY_NAME, parent_pou_name)
|
|
751
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
752
|
-
|
|
753
|
-
except Exception as e:
|
|
754
|
-
detailed_error = traceback.format_exc()
|
|
755
|
-
error_message = "Error creating property '%s' under POU '%s' in project '%s': %s\\n%s" % (PROPERTY_NAME, PARENT_POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
756
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
757
|
-
`;
|
|
758
|
-
const CREATE_METHOD_SCRIPT_TEMPLATE = `
|
|
759
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
760
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
761
|
-
${FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}
|
|
762
|
-
PARENT_POU_FULL_PATH = "{PARENT_POU_FULL_PATH}" # e.g., "Application/MyFB"
|
|
763
|
-
METHOD_NAME = "{METHOD_NAME}"
|
|
764
|
-
RETURN_TYPE = "{RETURN_TYPE}" # Can be empty string for no return type
|
|
765
|
-
# Optional: Language
|
|
766
|
-
# LANG_GUID_STR = "{LANG_GUID_STR}" # Example if needed
|
|
767
|
-
|
|
768
|
-
try:
|
|
769
|
-
print("DEBUG: create_method script: ParentPOU='%s', Name='%s', ReturnType='%s', Project='%s'" % (PARENT_POU_FULL_PATH, METHOD_NAME, RETURN_TYPE, PROJECT_FILE_PATH))
|
|
770
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
771
|
-
if not PARENT_POU_FULL_PATH: raise ValueError("Parent POU full path empty.")
|
|
772
|
-
if not METHOD_NAME: raise ValueError("Method name empty.")
|
|
773
|
-
# RETURN_TYPE can be empty
|
|
774
|
-
|
|
775
|
-
# Find the parent POU object
|
|
776
|
-
parent_pou_object = find_object_by_path_robust(primary_project, PARENT_POU_FULL_PATH, "parent POU")
|
|
777
|
-
if not parent_pou_object: raise ValueError("Parent POU object not found: %s" % PARENT_POU_FULL_PATH)
|
|
778
|
-
|
|
779
|
-
parent_pou_name = getattr(parent_pou_object, 'get_name', lambda: PARENT_POU_FULL_PATH)()
|
|
780
|
-
print("DEBUG: Found Parent POU object: %s" % parent_pou_name)
|
|
781
|
-
|
|
782
|
-
# Check if parent object supports creating methods (should implement ScriptIecLanguageMemberContainer)
|
|
783
|
-
if not hasattr(parent_pou_object, 'create_method'):
|
|
784
|
-
raise TypeError("Parent object '%s' of type %s does not support create_method." % (parent_pou_name, type(parent_pou_object).__name__))
|
|
785
|
-
|
|
786
|
-
# Default language to None (usually ST)
|
|
787
|
-
lang_guid = None
|
|
788
|
-
# Use None if RETURN_TYPE is empty string, otherwise use the string
|
|
789
|
-
actual_return_type = RETURN_TYPE if RETURN_TYPE else None
|
|
790
|
-
print("DEBUG: Calling create_method: Name='%s', ReturnType=%s, Lang=%s" % (METHOD_NAME, actual_return_type, lang_guid))
|
|
791
|
-
|
|
792
|
-
# Call the create_method method ON THE PARENT POU
|
|
793
|
-
new_method_object = parent_pou_object.create_method(
|
|
794
|
-
name=METHOD_NAME,
|
|
795
|
-
return_type=actual_return_type,
|
|
796
|
-
language=lang_guid # Pass None to use default
|
|
797
|
-
)
|
|
798
|
-
|
|
799
|
-
if new_method_object:
|
|
800
|
-
new_meth_name = getattr(new_method_object, 'get_name', lambda: METHOD_NAME)()
|
|
801
|
-
print("DEBUG: Method object created: %s" % new_meth_name)
|
|
802
|
-
|
|
803
|
-
# --- SAVE THE PROJECT TO PERSIST THE NEW METHOD OBJECT ---
|
|
804
|
-
try:
|
|
805
|
-
print("DEBUG: Saving Project (after method creation)...")
|
|
806
|
-
primary_project.save()
|
|
807
|
-
print("DEBUG: Project saved successfully after method creation.")
|
|
808
|
-
except Exception as save_err:
|
|
809
|
-
print("ERROR: Failed to save Project after creating method: %s" % save_err)
|
|
810
|
-
detailed_error = traceback.format_exc()
|
|
811
|
-
error_message = "Error saving Project after creating method '%s': %s\\n%s" % (METHOD_NAME, save_err, detailed_error)
|
|
812
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
813
|
-
# --- END SAVING ---
|
|
814
|
-
|
|
815
|
-
print("Method Created: %s" % new_meth_name)
|
|
816
|
-
print("Parent POU: %s" % PARENT_POU_FULL_PATH)
|
|
817
|
-
print("Return Type: %s" % (RETURN_TYPE if RETURN_TYPE else "(None)"))
|
|
818
|
-
print("SCRIPT_SUCCESS: Method created successfully."); sys.exit(0)
|
|
819
|
-
else:
|
|
820
|
-
error_message = "Failed to create method '%s' under '%s'. create_method returned None." % (METHOD_NAME, parent_pou_name)
|
|
821
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
822
|
-
|
|
823
|
-
except Exception as e:
|
|
824
|
-
detailed_error = traceback.format_exc()
|
|
825
|
-
error_message = "Error creating method '%s' under POU '%s' in project '%s': %s\\n%s" % (METHOD_NAME, PARENT_POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
826
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
827
|
-
`;
|
|
828
|
-
const COMPILE_PROJECT_SCRIPT_TEMPLATE = `
|
|
829
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
830
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
831
|
-
try:
|
|
832
|
-
print("DEBUG: compile_project script: Project='%s'" % PROJECT_FILE_PATH)
|
|
833
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
834
|
-
project_name = os.path.basename(PROJECT_FILE_PATH)
|
|
835
|
-
target_app = None
|
|
836
|
-
app_name = "N/A"
|
|
837
|
-
|
|
838
|
-
# Try getting active application first
|
|
839
|
-
try:
|
|
840
|
-
target_app = primary_project.active_application
|
|
841
|
-
if target_app:
|
|
842
|
-
app_name = getattr(target_app, 'get_name', lambda: "Unnamed App (Active)")()
|
|
843
|
-
print("DEBUG: Found active application: %s" % app_name)
|
|
844
|
-
except Exception as active_err:
|
|
845
|
-
print("WARN: Could not get active application: %s. Searching..." % active_err)
|
|
846
|
-
|
|
847
|
-
# If no active app, search for the first one
|
|
848
|
-
if not target_app:
|
|
849
|
-
print("DEBUG: Searching for first compilable application...")
|
|
850
|
-
apps = []
|
|
851
|
-
try:
|
|
852
|
-
# Search recursively through all project objects
|
|
853
|
-
all_children = primary_project.get_children(True)
|
|
854
|
-
for child in all_children:
|
|
855
|
-
# Check using the marker property and if build method exists
|
|
856
|
-
if hasattr(child, 'is_application') and child.is_application and hasattr(child, 'build'):
|
|
857
|
-
app_name_found = getattr(child, 'get_name', lambda: "Unnamed App")()
|
|
858
|
-
print("DEBUG: Found potential application object: %s" % app_name_found)
|
|
859
|
-
apps.append(child)
|
|
860
|
-
break # Take the first one found
|
|
861
|
-
except Exception as find_err: print("WARN: Error finding application object: %s" % find_err)
|
|
862
|
-
|
|
863
|
-
if not apps: raise RuntimeError("No compilable application found in project '%s'" % project_name)
|
|
864
|
-
target_app = apps[0]
|
|
865
|
-
app_name = getattr(target_app, 'get_name', lambda: "Unnamed App (First Found)")()
|
|
866
|
-
print("WARN: Compiling first found application: %s" % app_name)
|
|
867
|
-
|
|
868
|
-
print("DEBUG: Calling build() on app '%s'..." % app_name)
|
|
869
|
-
if not hasattr(target_app, 'build'):
|
|
870
|
-
raise TypeError("Selected object '%s' is not an application or doesn't support build()." % app_name)
|
|
871
|
-
|
|
872
|
-
# Execute the build
|
|
873
|
-
target_app.build();
|
|
874
|
-
print("DEBUG: Build command executed for application '%s'." % app_name)
|
|
875
|
-
|
|
876
|
-
# Check messages is harder without direct access to message store from script.
|
|
877
|
-
# Rely on CODESYS UI or log output for now.
|
|
878
|
-
print("Compile Initiated For Application: %s" % app_name); print("In Project: %s" % project_name)
|
|
879
|
-
print("SCRIPT_SUCCESS: Application compilation initiated."); sys.exit(0)
|
|
880
|
-
except Exception as e:
|
|
881
|
-
detailed_error = traceback.format_exc()
|
|
882
|
-
error_message = "Error initiating compilation for project %s: %s\\n%s" % (PROJECT_FILE_PATH, e, detailed_error)
|
|
883
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
884
|
-
`;
|
|
885
|
-
const GET_PROJECT_STRUCTURE_SCRIPT_TEMPLATE = `
|
|
886
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
887
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
888
|
-
# FIND_OBJECT_BY_PATH_PYTHON_SNIPPET is NOT needed here, we start from project root.
|
|
889
|
-
|
|
890
|
-
def get_object_structure(obj, indent=0, max_depth=10): # Add max_depth
|
|
891
|
-
lines = []; indent_str = " " * indent
|
|
892
|
-
if indent > max_depth:
|
|
893
|
-
lines.append("%s- Max recursion depth reached." % indent_str)
|
|
894
|
-
return lines
|
|
895
|
-
try:
|
|
896
|
-
name = "Unnamed"; obj_type = type(obj).__name__
|
|
897
|
-
guid_str = ""
|
|
898
|
-
folder_str = ""
|
|
899
|
-
try:
|
|
900
|
-
name = getattr(obj, 'get_name', lambda: "Unnamed")() or "Unnamed" # Safer get_name
|
|
901
|
-
if hasattr(obj, 'guid'): guid_str = " {%s}" % obj.guid
|
|
902
|
-
if hasattr(obj, 'is_folder') and obj.is_folder: folder_str = " [Folder]"
|
|
903
|
-
except Exception as name_err:
|
|
904
|
-
print("WARN: Error getting name/guid/folder status for an object: %s" % name_err)
|
|
905
|
-
name = "!!! Error Getting Name !!!"
|
|
906
|
-
|
|
907
|
-
lines.append("%s- %s (%s)%s%s" % (indent_str, name, obj_type, folder_str, guid_str))
|
|
908
|
-
|
|
909
|
-
# Get children only if the object potentially has them
|
|
910
|
-
children = []
|
|
911
|
-
can_have_children = hasattr(obj, 'get_children') and (
|
|
912
|
-
not hasattr(obj, 'is_folder') or # If it's not clear if it's a folder (e.g., project root)
|
|
913
|
-
(hasattr(obj, 'is_folder') and obj.is_folder) or # If it is a folder
|
|
914
|
-
# Add known container types explicitly, check marker interfaces too
|
|
915
|
-
hasattr(obj, 'is_project') or hasattr(obj, 'is_application') or hasattr(obj, 'is_device') or hasattr(obj,'is_pou')
|
|
916
|
-
)
|
|
917
|
-
|
|
918
|
-
if can_have_children:
|
|
919
|
-
try:
|
|
920
|
-
children = obj.get_children(False)
|
|
921
|
-
# print("DEBUG: %s has %d children" % (name, len(children))) # Verbose
|
|
922
|
-
except Exception as get_child_err:
|
|
923
|
-
lines.append("%s ERROR getting children: %s" % (indent_str, get_child_err))
|
|
924
|
-
# traceback.print_exc() # Optional
|
|
925
|
-
|
|
926
|
-
for child in children:
|
|
927
|
-
lines.extend(get_object_structure(child, indent + 1, max_depth)) # Recurse
|
|
928
|
-
|
|
929
|
-
except Exception as e:
|
|
930
|
-
lines.append("%s- Error processing node: %s" % (indent_str, e))
|
|
931
|
-
traceback.print_exc() # Print detailed error for this node
|
|
932
|
-
return lines
|
|
933
|
-
try:
|
|
934
|
-
print("DEBUG: Getting structure for: %s" % PROJECT_FILE_PATH)
|
|
935
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
936
|
-
project_name = os.path.basename(PROJECT_FILE_PATH)
|
|
937
|
-
print("DEBUG: Getting structure for project: %s" % project_name)
|
|
938
|
-
# Use the project object obtained from ensure_project_open
|
|
939
|
-
structure_list = get_object_structure(primary_project, max_depth=15) # Set a reasonable depth
|
|
940
|
-
structure_output = "\\n".join(structure_list)
|
|
941
|
-
# Ensure markers are printed distinctly
|
|
942
|
-
print("\\n--- PROJECT STRUCTURE START ---")
|
|
943
|
-
print(structure_output)
|
|
944
|
-
print("--- PROJECT STRUCTURE END ---\\n")
|
|
945
|
-
print("SCRIPT_SUCCESS: Project structure retrieved."); sys.exit(0)
|
|
946
|
-
except Exception as e:
|
|
947
|
-
detailed_error = traceback.format_exc()
|
|
948
|
-
error_message = "Error getting structure for %s: %s\\n%s" % (PROJECT_FILE_PATH, e, detailed_error)
|
|
949
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
950
|
-
`;
|
|
951
|
-
const GET_POU_CODE_SCRIPT_TEMPLATE = `
|
|
952
|
-
import sys, scriptengine as script_engine, os, traceback
|
|
953
|
-
${ENSURE_PROJECT_OPEN_PYTHON_SNIPPET}
|
|
954
|
-
${FIND_OBJECT_BY_PATH_PYTHON_SNIPPET}
|
|
955
|
-
POU_FULL_PATH = "{POU_FULL_PATH}"; CODE_START_MARKER = "### POU CODE START ###"; CODE_END_MARKER = "### POU CODE END ###"
|
|
956
|
-
DECL_START_MARKER = "### POU DECLARATION START ###"; DECL_END_MARKER = "### POU DECLARATION END ###"
|
|
957
|
-
IMPL_START_MARKER = "### POU IMPLEMENTATION START ###"; IMPL_END_MARKER = "### POU IMPLEMENTATION END ###"
|
|
958
|
-
|
|
959
|
-
try:
|
|
960
|
-
print("DEBUG: Getting code: POU_FULL_PATH='%s', Project='%s'" % (POU_FULL_PATH, PROJECT_FILE_PATH))
|
|
961
|
-
primary_project = ensure_project_open(PROJECT_FILE_PATH)
|
|
962
|
-
if not POU_FULL_PATH: raise ValueError("POU full path empty.")
|
|
963
|
-
|
|
964
|
-
# Find the target POU/Method/Property object
|
|
965
|
-
target_object = find_object_by_path_robust(primary_project, POU_FULL_PATH, "target object")
|
|
966
|
-
if not target_object: raise ValueError("Target object not found using path: %s" % POU_FULL_PATH)
|
|
967
|
-
|
|
968
|
-
target_name = getattr(target_object, 'get_name', lambda: POU_FULL_PATH)()
|
|
969
|
-
print("DEBUG: Found target object: %s" % target_name)
|
|
970
|
-
|
|
971
|
-
declaration_code = ""; implementation_code = ""
|
|
972
|
-
|
|
973
|
-
# --- Get Declaration Part ---
|
|
974
|
-
if hasattr(target_object, 'textual_declaration'):
|
|
975
|
-
decl_obj = target_object.textual_declaration
|
|
976
|
-
if decl_obj and hasattr(decl_obj, 'text'):
|
|
977
|
-
try:
|
|
978
|
-
declaration_code = decl_obj.text
|
|
979
|
-
print("DEBUG: Got declaration text.")
|
|
980
|
-
except Exception as decl_read_err:
|
|
981
|
-
print("ERROR: Failed to read declaration text: %s" % decl_read_err)
|
|
982
|
-
declaration_code = "/* ERROR reading declaration: %s */" % decl_read_err
|
|
983
|
-
else:
|
|
984
|
-
print("WARN: textual_declaration exists but is None or has no 'text' attribute.")
|
|
985
|
-
else:
|
|
986
|
-
print("WARN: No textual_declaration attribute.")
|
|
987
|
-
|
|
988
|
-
# --- Get Implementation Part ---
|
|
989
|
-
if hasattr(target_object, 'textual_implementation'):
|
|
990
|
-
impl_obj = target_object.textual_implementation
|
|
991
|
-
if impl_obj and hasattr(impl_obj, 'text'):
|
|
992
|
-
try:
|
|
993
|
-
implementation_code = impl_obj.text
|
|
994
|
-
print("DEBUG: Got implementation text.")
|
|
995
|
-
except Exception as impl_read_err:
|
|
996
|
-
print("ERROR: Failed to read implementation text: %s" % impl_read_err)
|
|
997
|
-
implementation_code = "/* ERROR reading implementation: %s */" % impl_read_err
|
|
998
|
-
else:
|
|
999
|
-
print("WARN: textual_implementation exists but is None or has no 'text' attribute.")
|
|
1000
|
-
else:
|
|
1001
|
-
print("WARN: No textual_implementation attribute.")
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
print("Code retrieved for: %s" % target_name)
|
|
1005
|
-
# Print declaration between markers, ensuring markers are on separate lines
|
|
1006
|
-
print("\\n" + DECL_START_MARKER)
|
|
1007
|
-
print(declaration_code)
|
|
1008
|
-
print(DECL_END_MARKER + "\\n")
|
|
1009
|
-
# Print implementation between markers
|
|
1010
|
-
print(IMPL_START_MARKER)
|
|
1011
|
-
print(implementation_code)
|
|
1012
|
-
print(IMPL_END_MARKER + "\\n")
|
|
1013
|
-
|
|
1014
|
-
# --- LEGACY MARKERS for backward compatibility if needed ---
|
|
1015
|
-
# Combine both for old marker format, adding a separator line
|
|
1016
|
-
# legacy_combined_code = declaration_code + "\\n\\n// Implementation\\n" + implementation_code
|
|
1017
|
-
# print(CODE_START_MARKER); print(legacy_combined_code); print(CODE_END_MARKER)
|
|
1018
|
-
# --- END LEGACY ---
|
|
1019
|
-
|
|
1020
|
-
print("SCRIPT_SUCCESS: Code retrieved."); sys.exit(0)
|
|
1021
|
-
except Exception as e:
|
|
1022
|
-
detailed_error = traceback.format_exc()
|
|
1023
|
-
error_message = "Error getting code for object '%s' in project '%s': %s\\n%s" % (POU_FULL_PATH, PROJECT_FILE_PATH, e, detailed_error)
|
|
1024
|
-
print(error_message); print("SCRIPT_ERROR: %s" % error_message); sys.exit(1)
|
|
1025
|
-
`;
|
|
1026
|
-
// --- End Python Script Templates ---
|
|
1027
|
-
// --- Zod Schemas (moved for clarity before usage) ---
|
|
136
|
+
// --- Zod Schemas ---
|
|
1028
137
|
const PouTypeEnum = zod_1.z.enum(["Program", "FunctionBlock", "Function"]);
|
|
1029
138
|
const ImplementationLanguageEnum = zod_1.z.enum(["ST", "LD", "FBD", "SFC", "IL", "CFC", "StructuredText", "LadderDiagram", "FunctionBlockDiagram", "SequentialFunctionChart", "InstructionList", "ContinuousFunctionChart"]);
|
|
1030
139
|
// --- End Zod Schemas ---
|
|
@@ -1035,45 +144,53 @@ except Exception as e:
|
|
|
1035
144
|
var _a, _b, _c, _d, _e;
|
|
1036
145
|
console.error(`SERVER.TS Resource request: ${uri.href}`);
|
|
1037
146
|
try {
|
|
1038
|
-
const
|
|
147
|
+
const scriptContent = yield (0, scriptLoader_1.loadScriptTemplate)('check_status');
|
|
148
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1039
149
|
const outputLines = result.output.split(/[\r\n]+/).filter(line => line.trim());
|
|
1040
150
|
const statusData = {};
|
|
1041
|
-
outputLines.forEach(line => {
|
|
1042
|
-
|
|
1043
|
-
|
|
151
|
+
outputLines.forEach(line => {
|
|
152
|
+
const match = line.match(/^([^:]+):\s*(.*)$/);
|
|
153
|
+
if (match) {
|
|
154
|
+
statusData[match[1].trim()] = match[2].trim();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
1044
157
|
const statusText = `CODESYS Status:\n - Scripting OK: ${(_a = statusData['Scripting OK']) !== null && _a !== void 0 ? _a : 'Unknown'}\n - Project Open: ${(_b = statusData['Project Open']) !== null && _b !== void 0 ? _b : 'Unknown'}\n - Project Name: ${(_c = statusData['Project Name']) !== null && _c !== void 0 ? _c : 'Unknown'}\n - Project Path: ${(_d = statusData['Project Path']) !== null && _d !== void 0 ? _d : 'N/A'}`;
|
|
1045
158
|
const isError = !result.success || ((_e = statusData['Scripting OK']) === null || _e === void 0 ? void 0 : _e.toLowerCase()) !== 'true';
|
|
1046
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1047
159
|
return { contents: [{ uri: uri.href, text: statusText, contentType: "text/plain" }], isError: isError };
|
|
1048
160
|
}
|
|
1049
161
|
catch (error) {
|
|
1050
162
|
console.error(`Error resource ${uri.href}:`, error);
|
|
1051
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1052
163
|
return { contents: [{ uri: uri.href, text: `Failed status script: ${error.message}`, contentType: "text/plain" }], isError: true };
|
|
1053
164
|
}
|
|
1054
165
|
}));
|
|
1055
|
-
//
|
|
166
|
+
// Templates
|
|
1056
167
|
const projectStructureTemplate = new mcp_js_1.ResourceTemplate("codesys://project/{+project_path}/structure", { list: undefined });
|
|
1057
168
|
const pouCodeTemplate = new mcp_js_1.ResourceTemplate("codesys://project/{+project_path}/pou/{+pou_path}/code", { list: undefined });
|
|
1058
|
-
// *** END DEFINE TEMPLATES ***
|
|
1059
169
|
server.resource("project-structure", projectStructureTemplate, (uri, params) => __awaiter(this, void 0, void 0, function* () {
|
|
1060
|
-
// *** DEFINE VARIABLES (like projectPath) ***
|
|
1061
170
|
const projectPathParam = params.project_path;
|
|
1062
171
|
if (typeof projectPathParam !== 'string') {
|
|
1063
|
-
return {
|
|
172
|
+
return {
|
|
173
|
+
contents: [{ uri: uri.href, text: `Error: Invalid project path type (${typeof projectPathParam}).`, contentType: "text/plain" }],
|
|
174
|
+
isError: true
|
|
175
|
+
};
|
|
1064
176
|
}
|
|
1065
|
-
const projectPath = projectPathParam;
|
|
177
|
+
const projectPath = projectPathParam;
|
|
1066
178
|
if (!projectPath) {
|
|
1067
|
-
return {
|
|
179
|
+
return {
|
|
180
|
+
contents: [{ uri: uri.href, text: "Error: Project path missing.", contentType: "text/plain" }],
|
|
181
|
+
isError: true
|
|
182
|
+
};
|
|
1068
183
|
}
|
|
1069
|
-
// *** END DEFINE VARIABLES ***
|
|
1070
184
|
console.error(`Resource request: project structure for ${projectPath}`);
|
|
1071
185
|
try {
|
|
1072
186
|
const absoluteProjPath = path.normalize(path.isAbsolute(projectPath) ? projectPath : path.join(WORKSPACE_DIR, projectPath));
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
const
|
|
1076
|
-
|
|
187
|
+
// Load script template and prepare parameters
|
|
188
|
+
const scriptTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('get_project_structure');
|
|
189
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
190
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + scriptTemplate;
|
|
191
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
192
|
+
'PROJECT_FILE_PATH': absoluteProjPath.replace(/\\/g, '\\\\')
|
|
193
|
+
});
|
|
1077
194
|
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1078
195
|
let structureText = `Error retrieving structure for ${absoluteProjPath}.\n\n${result.output}`;
|
|
1079
196
|
let isError = !result.success;
|
|
@@ -1094,42 +211,47 @@ except Exception as e:
|
|
|
1094
211
|
else {
|
|
1095
212
|
isError = true;
|
|
1096
213
|
}
|
|
1097
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1098
214
|
return { contents: [{ uri: uri.href, text: structureText, contentType: "text/plain" }], isError: isError };
|
|
1099
215
|
}
|
|
1100
216
|
catch (error) {
|
|
1101
217
|
console.error(`Error getting structure ${uri.href}:`, error);
|
|
1102
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1103
218
|
return { contents: [{ uri: uri.href, text: `Failed structure script for '${projectPath}': ${error.message}`, contentType: "text/plain" }], isError: true };
|
|
1104
219
|
}
|
|
1105
220
|
}));
|
|
1106
221
|
server.resource("pou-code", pouCodeTemplate, (uri, params) => __awaiter(this, void 0, void 0, function* () {
|
|
1107
|
-
// *** DEFINE VARIABLES (like projectPath, pouPath) ***
|
|
1108
222
|
const projectPathParam = params.project_path;
|
|
1109
223
|
const pouPathParam = params.pou_path;
|
|
1110
224
|
if (typeof projectPathParam !== 'string' || typeof pouPathParam !== 'string') {
|
|
1111
|
-
return {
|
|
225
|
+
return {
|
|
226
|
+
contents: [{ uri: uri.href, text: "Error: Invalid project or POU path type.", contentType: "text/plain" }],
|
|
227
|
+
isError: true
|
|
228
|
+
};
|
|
1112
229
|
}
|
|
1113
|
-
const projectPath = projectPathParam;
|
|
1114
|
-
const pouPath = pouPathParam;
|
|
230
|
+
const projectPath = projectPathParam;
|
|
231
|
+
const pouPath = pouPathParam;
|
|
1115
232
|
if (!projectPath || !pouPath) {
|
|
1116
|
-
return {
|
|
233
|
+
return {
|
|
234
|
+
contents: [{ uri: uri.href, text: "Error: Project or POU path missing.", contentType: "text/plain" }],
|
|
235
|
+
isError: true
|
|
236
|
+
};
|
|
1117
237
|
}
|
|
1118
|
-
// *** END DEFINE VARIABLES ***
|
|
1119
238
|
console.error(`Resource request: POU code: Project='${projectPath}', POU='${pouPath}'`);
|
|
1120
239
|
try {
|
|
1121
240
|
const absoluteProjPath = path.normalize(path.isAbsolute(projectPath) ? projectPath : path.join(WORKSPACE_DIR, projectPath));
|
|
1122
241
|
const sanitizedPouPath = String(pouPath).replace(/\\/g, '/').replace(/^\/+|\/+$/g, '');
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
242
|
+
// Load script template and prepare parameters
|
|
243
|
+
const scriptTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('get_pou_code');
|
|
244
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
245
|
+
const findObjectByPathTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('find_object_by_path');
|
|
246
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + findObjectByPathTemplate + '\n\n' + scriptTemplate;
|
|
247
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
248
|
+
'PROJECT_FILE_PATH': absoluteProjPath.replace(/\\/g, '\\\\'),
|
|
249
|
+
'POU_FULL_PATH': sanitizedPouPath
|
|
250
|
+
});
|
|
1128
251
|
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1129
252
|
let codeText = `Error retrieving code for object '${sanitizedPouPath}' in project '${absoluteProjPath}'.\n\n${result.output}`;
|
|
1130
253
|
let isError = !result.success;
|
|
1131
254
|
if (result.success && result.output.includes("SCRIPT_SUCCESS")) {
|
|
1132
|
-
// ... (marker parsing logic using new markers) ...
|
|
1133
255
|
const declStartMarker = "### POU DECLARATION START ###";
|
|
1134
256
|
const declEndMarker = "### POU DECLARATION END ###";
|
|
1135
257
|
const implStartMarker = "### POU IMPLEMENTATION START ###";
|
|
@@ -1153,45 +275,53 @@ except Exception as e:
|
|
|
1153
275
|
console.error(`WARN: Implementation markers not found correctly for ${sanitizedPouPath}`);
|
|
1154
276
|
}
|
|
1155
277
|
codeText = `// ----- Declaration -----\n${declaration}\n\n// ----- Implementation -----\n${implementation}`;
|
|
1156
|
-
// *** END MARKER PARSING ***
|
|
1157
278
|
}
|
|
1158
279
|
else {
|
|
1159
280
|
isError = true;
|
|
1160
281
|
}
|
|
1161
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1162
282
|
return { contents: [{ uri: uri.href, text: codeText, contentType: "text/plain" }], isError: isError };
|
|
1163
283
|
}
|
|
1164
284
|
catch (error) {
|
|
1165
285
|
console.error(`Error getting POU code ${uri.href}:`, error);
|
|
1166
|
-
// **** RETURN MCP STRUCTURE ****
|
|
1167
286
|
return { contents: [{ uri: uri.href, text: `Failed POU code script for '${pouPath}' in '${projectPath}': ${error.message}`, contentType: "text/plain" }], isError: true };
|
|
1168
287
|
}
|
|
1169
288
|
}));
|
|
1170
289
|
// --- End Resources ---
|
|
1171
290
|
// --- Tools ---
|
|
1172
|
-
server.tool("open_project",
|
|
1173
|
-
"Opens an existing CODESYS project file.", // Tool Description
|
|
1174
|
-
{
|
|
291
|
+
server.tool("open_project", "Opens an existing CODESYS project file.", {
|
|
1175
292
|
filePath: zod_1.z.string().describe("Path to the project file (e.g., 'C:/Projects/MyPLC.project' or '/Users/user/projects/my_project.project').")
|
|
1176
293
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1177
294
|
const { filePath } = args;
|
|
1178
295
|
let absPath = path.normalize(path.isAbsolute(filePath) ? filePath : path.join(WORKSPACE_DIR, filePath));
|
|
1179
296
|
console.error(`Tool call: open_project: ${absPath}`);
|
|
1180
297
|
try {
|
|
1181
|
-
|
|
1182
|
-
const
|
|
1183
|
-
|
|
298
|
+
// Load the script template and prepare with parameters
|
|
299
|
+
const scriptTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('open_project');
|
|
300
|
+
// Combine with required ensure_project_open script (will handle any imports)
|
|
301
|
+
const ensureProjectOpenScript = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
302
|
+
const combinedScript = ensureProjectOpenScript + '\n\n' + scriptTemplate;
|
|
303
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
304
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\')
|
|
305
|
+
});
|
|
306
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1184
307
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1185
|
-
return {
|
|
308
|
+
return {
|
|
309
|
+
content: [{
|
|
310
|
+
type: "text",
|
|
311
|
+
text: success ? `Project opened: ${absPath}` : `Failed open project ${absPath}. Output:\n${result.output}`
|
|
312
|
+
}],
|
|
313
|
+
isError: !success
|
|
314
|
+
};
|
|
1186
315
|
}
|
|
1187
316
|
catch (e) {
|
|
1188
317
|
console.error(`Error open_project ${absPath}: ${e}`);
|
|
1189
|
-
return {
|
|
318
|
+
return {
|
|
319
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
320
|
+
isError: true
|
|
321
|
+
};
|
|
1190
322
|
}
|
|
1191
323
|
}));
|
|
1192
|
-
server.tool("create_project",
|
|
1193
|
-
"Creates a new CODESYS project from the standard template.", // Tool Description
|
|
1194
|
-
{
|
|
324
|
+
server.tool("create_project", "Creates a new CODESYS project from the standard template.", {
|
|
1195
325
|
filePath: zod_1.z.string().describe("Path where the new project file should be created (e.g., 'C:/Projects/NewPLC.project' or '/Users/user/projects/new_project.project').")
|
|
1196
326
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1197
327
|
const { filePath } = args;
|
|
@@ -1199,7 +329,7 @@ except Exception as e:
|
|
|
1199
329
|
console.error(`Tool call: create_project (copy template): ${absPath}`);
|
|
1200
330
|
let templatePath = "";
|
|
1201
331
|
try {
|
|
1202
|
-
// Template finding logic
|
|
332
|
+
// Template finding logic
|
|
1203
333
|
const baseDir = path.dirname(path.dirname(codesysExePath));
|
|
1204
334
|
templatePath = path.normalize(path.join(baseDir, 'Templates', 'Standard.project'));
|
|
1205
335
|
if (!(yield fileExists(templatePath))) {
|
|
@@ -1226,52 +356,74 @@ except Exception as e:
|
|
|
1226
356
|
else {
|
|
1227
357
|
console.error(`DEBUG: Found template relative to exe: ${templatePath}`);
|
|
1228
358
|
}
|
|
1229
|
-
// *** END TEMPLATE FINDING ***
|
|
1230
359
|
}
|
|
1231
360
|
catch (e) {
|
|
1232
361
|
console.error(`Template Error: ${e.message}`);
|
|
1233
|
-
return {
|
|
362
|
+
return {
|
|
363
|
+
content: [{ type: "text", text: `Template Error: ${e.message}` }],
|
|
364
|
+
isError: true
|
|
365
|
+
};
|
|
1234
366
|
}
|
|
1235
367
|
try {
|
|
1236
|
-
|
|
1237
|
-
const
|
|
1238
|
-
const
|
|
1239
|
-
.replace(
|
|
1240
|
-
.replace(
|
|
1241
|
-
|
|
1242
|
-
|
|
368
|
+
// Load script template and interpolate parameters
|
|
369
|
+
const scriptTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('create_project');
|
|
370
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(scriptTemplate, {
|
|
371
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\'),
|
|
372
|
+
'TEMPLATE_PROJECT_PATH': templatePath.replace(/\\/g, '\\\\')
|
|
373
|
+
});
|
|
374
|
+
console.error(">>> create_project (copy-then-open): PREPARED SCRIPT:", scriptContent.substring(0, 500) + "...");
|
|
375
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1243
376
|
console.error(">>> create_project (copy-then-open): EXECUTION RESULT:", JSON.stringify(result));
|
|
1244
377
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1245
|
-
return {
|
|
378
|
+
return {
|
|
379
|
+
content: [{
|
|
380
|
+
type: "text",
|
|
381
|
+
text: success ? `Project created from template: ${absPath}` : `Failed create project ${absPath} from template. Output:\n${result.output}`
|
|
382
|
+
}],
|
|
383
|
+
isError: !success
|
|
384
|
+
};
|
|
1246
385
|
}
|
|
1247
386
|
catch (e) {
|
|
1248
387
|
console.error(`Error create_project ${absPath}: ${e}`);
|
|
1249
|
-
return {
|
|
388
|
+
return {
|
|
389
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
390
|
+
isError: true
|
|
391
|
+
};
|
|
1250
392
|
}
|
|
1251
393
|
}));
|
|
1252
|
-
server.tool("save_project",
|
|
1253
|
-
"Saves the currently open CODESYS project.", // Tool Description
|
|
1254
|
-
{
|
|
394
|
+
server.tool("save_project", "Saves the currently open CODESYS project.", {
|
|
1255
395
|
projectFilePath: zod_1.z.string().describe("Path to the project file to ensure is open before saving (e.g., 'C:/Projects/MyPLC.project').")
|
|
1256
396
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1257
397
|
const { projectFilePath } = args;
|
|
1258
398
|
let absPath = path.normalize(path.isAbsolute(projectFilePath) ? projectFilePath : path.join(WORKSPACE_DIR, projectFilePath));
|
|
1259
399
|
console.error(`Tool call: save_project: ${absPath}`);
|
|
1260
400
|
try {
|
|
1261
|
-
|
|
1262
|
-
const
|
|
1263
|
-
const
|
|
401
|
+
// Load and combine script templates
|
|
402
|
+
const saveProjectTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('save_project');
|
|
403
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
404
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + saveProjectTemplate;
|
|
405
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
406
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\')
|
|
407
|
+
});
|
|
408
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1264
409
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1265
|
-
return {
|
|
410
|
+
return {
|
|
411
|
+
content: [{
|
|
412
|
+
type: "text",
|
|
413
|
+
text: success ? `Project saved: ${absPath}` : `Failed save project ${absPath}. Output:\n${result.output}`
|
|
414
|
+
}],
|
|
415
|
+
isError: !success
|
|
416
|
+
};
|
|
1266
417
|
}
|
|
1267
418
|
catch (e) {
|
|
1268
419
|
console.error(`Error save_project ${absPath}: ${e}`);
|
|
1269
|
-
return {
|
|
420
|
+
return {
|
|
421
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
422
|
+
isError: true
|
|
423
|
+
};
|
|
1270
424
|
}
|
|
1271
425
|
}));
|
|
1272
|
-
server.tool("create_pou",
|
|
1273
|
-
"Creates a new Program, Function Block, or Function POU within the specified CODESYS project.", // Tool Description
|
|
1274
|
-
{
|
|
426
|
+
server.tool("create_pou", "Creates a new Program, Function Block, or Function POU within the specified CODESYS project.", {
|
|
1275
427
|
projectFilePath: zod_1.z.string().describe("Path to the project file (e.g., 'C:/Projects/MyPLC.project')."),
|
|
1276
428
|
name: zod_1.z.string().describe("Name for the new POU (must be a valid IEC identifier)."),
|
|
1277
429
|
type: PouTypeEnum.describe("Type of POU (Program, FunctionBlock, Function)."),
|
|
@@ -1284,26 +436,39 @@ except Exception as e:
|
|
|
1284
436
|
const sanName = name.trim();
|
|
1285
437
|
console.error(`Tool call: create_pou: Name='${sanName}', Type='${type}', Lang='${language}', Parent='${sanParentPath}', Project='${absPath}'`);
|
|
1286
438
|
try {
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
439
|
+
// Load and combine script templates
|
|
440
|
+
const createPouTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('create_pou');
|
|
441
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
442
|
+
const findObjectByPathTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('find_object_by_path');
|
|
443
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + findObjectByPathTemplate + '\n\n' + createPouTemplate;
|
|
444
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
445
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\'),
|
|
446
|
+
'POU_NAME': sanName,
|
|
447
|
+
'POU_TYPE_STR': type,
|
|
448
|
+
'IMPL_LANGUAGE_STR': language,
|
|
449
|
+
'PARENT_PATH': sanParentPath
|
|
450
|
+
});
|
|
451
|
+
console.error(">>> create_pou: PREPARED SCRIPT:", scriptContent.substring(0, 500) + "...");
|
|
452
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1295
453
|
console.error(">>> create_pou: EXECUTION RESULT:", JSON.stringify(result));
|
|
1296
454
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1297
|
-
return {
|
|
455
|
+
return {
|
|
456
|
+
content: [{
|
|
457
|
+
type: "text",
|
|
458
|
+
text: success ? `POU '${sanName}' created in '${sanParentPath}' of ${absPath}. Project saved.` : `Failed create POU '${sanName}'. Output:\n${result.output}`
|
|
459
|
+
}],
|
|
460
|
+
isError: !success
|
|
461
|
+
};
|
|
1298
462
|
}
|
|
1299
463
|
catch (e) {
|
|
1300
464
|
console.error(`Error create_pou ${sanName} in ${absPath}: ${e}`);
|
|
1301
|
-
return {
|
|
465
|
+
return {
|
|
466
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
467
|
+
isError: true
|
|
468
|
+
};
|
|
1302
469
|
}
|
|
1303
470
|
}));
|
|
1304
|
-
server.tool("set_pou_code",
|
|
1305
|
-
"Sets the declaration and/or implementation code for a specific POU, Method, or Property.", // Tool Description
|
|
1306
|
-
{
|
|
471
|
+
server.tool("set_pou_code", "Sets the declaration and/or implementation code for a specific POU, Method, or Property.", {
|
|
1307
472
|
projectFilePath: zod_1.z.string().describe("Path to the project file (e.g., 'C:/Projects/MyPLC.project')."),
|
|
1308
473
|
pouPath: zod_1.z.string().describe("Full relative path to the target object (e.g., 'Application/MyPOU', 'MyFolder/MyFB/MyMethod', 'MyFolder/MyFB/MyProperty')."),
|
|
1309
474
|
declarationCode: zod_1.z.string().describe("Code for the declaration part (VAR...END_VAR). If omitted, the declaration is not changed.").optional(),
|
|
@@ -1311,36 +476,51 @@ except Exception as e:
|
|
|
1311
476
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1312
477
|
const { projectFilePath, pouPath, declarationCode, implementationCode } = args;
|
|
1313
478
|
if (declarationCode === undefined && implementationCode === undefined) {
|
|
1314
|
-
return {
|
|
479
|
+
return {
|
|
480
|
+
content: [{ type: "text", text: "Error: At least one of declarationCode or implementationCode must be provided." }],
|
|
481
|
+
isError: true
|
|
482
|
+
};
|
|
1315
483
|
}
|
|
1316
484
|
let absPath = path.normalize(path.isAbsolute(projectFilePath) ? projectFilePath : path.join(WORKSPACE_DIR, projectFilePath));
|
|
1317
485
|
const sanPouPath = pouPath.replace(/\\/g, '/').replace(/^\/+|\/+$/g, '');
|
|
1318
486
|
console.error(`Tool call: set_pou_code: Target='${sanPouPath}', Project='${absPath}'`);
|
|
1319
487
|
try {
|
|
1320
|
-
|
|
488
|
+
// Load and combine script templates
|
|
489
|
+
const setPouCodeTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('set_pou_code');
|
|
490
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
491
|
+
const findObjectByPathTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('find_object_by_path');
|
|
492
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + findObjectByPathTemplate + '\n\n' + setPouCodeTemplate;
|
|
1321
493
|
// Escape content for Python triple-quoted strings
|
|
1322
494
|
const sanDeclCode = (declarationCode !== null && declarationCode !== void 0 ? declarationCode : "").replace(/\\/g, '\\\\').replace(/"""/g, '\\"\\"\\"');
|
|
1323
495
|
const sanImplCode = (implementationCode !== null && implementationCode !== void 0 ? implementationCode : "").replace(/\\/g, '\\\\').replace(/"""/g, '\\"\\"\\"');
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
496
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
497
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\'),
|
|
498
|
+
'POU_FULL_PATH': sanPouPath,
|
|
499
|
+
'DECLARATION_CONTENT': sanDeclCode,
|
|
500
|
+
'IMPLEMENTATION_CONTENT': sanImplCode
|
|
501
|
+
});
|
|
502
|
+
console.error(">>> set_pou_code: PREPARED SCRIPT:", scriptContent.substring(0, 500) + "...");
|
|
503
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1330
504
|
console.error(">>> set_pou_code: EXECUTION RESULT:", JSON.stringify(result));
|
|
1331
505
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1332
|
-
return {
|
|
506
|
+
return {
|
|
507
|
+
content: [{
|
|
508
|
+
type: "text",
|
|
509
|
+
text: success ? `Code set for '${sanPouPath}' in ${absPath}. Project saved.` : `Failed set code for '${sanPouPath}'. Output:\n${result.output}`
|
|
510
|
+
}],
|
|
511
|
+
isError: !success
|
|
512
|
+
};
|
|
1333
513
|
}
|
|
1334
514
|
catch (e) {
|
|
1335
515
|
console.error(`Error set_pou_code ${sanPouPath} in ${absPath}: ${e}`);
|
|
1336
|
-
return {
|
|
516
|
+
return {
|
|
517
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
518
|
+
isError: true
|
|
519
|
+
};
|
|
1337
520
|
}
|
|
1338
521
|
}));
|
|
1339
|
-
server.tool("create_property",
|
|
1340
|
-
"Creates a new Property within a specific Function Block POU.", // Tool Description
|
|
1341
|
-
{
|
|
522
|
+
server.tool("create_property", "Creates a new Property within a specific Function Block POU.", {
|
|
1342
523
|
projectFilePath: zod_1.z.string().describe("Path to the project file (e.g., 'C:/Projects/MyPLC.project')."),
|
|
1343
|
-
// Assuming properties can only be added to FBs in standard CODESYS scripting
|
|
1344
524
|
parentPouPath: zod_1.z.string().describe("Relative path to the parent Function Block POU (e.g., 'Application/MyFB')."),
|
|
1345
525
|
propertyName: zod_1.z.string().describe("Name for the new property (must be a valid IEC identifier)."),
|
|
1346
526
|
propertyType: zod_1.z.string().describe("Data type of the property (e.g., 'BOOL', 'INT', 'MyDUT').")
|
|
@@ -1352,36 +532,48 @@ except Exception as e:
|
|
|
1352
532
|
const sanPropType = propertyType.trim();
|
|
1353
533
|
console.error(`Tool call: create_property: Name='${sanPropName}', Type='${sanPropType}', ParentPOU='${sanParentPath}', Project='${absPath}'`);
|
|
1354
534
|
if (!sanPropName || !sanPropType) {
|
|
1355
|
-
return {
|
|
535
|
+
return {
|
|
536
|
+
content: [{ type: "text", text: `Error: Property name and type cannot be empty.` }],
|
|
537
|
+
isError: true
|
|
538
|
+
};
|
|
1356
539
|
}
|
|
1357
540
|
try {
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
541
|
+
// Load and combine script templates
|
|
542
|
+
const createPropertyTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('create_property');
|
|
543
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
544
|
+
const findObjectByPathTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('find_object_by_path');
|
|
545
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + findObjectByPathTemplate + '\n\n' + createPropertyTemplate;
|
|
546
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
547
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\'),
|
|
548
|
+
'PARENT_POU_FULL_PATH': sanParentPath,
|
|
549
|
+
'PROPERTY_NAME': sanPropName,
|
|
550
|
+
'PROPERTY_TYPE': sanPropType
|
|
551
|
+
});
|
|
552
|
+
console.error(">>> create_property: PREPARED SCRIPT:", scriptContent.substring(0, 500) + "...");
|
|
553
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1365
554
|
console.error(">>> create_property: EXECUTION RESULT:", JSON.stringify(result));
|
|
1366
555
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1367
556
|
return {
|
|
1368
|
-
content: [{
|
|
557
|
+
content: [{
|
|
558
|
+
type: "text",
|
|
559
|
+
text: success ? `Property '${sanPropName}' created under '${sanParentPath}' in ${absPath}. Project saved.` : `Failed to create property '${sanPropName}'. Output:\n${result.output}`
|
|
560
|
+
}],
|
|
1369
561
|
isError: !success
|
|
1370
562
|
};
|
|
1371
563
|
}
|
|
1372
564
|
catch (e) {
|
|
1373
565
|
console.error(`Error create_property ${sanPropName} in ${absPath}: ${e}`);
|
|
1374
|
-
return {
|
|
566
|
+
return {
|
|
567
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
568
|
+
isError: true
|
|
569
|
+
};
|
|
1375
570
|
}
|
|
1376
571
|
}));
|
|
1377
|
-
server.tool("create_method",
|
|
1378
|
-
"Creates a new Method within a specific Function Block POU.", // Tool Description
|
|
1379
|
-
{
|
|
572
|
+
server.tool("create_method", "Creates a new Method within a specific Function Block POU.", {
|
|
1380
573
|
projectFilePath: zod_1.z.string().describe("Path to the project file (e.g., 'C:/Projects/MyPLC.project')."),
|
|
1381
|
-
// Assuming methods can typically only be added to FBs in standard CODESYS scripting
|
|
1382
574
|
parentPouPath: zod_1.z.string().describe("Relative path to the parent Function Block POU (e.g., 'Application/MyFB')."),
|
|
1383
575
|
methodName: zod_1.z.string().describe("Name of the new method (must be a valid IEC identifier)."),
|
|
1384
|
-
returnType: zod_1.z.string().optional().describe("Return type (e.g., 'BOOL', 'INT'). Leave empty or omit for no return value (PROCEDURE).")
|
|
576
|
+
returnType: zod_1.z.string().optional().describe("Return type (e.g., 'BOOL', 'INT'). Leave empty or omit for no return value (PROCEDURE).")
|
|
1385
577
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1386
578
|
const { projectFilePath, parentPouPath, methodName, returnType } = args;
|
|
1387
579
|
let absPath = path.normalize(path.isAbsolute(projectFilePath) ? projectFilePath : path.join(WORKSPACE_DIR, projectFilePath));
|
|
@@ -1390,40 +582,58 @@ except Exception as e:
|
|
|
1390
582
|
const sanReturnType = (returnType !== null && returnType !== void 0 ? returnType : "").trim();
|
|
1391
583
|
console.error(`Tool call: create_method: Name='${sanMethName}', Return='${sanReturnType}', ParentPOU='${sanParentPath}', Project='${absPath}'`);
|
|
1392
584
|
if (!sanMethName) {
|
|
1393
|
-
return {
|
|
585
|
+
return {
|
|
586
|
+
content: [{ type: "text", text: `Error: Method name cannot be empty.` }],
|
|
587
|
+
isError: true
|
|
588
|
+
};
|
|
1394
589
|
}
|
|
1395
590
|
try {
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
591
|
+
// Load and combine script templates
|
|
592
|
+
const createMethodTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('create_method');
|
|
593
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
594
|
+
const findObjectByPathTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('find_object_by_path');
|
|
595
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + findObjectByPathTemplate + '\n\n' + createMethodTemplate;
|
|
596
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
597
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\'),
|
|
598
|
+
'PARENT_POU_FULL_PATH': sanParentPath,
|
|
599
|
+
'METHOD_NAME': sanMethName,
|
|
600
|
+
'RETURN_TYPE': sanReturnType
|
|
601
|
+
});
|
|
602
|
+
console.error(">>> create_method: PREPARED SCRIPT:", scriptContent.substring(0, 500) + "...");
|
|
603
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1403
604
|
console.error(">>> create_method: EXECUTION RESULT:", JSON.stringify(result));
|
|
1404
605
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1405
606
|
return {
|
|
1406
|
-
content: [{
|
|
607
|
+
content: [{
|
|
608
|
+
type: "text",
|
|
609
|
+
text: success ? `Method '${sanMethName}' created under '${sanParentPath}' in ${absPath}. Project saved.` : `Failed to create method '${sanMethName}'. Output:\n${result.output}`
|
|
610
|
+
}],
|
|
1407
611
|
isError: !success
|
|
1408
612
|
};
|
|
1409
613
|
}
|
|
1410
614
|
catch (e) {
|
|
1411
615
|
console.error(`Error create_method ${sanMethName} in ${absPath}: ${e}`);
|
|
1412
|
-
return {
|
|
616
|
+
return {
|
|
617
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
618
|
+
isError: true
|
|
619
|
+
};
|
|
1413
620
|
}
|
|
1414
621
|
}));
|
|
1415
|
-
server.tool("compile_project",
|
|
1416
|
-
"Compiles (Builds) the primary application within a CODESYS project.", // Tool Description
|
|
1417
|
-
{
|
|
622
|
+
server.tool("compile_project", "Compiles (Builds) the primary application within a CODESYS project.", {
|
|
1418
623
|
projectFilePath: zod_1.z.string().describe("Path to the project file containing the application to compile (e.g., 'C:/Projects/MyPLC.project').")
|
|
1419
624
|
}, (args) => __awaiter(this, void 0, void 0, function* () {
|
|
1420
625
|
const { projectFilePath } = args;
|
|
1421
626
|
let absPath = path.normalize(path.isAbsolute(projectFilePath) ? projectFilePath : path.join(WORKSPACE_DIR, projectFilePath));
|
|
1422
627
|
console.error(`Tool call: compile_project: ${absPath}`);
|
|
1423
628
|
try {
|
|
1424
|
-
|
|
1425
|
-
const
|
|
1426
|
-
const
|
|
629
|
+
// Load and combine script templates
|
|
630
|
+
const compileProjectTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('compile_project');
|
|
631
|
+
const ensureProjectOpenTemplate = yield (0, scriptLoader_1.loadScriptTemplate)('ensure_project_open');
|
|
632
|
+
const combinedScript = ensureProjectOpenTemplate + '\n\n' + compileProjectTemplate;
|
|
633
|
+
const scriptContent = (0, scriptLoader_1.interpolateScript)(combinedScript, {
|
|
634
|
+
'PROJECT_FILE_PATH': absPath.replace(/\\/g, '\\\\')
|
|
635
|
+
});
|
|
636
|
+
const result = yield (0, codesys_interop_1.executeCodesysScript)(scriptContent, codesysExePath, codesysProfileName);
|
|
1427
637
|
const success = result.success && result.output.includes("SCRIPT_SUCCESS");
|
|
1428
638
|
// Check for actual compile errors in the output log
|
|
1429
639
|
const hasCompileErrors = result.output.includes("Compile complete --") && !/ 0 error\(s\),/.test(result.output);
|
|
@@ -1435,11 +645,17 @@ except Exception as e:
|
|
|
1435
645
|
// Report as error if compile fails, even if script technically succeeded
|
|
1436
646
|
isError = true;
|
|
1437
647
|
}
|
|
1438
|
-
return {
|
|
648
|
+
return {
|
|
649
|
+
content: [{ type: "text", text: message }],
|
|
650
|
+
isError: isError
|
|
651
|
+
};
|
|
1439
652
|
}
|
|
1440
653
|
catch (e) {
|
|
1441
654
|
console.error(`Error compile_project ${absPath}: ${e}`);
|
|
1442
|
-
return {
|
|
655
|
+
return {
|
|
656
|
+
content: [{ type: "text", text: `Error: ${e.message}` }],
|
|
657
|
+
isError: true
|
|
658
|
+
};
|
|
1443
659
|
}
|
|
1444
660
|
}));
|
|
1445
661
|
// --- End Tools ---
|
|
@@ -1451,16 +667,13 @@ except Exception as e:
|
|
|
1451
667
|
const transport = new stdio_js_1.StdioServerTransport();
|
|
1452
668
|
console.error("SERVER.TS: Connecting MCP server via stdio...");
|
|
1453
669
|
// No need to await connect here if startMcpServer is called by bin.ts which awaits it
|
|
1454
|
-
// await server.connect(transport);
|
|
1455
670
|
server.connect(transport); // Connect but don't await here, let bin.ts handle waiting
|
|
1456
671
|
console.error("SERVER.TS: MCP Server connection initiated via stdio.");
|
|
1457
|
-
// console.error("SERVER.TS: server.connect() promise resolved successfully."); // This log might be premature now
|
|
1458
672
|
}
|
|
1459
673
|
catch (error) {
|
|
1460
674
|
console.error("FATAL: Failed to initiate MCP server connection:", error);
|
|
1461
675
|
// Re-throw error so bin.ts can catch it
|
|
1462
676
|
throw error;
|
|
1463
|
-
// process.exit(1);
|
|
1464
677
|
}
|
|
1465
678
|
// --- End Server Connection ---
|
|
1466
679
|
});
|