lunisphere-mcp 1.1.0 → 1.2.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/bin/cli.js +27 -2
- package/lib/autostart.js +20 -5
- package/lib/poller.js +14 -2
- package/lib/tray.js +183 -0
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -7,13 +7,21 @@ const { LocalToolkit } = require('../lib/tools');
|
|
|
7
7
|
const { RelayPoller } = require('../lib/poller');
|
|
8
8
|
const { ensureLatestAndMaybeRespawn } = require('../lib/updater');
|
|
9
9
|
const {
|
|
10
|
+
CONFIG_DIR,
|
|
11
|
+
CONFIG_FILE,
|
|
12
|
+
PID_FILE,
|
|
13
|
+
TRAY_PID_FILE,
|
|
14
|
+
LOG_FILE,
|
|
15
|
+
VBS_FILE,
|
|
10
16
|
loadConfig,
|
|
11
17
|
saveConfig,
|
|
12
18
|
writePid,
|
|
13
19
|
stopExistingAgent,
|
|
14
20
|
appendLog,
|
|
15
21
|
runSetup,
|
|
16
|
-
uninstallStartup
|
|
22
|
+
uninstallStartup,
|
|
23
|
+
writeStatusFile,
|
|
24
|
+
launchWindowsTray
|
|
17
25
|
} = require('../lib/autostart');
|
|
18
26
|
|
|
19
27
|
function parseArgs(argv) {
|
|
@@ -127,6 +135,18 @@ Options:
|
|
|
127
135
|
|
|
128
136
|
stopExistingAgent();
|
|
129
137
|
writePid(process.pid);
|
|
138
|
+
writeStatusFile(CONFIG_DIR, { online: false, deviceName: '', toolsCount: 0, pid: process.pid });
|
|
139
|
+
|
|
140
|
+
if (args.service && process.platform === 'win32') {
|
|
141
|
+
launchWindowsTray({
|
|
142
|
+
configDir: CONFIG_DIR,
|
|
143
|
+
configFile: CONFIG_FILE,
|
|
144
|
+
pidFile: PID_FILE,
|
|
145
|
+
trayPidFile: TRAY_PID_FILE,
|
|
146
|
+
logFile: LOG_FILE,
|
|
147
|
+
vbsPath: VBS_FILE
|
|
148
|
+
});
|
|
149
|
+
}
|
|
130
150
|
|
|
131
151
|
console.log('===============================================');
|
|
132
152
|
console.log(` Lunisphere MCP PC Client v${pkg.version}`);
|
|
@@ -136,7 +156,12 @@ Options:
|
|
|
136
156
|
console.log('===============================================');
|
|
137
157
|
|
|
138
158
|
const toolkit = new LocalToolkit({ rootDir, allowShell });
|
|
139
|
-
const poller = new RelayPoller({
|
|
159
|
+
const poller = new RelayPoller({
|
|
160
|
+
serverUrl,
|
|
161
|
+
deviceKey,
|
|
162
|
+
toolkit,
|
|
163
|
+
onStatusChange: (status) => writeStatusFile(CONFIG_DIR, { ...status, pid: process.pid })
|
|
164
|
+
});
|
|
140
165
|
|
|
141
166
|
process.on('SIGINT', () => {
|
|
142
167
|
console.log('\n[STOP] Shutting down Lunisphere MCP Client...');
|
package/lib/autostart.js
CHANGED
|
@@ -12,10 +12,18 @@ const {
|
|
|
12
12
|
installLatestGlobal
|
|
13
13
|
} = require('./updater');
|
|
14
14
|
|
|
15
|
+
const {
|
|
16
|
+
stopExistingTray,
|
|
17
|
+
writeStatusFile,
|
|
18
|
+
launchWindowsTray
|
|
19
|
+
} = require('./tray');
|
|
20
|
+
|
|
15
21
|
const CONFIG_DIR = path.join(os.homedir(), '.lunisphere-mcp');
|
|
16
22
|
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
17
23
|
const PID_FILE = path.join(CONFIG_DIR, 'agent.pid');
|
|
24
|
+
const TRAY_PID_FILE = path.join(CONFIG_DIR, 'tray.pid');
|
|
18
25
|
const LOG_FILE = path.join(CONFIG_DIR, 'agent.log');
|
|
26
|
+
const VBS_FILE = path.join(CONFIG_DIR, 'launch.vbs');
|
|
19
27
|
const TASK_NAME = 'LunisphereMCPRelay';
|
|
20
28
|
|
|
21
29
|
function ensureConfigDir() {
|
|
@@ -223,19 +231,20 @@ async function runSetup({ serverUrl, deviceKey, rootDir, allowShell, currentVers
|
|
|
223
231
|
}
|
|
224
232
|
|
|
225
233
|
if (startupInfo.mode === 'admin') {
|
|
226
|
-
console.log('[STARTUP] Registered silently in Administrator mode (zero popups
|
|
234
|
+
console.log('[STARTUP] Registered silently in Administrator mode (System Tray, zero popups).');
|
|
227
235
|
} else if (process.platform === 'win32') {
|
|
228
|
-
console.log(`[STARTUP] Registered silently in User mode (${startupInfo.mode}, zero popups
|
|
229
|
-
console.log('[NOTE] Run
|
|
236
|
+
console.log(`[STARTUP] Registered silently in User mode (${startupInfo.mode}, System Tray, zero popups).`);
|
|
237
|
+
console.log('[NOTE] Run once from an Administrator terminal if you want elevated Admin mode on boot.');
|
|
230
238
|
} else {
|
|
231
239
|
console.log(`[STARTUP] Registered background service (${startupInfo.mode}).`);
|
|
232
240
|
}
|
|
233
241
|
|
|
234
|
-
console.log(
|
|
242
|
+
console.log('[SETUP OK] Running in background & System Tray. You can close this window.');
|
|
235
243
|
}
|
|
236
244
|
|
|
237
245
|
function uninstallStartup() {
|
|
238
246
|
stopExistingAgent();
|
|
247
|
+
stopExistingTray(TRAY_PID_FILE);
|
|
239
248
|
if (process.platform === 'win32') {
|
|
240
249
|
try { execFileSync('schtasks.exe', ['/Delete', '/TN', TASK_NAME, '/F'], { stdio: 'ignore', windowsHide: true }); } catch (_) {}
|
|
241
250
|
try {
|
|
@@ -260,12 +269,18 @@ function uninstallStartup() {
|
|
|
260
269
|
module.exports = {
|
|
261
270
|
CONFIG_DIR,
|
|
262
271
|
CONFIG_FILE,
|
|
272
|
+
PID_FILE,
|
|
273
|
+
TRAY_PID_FILE,
|
|
263
274
|
LOG_FILE,
|
|
275
|
+
VBS_FILE,
|
|
264
276
|
saveConfig,
|
|
265
277
|
loadConfig,
|
|
266
278
|
writePid,
|
|
267
279
|
stopExistingAgent,
|
|
268
280
|
appendLog,
|
|
269
281
|
runSetup,
|
|
270
|
-
uninstallStartup
|
|
282
|
+
uninstallStartup,
|
|
283
|
+
writeStatusFile,
|
|
284
|
+
launchWindowsTray,
|
|
285
|
+
stopExistingTray
|
|
271
286
|
};
|
package/lib/poller.js
CHANGED
|
@@ -3,14 +3,23 @@
|
|
|
3
3
|
const os = require('os');
|
|
4
4
|
|
|
5
5
|
class RelayPoller {
|
|
6
|
-
constructor({ serverUrl, deviceKey, toolkit }) {
|
|
6
|
+
constructor({ serverUrl, deviceKey, toolkit, onStatusChange = null }) {
|
|
7
7
|
this.serverUrl = serverUrl.replace(/\/+$/, '');
|
|
8
8
|
this.deviceKey = deviceKey;
|
|
9
9
|
this.toolkit = toolkit;
|
|
10
|
+
this.onStatusChange = onStatusChange;
|
|
10
11
|
this.running = false;
|
|
11
12
|
this.backoffMs = 2000;
|
|
12
13
|
}
|
|
13
14
|
|
|
15
|
+
_notify(online, extra = {}) {
|
|
16
|
+
if (typeof this.onStatusChange === 'function') {
|
|
17
|
+
try {
|
|
18
|
+
this.onStatusChange({ online, ...extra });
|
|
19
|
+
} catch (_) {}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
14
23
|
_headers() {
|
|
15
24
|
return {
|
|
16
25
|
'Content-Type': 'application/json',
|
|
@@ -58,7 +67,9 @@ class RelayPoller {
|
|
|
58
67
|
while (this.running) {
|
|
59
68
|
try {
|
|
60
69
|
const reg = await this.register();
|
|
61
|
-
|
|
70
|
+
const toolsCount = this.toolkit.getToolSchemas().length;
|
|
71
|
+
console.log(`[ONLINE] Connected as "${reg.deviceName}" (${reg.deviceId}) | Tools: ${toolsCount}`);
|
|
72
|
+
this._notify(true, { deviceName: reg.deviceName, deviceId: reg.deviceId, toolsCount });
|
|
62
73
|
this.backoffMs = 2000;
|
|
63
74
|
|
|
64
75
|
while (this.running) {
|
|
@@ -95,6 +106,7 @@ class RelayPoller {
|
|
|
95
106
|
}
|
|
96
107
|
} catch (err) {
|
|
97
108
|
if (!this.running) break;
|
|
109
|
+
this._notify(false, { error: err.message });
|
|
98
110
|
console.error(`[RECONNECT] ${err.message} — retrying in ${this.backoffMs / 1000}s...`);
|
|
99
111
|
await new Promise((r) => setTimeout(r, this.backoffMs));
|
|
100
112
|
this.backoffMs = Math.min(15000, this.backoffMs * 2);
|
package/lib/tray.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { spawn } = require('child_process');
|
|
6
|
+
|
|
7
|
+
function buildTrayScript({ configDir, configFile, pidFile, trayPidFile, logFile, vbsPath }) {
|
|
8
|
+
const esc = (p) => String(p).replace(/'/g, "''");
|
|
9
|
+
return `
|
|
10
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
11
|
+
Add-Type -AssemblyName System.Drawing
|
|
12
|
+
|
|
13
|
+
$ErrorActionPreference = 'SilentlyContinue'
|
|
14
|
+
$configFile = '${esc(configFile)}'
|
|
15
|
+
$statusFile = '${esc(path.join(configDir, 'status.json'))}'
|
|
16
|
+
$pidFile = '${esc(pidFile)}'
|
|
17
|
+
$trayPidFile = '${esc(trayPidFile)}'
|
|
18
|
+
$logFile = '${esc(logFile)}'
|
|
19
|
+
$vbsPath = '${esc(vbsPath)}'
|
|
20
|
+
|
|
21
|
+
Set-Content -Path $trayPidFile -Value $PID
|
|
22
|
+
|
|
23
|
+
function New-BadgeIcon([bool]$isOnline) {
|
|
24
|
+
$bmp = New-Object System.Drawing.Bitmap 16, 16
|
|
25
|
+
$g = [System.Drawing.Graphics]::FromImage($bmp)
|
|
26
|
+
$g.Clear([System.Drawing.Color]::FromArgb(255, 18, 18, 22))
|
|
27
|
+
$borderPen = New-Object System.Drawing.Pen ([System.Drawing.Color]::FromArgb(255, 70, 70, 80)), 1
|
|
28
|
+
$g.DrawRectangle($borderPen, 0, 0, 15, 15)
|
|
29
|
+
$dotBrush = if ($isOnline) {
|
|
30
|
+
New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(255, 34, 197, 94))
|
|
31
|
+
} else {
|
|
32
|
+
New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(255, 234, 179, 8))
|
|
33
|
+
}
|
|
34
|
+
$g.FillEllipse($dotBrush, 4, 4, 8, 8)
|
|
35
|
+
$g.Dispose()
|
|
36
|
+
return [System.Drawing.Icon]::FromHandle($bmp.GetHicon())
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
$iconOnline = New-BadgeIcon $true
|
|
40
|
+
$iconOffline = New-BadgeIcon $false
|
|
41
|
+
|
|
42
|
+
$notify = New-Object System.Windows.Forms.NotifyIcon
|
|
43
|
+
$notify.Icon = $iconOffline
|
|
44
|
+
$notify.Text = 'Lunisphere MCP Relay (Starting...)'
|
|
45
|
+
$notify.Visible = $true
|
|
46
|
+
|
|
47
|
+
$menu = New-Object System.Windows.Forms.ContextMenuStrip
|
|
48
|
+
$statusItem = New-Object System.Windows.Forms.ToolStripMenuItem 'Status: Connecting...'
|
|
49
|
+
$statusItem.Enabled = $false
|
|
50
|
+
$openDashItem = New-Object System.Windows.Forms.ToolStripMenuItem 'Open Dashboard'
|
|
51
|
+
$viewLogItem = New-Object System.Windows.Forms.ToolStripMenuItem 'View Agent Log'
|
|
52
|
+
$restartItem = New-Object System.Windows.Forms.ToolStripMenuItem 'Restart & Check Updates'
|
|
53
|
+
$exitItem = New-Object System.Windows.Forms.ToolStripMenuItem 'Exit'
|
|
54
|
+
|
|
55
|
+
[void]$menu.Items.Add($statusItem)
|
|
56
|
+
[void]$menu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
|
|
57
|
+
[void]$menu.Items.Add($openDashItem)
|
|
58
|
+
[void]$menu.Items.Add($viewLogItem)
|
|
59
|
+
[void]$menu.Items.Add($restartItem)
|
|
60
|
+
[void]$menu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
|
|
61
|
+
[void]$menu.Items.Add($exitItem)
|
|
62
|
+
$notify.ContextMenuStrip = $menu
|
|
63
|
+
|
|
64
|
+
$openDashItem.Add_Click({
|
|
65
|
+
$url = 'https://mcp.lunisphere.com'
|
|
66
|
+
if (Test-Path $configFile) {
|
|
67
|
+
try {
|
|
68
|
+
$cfg = Get-Content $configFile -Raw | ConvertFrom-Json
|
|
69
|
+
if ($cfg.server) { $url = $cfg.server }
|
|
70
|
+
} catch {}
|
|
71
|
+
}
|
|
72
|
+
Start-Process $url
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
$viewLogItem.Add_Click({
|
|
76
|
+
if (Test-Path $logFile) { Start-Process 'notepad.exe' $logFile }
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
$restartItem.Add_Click({
|
|
80
|
+
if (Test-Path $pidFile) {
|
|
81
|
+
$agentPid = [int](Get-Content $pidFile -Raw).Trim()
|
|
82
|
+
if ($agentPid -gt 0) { Stop-Process -Id $agentPid -Force }
|
|
83
|
+
}
|
|
84
|
+
$notify.Visible = $false
|
|
85
|
+
$notify.Dispose()
|
|
86
|
+
if (Test-Path $vbsPath) { Start-Process 'wscript.exe' -ArgumentList "\`"$vbsPath\`"" }
|
|
87
|
+
[System.Windows.Forms.Application]::Exit()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
$exitItem.Add_Click({
|
|
91
|
+
if (Test-Path $pidFile) {
|
|
92
|
+
$agentPid = [int](Get-Content $pidFile -Raw).Trim()
|
|
93
|
+
if ($agentPid -gt 0) { Stop-Process -Id $agentPid -Force }
|
|
94
|
+
}
|
|
95
|
+
$notify.Visible = $false
|
|
96
|
+
$notify.Dispose()
|
|
97
|
+
Remove-Item -Path $trayPidFile -Force
|
|
98
|
+
[System.Windows.Forms.Application]::Exit()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
$timer = New-Object System.Windows.Forms.Timer
|
|
102
|
+
$timer.Interval = 2500
|
|
103
|
+
$timer.Add_Tick({
|
|
104
|
+
if (Test-Path $pidFile) {
|
|
105
|
+
$agentPid = [int](Get-Content $pidFile -Raw).Trim()
|
|
106
|
+
$proc = Get-Process -Id $agentPid
|
|
107
|
+
if (-not $proc) {
|
|
108
|
+
$notify.Visible = $false
|
|
109
|
+
$notify.Dispose()
|
|
110
|
+
Remove-Item -Path $trayPidFile -Force
|
|
111
|
+
[System.Windows.Forms.Application]::Exit()
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (Test-Path $statusFile) {
|
|
116
|
+
try {
|
|
117
|
+
$st = Get-Content $statusFile -Raw | ConvertFrom-Json
|
|
118
|
+
if ($st.online) {
|
|
119
|
+
$notify.Icon = $iconOnline
|
|
120
|
+
$txt = "Lunisphere MCP: Online ($($st.deviceName))"
|
|
121
|
+
if ($txt.Length -gt 63) { $txt = $txt.Substring(0, 63) }
|
|
122
|
+
$notify.Text = $txt
|
|
123
|
+
$statusItem.Text = "Online · $($st.deviceName) ($($st.toolsCount) tools)"
|
|
124
|
+
} else {
|
|
125
|
+
$notify.Icon = $iconOffline
|
|
126
|
+
$notify.Text = 'Lunisphere MCP: Reconnecting...'
|
|
127
|
+
$statusItem.Text = 'Reconnecting...'
|
|
128
|
+
}
|
|
129
|
+
} catch {}
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
$timer.Start()
|
|
133
|
+
|
|
134
|
+
[System.Windows.Forms.Application]::Run()
|
|
135
|
+
`;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function stopExistingTray(trayPidFile) {
|
|
139
|
+
if (!fs.existsSync(trayPidFile)) return;
|
|
140
|
+
try {
|
|
141
|
+
const oldPid = parseInt(fs.readFileSync(trayPidFile, 'utf8').trim(), 10);
|
|
142
|
+
if (oldPid) {
|
|
143
|
+
try { process.kill(oldPid, 'SIGTERM'); } catch (_) {}
|
|
144
|
+
}
|
|
145
|
+
} catch (_) {}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function writeStatusFile(configDir, status) {
|
|
149
|
+
try {
|
|
150
|
+
fs.writeFileSync(
|
|
151
|
+
path.join(configDir, 'status.json'),
|
|
152
|
+
JSON.stringify({ ...status, updatedAt: new Date().toISOString() }),
|
|
153
|
+
'utf8'
|
|
154
|
+
);
|
|
155
|
+
} catch (_) {}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function launchWindowsTray({ configDir, configFile, pidFile, trayPidFile, logFile, vbsPath }) {
|
|
159
|
+
if (process.platform !== 'win32') return;
|
|
160
|
+
stopExistingTray(trayPidFile);
|
|
161
|
+
const psPath = path.join(configDir, 'tray.ps1');
|
|
162
|
+
fs.writeFileSync(
|
|
163
|
+
psPath,
|
|
164
|
+
buildTrayScript({ configDir, configFile, pidFile, trayPidFile, logFile, vbsPath }),
|
|
165
|
+
'utf8'
|
|
166
|
+
);
|
|
167
|
+
const child = spawn(
|
|
168
|
+
'powershell.exe',
|
|
169
|
+
['-NoProfile', '-ExecutionPolicy', 'Bypass', '-WindowStyle', 'Hidden', '-File', psPath],
|
|
170
|
+
{
|
|
171
|
+
detached: true,
|
|
172
|
+
stdio: 'ignore',
|
|
173
|
+
windowsHide: true
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
child.unref();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
module.exports = {
|
|
180
|
+
stopExistingTray,
|
|
181
|
+
writeStatusFile,
|
|
182
|
+
launchWindowsTray
|
|
183
|
+
};
|
package/package.json
CHANGED