@mandujs/mcp 0.36.2 → 0.37.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 (153) hide show
  1. package/package.json +4 -4
  2. package/src/activity-adapter.ts +23 -23
  3. package/src/adapters/index.ts +20 -20
  4. package/src/adapters/monitor-adapter.ts +100 -100
  5. package/src/adapters/tool-adapter.ts +90 -90
  6. package/src/executor/index.ts +22 -22
  7. package/src/executor/tool-executor.ts +148 -148
  8. package/src/hooks/config-watcher.ts +173 -173
  9. package/src/hooks/index.ts +23 -23
  10. package/src/hooks/mcp-hooks.ts +227 -227
  11. package/src/index.ts +0 -0
  12. package/src/logging/index.ts +15 -15
  13. package/src/logging/mcp-transport.ts +134 -134
  14. package/src/profiles.ts +34 -34
  15. package/src/registry/index.ts +13 -13
  16. package/src/registry/mcp-tool-registry.ts +298 -298
  17. package/src/resources/skills/guides.ts +1136 -1136
  18. package/src/resources/skills/index.ts +12 -12
  19. package/src/resources/skills/mandu-composition/SKILL.md +91 -91
  20. package/src/resources/skills/mandu-composition/metadata.json +13 -13
  21. package/src/resources/skills/mandu-composition/rules/_sections.md +26 -26
  22. package/src/resources/skills/mandu-composition/rules/_template.md +77 -77
  23. package/src/resources/skills/mandu-composition/rules/comp-arch-avoid-boolean-props.md +146 -146
  24. package/src/resources/skills/mandu-composition/rules/comp-arch-compound-components.md +164 -164
  25. package/src/resources/skills/mandu-composition/rules/comp-island-event.md +161 -161
  26. package/src/resources/skills/mandu-composition/rules/comp-island-slot-split.md +167 -167
  27. package/src/resources/skills/mandu-composition/rules/comp-pattern-children.md +149 -149
  28. package/src/resources/skills/mandu-composition/rules/comp-state-context-interface.md +148 -148
  29. package/src/resources/skills/mandu-composition/rules/comp-state-lift-state.md +150 -150
  30. package/src/resources/skills/mandu-deployment/SKILL.md +92 -92
  31. package/src/resources/skills/mandu-deployment/_sections.md +41 -41
  32. package/src/resources/skills/mandu-deployment/_template.md +38 -38
  33. package/src/resources/skills/mandu-deployment/metadata.json +13 -13
  34. package/src/resources/skills/mandu-deployment/rules/db-provider-supabase.md +300 -300
  35. package/src/resources/skills/mandu-deployment/rules/deploy-build-bun.md +109 -109
  36. package/src/resources/skills/mandu-deployment/rules/deploy-build-output.md +115 -115
  37. package/src/resources/skills/mandu-deployment/rules/deploy-cicd-github.md +219 -219
  38. package/src/resources/skills/mandu-deployment/rules/deploy-docker-bun.md +150 -150
  39. package/src/resources/skills/mandu-deployment/rules/deploy-docker-compose.md +223 -223
  40. package/src/resources/skills/mandu-deployment/rules/deploy-platform-fly.md +152 -152
  41. package/src/resources/skills/mandu-deployment/rules/deploy-platform-render.md +179 -179
  42. package/src/resources/skills/mandu-deployment/rules/deploy-platform-vercel.md +140 -140
  43. package/src/resources/skills/mandu-fs-routes/SKILL.md +82 -82
  44. package/src/resources/skills/mandu-fs-routes/metadata.json +12 -12
  45. package/src/resources/skills/mandu-fs-routes/rules/_sections.md +36 -36
  46. package/src/resources/skills/mandu-fs-routes/rules/_template.md +69 -69
  47. package/src/resources/skills/mandu-fs-routes/rules/routes-api-methods.md +65 -65
  48. package/src/resources/skills/mandu-fs-routes/rules/routes-dynamic-param.md +93 -93
  49. package/src/resources/skills/mandu-fs-routes/rules/routes-naming-page.md +55 -55
  50. package/src/resources/skills/mandu-guard/SKILL.md +129 -129
  51. package/src/resources/skills/mandu-guard/metadata.json +12 -12
  52. package/src/resources/skills/mandu-guard/rules/_sections.md +36 -36
  53. package/src/resources/skills/mandu-guard/rules/_template.md +82 -82
  54. package/src/resources/skills/mandu-guard/rules/guard-config-rules.md +100 -100
  55. package/src/resources/skills/mandu-guard/rules/guard-layer-direction.md +76 -76
  56. package/src/resources/skills/mandu-guard/rules/guard-preset-mandu.md +81 -81
  57. package/src/resources/skills/mandu-guard/rules/guard-validate-import.md +80 -80
  58. package/src/resources/skills/mandu-hydration/SKILL.md +91 -91
  59. package/src/resources/skills/mandu-hydration/metadata.json +12 -12
  60. package/src/resources/skills/mandu-hydration/rules/_sections.md +31 -31
  61. package/src/resources/skills/mandu-hydration/rules/_template.md +72 -72
  62. package/src/resources/skills/mandu-hydration/rules/hydration-data-event.md +109 -109
  63. package/src/resources/skills/mandu-hydration/rules/hydration-directive-use-client.md +55 -55
  64. package/src/resources/skills/mandu-hydration/rules/hydration-island-setup.md +113 -113
  65. package/src/resources/skills/mandu-hydration/rules/hydration-priority-visible.md +68 -68
  66. package/src/resources/skills/mandu-performance/SKILL.md +85 -85
  67. package/src/resources/skills/mandu-performance/metadata.json +14 -14
  68. package/src/resources/skills/mandu-performance/rules/_sections.md +31 -31
  69. package/src/resources/skills/mandu-performance/rules/_template.md +64 -64
  70. package/src/resources/skills/mandu-performance/rules/perf-async-defer-await.md +103 -103
  71. package/src/resources/skills/mandu-performance/rules/perf-async-parallel.md +95 -95
  72. package/src/resources/skills/mandu-performance/rules/perf-bun-file.md +124 -124
  73. package/src/resources/skills/mandu-performance/rules/perf-bun-serve.md +125 -125
  74. package/src/resources/skills/mandu-performance/rules/perf-bundle-imports.md +80 -80
  75. package/src/resources/skills/mandu-performance/rules/perf-bundle-island-lazy.md +145 -145
  76. package/src/resources/skills/mandu-performance/rules/perf-cache-react.md +98 -98
  77. package/src/resources/skills/mandu-performance/rules/perf-render-transitions.md +154 -154
  78. package/src/resources/skills/mandu-security/SKILL.md +87 -87
  79. package/src/resources/skills/mandu-security/metadata.json +13 -13
  80. package/src/resources/skills/mandu-security/rules/_sections.md +31 -31
  81. package/src/resources/skills/mandu-security/rules/_template.md +74 -74
  82. package/src/resources/skills/mandu-security/rules/sec-auth-guard.md +127 -127
  83. package/src/resources/skills/mandu-security/rules/sec-env-management.md +133 -133
  84. package/src/resources/skills/mandu-security/rules/sec-input-validate.md +148 -148
  85. package/src/resources/skills/mandu-security/rules/sec-protect-csrf.md +146 -146
  86. package/src/resources/skills/mandu-security/rules/sec-protect-headers.md +138 -138
  87. package/src/resources/skills/mandu-slot/SKILL.md +85 -85
  88. package/src/resources/skills/mandu-slot/metadata.json +12 -12
  89. package/src/resources/skills/mandu-slot/rules/_sections.md +36 -36
  90. package/src/resources/skills/mandu-slot/rules/_template.md +63 -63
  91. package/src/resources/skills/mandu-slot/rules/slot-basic-structure.md +38 -38
  92. package/src/resources/skills/mandu-slot/rules/slot-ctx-response.md +56 -56
  93. package/src/resources/skills/mandu-slot/rules/slot-guard-auth.md +59 -59
  94. package/src/resources/skills/mandu-slot/rules/slot-http-methods.md +64 -64
  95. package/src/resources/skills/mandu-styling/SKILL.md +154 -154
  96. package/src/resources/skills/mandu-styling/_sections.md +43 -43
  97. package/src/resources/skills/mandu-styling/_template.md +32 -32
  98. package/src/resources/skills/mandu-styling/metadata.json +15 -15
  99. package/src/resources/skills/mandu-styling/rules/style-component-compound.md +235 -235
  100. package/src/resources/skills/mandu-styling/rules/style-component-slots.md +255 -255
  101. package/src/resources/skills/mandu-styling/rules/style-component-tokens.md +205 -205
  102. package/src/resources/skills/mandu-styling/rules/style-island-animations.md +272 -272
  103. package/src/resources/skills/mandu-styling/rules/style-island-scoping.md +167 -167
  104. package/src/resources/skills/mandu-styling/rules/style-island-variants.md +221 -221
  105. package/src/resources/skills/mandu-styling/rules/style-perf-critical.md +209 -209
  106. package/src/resources/skills/mandu-styling/rules/style-perf-purge.md +192 -192
  107. package/src/resources/skills/mandu-styling/rules/style-setup-modules.md +162 -162
  108. package/src/resources/skills/mandu-styling/rules/style-setup-panda.md +164 -164
  109. package/src/resources/skills/mandu-styling/rules/style-setup-tailwind.md +170 -170
  110. package/src/resources/skills/mandu-styling/rules/style-tailwind-v4-gotchas.md +179 -179
  111. package/src/resources/skills/mandu-styling/rules/style-theme-darkmode.md +229 -229
  112. package/src/resources/skills/mandu-testing/SKILL.md +99 -99
  113. package/src/resources/skills/mandu-testing/metadata.json +13 -13
  114. package/src/resources/skills/mandu-testing/rules/_sections.md +26 -26
  115. package/src/resources/skills/mandu-testing/rules/_template.md +65 -65
  116. package/src/resources/skills/mandu-testing/rules/test-component-island.md +195 -195
  117. package/src/resources/skills/mandu-testing/rules/test-e2e-playwright.md +196 -196
  118. package/src/resources/skills/mandu-testing/rules/test-mock-fetch.md +219 -219
  119. package/src/resources/skills/mandu-testing/rules/test-slot-unit.md +192 -192
  120. package/src/resources/skills/mandu-ui/SKILL.md +117 -117
  121. package/src/resources/skills/mandu-ui/_sections.md +23 -23
  122. package/src/resources/skills/mandu-ui/_template.md +32 -32
  123. package/src/resources/skills/mandu-ui/metadata.json +13 -13
  124. package/src/resources/skills/mandu-ui/rules/ui-accessibility-aria.md +232 -232
  125. package/src/resources/skills/mandu-ui/rules/ui-accessibility-focus.md +238 -238
  126. package/src/resources/skills/mandu-ui/rules/ui-composition-patterns.md +259 -259
  127. package/src/resources/skills/mandu-ui/rules/ui-island-integration.md +258 -258
  128. package/src/resources/skills/mandu-ui/rules/ui-radix-patterns.md +213 -213
  129. package/src/resources/skills/mandu-ui/rules/ui-shadcn-setup.md +209 -209
  130. package/src/resources/skills/recipes.ts +932 -932
  131. package/src/server.ts +3 -3
  132. package/src/tools/ate-exemplar.ts +92 -92
  133. package/src/tools/ate-flakes.ts +90 -90
  134. package/src/tools/ate-mutate.ts +103 -103
  135. package/src/tools/ate-mutation-report.ts +64 -64
  136. package/src/tools/ate-oracle-pending.ts +49 -49
  137. package/src/tools/ate-oracle-replay.ts +44 -44
  138. package/src/tools/ate-oracle-verdict.ts +70 -70
  139. package/src/tools/ate-prompt.ts +146 -146
  140. package/src/tools/brain.ts +3 -3
  141. package/src/tools/deploy-plan.ts +378 -378
  142. package/src/tools/deploy-preview.ts +316 -316
  143. package/src/tools/design.ts +825 -825
  144. package/src/tools/docs.ts +350 -350
  145. package/src/tools/kitchen.ts +109 -23
  146. package/src/tools/lint.ts +226 -226
  147. package/src/tools/loop-close.ts +175 -175
  148. package/src/tools/negotiate.ts +263 -263
  149. package/src/tools/run-tests.ts +424 -424
  150. package/src/tools/runtime.ts +1 -1
  151. package/src/tools/slot-validation.ts +199 -199
  152. package/src/tx-lock.ts +73 -73
  153. package/src/utils/runtime-control.ts +52 -52
@@ -1,424 +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 };
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 };