@mandujs/mcp 0.30.0 → 0.32.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.
@@ -10,6 +10,8 @@ import {
10
10
  applyHealing,
11
11
  healAll,
12
12
  explainRule,
13
+ // Follow-up E — type-aware lint bridge
14
+ runTsgolint,
13
15
  type GuardConfig,
14
16
  type ViolationType,
15
17
  type GuardPreset,
@@ -20,7 +22,7 @@ export const guardToolDefinitions: Tool[] = [
20
22
  {
21
23
  name: "mandu.guard.check",
22
24
  description:
23
- "Run guard checks to validate spec integrity, generated files, and slot files",
25
+ "Run guard checks to validate spec integrity, generated files, and slot files. Set typeAware=true to additionally run `oxlint --type-aware` (tsgolint) and merge its results.",
24
26
  annotations: {
25
27
  readOnlyHint: true,
26
28
  },
@@ -31,6 +33,11 @@ export const guardToolDefinitions: Tool[] = [
31
33
  type: "boolean",
32
34
  description: "If true, attempt to automatically fix violations",
33
35
  },
36
+ typeAware: {
37
+ type: "boolean",
38
+ description:
39
+ "If true, invoke `oxlint --type-aware` after the architecture check and include its violations in the response under `typeAware`. When the config has `guard.typeAware` set, this defaults to true; set false to opt out for a single call.",
40
+ },
34
41
  },
35
42
  required: [],
36
43
  },
@@ -119,7 +126,10 @@ export function guardTools(projectRoot: string) {
119
126
 
120
127
  const handlers: Record<string, (args: Record<string, unknown>) => Promise<unknown>> = {
121
128
  "mandu.guard.check": async (args: Record<string, unknown>) => {
122
- const { autoCorrect = false } = args as { autoCorrect?: boolean };
129
+ const { autoCorrect = false, typeAware: typeAwareArg } = args as {
130
+ autoCorrect?: boolean;
131
+ typeAware?: boolean;
132
+ };
123
133
 
124
134
  // Load manifest
125
135
  const manifestResult = await loadManifest(paths.manifestPath);
@@ -133,12 +143,53 @@ export function guardTools(projectRoot: string) {
133
143
  // Run guard check
134
144
  const checkResult = await runGuardCheck(manifestResult.data, projectRoot);
135
145
 
146
+ // Follow-up E — resolve type-aware defaulting from config, then
147
+ // run the bridge when enabled. Result envelope is always included
148
+ // in the tool response so MCP clients have a single stable shape.
149
+ let projectConfig: Awaited<ReturnType<typeof readConfig>> | undefined;
150
+ try {
151
+ projectConfig = await readConfig(projectRoot);
152
+ } catch {
153
+ projectConfig = undefined;
154
+ }
155
+ const typeAwareCfg = (
156
+ projectConfig?.guard as
157
+ | { typeAware?: Record<string, unknown> }
158
+ | undefined
159
+ )?.typeAware;
160
+ const typeAwareEnabled =
161
+ typeAwareArg !== undefined ? typeAwareArg : typeAwareCfg !== undefined;
162
+
163
+ let typeAwareResponse: Record<string, unknown> | undefined;
164
+ if (typeAwareEnabled) {
165
+ const bridge = await runTsgolint({
166
+ projectRoot,
167
+ rules: typeAwareCfg?.rules as string[] | undefined,
168
+ severity: typeAwareCfg?.severity as
169
+ | "off"
170
+ | "warn"
171
+ | "error"
172
+ | undefined,
173
+ configPath: typeAwareCfg?.configPath as string | undefined,
174
+ });
175
+ typeAwareResponse = {
176
+ skipped: bridge.skipped,
177
+ summary: bridge.summary,
178
+ violations: bridge.violations,
179
+ };
180
+ }
181
+
136
182
  if (checkResult.passed) {
137
183
  return {
138
- passed: true,
184
+ passed: typeAwareResponse
185
+ ? (typeAwareResponse.violations as Array<{ severity: string }>).filter(
186
+ (v) => v.severity === "error",
187
+ ).length === 0
188
+ : true,
139
189
  violations: [],
140
190
  message: "All guard checks passed",
141
191
  relatedSkills: ["mandu-guard-guide", "mandu-debug"],
192
+ ...(typeAwareResponse ? { typeAware: typeAwareResponse } : {}),
142
193
  };
143
194
  }
144
195
 
@@ -161,6 +212,7 @@ export function guardTools(projectRoot: string) {
161
212
  rolledBack: autoCorrectResult.rolledBack,
162
213
  changeId: autoCorrectResult.changeId,
163
214
  },
215
+ ...(typeAwareResponse ? { typeAware: typeAwareResponse } : {}),
164
216
  };
165
217
  }
166
218
 
@@ -175,6 +227,7 @@ export function guardTools(projectRoot: string) {
175
227
  message: `Found ${checkResult.violations.length} violation(s)`,
176
228
  tip: "Use autoCorrect: true to attempt automatic fixes",
177
229
  relatedSkills: ["mandu-guard-guide", "mandu-debug"],
230
+ ...(typeAwareResponse ? { typeAware: typeAwareResponse } : {}),
178
231
  };
179
232
  },
180
233
 
@@ -65,6 +65,10 @@ export { runTestsTools, runTestsToolDefinitions } from "./run-tests.js";
65
65
  export { deployPreviewTools, deployPreviewToolDefinitions } from "./deploy-preview.js";
66
66
  export { aiBriefTools, aiBriefToolDefinitions } from "./ai-brief.js";
67
67
  export { loopCloseTools, loopCloseToolDefinitions } from "./loop-close.js";
68
+ // #243 — docs search/get for agents grounding answers in real framework docs
69
+ export { docsTools, docsToolDefinitions } from "./docs.js";
70
+ // #240 guardrail-default — lint run + setup for existing projects
71
+ export { lintTools, lintToolDefinitions } from "./lint.js";
68
72
  // Phase 18.ι — AI refactor MCP tools
69
73
  export {
70
74
  rewriteGeneratedBarrelTools,
@@ -135,6 +139,8 @@ import { runTestsTools, runTestsToolDefinitions } from "./run-tests.js";
135
139
  import { deployPreviewTools, deployPreviewToolDefinitions } from "./deploy-preview.js";
136
140
  import { aiBriefTools, aiBriefToolDefinitions } from "./ai-brief.js";
137
141
  import { loopCloseTools, loopCloseToolDefinitions } from "./loop-close.js";
142
+ import { docsTools, docsToolDefinitions } from "./docs.js";
143
+ import { lintTools, lintToolDefinitions } from "./lint.js";
138
144
  // Phase 18.ι — AI refactor MCP tools
139
145
  import {
140
146
  rewriteGeneratedBarrelTools,
@@ -250,6 +256,8 @@ const TOOL_MODULES: ToolModule[] = [
250
256
  { category: "deploy-preview", definitions: deployPreviewToolDefinitions, handlers: deployPreviewTools },
251
257
  { category: "ai-brief", definitions: aiBriefToolDefinitions, handlers: aiBriefTools },
252
258
  { category: "loop-close", definitions: loopCloseToolDefinitions, handlers: loopCloseTools },
259
+ { category: "docs", definitions: docsToolDefinitions, handlers: docsTools },
260
+ { category: "lint", definitions: lintToolDefinitions, handlers: lintTools },
253
261
  // Phase 18.ι — AI refactor tools (destructive writes; dry-run by default)
254
262
  {
255
263
  category: "refactor-barrel",
@@ -0,0 +1,226 @@
1
+ /**
2
+ * MCP tools — `mandu.lint` + `mandu.lint.setup`
3
+ *
4
+ * Gives agents a first-class way to:
5
+ * 1. Run oxlint on a project and get structured error/warning counts
6
+ * (read-only, no side effects).
7
+ * 2. One-shot install oxlint + scaffold `.oxlintrc.json` + wire
8
+ * scripts on an existing project (destructive — writes files).
9
+ *
10
+ * The setup tool mirrors `mandu lint --setup`. We spawn the CLI as a
11
+ * subprocess rather than linking to the CLI source so MCP builds
12
+ * don't pull `packages/cli` into their import graph.
13
+ */
14
+
15
+ import type { Tool } from "@modelcontextprotocol/sdk/types.js";
16
+ import path from "node:path";
17
+ import fs from "node:fs/promises";
18
+
19
+ // ─────────────────────────────────────────────────────────────────────────
20
+ // Shared helpers
21
+ // ─────────────────────────────────────────────────────────────────────────
22
+
23
+ async function oxlintAvailable(rootDir: string): Promise<boolean> {
24
+ const binName = process.platform === "win32" ? "oxlint.exe" : "oxlint";
25
+ const localBin = path.resolve(rootDir, "node_modules", ".bin", binName);
26
+ try {
27
+ await fs.access(localBin);
28
+ return true;
29
+ } catch {
30
+ // Not in local node_modules — probe PATH.
31
+ }
32
+ try {
33
+ const proc = Bun.spawn({
34
+ cmd: ["bun", "x", "oxlint", "--version"],
35
+ cwd: rootDir,
36
+ stdout: "ignore",
37
+ stderr: "ignore",
38
+ });
39
+ return (await proc.exited) === 0;
40
+ } catch {
41
+ return false;
42
+ }
43
+ }
44
+
45
+ interface LintParsed {
46
+ errors: number;
47
+ warnings: number;
48
+ raw: string;
49
+ parseFailed: boolean;
50
+ }
51
+
52
+ async function runOxlintOnce(rootDir: string, typeAware: boolean): Promise<LintParsed> {
53
+ const cmd = typeAware
54
+ ? ["bun", "x", "oxlint", "--type-aware", "."]
55
+ : ["bun", "x", "oxlint", "."];
56
+ const proc = Bun.spawn({
57
+ cmd,
58
+ cwd: rootDir,
59
+ stdout: "pipe",
60
+ stderr: "pipe",
61
+ });
62
+ const [stdout, stderr] = await Promise.all([
63
+ new Response(proc.stdout).text(),
64
+ new Response(proc.stderr).text(),
65
+ ]);
66
+ await proc.exited;
67
+ const raw = stderr + stdout;
68
+ const match = raw.match(/Found (\d+) warnings? and (\d+) errors?/);
69
+ if (!match) {
70
+ return { errors: 0, warnings: 0, raw, parseFailed: true };
71
+ }
72
+ return {
73
+ errors: Number(match[2]),
74
+ warnings: Number(match[1]),
75
+ raw,
76
+ parseFailed: false,
77
+ };
78
+ }
79
+
80
+ // ─────────────────────────────────────────────────────────────────────────
81
+ // mandu.lint — run oxlint, return counts
82
+ // ─────────────────────────────────────────────────────────────────────────
83
+
84
+ interface LintInput {
85
+ typeAware?: unknown;
86
+ }
87
+
88
+ interface LintResult {
89
+ installed: boolean;
90
+ ran: boolean;
91
+ errors: number;
92
+ warnings: number;
93
+ passed: boolean;
94
+ hint?: string;
95
+ rawTail?: string;
96
+ }
97
+
98
+ async function handleLint(projectRoot: string, input: LintInput): Promise<LintResult> {
99
+ const typeAware = input.typeAware === true;
100
+ const installed = await oxlintAvailable(projectRoot);
101
+ if (!installed) {
102
+ return {
103
+ installed: false,
104
+ ran: false,
105
+ errors: 0,
106
+ warnings: 0,
107
+ passed: false,
108
+ hint:
109
+ "oxlint is not installed in this project. Call `mandu.lint.setup` or run `mandu lint --setup` in the shell to install + configure.",
110
+ };
111
+ }
112
+ const result = await runOxlintOnce(projectRoot, typeAware);
113
+ if (result.parseFailed) {
114
+ return {
115
+ installed: true,
116
+ ran: true,
117
+ errors: 0,
118
+ warnings: 0,
119
+ passed: false,
120
+ hint: "oxlint ran but output could not be parsed. See `rawTail` for the last 2 KB.",
121
+ rawTail: result.raw.slice(-2048),
122
+ };
123
+ }
124
+ return {
125
+ installed: true,
126
+ ran: true,
127
+ errors: result.errors,
128
+ warnings: result.warnings,
129
+ passed: result.errors === 0,
130
+ };
131
+ }
132
+
133
+ // ─────────────────────────────────────────────────────────────────────────
134
+ // mandu.lint.setup — install oxlint + wire scripts + baseline
135
+ // ─────────────────────────────────────────────────────────────────────────
136
+
137
+ interface LintSetupInput {
138
+ dryRun?: unknown;
139
+ }
140
+
141
+ interface LintSetupResult {
142
+ ok: boolean;
143
+ stdout: string;
144
+ stderr: string;
145
+ exitCode: number;
146
+ dryRun: boolean;
147
+ hint?: string;
148
+ }
149
+
150
+ async function handleLintSetup(projectRoot: string, input: LintSetupInput): Promise<LintSetupResult> {
151
+ const dryRun = input.dryRun === true;
152
+ const args = ["run", "--cwd", projectRoot, "mandu", "lint", "--setup", "--yes"];
153
+ if (dryRun) args.push("--dry-run");
154
+ const proc = Bun.spawn({
155
+ cmd: ["bun", ...args],
156
+ cwd: projectRoot,
157
+ stdout: "pipe",
158
+ stderr: "pipe",
159
+ });
160
+ const [stdout, stderr] = await Promise.all([
161
+ new Response(proc.stdout).text(),
162
+ new Response(proc.stderr).text(),
163
+ ]);
164
+ const exitCode = await proc.exited;
165
+ const ok = exitCode === 0;
166
+ return {
167
+ ok,
168
+ stdout,
169
+ stderr,
170
+ exitCode,
171
+ dryRun,
172
+ hint: ok
173
+ ? dryRun
174
+ ? "Dry-run completed. Re-run without `dryRun: true` to apply the changes."
175
+ : "Setup completed. Run `mandu.lint` to see the current baseline."
176
+ : "Setup command failed. See `stderr` for details. A frequent cause is `mandu` CLI not being installed — `bun add -D @mandujs/cli` in the project first.",
177
+ };
178
+ }
179
+
180
+ // ─────────────────────────────────────────────────────────────────────────
181
+ // MCP tool definitions + handler map
182
+ // ─────────────────────────────────────────────────────────────────────────
183
+
184
+ export const lintToolDefinitions: Tool[] = [
185
+ {
186
+ name: "mandu.lint",
187
+ description:
188
+ "Run oxlint on the project and return structured error/warning counts. Read-only — never modifies files. Pass `typeAware: true` to also run `oxlint --type-aware` (requires `oxlint-tsgolint`). Agents should call this after edits as part of the guardrail chain alongside `mandu.guard.check` and `mandu.doctor`.",
189
+ annotations: { readOnlyHint: true },
190
+ inputSchema: {
191
+ type: "object",
192
+ properties: {
193
+ typeAware: {
194
+ type: "boolean",
195
+ description:
196
+ "Run `oxlint --type-aware` in addition to the core pass (requires `oxlint-tsgolint`). Default false.",
197
+ },
198
+ },
199
+ required: [],
200
+ },
201
+ },
202
+ {
203
+ name: "mandu.lint.setup",
204
+ description:
205
+ "Install oxlint into an existing project: copies `.oxlintrc.json`, wires `lint`/`lint:fix` scripts, adds `oxlint` devDep, runs `bun install`. Idempotent — re-running produces no additional changes. Destructive — writes files. Set `dryRun: true` to print the plan only.",
206
+ annotations: { readOnlyHint: false, destructiveHint: true },
207
+ inputSchema: {
208
+ type: "object",
209
+ properties: {
210
+ dryRun: {
211
+ type: "boolean",
212
+ description: "Print the plan without writing files. Default false.",
213
+ },
214
+ },
215
+ required: [],
216
+ },
217
+ },
218
+ ];
219
+
220
+ export function lintTools(projectRoot: string) {
221
+ const handlers: Record<string, (args: Record<string, unknown>) => Promise<unknown>> = {
222
+ "mandu.lint": async (args) => handleLint(projectRoot, args as LintInput),
223
+ "mandu.lint.setup": async (args) => handleLintSetup(projectRoot, args as LintSetupInput),
224
+ };
225
+ return handlers;
226
+ }