@opute/host-agent 0.1.1 → 0.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.
Files changed (3) hide show
  1. package/README.md +31 -19
  2. package/index.js +95 -19
  3. package/package.json +16 -4
package/README.md CHANGED
@@ -1,29 +1,35 @@
1
1
  # @opute/host-agent
2
2
 
3
3
  Downloads and launches the checksum-verified Opute Go host agent as a local
4
- **Streamable HTTP** MCP server for VS Code, Claude Desktop, Cursor, and other
5
- MCP clients.
4
+ **Streamable HTTP** MCP server. Cursor, VS Code, and Claude Desktop snippets
5
+ below are copy/paste examples, not named-client certifications.
6
6
 
7
7
  ## Quick start
8
8
 
9
9
  ```bash
10
- # Start in the foreground (logs on stderr)
11
- npx -y @opute/host-agent start
10
+ # Required canonical identity; keep the same value for this Host Agent
11
+ export OPUTE_REMOTE_AGENT_ID="local-$(openssl rand -hex 8)"
12
+ # Bearer token for authenticated /mcp calls
13
+ export MCP_AUTH_TOKEN="$(openssl rand -hex 32)"
12
14
 
13
- # Or run as a background daemon and print the MCP URL
14
15
  npx -y @opute/host-agent start --background
16
+ npx -y @opute/host-agent url # http://127.0.0.1:3014/mcp
15
17
  npx -y @opute/host-agent status
16
18
  npx -y @opute/host-agent stop
17
19
  ```
18
20
 
19
- Point your MCP client at the printed URL (default port **3014**):
21
+ Point your MCP client at the printed URL (default port **3014**). When
22
+ `MCP_AUTH_TOKEN` is set, send it as a Bearer token:
20
23
 
21
24
  ```json
22
25
  {
23
26
  "servers": {
24
27
  "oputeLocal": {
25
28
  "type": "http",
26
- "url": "http://127.0.0.1:3014/mcp"
29
+ "url": "http://127.0.0.1:3014/mcp",
30
+ "headers": {
31
+ "Authorization": "Bearer dev-token"
32
+ }
27
33
  }
28
34
  }
29
35
  }
@@ -31,6 +37,10 @@ Point your MCP client at the printed URL (default port **3014**):
31
37
 
32
38
  stdio MCP transport is not supported.
33
39
 
40
+ Safe first-run sequence once the client is connected: `tools/list` → read-only
41
+ host/VM inspection tools (for example `get_host_info`, `list_vms`). Mutating
42
+ tools stay denied until `OPUTE_STANDALONE_ALLOW_MUTATIONS=true`.
43
+
34
44
  ## Commands
35
45
 
36
46
  | Command | Description |
@@ -47,21 +57,23 @@ stdio MCP transport is not supported.
47
57
  |----------|---------|
48
58
  | `HOST_MCP_PORT` | Listen port (default `3014`) |
49
59
  | `HOST_MCP_BIND_HOST` | Bind host (default `127.0.0.1`) |
60
+ | `OPUTE_REMOTE_AGENT_ID` | Required explicit opaque canonical agent identity |
61
+ | `MCP_AUTH_TOKEN` | Bearer bootstrap token for `/mcp` |
50
62
  | `OPUTE_HOST_AGENT_BINARY` | Use a local binary instead of downloading a release |
51
63
  | `OPUTE_STANDALONE_ALLOW_MUTATIONS=true` | Enable mutating infrastructure tools |
52
64
  | `OPUTE_STANDALONE_STATE_DIR` | Local SQLite / operation journal directory |
53
65
 
54
- For development, set `OPUTE_HOST_AGENT_BINARY` to a locally built binary.
55
- Released packages download the matching versioned artifact and verify it
56
- against the release `SHA256SUMS` manifest. `OPUTE_HOST_AGENT_SHA256` may be
57
- supplied to pin a development or mirror artifact explicitly.
66
+ The launcher deliberately strips most other `OPUTE_*` variables so a
67
+ Platform-enrolled shell cannot leak enrollment secrets into standalone.
68
+
69
+ For development, set `OPUTE_HOST_AGENT_BINARY` to a locally built binary
70
+ (`make build` → `dist/opute-host-agent`). Released packages download the
71
+ matching versioned artifact and verify it against the release `SHA256SUMS`
72
+ manifest.
58
73
 
59
- The stable MVP claim covers local Incus inspection and VM lifecycle. K3s,
60
- PostgreSQL, SQL execution, and Cloudflare Tunnel tools are exposed as
61
- experimental capabilities and require the same explicit mutation opt-in.
74
+ ## Platform support
62
75
 
63
- The launcher supports Linux x64 and arm64. Native Windows and macOS are not
64
- supported by the Incus provider; Windows users should run the Linux binary
65
- inside WSL and connect from the Windows MCP client over HTTP to
66
- `http://127.0.0.1:3014/mcp` (with WSL port forwarding as needed). The launcher
67
- does not install dependencies or run a postinstall hook.
76
+ Linux x64 and arm64 only. Native Windows and macOS are not supported by the
77
+ Incus provider. On Windows, run the Linux binary inside WSL and point the
78
+ Windows MCP client at `http://127.0.0.1:3014/mcp` (enable WSL localhost
79
+ forwarding / portproxy as needed).
package/index.js CHANGED
@@ -6,6 +6,7 @@ const path = require('node:path')
6
6
  const crypto = require('node:crypto')
