@rse/ase 0.0.27 → 0.0.28

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.
@@ -10,17 +10,50 @@ import { fileURLToPath } from "node:url";
10
10
  import { spawn } from "node:child_process";
11
11
  import Hapi from "@hapi/hapi";
12
12
  import axios from "axios";
13
- import { isMap, isScalar } from "yaml";
13
+ import { isMap } from "yaml";
14
14
  import prettyMs from "pretty-ms";
15
+ import * as v from "valibot";
15
16
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
16
17
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
17
18
  import { z } from "zod";
18
19
  import { DateTime } from "luxon";
19
- import { Config, configSchema, parseScope } from "./ase-config.js";
20
- import { renderDiagram, detectTermWidth, detectTermHeight } from "./ase-diagram.js";
21
- import { taskLoad, taskSave, taskDelete, taskList } from "./ase-task.js";
22
- import { SERVICE_HOST as HOST, serviceSchema, probe } from "./ase-service-probe.js";
20
+ import { Config, configSchema } from "./ase-config.js";
21
+ import { DiagramMCP } from "./ase-diagram.js";
22
+ import { TaskMCP } from "./ase-task.js";
23
+ import PersonaMCP from "./ase-persona.js";
23
24
  import pkg from "../package.json" with { type: "json" };
25
+ /* shared service host */
26
+ export const SERVICE_HOST = "127.0.0.1";
27
+ const HOST = SERVICE_HOST;
28
+ /* schema for ".ase/service.yaml" */
29
+ export const serviceSchema = v.nullish(v.strictObject({
30
+ port: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1024), v.maxValue(65535)))
31
+ }));
32
+ /* distinguish ECONNREFUSED from other Axios transport errors */
33
+ export const isConnRefused = (err) => {
34
+ const e = err;
35
+ return e?.code === "ECONNREFUSED" || e?.cause?.code === "ECONNREFUSED";
36
+ };
37
+ /* probe the service and verify ASE identity banner */
38
+ export const probe = async (port, projectId) => {
39
+ try {
40
+ const r = await axios.request({
41
+ method: "OPTIONS",
42
+ url: `http://${SERVICE_HOST}:${port}/`,
43
+ timeout: 2000,
44
+ validateStatus: () => true
45
+ });
46
+ if (r.status < 200 || r.status >= 300)
47
+ return false;
48
+ const d = r.data;
49
+ return d?.ase === true && d?.projectId === projectId;
50
+ }
51
+ catch (err) {
52
+ if (isConnRefused(err))
53
+ return null;
54
+ throw err;
55
+ }
56
+ };
24
57
  const SERVE_ENV = "ASE_SERVICE_SERVE";
25
58
  const PORT_ENV = "ASE_SERVICE_PORT";
26
59
  const IDLE_MS = 30 * 60 * 1000;
@@ -28,91 +61,142 @@ const TICK_MS = 60 * 1000;
28
61
  const PORT_MIN = 42000;
29
62
  const PORT_MAX = 44000;
30
63
  const PORT_TRIES = 20;
