@codesys/mcp-toolkit 1.1.3 → 1.1.5

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/bin.js CHANGED
@@ -8,11 +8,45 @@
8
8
  * This is the entry point for the MCP toolkit when invoked as a command-line tool.
9
9
  * It handles argument parsing and passes configuration to server.ts via function parameters.
10
10
  */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
23
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
24
+ }) : function(o, v) {
25
+ o["default"] = v;
26
+ });
27
+ var __importStar = (this && this.__importStar) || (function () {
28
+ var ownKeys = function(o) {
29
+ ownKeys = Object.getOwnPropertyNames || function (o) {
30
+ var ar = [];
31
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
32
+ return ar;
33
+ };
34
+ return ownKeys(o);
35
+ };
36
+ return function (mod) {
37
+ if (mod && mod.__esModule) return mod;
38
+ var result = {};
39
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
40
+ __setModuleDefault(result, mod);
41
+ return result;
42
+ };
43
+ })();
11
44
  Object.defineProperty(exports, "__esModule", { value: true });
12
45
  // Use Commander for initial parsing
13
46
  const commander_1 = require("commander");
14
47
  // Import the server setup function
15
48
  const server_js_1 = require("./server.js"); // Adjust path if needed
49
+ const path = __importStar(require("path")); // Import path module
16
50
  console.error(">>> BIN.TS Starting <<<");
17
51
  // Define version from package.json
18
52
  // Use import assertion for JSON in ES modules if possible, otherwise require is okay for CommonJS
@@ -35,9 +69,12 @@ commander_1.program
35
69
  .option('-f, --codesys-profile <profile>', // Changed alias to 'f' to avoid conflict if you add other '-p' options
36
70
  'CODESYS profile name (overrides CODESYS_PROFILE env var)', process.env.CODESYS_PROFILE || 'CODESYS V3.5 SP21' // Default AFTER reading env var
37
71
  )
38
- // Add workspace option if needed, defaults to cwd
39
72
  .option('-w, --workspace <dir>', 'Workspace directory for relative project paths', process.cwd() // Default to current working directory
40
73
  )
74
+ // Add option for the CODESYS scripts directory
75
+ .option('-s, --codesys-scripts-dir <dir>', 'Directory containing CODESYS Python scripts (e.g., listener_script.py)',
76
+ // Default to a 'codesys_scripts' directory relative to the bin.ts file
77
+ path.join(__dirname, '..', 'codesys_scripts'))
41
78
  .parse(process.argv); // Parse the arguments
42
79
  const options = commander_1.program.opts();
43
80
  // --- Log the options Commander parsed ---
@@ -47,6 +84,7 @@ console.error(`BIN.TS Options: ${JSON.stringify(options)}`);
47
84
  console.error(` CODESYS Path Used: ${options.codesysPath}`);
48
85
  console.error(` CODESYS Profile Used: ${options.codesysProfile}`);
49
86
  console.error(` Workspace Used: ${options.workspace}`);
87
+ console.error(` CODESYS Scripts Dir Used: ${options.codesysScriptsDir}`); // Log the new option
50
88
  console.error(` Node.js: ${process.version}`);
51
89
  console.error(`========================================`);
52
90
  // --- End Logging ---
@@ -54,7 +92,8 @@ console.error(`========================================`);
54
92
  const serverConfig = {
55
93
  codesysPath: options.codesysPath.trim(),
56
94
  profileName: options.codesysProfile.trim(),
57
- workspaceDir: options.workspace.trim() // Pass workspace dir
95
+ workspaceDir: options.workspace.trim(), // Pass workspace dir
96
+ codesysScriptsDir: options.codesysScriptsDir.trim() // Pass the scripts directory
58
97
  };
59
98
  // --- End Prepare Config ---
60
99
  // --- Call the server setup function ---
