@mandujs/mcp 0.19.5 → 0.20.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/package.json +45 -44
- package/src/index.ts +0 -0
- package/src/new-resources.ts +12 -0
- package/src/tools/ai-brief.ts +443 -0
- package/src/tools/deploy-preview.ts +316 -0
- package/src/tools/index.ts +14 -0
- package/src/tools/loop-close.ts +175 -0
- package/src/tools/run-tests.ts +424 -0
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP tool — `mandu.run.tests`
|
|
3
|
+
*
|
|
4
|
+
* Invokes `mandu test` as a child process via `Bun.spawn` and parses the
|
|
5
|
+
* resulting Bun test output into a structured summary:
|
|
6
|
+
*
|
|
7
|
+
* { passed, failed, skipped, failing_tests: [{ name, file, error }] }
|
|
8
|
+
*
|
|
9
|
+
* Design notes:
|
|
10
|
+
* • Input is validated against a minimal runtime schema (see `validateInput`).
|
|
11
|
+
* Bad input produces a structured `{ error, field, hint }` object — the
|
|
12
|
+
* error-handler's `isSoftErrorResult` detector will surface this as
|
|
13
|
+
* `isError: true` to MCP clients.
|
|
14
|
+
* • If no test files are discovered we return `{ passed: 0, failed: 0,
|
|
15
|
+
* skipped: 0, note: "no test files" }` without failing the caller.
|
|
16
|
+
* • The child process is spawned with a 10-minute ceiling via Promise.race —
|
|
17
|
+
* well above normal test suites but short enough that a stuck process
|
|
18
|
+
* never hangs the MCP server.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { Tool } from "@modelcontextprotocol/sdk/types.js";
|
|
22
|
+
import { spawn } from "bun";
|
|
23
|
+
import path from "path";
|
|
24
|
+
|
|
25
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
26
|
+
// Types
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
28
|
+
|
|
29
|
+
type RunTarget = "unit" | "integration" | "e2e" | "all";
|
|
30
|
+
|
|
31
|
+
interface RunTestsInput {
|
|
32
|
+
target?: RunTarget;
|
|
33
|
+
filter?: string;
|
|
34
|
+
coverage?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface FailingTest {
|
|
38
|
+
name: string;
|
|
39
|
+
file?: string;
|
|
40
|
+
error?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface RunTestsResult {
|
|
44
|
+
target: RunTarget;
|
|
45
|
+
passed: number;
|
|
46
|
+
failed: number;
|
|
47
|
+
skipped: number;
|
|
48
|
+
duration_ms?: number;
|
|
49
|
+
failing_tests: FailingTest[];
|
|
50
|
+
exit_code: number;
|
|
51
|
+
note?: string;
|
|
52
|
+
/** Trailing 2000 chars of stdout for diagnostic context. */
|
|
53
|
+
stdout_tail?: string;
|
|
54
|
+
/** Trailing 2000 chars of stderr for diagnostic context. */
|
|
55
|
+
stderr_tail?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
59
|
+
// Validation
|
|
60
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
61
|
+
|
|
62
|
+
const VALID_TARGETS = new Set<RunTarget>(["unit", "integration", "e2e", "all"]);
|
|
63
|
+
const COMMAND_TIMEOUT_MS = 10 * 60_000;
|
|
64
|
+
|
|
65
|
+
function validateInput(raw: Record<string, unknown>): {
|
|
66
|
+
ok: true;
|
|
67
|
+
value: Required<Pick<RunTestsInput, "target" | "coverage">> &
|
|
68
|
+
Pick<RunTestsInput, "filter">;
|
|
69
|
+
} | { ok: false; error: string; field: string; hint: string } {
|
|
70
|
+
const target = raw.target ?? "all";
|
|
71
|
+
if (typeof target !== "string" || !VALID_TARGETS.has(target as RunTarget)) {
|
|
72
|
+
return {
|
|
73
|
+
ok: false,
|
|
74
|
+
error: "Invalid 'target' — expected 'unit', 'integration', 'e2e', or 'all'",
|
|
75
|
+
field: "target",
|
|
76
|
+
hint: "Omit to default to 'all'",
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const filter = raw.filter;
|
|
81
|
+
if (filter !== undefined && typeof filter !== "string") {
|
|
82
|
+
return {
|
|
83
|
+
ok: false,
|
|
84
|
+
error: "'filter' must be a string",
|
|
85
|
+
field: "filter",
|
|
86
|
+
hint: "Pass a bun-test filter pattern, e.g. 'my-describe > my-case'",
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const coverage = raw.coverage;
|
|
91
|
+
if (coverage !== undefined && typeof coverage !== "boolean") {
|
|
92
|
+
return {
|
|
93
|
+
ok: false,
|
|
94
|
+
error: "'coverage' must be a boolean",
|
|
95
|
+
field: "coverage",
|
|
96
|
+
hint: "Pass true to emit a coverage report",
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
ok: true,
|
|
102
|
+
value: {
|
|
103
|
+
target: target as RunTarget,
|
|
104
|
+
coverage: coverage === true,
|
|
105
|
+
...(typeof filter === "string" && filter.length > 0 ? { filter } : {}),
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
111
|
+
// Parser — Bun test output → RunTestsResult
|
|
112
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Parse bun-test style output. Bun emits:
|
|
116
|
+
* `(pass) describe > test`
|
|
117
|
+
* `(fail) describe > test`
|
|
118
|
+
* `(skip) describe > test`
|
|
119
|
+
*
|
|
120
|
+
* And a trailing summary block:
|
|
121
|
+
* `N pass`
|
|
122
|
+
* `M fail`
|
|
123
|
+
* `K skipped`
|
|
124
|
+
* `Ran ... tests across ... files. [x.xxs]`
|
|
125
|
+
*
|
|
126
|
+
* This parser is intentionally forgiving: counts are taken from the
|
|
127
|
+
* explicit summary lines when present, else derived from `(pass|fail|skip)`
|
|
128
|
+
* markers.
|
|
129
|
+
*/
|
|
130
|
+
export function parseBunTestOutput(raw: string): {
|
|
131
|
+
passed: number;
|
|
132
|
+
failed: number;
|
|
133
|
+
skipped: number;
|
|
134
|
+
duration_ms?: number;
|
|
135
|
+
failing_tests: FailingTest[];
|
|
136
|
+
} {
|
|
137
|
+
const lines = raw.split(/\r?\n/);
|
|
138
|
+
let passed = 0;
|
|
139
|
+
let failed = 0;
|
|
140
|
+
let skipped = 0;
|
|
141
|
+
let duration_ms: number | undefined;
|
|
142
|
+
const failing_tests: FailingTest[] = [];
|
|
143
|
+
|
|
144
|
+
let currentFile: string | undefined;
|
|
145
|
+
let pendingFailure: FailingTest | null = null;
|
|
146
|
+
|
|
147
|
+
for (const line of lines) {
|
|
148
|
+
const trimmed = line.trim();
|
|
149
|
+
|
|
150
|
+
// Track the current file heading (e.g. "src/foo.test.ts:"):
|
|
151
|
+
const fileMatch = /^([^\s()]+\.(?:test|spec)\.(?:ts|tsx|js|jsx|mjs|cjs)):$/.exec(trimmed);
|
|
152
|
+
if (fileMatch) {
|
|
153
|
+
currentFile = fileMatch[1];
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// `(fail) ...` → start a failing test record.
|
|
158
|
+
const failMatch = /^\(fail\)\s+(.+)$/.exec(trimmed);
|
|
159
|
+
if (failMatch) {
|
|
160
|
+
if (pendingFailure) {
|
|
161
|
+
failing_tests.push(pendingFailure);
|
|
162
|
+
}
|
|
163
|
+
pendingFailure = {
|
|
164
|
+
name: failMatch[1].trim(),
|
|
165
|
+
file: currentFile,
|
|
166
|
+
};
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// `(skip) ...` counts as skipped but doesn't emit a record.
|
|
171
|
+
if (/^\(skip\)\s+/.test(trimmed)) {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// If we're inside a failure block, capture the first few non-empty
|
|
176
|
+
// lines that follow as error context.
|
|
177
|
+
if (pendingFailure && trimmed.length > 0) {
|
|
178
|
+
const isNextTestMarker = /^\(pass|fail|skip\)/.test(trimmed);
|
|
179
|
+
if (!isNextTestMarker) {
|
|
180
|
+
pendingFailure.error = pendingFailure.error
|
|
181
|
+
? `${pendingFailure.error}\n${trimmed}`
|
|
182
|
+
: trimmed;
|
|
183
|
+
// Cap the captured error to keep payloads tight.
|
|
184
|
+
if (pendingFailure.error.length > 800) {
|
|
185
|
+
pendingFailure.error = pendingFailure.error.slice(0, 800);
|
|
186
|
+
}
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// End-of-block marker flushes the current failure record.
|
|
192
|
+
if (pendingFailure && (trimmed.length === 0 || /^\d+\s+(pass|fail|skipped)\b/.test(trimmed))) {
|
|
193
|
+
failing_tests.push(pendingFailure);
|
|
194
|
+
pendingFailure = null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// Totals block — authoritative if present.
|
|
198
|
+
const passMatch = /^(\d+)\s+pass\b/.exec(trimmed);
|
|
199
|
+
if (passMatch) {
|
|
200
|
+
passed = Number(passMatch[1]);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const failSumMatch = /^(\d+)\s+fail\b/.exec(trimmed);
|
|
204
|
+
if (failSumMatch) {
|
|
205
|
+
failed = Number(failSumMatch[1]);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
const skipMatch = /^(\d+)\s+skipped\b/.exec(trimmed);
|
|
209
|
+
if (skipMatch) {
|
|
210
|
+
skipped = Number(skipMatch[1]);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Duration: `Ran 123 tests across 10 files. [1.23s]`
|
|
215
|
+
const dur = /\[([\d.]+)s\]/.exec(trimmed);
|
|
216
|
+
if (dur && /Ran\s+\d+\s+tests/.test(trimmed)) {
|
|
217
|
+
duration_ms = Math.round(Number(dur[1]) * 1000);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (pendingFailure) {
|
|
222
|
+
failing_tests.push(pendingFailure);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return { passed, failed, skipped, duration_ms, failing_tests };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
229
|
+
// Child process invocation
|
|
230
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
231
|
+
|
|
232
|
+
function tailString(s: string, max = 2000): string {
|
|
233
|
+
if (s.length <= max) return s;
|
|
234
|
+
return s.slice(-max);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function isNoTestFilesSignal(stdout: string, stderr: string): boolean {
|
|
238
|
+
// Bun reports "0 tests" or exits with a "No tests found" banner depending
|
|
239
|
+
// on version. We match on both variants.
|
|
240
|
+
const combined = `${stdout}\n${stderr}`;
|
|
241
|
+
if (/Ran\s+0\s+tests/i.test(combined)) return true;
|
|
242
|
+
if (/No tests found/i.test(combined)) return true;
|
|
243
|
+
if (/no test files/i.test(combined)) return true;
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Resolve the `mandu` CLI entry. We prefer the workspace binary
|
|
249
|
+
* (`packages/cli/src/main.ts`) when running inside the monorepo, else
|
|
250
|
+
* fall back to `mandu` on PATH.
|
|
251
|
+
*
|
|
252
|
+
* The CLI entry is invoked directly via `bun run <path>` so users get
|
|
253
|
+
* the version bundled with their project without relying on global installs.
|
|
254
|
+
*/
|
|
255
|
+
async function resolveManduCommand(projectRoot: string): Promise<string[]> {
|
|
256
|
+
// Prefer a local `.bin/mandu` if the project installed `@mandujs/cli`.
|
|
257
|
+
const localBin = path.join(projectRoot, "node_modules", ".bin", "mandu");
|
|
258
|
+
try {
|
|
259
|
+
const f = Bun.file(localBin);
|
|
260
|
+
if (await f.exists()) {
|
|
261
|
+
return ["bun", "run", localBin];
|
|
262
|
+
}
|
|
263
|
+
} catch {}
|
|
264
|
+
|
|
265
|
+
// Monorepo: packages/cli/src/main.ts is directly executable via bun.
|
|
266
|
+
const monorepoCli = path.resolve(projectRoot, "packages", "cli", "src", "main.ts");
|
|
267
|
+
try {
|
|
268
|
+
const f = Bun.file(monorepoCli);
|
|
269
|
+
if (await f.exists()) {
|
|
270
|
+
return ["bun", "run", monorepoCli];
|
|
271
|
+
}
|
|
272
|
+
} catch {}
|
|
273
|
+
|
|
274
|
+
// Fallback: rely on PATH.
|
|
275
|
+
return ["mandu"];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async function runProcess(
|
|
279
|
+
cmd: string[],
|
|
280
|
+
cwd: string,
|
|
281
|
+
timeoutMs: number,
|
|
282
|
+
): Promise<{ stdout: string; stderr: string; exitCode: number; timedOut: boolean }> {
|
|
283
|
+
const proc = spawn(cmd, {
|
|
284
|
+
cwd,
|
|
285
|
+
stdout: "pipe",
|
|
286
|
+
stderr: "pipe",
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
let timedOut = false;
|
|
290
|
+
const timeoutHandle: ReturnType<typeof setTimeout> = setTimeout(() => {
|
|
291
|
+
timedOut = true;
|
|
292
|
+
try {
|
|
293
|
+
proc.kill();
|
|
294
|
+
} catch {}
|
|
295
|
+
}, timeoutMs);
|
|
296
|
+
|
|
297
|
+
try {
|
|
298
|
+
const [stdout, stderr, exitCode] = await Promise.all([
|
|
299
|
+
new Response(proc.stdout).text(),
|
|
300
|
+
new Response(proc.stderr).text(),
|
|
301
|
+
proc.exited,
|
|
302
|
+
]);
|
|
303
|
+
return { stdout, stderr, exitCode: exitCode ?? 1, timedOut };
|
|
304
|
+
} finally {
|
|
305
|
+
clearTimeout(timeoutHandle);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
310
|
+
// Public handler
|
|
311
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
312
|
+
|
|
313
|
+
async function runManduTests(
|
|
314
|
+
projectRoot: string,
|
|
315
|
+
input: RunTestsInput,
|
|
316
|
+
): Promise<RunTestsResult | { error: string; field?: string; hint?: string }> {
|
|
317
|
+
const validated = validateInput(input as Record<string, unknown>);
|
|
318
|
+
if (!validated.ok) {
|
|
319
|
+
return {
|
|
320
|
+
error: validated.error,
|
|
321
|
+
field: validated.field,
|
|
322
|
+
hint: validated.hint,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const { target, filter, coverage } = validated.value;
|
|
327
|
+
|
|
328
|
+
const base = await resolveManduCommand(projectRoot);
|
|
329
|
+
const args = [...base, "test"];
|
|
330
|
+
if (target !== "all") args.push(target);
|
|
331
|
+
if (filter) args.push("--filter", filter);
|
|
332
|
+
if (coverage) args.push("--coverage");
|
|
333
|
+
|
|
334
|
+
let proc: { stdout: string; stderr: string; exitCode: number; timedOut: boolean };
|
|
335
|
+
try {
|
|
336
|
+
proc = await runProcess(args, projectRoot, COMMAND_TIMEOUT_MS);
|
|
337
|
+
} catch (err) {
|
|
338
|
+
return {
|
|
339
|
+
error: `Failed to spawn test runner: ${err instanceof Error ? err.message : String(err)}`,
|
|
340
|
+
hint: "Verify that @mandujs/cli is installed and accessible",
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// No-tests case: the caller gets a benign zeroed summary.
|
|
345
|
+
if (isNoTestFilesSignal(proc.stdout, proc.stderr) && proc.exitCode !== 0) {
|
|
346
|
+
return {
|
|
347
|
+
target,
|
|
348
|
+
passed: 0,
|
|
349
|
+
failed: 0,
|
|
350
|
+
skipped: 0,
|
|
351
|
+
failing_tests: [],
|
|
352
|
+
exit_code: proc.exitCode,
|
|
353
|
+
note: "no test files",
|
|
354
|
+
stdout_tail: tailString(proc.stdout),
|
|
355
|
+
stderr_tail: tailString(proc.stderr),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const parsed = parseBunTestOutput(`${proc.stdout}\n${proc.stderr}`);
|
|
360
|
+
|
|
361
|
+
const result: RunTestsResult = {
|
|
362
|
+
target,
|
|
363
|
+
passed: parsed.passed,
|
|
364
|
+
failed: parsed.failed,
|
|
365
|
+
skipped: parsed.skipped,
|
|
366
|
+
failing_tests: parsed.failing_tests,
|
|
367
|
+
exit_code: proc.exitCode,
|
|
368
|
+
stdout_tail: tailString(proc.stdout),
|
|
369
|
+
stderr_tail: tailString(proc.stderr),
|
|
370
|
+
};
|
|
371
|
+
if (parsed.duration_ms !== undefined) result.duration_ms = parsed.duration_ms;
|
|
372
|
+
if (proc.timedOut) result.note = "timed out";
|
|
373
|
+
if (parsed.passed === 0 && parsed.failed === 0 && parsed.skipped === 0) {
|
|
374
|
+
result.note = result.note ?? "no test files";
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
return result;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
381
|
+
// MCP tool definition + handler map
|
|
382
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
383
|
+
|
|
384
|
+
export const runTestsToolDefinitions: Tool[] = [
|
|
385
|
+
{
|
|
386
|
+
name: "mandu.run.tests",
|
|
387
|
+
description:
|
|
388
|
+
"Run the project's tests via `mandu test` and return a structured summary: passed / failed / skipped counts plus a list of failing tests with file and error context. Safe to call repeatedly — no writes, just spawns the child process and parses its output.",
|
|
389
|
+
annotations: {
|
|
390
|
+
readOnlyHint: true,
|
|
391
|
+
},
|
|
392
|
+
inputSchema: {
|
|
393
|
+
type: "object",
|
|
394
|
+
properties: {
|
|
395
|
+
target: {
|
|
396
|
+
type: "string",
|
|
397
|
+
enum: ["unit", "integration", "e2e", "all"],
|
|
398
|
+
description:
|
|
399
|
+
"Which test target to run (default: 'all'). Maps directly to `mandu test <target>`.",
|
|
400
|
+
},
|
|
401
|
+
filter: {
|
|
402
|
+
type: "string",
|
|
403
|
+
description:
|
|
404
|
+
"Forward `--filter <pattern>` to `bun test` — restricts to matching describe/it names.",
|
|
405
|
+
},
|
|
406
|
+
coverage: {
|
|
407
|
+
type: "boolean",
|
|
408
|
+
description: "Pass `--coverage` to emit a coverage report (default: false).",
|
|
409
|
+
},
|
|
410
|
+
},
|
|
411
|
+
required: [],
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
];
|
|
415
|
+
|
|
416
|
+
export function runTestsTools(projectRoot: string) {
|
|
417
|
+
const handlers: Record<string, (args: Record<string, unknown>) => Promise<unknown>> = {
|
|
418
|
+
"mandu.run.tests": async (args) => runManduTests(projectRoot, args as RunTestsInput),
|
|
419
|
+
};
|
|
420
|
+
return handlers;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Re-export with canonical snake-case alias for parsimony (used by tests).
|
|
424
|
+
export { parseBunTestOutput as parseTestOutput };
|