@codesys/mcp-toolkit 1.0.4 → 1.0.6

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.
@@ -81,51 +81,41 @@ function executeCodesysScript(scriptContent, codesysExePath, codesysProfileName)
81
81
  process.stderr.write(`Temporary script written to: ${tempFilePath} with latin1 encoding and LF line endings\n`); // Use process.stderr
82
82
  // Add --additionalfolder argument
83
83
  const additionalFolderPath = "C:\\Program Files\\CODESYS 3.5.21.0\\CODESYS\\AdditionalFolders\\Default"; // Assuming Default instance
84
- // Fix: Use shell:false and pass command and arguments separately to properly handle paths with spaces
85
- const command = codesysExePath;
86
- const args = [
87
- `--profile=${codesysProfileName}`,
88
- '--noUI',
89
- `--runscript=${tempFilePath}`
90
- ];
91
- process.stderr.write(`Executing: ${command} ${args.join(' ')}\n`); // Log the command
92
- // Spawn the CODESYS process using shell:false for proper path handling with spaces
93
- const codesysProcess = (0, child_process_1.spawn)(command, args, {
94
- stdio: ['ignore', 'pipe', 'pipe'], // Ignore stdin, pipe stdout and stderr
95
- shell: false // Don't use shell to avoid issues with paths containing spaces
96
- });
97
- // Capture stdout
98
- codesysProcess.stdout.on('data', (data) => {
99
- const chunk = data.toString();
100
- // console.error('CODESYS stdout:', chunk); // Optional: Original debug log
101
- output += chunk;
102
- });
103
- // Capture stderr
104
- codesysProcess.stderr.on('data', (data) => {
105
- const chunk = data.toString();
106
- process.stderr.write(`CODESYS stderr: ${chunk}\n`); // Use process.stderr
107
- output += chunk; // Include stderr in the output for debugging
108
- });
109
- // Wait for the process to exit with a timeout
84
+ // Fix: Use shell:true but ensure proper quoting for paths with spaces
85
+ const commandWithArgs = `"${codesysExePath}" --profile="${codesysProfileName}" --noUI --runscript="${tempFilePath}"`;
86
+ process.stderr.write(`Executing: ${commandWithArgs}\n`); // Log the command
87
+ // Use exec instead of spawn - more reliable for command line processing
110
88
  const exitCode = yield new Promise((resolve, reject) => {
111
- // Add timeout handling
112
89
  const timeoutMs = 30000; // 30 seconds
113
- const timeout = setTimeout(() => {
90
+ let timeoutHandle = null;
91
+ // Start the command using exec
92
+ const childProcess = (0, child_process_1.exec)(commandWithArgs, (error, stdout, stderr) => {
93
+ var _a;
94
+ if (timeoutHandle)
95
+ clearTimeout(timeoutHandle);
96
+ // Save all output
97
+ if (stdout)
98
+ output += stdout;
99
+ if (stderr) {
100
+ process.stderr.write(`CODESYS stderr: ${stderr}\n`);
101
+ output += stderr;
102
+ }
103
+ if (error) {
104
+ // Command failed
105
+ process.stderr.write(`CODESYS process error: ${error.message}\n`);
106
+ resolve((_a = error.code) !== null && _a !== void 0 ? _a : 1); // Return error code
107
+ }
108
+ else {
109
+ // Command succeeded
110
+ resolve(0);
111
+ }
112
+ });
113
+ // Set a timeout
114
+ timeoutHandle = setTimeout(() => {
114
115
  process.stderr.write(`TIMEOUT: CODESYS process did not complete in ${timeoutMs}ms\n`);
115
- // Force resolve with 0 to proceed with parsing output collected so far
116
- codesysProcess.kill();
117
- resolve(0);
116
+ childProcess.kill();
117
+ resolve(0); // Continue processing with what we have
118
118
  }, timeoutMs);
119
- codesysProcess.on('close', (code) => {
120
- clearTimeout(timeout);
121
- resolve(code !== null && code !== void 0 ? code : 0);
122
- });
123
- codesysProcess.on('error', (err) => {
124
- clearTimeout(timeout);
125
- // Error starting the process itself (e.g., command not found)
126
- process.stderr.write(`Failed to start CODESYS process: ${err.message}\n`); // Use process.stderr
127
- reject(err);
128
- });
129
119
  });
130
120
  process.stderr.write(`CODESYS process exited with code: ${exitCode}\n`); // Use process.stderr
131
121
  // Determine success based on exit code and output markers
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codesys/mcp-toolkit",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "main": "dist/server.js",
5
5
  "bin": {
6
6
  "codesys-mcp-toolkit": "./dist/bin.js"
package/test-direct.js ADDED
@@ -0,0 +1,40 @@
1
+ const { spawn } = require('child_process');
2
+
3
+ // Paths
4
+ const codesysPath = "C:\\Program Files\\CODESYS 3.5.21.0\\CODESYS\\Common\\CODESYS.exe";
5
+ const profileName = "CODESYS V3.5 SP21";
6
+
7
+ // Log what we're doing
8
+ console.log(`Testing direct spawn of CODESYS\n`);
9
+
10
+ // Create a test Python script
11
+ const fs = require('fs');
12
+ const os = require('os');
13
+ const path = require('path');
14
+ const tempFile = path.join(os.tmpdir(), 'direct_test.py');
15
+ fs.writeFileSync(tempFile, 'print("DIRECT TEST SUCCESS")\n');
16
+
17
+ console.log(`Script file: ${tempFile}`);
18
+ console.log(`CODESYS path: ${codesysPath}`);
19
+
20
+ // Method 1: Using spawn with shell:true and proper quotes
21
+ const command = `"${codesysPath}" --profile="${profileName}" --noUI --runscript="${tempFile}"`;
22
+ console.log(`\nExecuting command: ${command}`);
23
+
24
+ try {
25
+ // Use exec instead of spawn for simpler testing
26
+ require('child_process').exec(command, (error, stdout, stderr) => {
27
+ console.log("\n--- RESULTS ---");
28
+ if (error) {
29
+ console.log(`Error: ${error.message}`);
30
+ return;
31
+ }
32
+ if (stderr) {
33
+ console.log(`stderr: ${stderr}`);
34
+ }
35
+ console.log(`stdout: ${stdout}`);
36
+ console.log("--- END RESULTS ---");
37
+ });
38
+ } catch (err) {
39
+ console.log(`Caught error: ${err.message}`);
40
+ }