dsh-reverse-engine 0.0.0-stage → 0.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/CHANGELOG.md +17 -0
- package/LICENSE +201 -0
- package/NOTICE +49 -0
- package/README.md +124 -2
- package/README.zh-CN.md +108 -0
- package/client.js +1509 -0
- package/cordis.patch.yml +6 -0
- package/docs/ARCHITECTURE.md +114 -0
- package/docs/CONTRACT.md +91 -0
- package/docs/VERIFICATION.md +55 -0
- package/icon.svg +23 -0
- package/index.js +218 -0
- package/lib/catalog.js +123 -0
- package/lib/clients.js +206 -0
- package/lib/engine.js +488 -0
- package/lib/http-endpoint.js +153 -0
- package/lib/rea-catalog.json +671 -0
- package/lib/rea-clients.json +177 -0
- package/lib/routes.js +212 -0
- package/lib/runtime.js +299 -0
- package/lib/service.js +454 -0
- package/lib/settings.js +165 -0
- package/lib/tools.js +245 -0
- package/lib/util.js +312 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +77 -4
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional HTTP MCP endpoint for agents that cannot spawn a stdio child.
|
|
3
|
+
*
|
|
4
|
+
* This is a second REA process, separate from the stdio engine the plugin owns:
|
|
5
|
+
* REA's `--http` mode replaces stdio for that process and serves one stateful
|
|
6
|
+
* client at a time. The endpoint has no authentication, so the console only
|
|
7
|
+
* exposes it on loopback unless the user explicitly chooses another host.
|
|
8
|
+
*/
|
|
9
|
+
import { spawn } from 'node:child_process'
|
|
10
|
+
import { ReaError, shortError } from './util.js'
|
|
11
|
+
import { engineEnvironment } from './settings.js'
|
|
12
|
+
|
|
13
|
+
export class HttpEndpoint {
|
|
14
|
+
#settings
|
|
15
|
+
#resolveRuntime
|
|
16
|
+
#logger
|
|
17
|
+
#child = null
|
|
18
|
+
#url = null
|
|
19
|
+
#error = null
|
|
20
|
+
#stderr = []
|
|
21
|
+
#starting = null
|
|
22
|
+
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.#settings = options.settings
|
|
25
|
+
this.#resolveRuntime = options.resolveRuntime
|
|
26
|
+
this.#logger = options.logger ?? {}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
setSettings(settings) {
|
|
30
|
+
this.#settings = settings
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
snapshot() {
|
|
34
|
+
return {
|
|
35
|
+
running: Boolean(this.#child),
|
|
36
|
+
url: this.#url,
|
|
37
|
+
host: this.#settings.httpHost,
|
|
38
|
+
port: this.#settings.httpPort,
|
|
39
|
+
pid: this.#child?.pid ?? null,
|
|
40
|
+
error: this.#error,
|
|
41
|
+
stderr: this.#stderr.slice(-20),
|
|
42
|
+
warning: !isLoopback(this.#settings.httpHost)
|
|
43
|
+
? 'This endpoint has no authentication and is bound to a non-loopback address. Use it only on a trusted network.'
|
|
44
|
+
: null,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async start() {
|
|
49
|
+
if (this.#child) return this.snapshot()
|
|
50
|
+
if (this.#starting) return this.#starting
|
|
51
|
+
this.#starting = this.#doStart().finally(() => {
|
|
52
|
+
this.#starting = null
|
|
53
|
+
})
|
|
54
|
+
return this.#starting
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async #doStart() {
|
|
58
|
+
const runtime = await this.#resolveRuntime()
|
|
59
|
+
const env = engineEnvironment(this.#settings)
|
|
60
|
+
env.ELECTRON_RUN_AS_NODE = '1'
|
|
61
|
+
if (!env.REA_LOG_LEVEL) env.REA_LOG_LEVEL = 'info'
|
|
62
|
+
const args = [runtime.rea.script, 'mcp', '--http', '--host', this.#settings.httpHost, '--port', String(this.#settings.httpPort)]
|
|
63
|
+
const child = spawn(runtime.node.path, args, {
|
|
64
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
65
|
+
env,
|
|
66
|
+
cwd: this.#settings.dataDirectory,
|
|
67
|
+
windowsHide: true,
|
|
68
|
+
})
|
|
69
|
+
this.#child = child
|
|
70
|
+
this.#error = null
|
|
71
|
+
this.#url = null
|
|
72
|
+
child.stdout.setEncoding('utf8')
|
|
73
|
+
child.stderr.setEncoding('utf8')
|
|
74
|
+
child.stdout.on('data', chunk => this.#ingest(chunk))
|
|
75
|
+
child.stderr.on('data', chunk => this.#ingest(chunk))
|
|
76
|
+
child.once('error', error => {
|
|
77
|
+
this.#error = `Cannot start the HTTP endpoint: ${error.message}`
|
|
78
|
+
this.#child = null
|
|
79
|
+
})
|
|
80
|
+
child.once('exit', (code, signal) => {
|
|
81
|
+
if (this.#child === child) {
|
|
82
|
+
this.#child = null
|
|
83
|
+
if (!this.#error && !this.#url) this.#error = `The HTTP endpoint exited (${signal ?? `code ${code}`}).`
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const deadline = Date.now() + 30_000
|
|
88
|
+
while (!this.#url && !this.#error && Date.now() < deadline) {
|
|
89
|
+
await new Promise(resolve => setTimeout(resolve, 120))
|
|
90
|
+
if (!this.#child) break
|
|
91
|
+
}
|
|
92
|
+
if (!this.#url) {
|
|
93
|
+
await this.stop()
|
|
94
|
+
throw new ReaError('http-start-failed', this.#error ?? 'The HTTP endpoint did not announce a listening URL.', { stderr: this.#stderr.slice(-20) })
|
|
95
|
+
}
|
|
96
|
+
return this.snapshot()
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#ingest(chunk) {
|
|
100
|
+
for (const line of String(chunk).split(/\r?\n/)) {
|
|
101
|
+
if (!line.trim()) continue
|
|
102
|
+
this.#stderr.push(line.slice(0, 1000))
|
|
103
|
+
if (this.#stderr.length > 100) this.#stderr.shift()
|
|
104
|
+
const match = /"msg":"MCP transport listening".*?"url":"([^"]+)"/.exec(line) ?? /"url":"([^"]+)".*?"msg":"MCP transport listening"/.exec(line)
|
|
105
|
+
if (match) this.#url = match[1]
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async stop() {
|
|
110
|
+
const child = this.#child
|
|
111
|
+
this.#child = null
|
|
112
|
+
this.#url = null
|
|
113
|
+
if (!child) return
|
|
114
|
+
await new Promise(resolve => {
|
|
115
|
+
const timer = setTimeout(() => {
|
|
116
|
+
try {
|
|
117
|
+
child.kill('SIGKILL')
|
|
118
|
+
} catch {
|
|
119
|
+
/* ignore */
|
|
120
|
+
}
|
|
121
|
+
resolve()
|
|
122
|
+
}, 3_000)
|
|
123
|
+
timer.unref?.()
|
|
124
|
+
child.once('exit', () => {
|
|
125
|
+
clearTimeout(timer)
|
|
126
|
+
resolve()
|
|
127
|
+
})
|
|
128
|
+
try {
|
|
129
|
+
child.kill('SIGTERM')
|
|
130
|
+
} catch {
|
|
131
|
+
clearTimeout(timer)
|
|
132
|
+
resolve()
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
this.#logger.info?.('[dsh-reverse-engine] HTTP MCP endpoint stopped')
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async restart() {
|
|
139
|
+
await this.stop()
|
|
140
|
+
return this.start()
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async close() {
|
|
144
|
+
await this.stop()
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function isLoopback(host) {
|
|
149
|
+
const value = String(host ?? '').toLowerCase()
|
|
150
|
+
return value === '127.0.0.1' || value === 'localhost' || value === '::1' || value === '[::1]'
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export { isLoopback, shortError }
|