31
- /* try binding a single candidate port to verify availability */
32
- const tryBind = (port) => {
33
- return new Promise((resolve) => {
34
- const s = net.createServer();
35
- s.once("error", () => {
36
- resolve(false);
37
- });
38
- s.once("listening", () => {
39
- s.close(() => resolve(true));
64
+ /* reusable functionality: port allocation, persistence, and process spawning */
65
+ export class Service {
66
+ /* try binding a single candidate port to verify availability */
67
+ static tryBind(port) {
68
+ return new Promise((resolve) => {
69
+ const s = net.createServer();
70
+ s.once("error", () => {
71
+ resolve(false);
72
+ });
73
+ s.once("listening", () => {
74
+ s.close(() => resolve(true));
75
+ });
76
+ s.listen(port, HOST);
40
77
  });
41
- s.listen(port, HOST);
42
- });
43
- };
44
- /* allocate a fresh random port in PORT_MIN..PORT_MAX */
45
- const allocatePort = async () => {
46
- for (let i = 0; i < PORT_TRIES; i++) {
47
- const p = PORT_MIN + Math.floor(Math.random() * (PORT_MAX - PORT_MIN + 1));
48
- if (await tryBind(p))
49
- return p;
50
78
  }
51
- throw new Error(`failed to allocate a port in ${PORT_MIN}..${PORT_MAX} after ${PORT_TRIES} attempts`);
52
- };
53
- /* persist an allocated port into ".ase/service.yaml" */
54
- const persistPort = (svc, port) => {
55
- svc.set("port", port);
56
- svc.write();
57
- };
58
- /* clear the persisted port and remove ".ase/service.yaml" if it is empty */
59
- const clearPort = (svc) => {
60
- svc.delete("port");
61
- const root = svc.get();
62
- const empty = root === undefined || root === null || (isMap(root) && root.items.length === 0);
63
- if (empty) {
64
- if (fs.existsSync(svc.filename))
65
- fs.rmSync(svc.filename);
79
+ /* allocate a fresh random port in PORT_MIN..PORT_MAX */
80
+ static async allocatePort() {
81
+ for (let i = 0; i < PORT_TRIES; i++) {
82
+ const p = PORT_MIN + Math.floor(Math.random() * (PORT_MAX - PORT_MIN + 1));
83
+ if (await Service.tryBind(p))
84
+ return p;
85
+ }
86
+ throw new Error(`failed to allocate a port in ${PORT_MIN}..${PORT_MAX} after ${PORT_TRIES} attempts`);
66
87
  }
67
- else
88
+ /* persist an allocated port into ".ase/service.yaml" */
89
+ static persistPort(svc, port) {
90
+ svc.set("port", port);
68
91
  svc.write();
69
- };
70
- /* spawn the current executable detached as a background service */
71
- const spawnDetached = (aseDir, port) => {
72
- fs.mkdirSync(aseDir, { recursive: true });
73
- const logFile = path.join(aseDir, "service.log");
74
- const fd = fs.openSync(logFile, "a");
75
- const entry = fileURLToPath(new URL("./ase.js", import.meta.url));
76
- const child = spawn(process.execPath, [entry, "service", "start"], {
77
- detached: true,
78
- env: { ...process.env, [SERVE_ENV]: "1", [PORT_ENV]: String(port) },
79
- stdio: ["ignore", fd, fd]
80
- });
81
- fs.closeSync(fd);
82
- return { child, logFile };
83
- };
84
- /* read the last N non-empty lines of a log file for diagnostics */
85
- const readLogTail = (logFile, lines) => {
86
- let fd = null;
87
- try {
88
- fd = fs.openSync(logFile, "r");
89
- const size = fs.fstatSync(fd).size;
90
- const CHUNK = 8192;
91
- const buf = Buffer.alloc(CHUNK);
92
- let pos = size;
93
- let tail = "";
94
- let count = 0;
95
- while (pos > 0 && count <= lines) {
96
- const n = Math.min(CHUNK, pos);
97
- pos -= n;
98
- fs.readSync(fd, buf, 0, n, pos);
99
- tail = buf.toString("utf8", 0, n) + tail;
100
- count = 0;
101
- for (let i = 0; i < tail.length; i++)
102
- if (tail.charCodeAt(i) === 10)
103
- count++;
92
+ }
93
+ /* clear the persisted port and remove ".ase/service.yaml" if it is empty */
94
+ static clearPort(svc) {
95
+ svc.delete("port");
96
+ const root = svc.get();
97
+ const empty = root === undefined || root === null || (isMap(root) && root.items.length === 0);
98
+ if (empty) {
99
+ if (fs.existsSync(svc.filename))
100
+ fs.rmSync(svc.filename);
101
+ }
102
+ else
103
+ svc.write();
104
+ }
105
+ /* spawn the current executable detached as a background service */
106
+ static spawnDetached(aseDir, port) {
107
+ fs.mkdirSync(aseDir, { recursive: true });
108
+ const logFile = path.join(aseDir, "service.log");
109
+ const fd = fs.openSync(logFile, "a");
110
+ const entry = fileURLToPath(new URL("./ase.js", import.meta.url));
111
+ const child = spawn(process.execPath, [entry, "service", "start"], {
112
+ detached: true,
113
+ env: { ...process.env, [SERVE_ENV]: "1", [PORT_ENV]: String(port) },
114
+ stdio: ["ignore", fd, fd]
115
+ });
116
+ fs.closeSync(fd);
117
+ return { child, logFile };
118
+ }
119
+ /* read the last N non-empty lines of a log file for diagnostics */
120
+ static readLogTail(logFile, lines) {
121
+ let fd = null;
122
+ try {
123
+ fd = fs.openSync(logFile, "r");
124
+ const size = fs.fstatSync(fd).size;
125
+ const CHUNK = 8192;
126
+ const buf = Buffer.alloc(CHUNK);
127
+ let pos = size;
128
+ let tail = "";
129
+ let count = 0;
130
+ while (pos > 0 && count <= lines) {
131
+ const n = Math.min(CHUNK, pos);
132
+ pos -= n;
133
+ fs.readSync(fd, buf, 0, n, pos);
134
+ tail = buf.toString("utf8", 0, n) + tail;
135
+ count = 0;
136
+ for (let i = 0; i < tail.length; i++)
137
+ if (tail.charCodeAt(i) === 10)
138
+ count++;
139
+ }
140
+ const all = tail.split("\n").filter((l) => l.length > 0);
141
+ return all.slice(-lines).join("\n");
142
+ }
143
+ catch {
144
+ return "";
145
+ }
146
+ finally {
147
+ if (fd !== null)
148
+ fs.closeSync(fd);
104
149
  }
105
- const all = tail.split("\n").filter((l) => l.length > 0);
106
- return all.slice(-lines).join("\n");
107
150
  }
108
- catch {
109
- return "";
151
+ }
152
+ /* MCP registration entry point for service-identity tools */
153
+ export class ServiceMCP {
154
+ ctx;
155
+ constructor(ctx) {
156
+ this.ctx = ctx;
110
157
  }
111
- finally {
112
- if (fd !== null)
113
- fs.closeSync(fd);
158
+ register(mcp) {
159
+ mcp.registerTool("ping", {
160
+ title: "ASE service ping",
161
+ description: "Return ASE service identity, port, and uptime.",
162
+ inputSchema: {}
163
+ }, async () => {
164
+ const status = {
165
+ ok: true,
166
+ projectId: this.ctx.projectId,
167
+ port: this.ctx.port,
168
+ uptimeMs: Date.now() - this.ctx.startTime
169
+ };
170
+ return {
171
+ content: [{ type: "text", text: JSON.stringify(status) }]
172
+ };
173
+ });
174
+ mcp.registerTool("timestamp", {
175
+ title: "ASE timestamp",
176
+ description: "Return the current local date/time formatted via a Luxon format string. " +
177
+ "Pass the Luxon format tokens as `format` (default: `yyyy-LL-dd HH:mm`). " +
178
+ "Returns the formatted timestamp as `text`.",
179
+ inputSchema: {
180
+ format: z.string().default("yyyy-LL-dd HH:mm")
181
+ .describe("Luxon format tokens (default: `yyyy-LL-dd HH:mm`)")
182
+ }
183
+ }, async (args) => {
184
+ try {
185
+ const text = DateTime.now().toFormat(args.format);
186
+ return {
187
+ content: [{ type: "text", text }]
188
+ };
189
+ }
190
+ catch (err) {
191
+ const message = err instanceof Error ? err.message : String(err);
192
+ return {
193
+ isError: true,
194
+ content: [{ type: "text", text: `timestamp: format failed: ${message}` }]
195
+ };
196
+ }
197
+ });
114
198
  }
115
- };
199
+ }
116
200
  /* CLI command "ase service" */
117
201
  export default class ServiceCommand {
118
202
  log;
@@ -154,318 +238,13 @@ export default class ServiceCommand {
154
238
  lastActivity = Date.now();
155
239
  return h.continue;
156
240
  });
157
- /* build a fresh MCP server instance with the demo "ping" tool */
241
+ /* build a fresh MCP server instance with all registered tools */
158
242
  const buildMcpServer = () => {
159
243
  const mcp = new McpServer({ name: "ase", version: pkg.version });
160
- mcp.registerTool("ping", {
161
- title: "ASE service ping",
162
- description: "Return ASE service identity, port, and uptime.",
163
- inputSchema: {}
164
- }, async () => {
165
- const status = {
166
- ok: true,
167
- projectId: ctx.projectId,
168
- port: ctx.port,
169
- uptimeMs: Date.now() - startTime
170
- };
171
- return {
172
- content: [{ type: "text", text: JSON.stringify(status) }]
173
- };
174
- });
175
- mcp.registerTool("timestamp", {
176
- title: "ASE timestamp",
177
- description: "Return the current local date/time formatted via a Luxon format string. " +
178
- "Pass the Luxon format tokens as `format` (default: `yyyy-LL-dd HH:mm`). " +
179
- "Returns the formatted timestamp as `text`.",
180
- inputSchema: {
181
- format: z.string().default("yyyy-LL-dd HH:mm")
182
- .describe("Luxon format tokens (default: `yyyy-LL-dd HH:mm`)")
183
- }
184
- }, async (args) => {
185
- try {
186
- const text = DateTime.now().toFormat(args.format);
187
- return {
188
- content: [{ type: "text", text }]
189
- };
190
- }
191
- catch (err) {
192
- const message = err instanceof Error ? err.message : String(err);
193
- return {
194
- isError: true,
195
- content: [{ type: "text", text: `timestamp: format failed: ${message}` }]
196
- };
197
- }
198
- });
199
- mcp.registerTool("diagram", {
200
- title: "ASE diagram render",
201
- description: "Render a Mermaid diagram as Unicode/ASCII art. " +
202
- "Use for visualizing " +
203
- "structure/layout/components/dependencies as a Flowchart, " +
204
- "control-flow/branching/concurrency as a Flowchart, " +
205
- "state-machine/states/transitions as an UML State Diagram, " +
206
- "data-flow/actors/messages/protocols as an UML Sequence Diagram, " +
207
- "data-structure/classes/methods as an UML Class Diagram, " +
208
- "data-model/entities/relationships as an ER Diagram, or " +
209
- "metrics/distributions/time-series as an XY-Chart. " +
210
- "Pass the Mermaid diagram specification as `diagram`. " +
211
- "Returns the rendered art as `text`.",
212
- inputSchema: {
213
- diagram: z.string()
214
- .describe("Mermaid diagram specification"),
215
- ascii: z.boolean().default(false)
216
- .describe("emit plain ASCII (+-|) instead of Unicode box-drawing characters"),
217
- colorMode: z.enum(["none", "ansi16", "ansi256"]).default("none")
218
- .describe("color mode for ANSI escape sequences in the rendered output"),
219
- nodeMarginX: z.number().int().min(0).default(3)
220
- .describe("horizontal margin between nodes, in characters"),
221
- nodeMarginY: z.number().int().min(0).default(3)
222
- .describe("vertical margin between nodes, in lines"),
223
- nodePadding: z.number().int().min(0).default(1)
224
- .describe("inner horizontal and vertical padding within each node, in characters"),
225
- diagramClipX: z.number().int().min(0).default(0)
226
- .describe("extra horizontal clipping: subtract this many characters from `terminalWidth`"),
227
- diagramClipY: z.number().int().min(0).default(0)
228
- .describe("extra vertical clipping: subtract this many lines from `terminalHeight`"),
229
- terminalWidth: z.number().int().min(0).default(detectTermWidth())
230
- .describe("terminal width in characters; 0 disables horizontal clipping; defaults to ASE_TERM_WIDTH env var if set"),
231
- terminalHeight: z.number().int().min(0).default(detectTermHeight())
232
- .describe("terminal height in lines; 0 disables vertical clipping; defaults to ASE_TERM_HEIGHT env var if set")
233
- }
234
- }, async (args) => {
235
- try {
236
- const out = renderDiagram(args.diagram, {
237
- ascii: args.ascii,
238
- colorMode: args.colorMode,
239
- nodeMarginX: args.nodeMarginX,
240
- nodeMarginY: args.nodeMarginY,
241
- nodePadding: args.nodePadding,
242
- diagramClipX: args.diagramClipX,
243
- diagramClipY: args.diagramClipY,
244
- terminalWidth: args.terminalWidth,
245
- terminalHeight: args.terminalHeight
246
- });
247
- return {
248
- content: [{ type: "text", text: out }]
249
- };
250
- }
251
- catch (err) {
252
- const message = err instanceof Error ? err.message : String(err);
253
- return {
254
- isError: true,
255
- content: [{ type: "text", text: `diagram: render failed: ${message}` }]
256
- };
257
- }
258
- });
259
- mcp.registerTool("task_list", {
260
- title: "ASE task list",
261
- description: "List all persisted tasks. " +
262
- "Returns a `tasks` array (in lexicographic `id` order) where each item has the " +
263
- "task `id`. If `verbose` is `true`, each item additionally has an `mtime` field " +
264
- "(last modification time of the task's `plan.md`, formatted as `YYYY-MM-DD HH:MM`). " +
265
- "Returns an empty array if no tasks exist.",
266
- inputSchema: {
267
- verbose: z.boolean().optional()
268
- .describe("if true, also include the `mtime` field per task (default: false)")
269
- },
270
- outputSchema: {
271
- tasks: z.array(z.object({
272
- id: z.string().describe("task identifier"),
273
- mtime: z.string().optional()
274
- .describe("plan.md modification time (`YYYY-MM-DD HH:MM`); only present if `verbose` is true")
275
- })).describe("all persisted tasks in lexicographic id order")
276
- }
277
- }, async (args) => {
278
- try {
279
- const verbose = args.verbose ?? false;
280
- const items = taskList(verbose);
281
- const tasks = verbose ?
282
- items.map((item) => ({ id: item.id, mtime: item.mtime })) :
283
- items.map((item) => ({ id: item.id }));
284
- const result = { tasks };
285
- return {
286
- structuredContent: result,
287
- content: [{ type: "text", text: JSON.stringify(result) }]
288
- };
289
- }
290
- catch (err) {
291
- const message = err instanceof Error ? err.message : String(err);
292
- return {
293
- isError: true,
294
- content: [{ type: "text", text: `task_list: ERROR: ${message}` }]
295
- };
296
- }
297
- });
298
- mcp.registerTool("task_load", {
299
- title: "ASE task load",
300
- description: "Load a previously persisted task by `id`. " +
301
- "Returns the task as `text`; returns an empty string if no task exists for the `id`.",
302
- inputSchema: {
303
- id: z.string()
304
- .describe("task identifier (allowed characters: A-Z, a-z, 0-9, '-')")
305
- }
306
- }, async (args) => {
307
- try {
308
- const text = taskLoad(args.id);
309
- return {
310
- content: [{ type: "text", text }]
311
- };
312
- }
313
- catch (err) {
314
- const message = err instanceof Error ? err.message : String(err);
315
- return {
316
- isError: true,
317
- content: [{ type: "text", text: `task_load: ERROR: ${message}` }]
318
- };
319
- }
320
- });
321
- mcp.registerTool("task_save", {
322
- title: "ASE task save",
323
- description: "Persist a task as `text` under `id`. " +
324
- "Overwrites any existing task for the same `id`.",
325
- inputSchema: {
326
- id: z.string()
327
- .describe("task identifier (allowed characters: A-Z, a-z, 0-9, '-')"),
328
- text: z.string()
329
- .describe("text content of the task")
330
- }
331
- }, async (args) => {
332
- try {
333
- taskSave(args.id, args.text);
334
- return {
335
- content: [{ type: "text", text: `task_save: OK: saved task "${args.id}"` }]
336
- };
337
- }
338
- catch (err) {
339
- const message = err instanceof Error ? err.message : String(err);
340
- return {
341
- isError: true,
342
- content: [{ type: "text", text: `task_save: ERROR: ${message}` }]
343
- };
344
- }
345
- });
346
- mcp.registerTool("task_delete", {
347
- title: "ASE task delete",
348
- description: "Delete a previously persisted task by `id`. " +
349
- "Returns a status `text` indicating whether a task existed and was removed.",
350
- inputSchema: {
351
- id: z.string()
352
- .describe("task identifier (allowed characters: A-Z, a-z, 0-9, '-')")
353
- }
354
- }, async (args) => {
355
- try {
356
- const removed = taskDelete(args.id);
357
- const msg = removed ?
358
- `task_delete: OK: removed task "${args.id}"` :
359
- `task_delete: WARNING: no task "${args.id}" to remove`;
360
- return {
361
- content: [{ type: "text", text: msg }]
362
- };
363
- }
364
- catch (err) {
365
- const message = err instanceof Error ? err.message : String(err);
366
- return {
367
- isError: true,
368
- content: [{ type: "text", text: `task_delete: ERROR: ${message}` }]
369
- };
370
- }
371
- });
372
- mcp.registerTool("persona", {
373
- title: "ASE persona style get/set",
374
- description: "Get or set the active ASE agent persona `style`. " +
375
- "If `style` is provided, it sets the persona style, " +
376
- "otherwise it returns the current persona `style`. " +
377
- "If `session` is provided, the operation is scoped to that session, " +
378
- "otherwise it operates on the broadest scope (user/project cascade). " +
379
- "Allowed styles: \"writer\" (decorative, eloquent, explaining), " +
380
- "\"engineer\" (brief, factual, accurate), " +
381
- "\"telegrapher\" (very brief, factual, abbreviating), " +
382
- "\"caveman\" (ultra brief, rough, stuttering).",
383
- inputSchema: {
384
- style: z.enum(["writer", "engineer", "telegrapher", "caveman"]).optional()
385
- .describe("persona style to set; if omitted, the current persona style is returned"),
386
- session: z.string().optional()
387
- .describe("session identifier (allowed characters: A-Z, a-z, 0-9, '-'); " +
388
- "if omitted, the operation is not scoped to a specific session")
389
- }
390
- }, async (args) => {
391
- try {
392
- const scope = args.session !== undefined ?
393
- parseScope(`session:${args.session}`) :
394
- parseScope(undefined);
395
- const cfg = new Config("config", configSchema, this.log, scope);
396
- cfg.read();
397
- if (args.style !== undefined) {
398
- cfg.set("agent.persona", args.style);
399
- cfg.write();
400
- const where = args.session !== undefined ?
401
- ` for session "${args.session}"` : "";
402
- const msg = `persona: OK: set agent.persona to "${args.style}"${where}`;
403
- return {
404
- content: [{ type: "text", text: msg }]
405
- };
406
- }
407
- const val = cfg.get("agent.persona");
408
- if (val === undefined)
409
- return {
410
- content: [{ type: "text", text: "engineer" }]
411
- };
412
- const text = String(isScalar(val) ? val.value : val);
413
- return {
414
- content: [{ type: "text", text }]
415
- };
416
- }
417
- catch (err) {
418
- const message = err instanceof Error ? err.message : String(err);
419
- return {
420
- isError: true,
421
- content: [{ type: "text", text: `persona: ERROR: ${message}` }]
422
- };
423
- }
424
- });
425
- mcp.registerTool("task_id", {
426
- title: "ASE task id get/set",
427
- description: "Get or set the active ASE task `id` for a given `session`. " +
428
- "If `id` is provided, it sets the task id in the given `session`, " +
429
- "otherwise it returns the current task `id` of the `session`.",
430
- inputSchema: {
431
- id: z.string().optional()
432
- .describe("task identifier to set (allowed characters: A-Z, a-z, 0-9, '-'); " +
433
- "if omitted, the current task id is returned"),
434
- session: z.string()
435
- .describe("session identifier (allowed characters: A-Z, a-z, 0-9, '-')")
436
- }
437
- }, async (args) => {
438
- try {
439
- const scope = parseScope(`session:${args.session}`);
440
- const cfg = new Config("config", configSchema, this.log, scope);
441
- cfg.read();
442
- if (args.id !== undefined) {
443
- cfg.set("agent.task", args.id);
444
- cfg.write();
445
- const msg = `task_id: OK: set agent.task to "${args.id}" ` +
446
- `for session "${args.session}"`;
447
- return {
448
- content: [{ type: "text", text: msg }]
449
- };
450
- }
451
- const val = cfg.get("agent.task");
452
- if (val === undefined)
453
- return {
454
- content: [{ type: "text", text: "" }]
455
- };
456
- const text = String(isScalar(val) ? val.value : val);
457
- return {
458
- content: [{ type: "text", text }]
459
- };
460
- }
461
- catch (err) {
462
- const message = err instanceof Error ? err.message : String(err);
463
- return {
464
- isError: true,
465
- content: [{ type: "text", text: `task_id: ERROR: ${message}` }]
466
- };
467
- }
468
- });
244
+ new ServiceMCP({ projectId: ctx.projectId, port: ctx.port, startTime }).register(mcp);
245
+ new DiagramMCP().register(mcp);
246
+ new TaskMCP(this.log).register(mcp);
247
+ new PersonaMCP(this.log).register(mcp);
469
248
  return mcp;
470
249
  };
471
250
  /* listen to HTTP/REST endpoints */
@@ -559,7 +338,7 @@ export default class ServiceCommand {
559
338
  /* start service */
560
339
  try {
561
340
  await server.start();
562
- persistPort(ctx.svc, ctx.port);
341
+ Service.persistPort(ctx.svc, ctx.port);
563
342
  this.log.write("info", `service: listening on port ${ctx.port}`);
564
343
  }
565
344
  catch (err) {
@@ -570,7 +349,7 @@ export default class ServiceCommand {
570
349
  if (match === true)
571
350
  process.exit(0);
572
351
  this.log.write("error", `service: port ${ctx.port} in use, but not responding!`);
573
- clearPort(ctx.svc);
352
+ Service.clearPort(ctx.svc);
574
353
  process.exit(1);
575
354
  }
576
355
  this.log.write("error", `service: ${e.message}`);
@@ -585,13 +364,13 @@ export default class ServiceCommand {
585
364
  this.log.write("info", "service: idle timeout reached, stopping");
586
365
  try {
587
366
  await server.stop({ timeout: 1000 });
588
- clearPort(ctx.svc);
367
+ Service.clearPort(ctx.svc);
589
368
  process.exit(0);
590
369
  }
591
370
  catch (err) {
592
371
  const e = err;
593
372
  this.log.write("error", `service: stop failed: ${e.message}`);
594
- clearPort(ctx.svc);
373
+ Service.clearPort(ctx.svc);
595
374
  process.exit(1);
596
375
  }
597
376
  }
@@ -603,7 +382,7 @@ export default class ServiceCommand {
603
382
  let port = ctx.port;
604
383
  if (process.env[SERVE_ENV] === "1") {
605
384
  const raw = process.env[PORT_ENV];
606
- port = raw !== undefined ? Number(raw) : await allocatePort();
385
+ port = raw !== undefined ? Number(raw) : await Service.allocatePort();
607
386
  await this.runService({ ...ctx, port });
608
387
  return await new Promise(() => { });
609
388
  }
@@ -618,8 +397,8 @@ export default class ServiceCommand {
618
397
  re-allocate, re-persist, re-spawn; early-break on foreign listener */
619
398
  let lastErr = new Error("service failed to start within timeout");
620
399
  for (let attempt = 0; attempt < 3; attempt++) {
621
- port = await allocatePort();
622
- const { child, logFile } = spawnDetached(ctx.aseDir, port);
400
+ port = await Service.allocatePort();
401
+ const { child, logFile } = Service.spawnDetached(ctx.aseDir, port);
623
402
  let exited = false;
624
403
  let exitCode = null;
625
404
  let resolveExit = () => { };
@@ -651,7 +430,7 @@ export default class ServiceCommand {
651
430
  break;
652
431
  }
653
432
  }
654
- const tail = readLogTail(logFile, 20);
433
+ const tail = Service.readLogTail(logFile, 20);
655
434
  const reason = exited ?
656
435
  `service exited during startup (code ${exitCode})` :
657
436
  foreign ?
@@ -675,7 +454,7 @@ export default class ServiceCommand {
675
454
  }
676
455
  }
677
456
  }
678
- clearPort(ctx.svc);
457
+ Service.clearPort(ctx.svc);
679
458
  throw lastErr;
680
459
  }
681
460
  /* status flow: report whether the service is running */
@@ -754,7 +533,7 @@ export default class ServiceCommand {
754
533
  }
755
534
  if (match === null) {
756
535
  this.log.write("info", `service: not running (port ${ctx.port} not responding)`);
757
- clearPort(ctx.svc);
536
+ Service.clearPort(ctx.svc);
758
537
  return 0;
759
538
  }
760
539
  const r = await axios.request({
@@ -764,7 +543,7 @@ export default class ServiceCommand {
764
543
  validateStatus: () => true
765
544
  });
766
545
  const ok = r.status >= 200 && r.status < 300;
767
- clearPort(ctx.svc);
546
+ Service.clearPort(ctx.svc);
768
547
  return ok ? 0 : 1;
769
548
  }
770
549
  /* register commands */