@codesys/mcp-toolkit 1.0.9 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +83 -186
- package/dist/bin.js +54 -22
- package/dist/codesys_interop.js +115 -74
- package/dist/server.js +541 -1378
- package/package.json +13 -10
- package/direct-codesys-mcp.js +0 -150
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codesys/mcp-toolkit",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Model Context Protocol (MCP) server for CODESYS automation platform",
|
|
4
5
|
"main": "dist/server.js",
|
|
5
|
-
"bin":
|
|
6
|
+
"bin": {
|
|
7
|
+
"codesys-mcp-tool": "./dist/bin.js"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
10
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
8
11
|
"build": "tsc",
|
|
9
|
-
"prepare": "npm run build"
|
|
12
|
+
"prepare": "npm run build",
|
|
13
|
+
"start": "node ./dist/server.js"
|
|
10
14
|
},
|
|
11
15
|
"keywords": [
|
|
12
16
|
"codesys",
|
|
@@ -19,7 +23,6 @@
|
|
|
19
23
|
"author": "Your Name <your.email@example.com>",
|
|
20
24
|
"license": "MIT",
|
|
21
25
|
"type": "commonjs",
|
|
22
|
-
"description": "Model Context Protocol (MCP) server for CODESYS automation platform",
|
|
23
26
|
"repository": {
|
|
24
27
|
"type": "git",
|
|
25
28
|
"url": "git+https://github.com/johannesPettersson80/codesys-mcp-toolkit.git"
|
|
@@ -29,17 +32,17 @@
|
|
|
29
32
|
"url": "https://github.com/johannesPettersson80/codesys-mcp-toolkit/issues"
|
|
30
33
|
},
|
|
31
34
|
"engines": {
|
|
32
|
-
"node": ">=
|
|
35
|
+
"node": ">=18.0.0"
|
|
33
36
|
},
|
|
34
37
|
"dependencies": {
|
|
35
38
|
"@modelcontextprotocol/sdk": "^1.10.2",
|
|
36
39
|
"axios": "^1.6.8",
|
|
37
|
-
"
|
|
40
|
+
"yargs": "^17.7.2",
|
|
38
41
|
"zod": "^3.24.3"
|
|
39
42
|
},
|
|
40
43
|
"devDependencies": {
|
|
41
|
-
"@types/
|
|
42
|
-
"@types/
|
|
43
|
-
"typescript": "^5.
|
|
44
|
+
"@types/node": "^20.14.1",
|
|
45
|
+
"@types/yargs": "^17.0.33",
|
|
46
|
+
"typescript": "^5.5.3"
|
|
44
47
|
}
|
|
45
|
-
}
|
|
48
|
+
}
|
package/direct-codesys-mcp.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
const { exec } = require('child_process');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const fs = require('fs');
|
|
5
|
-
const os = require('os');
|
|
6
|
-
const { McpServer } = require('@modelcontextprotocol/sdk/server/mcp.js');
|
|
7
|
-
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
|
|
8
|
-
const { z } = require('zod');
|
|
9
|
-
|
|
10
|
-
console.error("Starting standalone CODESYS MCP server...");
|
|
11
|
-
|
|
12
|
-
// Create a simple MCP server
|
|
13
|
-
const server = new McpServer({
|
|
14
|
-
name: "Standalone CODESYS MCP Server",
|
|
15
|
-
version: "1.0.0",
|
|
16
|
-
capabilities: { resources: {}, tools: {} }
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// Direct implementation of executeCodesysScript
|
|
20
|
-
async function executeCodesysScript(scriptContent, codesysExePath, codesysProfileName) {
|
|
21
|
-
// Validate inputs
|
|
22
|
-
if (!codesysExePath) throw new Error('CODESYS executable path was not provided');
|
|
23
|
-
if (!codesysProfileName) throw new Error('CODESYS profile name was not provided');
|
|
24
|
-
|
|
25
|
-
console.error(`DIRECT: Using CODESYS Path: ${codesysExePath}`);
|
|
26
|
-
console.error(`DIRECT: Using CODESYS Profile: ${codesysProfileName}`);
|
|
27
|
-
|
|
28
|
-
// Create temp script file
|
|
29
|
-
const tempDir = os.tmpdir();
|
|
30
|
-
const tempFileName = `codesys_script_${Date.now()}_${Math.random().toString(36).slice(2, 9)}.py`;
|
|
31
|
-
const tempFilePath = path.join(tempDir, tempFileName);
|
|
32
|
-
|
|
33
|
-
let output = '';
|
|
34
|
-
let success = false;
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
// Write script to temp file
|
|
38
|
-
fs.writeFileSync(tempFilePath, scriptContent, 'latin1');
|
|
39
|
-
console.error(`DIRECT: Wrote script to: ${tempFilePath}`);
|
|
40
|
-
|
|
41
|
-
// Build command with proper quoting
|
|
42
|
-
const commandWithArgs = `"${codesysExePath}" --profile="${codesysProfileName}" --noUI --runscript="${tempFilePath}"`;
|
|
43
|
-
console.error(`DIRECT: Command: ${commandWithArgs}`);
|
|
44
|
-
|
|
45
|
-
// Execute using exec
|
|
46
|
-
success = await new Promise((resolve) => {
|
|
47
|
-
exec(commandWithArgs, (error, stdout, stderr) => {
|
|
48
|
-
if (stdout) output += stdout;
|
|
49
|
-
if (stderr) output += stderr;
|
|
50
|
-
|
|
51
|
-
console.error(`DIRECT: stdout: ${stdout.slice(0, 200)}${stdout.length > 200 ? '...' : ''}`);
|
|
52
|
-
if (stderr) console.error(`DIRECT: stderr: ${stderr}`);
|
|
53
|
-
|
|
54
|
-
if (error) {
|
|
55
|
-
console.error(`DIRECT: Error: ${error.message}`);
|
|
56
|
-
resolve(false);
|
|
57
|
-
} else {
|
|
58
|
-
resolve(true);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Check for success/error markers in output
|
|
64
|
-
if (output.includes('SCRIPT_SUCCESS')) {
|
|
65
|
-
success = true;
|
|
66
|
-
} else if (output.includes('SCRIPT_ERROR')) {
|
|
67
|
-
success = false;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
} catch (err) {
|
|
71
|
-
console.error(`DIRECT: Exception: ${err.message}`);
|
|
72
|
-
output += `\nError: ${err.message}`;
|
|
73
|
-
success = false;
|
|
74
|
-
} finally {
|
|
75
|
-
// Clean up temp file
|
|
76
|
-
try {
|
|
77
|
-
fs.unlinkSync(tempFilePath);
|
|
78
|
-
console.error(`DIRECT: Deleted temp file: ${tempFilePath}`);
|
|
79
|
-
} catch (err) {
|
|
80
|
-
console.error(`DIRECT: Failed to delete temp file: ${err.message}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return { success, output };
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Implement a basic check_status tool
|
|
88
|
-
server.tool("check_status", {}, async () => {
|
|
89
|
-
try {
|
|
90
|
-
const codesysPath = process.argv[2] || "C:\\Program Files\\CODESYS 3.5.21.0\\CODESYS\\Common\\CODESYS.exe";
|
|
91
|
-
const profileName = process.argv[3] || "CODESYS V3.5 SP21";
|
|
92
|
-
|
|
93
|
-
console.error(`DIRECT: Check Status with ${codesysPath} and ${profileName}`);
|
|
94
|
-
|
|
95
|
-
const scriptContent = `
|
|
96
|
-
import sys, scriptengine as script_engine, os
|
|
97
|
-
try:
|
|
98
|
-
print("Python script running")
|
|
99
|
-
print("Checking CODESYS status...")
|
|
100
|
-
scripting_ok = True
|
|
101
|
-
primary_project = script_engine.projects.primary
|
|
102
|
-
print("Primary project: " + str(primary_project))
|
|
103
|
-
print("SCRIPT_SUCCESS: Status check completed")
|
|
104
|
-
sys.exit(0)
|
|
105
|
-
except Exception as e:
|
|
106
|
-
print("Error: " + str(e))
|
|
107
|
-
print("SCRIPT_ERROR: " + str(e))
|
|
108
|
-
sys.exit(1)
|
|
109
|
-
`;
|
|
110
|
-
|
|
111
|
-
const result = await executeCodesysScript(scriptContent, codesysPath, profileName);
|
|
112
|
-
return {
|
|
113
|
-
content: [{ type: "text", text: `Status check ${result.success ? 'succeeded' : 'failed'}.\nOutput:\n${result.output}` }],
|
|
114
|
-
isError: !result.success
|
|
115
|
-
};
|
|
116
|
-
} catch (err) {
|
|
117
|
-
console.error(`DIRECT: Tool error: ${err.message}`);
|
|
118
|
-
return { content: [{ type: "text", text: `Error: ${err.message}` }], isError: true };
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// Start the server
|
|
123
|
-
async function startServer() {
|
|
124
|
-
try {
|
|
125
|
-
const transport = new StdioServerTransport();
|
|
126
|
-
console.error("DIRECT: Connecting server via stdio...");
|
|
127
|
-
await server.connect(transport);
|
|
128
|
-
console.error("DIRECT: Server connected and listening");
|
|
129
|
-
} catch (error) {
|
|
130
|
-
console.error("DIRECT: Failed to start server:", error);
|
|
131
|
-
process.exit(1);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Show args
|
|
136
|
-
console.error(`DIRECT: Args: ${JSON.stringify(process.argv)}`);
|
|
137
|
-
|
|
138
|
-
// Start the server
|
|
139
|
-
startServer();
|
|
140
|
-
|
|
141
|
-
// Handle shutdown
|
|
142
|
-
process.on('SIGINT', () => {
|
|
143
|
-
console.error('\nDIRECT: SIGINT received, shutting down...');
|
|
144
|
-
process.exit(0);
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
process.on('SIGTERM', () => {
|
|
148
|
-
console.error('\nDIRECT: SIGTERM received, shutting down...');
|
|
149
|
-
process.exit(0);
|
|
150
|
-
});
|