@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
|
@@ -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
|
+
}
|