@@ -0,0 +1,158 @@
1
+ "use strict";
2
+ /**
3
+ * Script Loader Module
4
+ * Centralizes loading and management of Python script templates
5
+ *
6
+ * This module:
7
+ * - Loads script templates from the /scripts directory
8
+ * - Provides script template interpolation functions
9
+ * - Manages script parameter substitution
10
+ */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
23
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
24
+ }) : function(o, v) {
25
+ o["default"] = v;
26
+ });
27
+ var __importStar = (this && this.__importStar) || (function () {
28
+ var ownKeys = function(o) {
29
+ ownKeys = Object.getOwnPropertyNames || function (o) {
30
+ var ar = [];
31
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
32
+ return ar;
33
+ };
34
+ return ownKeys(o);
35
+ };
36
+ return function (mod) {
37
+ if (mod && mod.__esModule) return mod;
38
+ var result = {};
39
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
40
+ __setModuleDefault(result, mod);
41
+ return result;
42
+ };
43
+ })();
44
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
45
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
46
+ return new (P || (P = Promise))(function (resolve, reject) {
47
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
48
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
49
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
50
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
51
+ });
52
+ };
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.loadScriptTemplate = loadScriptTemplate;
55
+ exports.loadScriptTemplateSync = loadScriptTemplateSync;
56
+ exports.interpolateScript = interpolateScript;
57
+ exports.combineScripts = combineScripts;
58
+ exports.preloadAllScriptTemplates = preloadAllScriptTemplates;
59
+ const fs = __importStar(require("fs"));
60
+ const path = __importStar(require("path"));
61
+ const util_1 = require("util");
62
+ const readFileAsync = (0, util_1.promisify)(fs.readFile);
63
+ const SCRIPTS_DIR = path.join(__dirname, 'scripts');
64
+ // Cache for loaded scripts to avoid repeated file system access
65
+ const scriptCache = {};
66
+ /**
67
+ * Loads a script template from the scripts directory
68
+ * @param scriptName The name of the script file (without .py extension)
69
+ * @returns The script content as a string
70
+ */
71
+ function loadScriptTemplate(scriptName) {
72
+ return __awaiter(this, void 0, void 0, function* () {
73
+ // Add .py extension if not provided
74
+ const fileName = scriptName.endsWith('.py') ? scriptName : `${scriptName}.py`;
75
+ const filePath = path.join(SCRIPTS_DIR, fileName);
76
+ // Return from cache if available
77
+ if (scriptCache[fileName]) {
78
+ return scriptCache[fileName];
79
+ }
80
+ try {
81
+ const content = yield readFileAsync(filePath, 'utf8');
82
+ // Store in cache for future use
83
+ scriptCache[fileName] = content;
84
+ return content;
85
+ }
86
+ catch (error) {
87
+ throw new Error(`Failed to load script template '${fileName}': ${error.message}`);
88
+ }
89
+ });
90
+ }
91
+ /**
92
+ * Loads a script template synchronously (for use during startup)
93
+ * @param scriptName The name of the script file (without .py extension)
94
+ * @returns The script content as a string
95
+ */
96
+ function loadScriptTemplateSync(scriptName) {
97
+ // Add .py extension if not provided
98
+ const fileName = scriptName.endsWith('.py') ? scriptName : `${scriptName}.py`;
99
+ const filePath = path.join(SCRIPTS_DIR, fileName);
100
+ // Return from cache if available
101
+ if (scriptCache[fileName]) {
102
+ return scriptCache[fileName];
103
+ }
104
+ try {
105
+ const content = fs.readFileSync(filePath, 'utf8');
106
+ // Store in cache for future use
107
+ scriptCache[fileName] = content;
108
+ return content;
109
+ }
110
+ catch (error) {
111
+ throw new Error(`Failed to load script template '${fileName}': ${error.message}`);
112
+ }
113
+ }
114
+ /**
115
+ * Interpolates parameters into a script template
116
+ * @param template The script template string
117
+ * @param params Object containing parameter names and values
118
+ * @returns The script with parameter values substituted
119
+ */
120
+ function interpolateScript(template, params) {
121
+ let result = template;
122
+ // Replace each parameter in the template
123
+ for (const [key, value] of Object.entries(params)) {
124
+ // Convert value to string and escape backslashes for Python
125
+ const escapedValue = String(value).replace(/\\/g, '\\\\');
126
+ // Replace {PARAM_NAME} with the escaped value
127
+ const paramPattern = new RegExp(`\\{${key}\\}`, 'g');
128
+ result = result.replace(paramPattern, escapedValue);
129
+ }
130
+ return result;
131
+ }
132
+ /**
133
+ * Combines multiple script fragments with proper indentation
134
+ * @param scripts Object containing named script fragments
135
+ * @returns Combined script content
136
+ */
137
+ function combineScripts(scripts) {
138
+ return Object.values(scripts).join('\n\n');
139
+ }
140
+ /**
141
+ * Load all script templates from the scripts directory
142
+ * This is useful during development to preload the cache
143
+ */
144
+ function preloadAllScriptTemplates() {
145
+ return __awaiter(this, void 0, void 0, function* () {
146
+ try {
147
+ const files = fs.readdirSync(SCRIPTS_DIR);
148
+ for (const file of files) {
149
+ if (file.endsWith('.py')) {
150
+ yield loadScriptTemplate(file);
151
+ }
152
+ }
153
+ }
154
+ catch (error) {
155
+ console.error(`Failed to preload script templates: ${error.message}`);
156
+ }
157
+ });
158
+ }