7
7
  const http = require('node:http')
8
8
  const https = require('node:https')
9
+ const net = require('node:net')
9
10
  const zlib = require('node:zlib')
10
11
  const { spawn } = require('node:child_process')
11
12
 
@@ -216,26 +217,52 @@ function isPidRunning(pid) {
216
217
  }
217
218
  }
218
219
 
219
- function probeHealth(url, timeoutMs = 2000) {
220
+ function readHealth(url, timeoutMs = 2000) {
220
221
  return new Promise(resolve => {
221
222
  const req = http.get(url, { timeout: timeoutMs }, response => {
222
223
  const chunks = []
223
224
  response.on('data', chunk => { chunks.push(chunk) })
224
225
  response.on('end', () => {
225
226
  if (response.statusCode !== 200) {
226
- resolve(false)
227
+ resolve(null)
227
228
  return
228
229
  }
229
230
  try {
230
- const payload = JSON.parse(Buffer.concat(chunks).toString('utf8'))
231
- resolve(payload && payload.ok === true)
231
+ resolve(JSON.parse(Buffer.concat(chunks).toString('utf8')))
232
232
  } catch {
233
- resolve(false)
233
+ resolve(null)
234
234
  }
235
235
  })
236
236
  })
237
- req.on('timeout', () => { req.destroy(); resolve(false) })
238
- req.on('error', () => resolve(false))
237
+ req.on('timeout', () => { req.destroy(); resolve(null) })
238
+ req.on('error', () => resolve(null))
239
+ })
240
+ }
241
+
242
+ async function probeHealth(url, timeoutMs = 2000) {
243
+ const payload = await readHealth(url, timeoutMs)
244
+ return payload?.ok === true
245
+ }
246
+
247
+ async function probeOwnedHealth(url, instanceId, timeoutMs = 2000) {
248
+ const payload = await readHealth(url, timeoutMs)
249
+ return payload?.ok === true && payload.localInstanceId === instanceId
250
+ }
251
+
252
+ function probeTcp(host, port, timeoutMs = 500) {
253
+ return new Promise(resolve => {
254
+ const socket = net.createConnection({ host, port })
255
+ let settled = false
256
+ const finish = value => {
257
+ if (settled) return
258
+ settled = true
259
+ socket.destroy()
260
+ resolve(value)
261
+ }
262
+ socket.once('connect', () => finish(true))
263
+ socket.once('timeout', () => finish(false))
264
+ socket.once('error', () => finish(false))
265
+ socket.setTimeout(timeoutMs)
239
266
  })
240
267
  }
241
268
 
@@ -250,13 +277,28 @@ async function waitForHealth(url, timeoutMs = HEALTH_WAIT_MS, failure = null) {
250
277
  throw new Error(`timed out waiting for health at ${url}`)
251
278
  }
252
279
 
253
- function buildAgentEnv() {
280
+ async function waitForOwnedHealth(url, instanceId, timeoutMs = HEALTH_WAIT_MS, failure = null) {
281
+ const deadline = Date.now() + timeoutMs
282
+ while (Date.now() < deadline) {
283
+ const earlyFailure = typeof failure === 'function' ? failure() : null
284
+ if (earlyFailure) throw new Error(earlyFailure)
285
+ if (await probeOwnedHealth(url, instanceId)) return
286
+ await new Promise(resolve => setTimeout(resolve, 100))
287
+ }
288
+ throw new Error(`timed out waiting for owned health at ${url}`)
289
+ }
290
+
291
+ function buildAgentEnv(instanceId = '') {
254
292
  const env = { ...process.env }
255
293
  for (const key of Object.keys(env)) {
256
- if (key.startsWith('OPUTE_') || key === 'MCP_AUTH_TOKEN' || key === 'BRIDGE_TOKEN') {
257
- // Keep launcher-only and standalone-safe vars.
294
+ if (key.startsWith('OPUTE_') || key === 'MCP_AUTH_TOKEN') {
295
+ // Keep launcher-only, auth, identity, and standalone-safe vars.
296
+ // Other OPUTE_* values are stripped so a Platform-enrolled shell cannot
297
+ // leak enrollment secrets into a local standalone process.
258
298
  if (
259
- key === 'OPUTE_HOST_AGENT_BINARY'
299
+ key === 'MCP_AUTH_TOKEN'
300
+ || key === 'OPUTE_REMOTE_AGENT_ID'
301
+ || key === 'OPUTE_HOST_AGENT_BINARY'
260
302
  || key === 'OPUTE_HOST_AGENT_CACHE_DIR'
261
303
  || key === 'OPUTE_HOST_AGENT_RELEASE_BASE_URL'
262
304
  || key === 'OPUTE_HOST_AGENT_CHECKSUM_URL'
@@ -273,13 +315,20 @@ function buildAgentEnv() {
273
315
  }
274
316
  }
275
317
  env.OPUTE_AGENT_MODE = 'standalone'
276
- env.OPUTE_TRANSPORT = 'http'
318
+ if (instanceId) env.OPUTE_LOCAL_HOST_AGENT_INSTANCE_ID = instanceId
277
319
  env.HOST_MCP_BIND_HOST = bindHost()
278
320
  env.HOST_MCP_PORT = String(mcpPort())
279
321
  if (!env.OPUTE_INFRA_PROVIDER_ID) env.OPUTE_INFRA_PROVIDER_ID = 'incus'
322
+ requireCanonicalAgentID(env)
280
323
  return env
281
324
  }
282
325
 
326
+ function requireCanonicalAgentID(env = process.env) {
327
+ if (!String(env.OPUTE_REMOTE_AGENT_ID || '').trim()) {
328
+ throw new Error('OPUTE_REMOTE_AGENT_ID is required; set one explicit opaque canonical id before starting the Host Agent')
329
+ }
330
+ }
331
+
283
332
  function printUsage() {
284
333
  console.log(`Usage: opute-host-agent <command>
285
334
 
@@ -292,11 +341,17 @@ Commands:
292
341
  Environment:
293
342
  HOST_MCP_PORT Listen port (default 3014)
294
343
  HOST_MCP_BIND_HOST Bind host (default 127.0.0.1)
344
+ OPUTE_REMOTE_AGENT_ID Required explicit opaque canonical agent id
345
+ MCP_AUTH_TOKEN Bearer token required by /mcp when set
295
346
  OPUTE_HOST_AGENT_BINARY Use a local binary instead of downloading a release
296
347
  OPUTE_STANDALONE_ALLOW_MUTATIONS=true Enable mutating tools
297
348
 
298
- MCP client config:
299
- { "type": "http", "url": "http://127.0.0.1:3014/mcp" }
349
+ MCP client config (include Authorization when MCP_AUTH_TOKEN is set):
350
+ {
351
+ "type": "http",
352
+ "url": "http://127.0.0.1:3014/mcp",
353
+ "headers": { "Authorization": "Bearer <MCP_AUTH_TOKEN>" }
354
+ }
300
355
  `)
301
356
  }
302
357
 
@@ -325,20 +380,32 @@ async function startForeground(binary, passthroughArgs) {
325
380
  async function startBackground(binary, passthroughArgs) {
326
381
  const existing = readDaemonState()
327
382
  if (existing && isPidRunning(existing.pid)) {
328
- const existingUrl = mcpUrl(existing.host || bindHost(), existing.port || mcpPort())
329
- throw new Error(`standalone agent already running (pid ${existing.pid}) at ${existingUrl}`)
383
+ const existingHost = existing.host || bindHost()
384
+ const existingPort = existing.port || mcpPort()
385
+ const existingUrl = mcpUrl(existingHost, existingPort)
386
+ const existingHealth = healthUrl(existingHost, existingPort)
387
+ if (existing.instanceId && await probeOwnedHealth(existingHealth, existing.instanceId)) {
388
+ console.log(existingUrl)
389
+ return 0
390
+ }
391
+ throw new Error(`cannot prove daemon ownership for live pid ${existing.pid} at ${existingUrl}; refusing to kill it`)
330
392
  }
393
+ if (existing) clearDaemonState()
331
394
  const host = bindHost()
332
395
  const port = mcpPort()
333
396
  const url = mcpUrl(host, port)
334
397
  const health = healthUrl(host, port)
398
+ if (await probeTcp(host, port)) {
399
+ throw new Error(`port ${port} already in use by another process; stop it or set HOST_MCP_PORT`)
400
+ }
401
+ const instanceId = crypto.randomUUID()
335
402
  fs.mkdirSync(runtimeDir(), { recursive: true, mode: 0o700 })
336
403
  const logPath = path.join(runtimeDir(), 'daemon.log')
337
404
  const logFd = fs.openSync(logPath, 'a')
338
405
  const child = spawn(binary, ['--mode=standalone', '--transport=http', ...passthroughArgs], {
339
406
  detached: true,
340
407
  stdio: ['ignore', logFd, logFd],
341
- env: buildAgentEnv(),
408
+ env: buildAgentEnv(instanceId),
342
409
  })
343
410
  let spawnError = null
344
411
  let childExit = null
@@ -350,11 +417,12 @@ async function startBackground(binary, passthroughArgs) {
350
417
  pid: child.pid,
351
418
  host,
352
419
  port,
420
+ instanceId,
353
421
  startedAt: new Date().toISOString(),
354
422
  logPath,
355
423
  })
356
424
  try {
357
- await waitForHealth(health, HEALTH_WAIT_MS, () => {
425
+ await waitForOwnedHealth(health, instanceId, HEALTH_WAIT_MS, () => {
358
426
  if (spawnError) return `child process failed: ${spawnError.message}`
359
427
  if (childExit) return `child process exited before health (code=${childExit.code}, signal=${childExit.signal || 'none'})`
360
428
  return null
@@ -375,6 +443,11 @@ async function cmdStop() {
375
443
  console.error('standalone agent is not running')
376
444
  return 0
377
445
  }
446
+ const host = state.host || bindHost()
447
+ const port = state.port || mcpPort()
448
+ if (!state.instanceId || !(await probeOwnedHealth(healthUrl(host, port), state.instanceId))) {
449
+ throw new Error(`cannot prove daemon ownership for live pid ${state.pid}; refusing to kill it`)
450
+ }
378
451
  process.kill(state.pid, 'SIGTERM')
379
452
  const deadline = Date.now() + 10_000
380
453
  while (Date.now() < deadline && isPidRunning(state.pid)) {
@@ -395,7 +468,9 @@ async function cmdStatus() {
395
468
  const url = mcpUrl(host, port)
396
469
  const health = healthUrl(host, port)
397
470
  const running = state ? isPidRunning(state.pid) : false
398
- const healthy = await probeHealth(health)
471
+ const healthy = state?.instanceId
472
+ ? await probeOwnedHealth(health, state.instanceId)
473
+ : false
399
474
  console.log(JSON.stringify({
400
475
  running,
401
476
  healthy,
@@ -432,6 +507,7 @@ async function main() {
432
507
  throw new Error(`unknown command: ${command}`)
433
508
  }
434
509
 
510
+ requireCanonicalAgentID()
435
511
  const background = rest.includes('--background') || rest.includes('-d')
436
512
  const passthroughArgs = rest.filter(arg => arg !== '--background' && arg !== '-d')
437
513
  const binary = await resolveBinary()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opute/host-agent",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Local Streamable HTTP launcher for the Opute Go host agent",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -12,14 +12,26 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/wunderous/host-agents/issues"
14
14
  },
15
- "keywords": ["mcp", "model-context-protocol", "incus", "infrastructure", "opute"],
15
+ "keywords": [
16
+ "mcp",
17
+ "model-context-protocol",
18
+ "incus",
19
+ "infrastructure",
20
+ "opute"
21
+ ],
16
22
  "scripts": {
17
- "test": "node --test"
23
+ "test": "node --test index.test.js",
24
+ "test:published-canary": "RUN_PUBLISHED_NPM_CANARY=true node --test published-canary.test.js",
25
+ "test:published-readonly-canary": "RUN_PUBLISHED_READONLY_NPM_CANARY=true node --test published-readonly-canary.test.js"
18
26
  },
19
27
  "bin": {
20
28
  "opute-host-agent": "index.js"
21
29
  },
22
- "files": ["index.js", "package.json", "README.md"],
30
+ "files": [
31
+ "index.js",
32
+ "package.json",
33
+ "README.md"
34
+ ],
23
35
  "engines": {
24
36
  "node": ">=18"
25
37
  }