@dimassetoid/septum-mcp 0.2.1 → 0.5.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 (161) hide show
  1. package/README.md +46 -45
  2. package/package.json +8 -6
  3. package/src/cli/commands/check.ts +41 -26
  4. package/src/cli/commands/hook.ts +77 -105
  5. package/src/cli/commands/init.ts +35 -9
  6. package/src/cli/commands/stats.ts +33 -0
  7. package/src/cli/commands/sync-progress.ts +71 -0
  8. package/src/cli/commands/sync.ts +35 -9
  9. package/src/cli/hook-payload.ts +43 -0
  10. package/src/cli/index.ts +15 -51
  11. package/src/cli/scaffold/harness-config.ts +48 -8
  12. package/src/cli/templates/hooks.ts +51 -27
  13. package/src/cli/templates/rules.ts +34 -49
  14. package/src/cli/templates/skill.ts +46 -35
  15. package/src/core/boundary/audit.ts +383 -0
  16. package/src/core/boundary/evaluator.ts +299 -178
  17. package/src/core/boundary/staged-auditor.ts +188 -34
  18. package/src/core/config/domain-ownership.ts +47 -0
  19. package/src/core/config/domain-registration.ts +141 -0
  20. package/src/core/config/loader.ts +82 -54
  21. package/src/core/config/schema.ts +24 -46
  22. package/src/core/database/client.ts +2 -0
  23. package/src/core/database/repositories/dependency.repository.ts +326 -543
  24. package/src/core/database/repositories/domain.repository.ts +48 -4
  25. package/src/core/database/repositories/file.repository.ts +10 -0
  26. package/src/core/database/repositories/symbol.repository.ts +97 -259
  27. package/src/core/database/repository.ts +138 -24
  28. package/src/core/database/schema.ts +28 -33
  29. package/src/core/discovery/declarative-routes.ts +69 -0
  30. package/src/core/discovery/framework-detector.ts +149 -0
  31. package/src/core/discovery/navigation.ts +269 -0
  32. package/src/core/discovery/topology-detector.ts +153 -170
  33. package/src/core/errors.ts +19 -0
  34. package/src/core/ingestion/pipeline.ts +378 -212
  35. package/src/core/parser/behavioral-analyzer.ts +26 -22
  36. package/src/core/parser/boundary-tracker.ts +15 -0
  37. package/src/core/parser/extractors/base.ts +16 -4
  38. package/src/core/parser/extractors/csharp.ts +9 -5
  39. package/src/core/parser/extractors/django-routing.ts +163 -0
  40. package/src/core/parser/extractors/django-semantic.ts +400 -516
  41. package/src/core/parser/extractors/elysia-semantic.ts +172 -496
  42. package/src/core/parser/extractors/express-semantic.ts +195 -398
  43. package/src/core/parser/extractors/fastapi-semantic.ts +182 -399
  44. package/src/core/parser/extractors/fastify-semantic.ts +216 -732
  45. package/src/core/parser/extractors/flask-semantic.ts +92 -388
  46. package/src/core/parser/extractors/go-routing.ts +344 -0
  47. package/src/core/parser/extractors/go-semantic.ts +210 -508
  48. package/src/core/parser/extractors/go.ts +6 -3
  49. package/src/core/parser/extractors/graphql-entrypoint.ts +146 -0
  50. package/src/core/parser/extractors/hono-semantic.ts +132 -530
  51. package/src/core/parser/extractors/java.ts +7 -5
  52. package/src/core/parser/extractors/laravel-routing.ts +263 -0
  53. package/src/core/parser/extractors/laravel-semantic.ts +169 -333
  54. package/src/core/parser/extractors/manifest-helpers.ts +64 -0
  55. package/src/core/parser/extractors/mcp-cli-dispatch.ts +268 -0
  56. package/src/core/parser/extractors/mcp-cli-semantic.ts +84 -336
  57. package/src/core/parser/extractors/nestjs-semantic.ts +238 -510
  58. package/src/core/parser/extractors/nextjs-semantic.ts +164 -0
  59. package/src/core/parser/extractors/php-ast-helpers.ts +51 -0
  60. package/src/core/parser/extractors/php.ts +17 -1
  61. package/src/core/parser/extractors/python-route-ast.ts +249 -0
  62. package/src/core/parser/extractors/python.ts +9 -8
  63. package/src/core/parser/extractors/registry.ts +28 -5
  64. package/src/core/parser/extractors/route-prefixes.ts +43 -0
  65. package/src/core/parser/extractors/ruby.ts +10 -7
  66. package/src/core/parser/extractors/rust.ts +35 -17
  67. package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
  68. package/src/core/parser/extractors/symfony-routing.ts +114 -0
  69. package/src/core/parser/extractors/symfony-semantic.ts +75 -262
  70. package/src/core/parser/extractors/typescript-bindings.ts +333 -0
  71. package/src/core/parser/extractors/typescript-compiler-fallback.ts +380 -0
  72. package/src/core/parser/extractors/typescript-fallback.ts +207 -0
  73. package/src/core/parser/extractors/typescript-route-context.ts +58 -0
  74. package/src/core/parser/extractors/typescript.ts +36 -216
  75. package/src/core/parser/implementation-fingerprint.ts +97 -0
  76. package/src/core/parser/language-capabilities.ts +23 -7
  77. package/src/core/parser/schema/providers/laravel-migration-provider.ts +77 -51
  78. package/src/core/parser/schema/schema-provider-registry.ts +1 -3
  79. package/src/core/parser/schema/schema-provider.interface.ts +2 -1
  80. package/src/core/parser/semantic-revision.ts +3 -0
  81. package/src/core/parser/static-string.ts +51 -0
  82. package/src/core/parser/wasm-loader.ts +6 -2
  83. package/src/core/resolver/ast-method-tracer.ts +230 -0
  84. package/src/core/resolver/call-graph-helpers.ts +72 -0
  85. package/src/core/resolver/call-graph-tracer.ts +375 -311
  86. package/src/core/resolver/dependency-analysis.ts +593 -0
  87. package/src/core/resolver/import-candidates.ts +120 -0
  88. package/src/core/resolver/import-evidence.ts +88 -0
  89. package/src/core/resolver/import-resolver.ts +162 -181
  90. package/src/core/resolver/interpretation-inputs.ts +65 -0
  91. package/src/core/resolver/languages/go.ts +103 -0
  92. package/src/core/resolver/languages/managed.ts +200 -0
  93. package/src/core/resolver/languages/python.ts +133 -0
  94. package/src/core/resolver/languages/ruby.ts +40 -0
  95. package/src/core/resolver/languages/rust.ts +127 -0
  96. package/src/core/resolver/languages/shared.ts +74 -0
  97. package/src/core/resolver/module-resolver.ts +18 -7
  98. package/src/core/resolver/path-resolver.ts +6 -3
  99. package/src/core/resolver/polyglot-slice-tracer.ts +197 -0
  100. package/src/core/resolver/slice-catalog.ts +94 -0
  101. package/src/core/resolver/slice-format.ts +22 -0
  102. package/src/core/resolver/slice-selection.ts +237 -0
  103. package/src/core/resolver/symbol-impact.ts +477 -0
  104. package/src/core/resolver/symbol-locator.ts +82 -15
  105. package/src/core/resolver/vertical-slice-tracer.ts +57 -278
  106. package/src/core/resources/ignore.ts +21 -1
  107. package/src/core/resources/limits.ts +2 -1
  108. package/src/core/resources/source-files.ts +143 -20
  109. package/src/core/resources/source-inventory.ts +55 -0
  110. package/src/core/resources/work-queue.ts +167 -4
  111. package/src/core/retrieval/context-page.ts +116 -0
  112. package/src/core/retrieval/context.ts +457 -0
  113. package/src/core/retrieval/resolution-warnings.ts +21 -0
  114. package/src/core/retrieval/source-context.ts +448 -0
  115. package/src/core/retrieval/symbol-details.ts +453 -0
  116. package/src/core/retrieval/symbol-evidence.ts +97 -0
  117. package/src/core/retrieval/typescript-references.ts +293 -0
  118. package/src/core/retrieval/universal-context.ts +182 -0
  119. package/src/core/telemetry/summary.ts +114 -0
  120. package/src/core/telemetry/telemetry.ts +225 -37
  121. package/src/index.ts +0 -3
  122. package/src/mcp/formatters/compact-text.ts +439 -0
  123. package/src/mcp/resources-and-prompts.ts +253 -0
  124. package/src/mcp/schemas/boundary.ts +145 -0
  125. package/src/mcp/schemas/map.ts +250 -0
  126. package/src/mcp/schemas/shared.ts +52 -0
  127. package/src/mcp/schemas/slice.ts +60 -0
  128. package/src/mcp/schemas/symbol.ts +268 -0
  129. package/src/mcp/schemas.ts +5 -1105
  130. package/src/mcp/server.ts +263 -597
  131. package/src/mcp/tool-catalog.ts +331 -0
  132. package/src/mcp/tools/boundary.ts +69 -4
  133. package/src/mcp/tools/map.ts +126 -0
  134. package/src/mcp/tools/slice.ts +101 -3
  135. package/src/mcp/tools/symbol.ts +480 -26
  136. package/src/mcp/ui/app-client.ts +899 -0
  137. package/src/mcp/ui/app.html +478 -0
  138. package/src/mcp/workflow-playbooks.ts +181 -0
  139. package/src/mcp/workspace-boundary.ts +50 -0
  140. package/src/mcp/workspace-manager.ts +102 -18
  141. package/src/mcp/workspace-watcher.ts +38 -19
  142. package/src/types/index.ts +47 -53
  143. package/src/cli/commands/topology.ts +0 -161
  144. package/src/core/database/repositories/environment.repository.ts +0 -207
  145. package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
  146. package/src/core/session/session-manager.ts +0 -107
  147. package/src/core/topology/environment-detector.ts +0 -973
  148. package/src/core/topology/types.ts +0 -83
  149. package/src/mcp/tools/check-boundary.ts +0 -387
  150. package/src/mcp/tools/domain.ts +0 -40
  151. package/src/mcp/tools/feature.ts +0 -67
  152. package/src/mcp/tools/get-domain-catalog.ts +0 -226
  153. package/src/mcp/tools/get-environment-topology.ts +0 -138
  154. package/src/mcp/tools/get-feature-context.ts +0 -224
  155. package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
  156. package/src/mcp/tools/get-symbol-impact.ts +0 -64
  157. package/src/mcp/tools/get-symbol.ts +0 -175
  158. package/src/mcp/tools/locate-symbol.ts +0 -196
  159. package/src/mcp/tools/register-domain.ts +0 -75
  160. package/src/mcp/tools/register-feature.ts +0 -110
  161. package/src/mcp/tools/trace-vertical-slice.ts +0 -80
@@ -1,7 +1,13 @@
1
- import { readOptionalSourceFile } from "../../resources/source-files.ts";
1
+ import { listSourceFiles, type SourceInventory } from "../../resources/source-inventory.ts";
2
+ import { probeSourcePath, readOptionalSourceFile } from "../../resources/source-files.ts";
2
3
  import * as fs from "node:fs";
3
4
  import * as path from "node:path";
4
- import { blankCommentsAndStrings } from "../boundary-tracker.ts";
5
+ import ts from "typescript";
6
+ import type { ExecutionChainNode } from "../../../types/index.ts";
7
+ import { lineOf, TypeScriptBindings, nodes } from "./typescript-bindings.ts";
8
+ import { cliRegistrations, mcpRegistrations, type Registration } from "./mcp-cli-dispatch.ts";
9
+ import { isPathContained } from "../../resolver/path-resolver.ts";
10
+ import { readPackageJson } from "./manifest-helpers.ts";
5
11
  import type { SemanticSliceExtractor, VerticalSliceCandidate } from "./semantic-extractor.interface.ts";
6
12
 
7
13
  export class McpCliSemanticExtractor implements SemanticSliceExtractor {
@@ -17,352 +23,94 @@ export class McpCliSemanticExtractor implements SemanticSliceExtractor {
17
23
  if (hasMcp || hasCli) return true;
18
24
 
19
25
  // Check package.json for bin or MCP SDK
20
- const pkgPath = path.join(projectRoot, "package.json");
21
- if (fs.existsSync(pkgPath)) {
22
- try {
23
- const pkg = JSON.parse(readOptionalSourceFile(pkgPath, "utf-8"));
24
- if (pkg.bin || pkg.dependencies?.["@modelcontextprotocol/sdk"]) {
25
- return true;
26
- }
27
- } catch {}
26
+ const pkg = readPackageJson(projectRoot);
27
+ if (pkg && (pkg.bin || pkg.dependencies?.["@modelcontextprotocol/sdk"])) {
28
+ return true;
28
29
  }
29
30
 
30
31
  return false;
31
32
  }
32
33
 
33
- public extractSlices(projectRoot: string, domainId?: number): VerticalSliceCandidate[] {
34
- const slices: VerticalSliceCandidate[] = [];
35
- const { toolPrefix, cliBinName } = this.resolveProjectNaming(projectRoot);
36
- const registeredTools = this.scanRegisteredMcpToolNames(projectRoot);
37
-
38
- // 1. Extract MCP Tools
39
- const mcpToolsDir = path.join(projectRoot, "src/mcp/tools");
40
- if (fs.existsSync(mcpToolsDir)) {
41
- try {
42
- const toolFiles = fs
43
- .readdirSync(mcpToolsDir)
44
- .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
45
-
46
- for (const file of toolFiles) {
47
- const fullPath = path.join(mcpToolsDir, file);
48
- const relPath = path.relative(projectRoot, fullPath).replace(/\\/g, "/");
49
- const content = readOptionalSourceFile(fullPath, "utf-8");
50
- const baseName = path.basename(file, path.extname(file));
51
- const snakeBase = baseName.replace(/-/g, "_");
52
-
53
- // MCP-07: Only create ingress slices for whitelisted public MCP tools
54
- const toolIdentifier = registeredTools.get(snakeBase) || registeredTools.get(`${toolPrefix}_${snakeBase}`);
55
- if (!toolIdentifier) continue;
56
-
57
- // Detect handler function (both sync and async) and its 1-indexed line number
58
- const handlerMatch = /export\s+(?:async\s+)?function\s+([a-zA-Z0-9_]+)/.exec(content);
59
- const classMatch = /export\s+class\s+([A-Za-z0-9_]+)/.exec(content);
60
- const actionName = handlerMatch ? handlerMatch[1] : `handle_${snakeBase}`;
61
- const controllerClass = classMatch ? classMatch[1] : baseName;
62
- const handlerLine =
63
- handlerMatch && handlerMatch.index !== undefined
64
- ? content.slice(0, handlerMatch.index).split("\n").length
65
- : 1;
66
-
67
- // Detect args interface and its line number
68
- const argsMatch = /(?:export\s+)?interface\s+([a-zA-Z0-9_]+Args)\b/.exec(content);
69
- const formRequestClass = argsMatch ? argsMatch[1] : null;
70
- const argsLine =
71
- argsMatch && argsMatch.index !== undefined
72
- ? content.slice(0, argsMatch.index).split("\n").length
73
- : undefined;
74
-
75
- // Detect service/repository dependency and its actual file path
76
- const serviceDep = this.resolveServiceDependency(projectRoot, fullPath, content);
77
-
78
- const mcpChain: Array<{
79
- stage: string;
80
- symbol: string;
81
- file?: string;
82
- line?: number;
83
- description?: string;
84
- }> = [
85
- {
86
- stage: "ingress",
87
- symbol: `tool:${toolIdentifier}`,
88
- file: relPath,
89
- line: handlerLine,
90
- description: `MCP Tool Endpoint: ${toolIdentifier}`,
91
- },
92
- ];
93
-
94
- if (formRequestClass) {
95
- mcpChain.push({
96
- stage: "validation",
97
- symbol: formRequestClass,
98
- file: relPath,
99
- ...(argsLine ? { line: argsLine } : {}),
100
- description: "MCP Argument Schema",
101
- });
102
- }
103
-
104
- mcpChain.push({
105
- stage: "controller",
106
- symbol: classMatch ? `${controllerClass}::${actionName}` : actionName,
107
- file: relPath,
108
- line: handlerLine,
109
- description: "Tool Handler Execution",
110
- });
111
-
112
- if (serviceDep) {
113
- mcpChain.push({
114
- stage: "service",
115
- symbol: serviceDep.symbol,
116
- ...(serviceDep.file ? { file: serviceDep.file } : {}),
117
- description: "Core Engine Service",
118
- });
119
- }
120
-
121
- slices.push({
122
- domain_id: domainId ?? null,
123
- feature_key: `mcp.${baseName}`,
124
- http_method: "MCP",
125
- route_uri: `tool:${toolIdentifier}`,
126
- route_name: toolIdentifier,
127
- controller_class: controllerClass,
128
- action_name: actionName,
129
- controller_file: relPath,
130
- controller_line: handlerLine,
131
- architecture_style: "rpc",
132
- entry_kind: "mcp_tool",
133
- execution_chain_json: JSON.stringify(mcpChain),
134
- });
135
- }
136
- } catch {}
34
+ public extractSlices(projectRoot: string, domainId?: number, signal?: AbortSignal, inventory?: SourceInventory): VerticalSliceCandidate[] {
35
+ const root = path.resolve(projectRoot);
36
+ const files = new Set(listSourceFiles(root, /\.(?:[cm]?[jt]s|[jt]sx)$/, inventory));
37
+ const bindings = new TypeScriptBindings(root, files, signal);
38
+ const registrations: Registration[] = [];
39
+ const relative = (file: string) => path.relative(root, file).replace(/\\/g, "/");
40
+
41
+ for (const file of files) {
42
+ if (!/^src\/mcp\/server\.[cm]?[jt]s$/.test(relative(file))) continue;
43
+ const unit = bindings.load(file);
44
+ if (unit) registrations.push(...mcpRegistrations(unit.source, bindings));
137
45
  }
138
-
139
- // 2. Extract CLI Commands
140
- const cliCommandsDir = path.join(projectRoot, "src/cli/commands");
141
- if (fs.existsSync(cliCommandsDir)) {
142
- try {
143
- const commandFiles = fs
144
- .readdirSync(cliCommandsDir)
145
- .filter((f) => f.endsWith(".ts") || f.endsWith(".js"));
146
-
147
- for (const file of commandFiles) {
148
- const fullPath = path.join(cliCommandsDir, file);
149
- const relPath = path.relative(projectRoot, fullPath).replace(/\\/g, "/");
150
- const content = readOptionalSourceFile(fullPath, "utf-8");
151
- const commandName = path.basename(file, path.extname(file));
152
-
153
- const handlerMatch = /export\s+(?:async\s+)?function\s+([a-zA-Z0-9_]+)/.exec(content);
154
- const classMatch = /export\s+class\s+([A-Za-z0-9_]+)/.exec(content);
155
- const actionName = handlerMatch
156
- ? handlerMatch[1]
157
- : `handle${commandName.charAt(0).toUpperCase() + commandName.slice(1)}Command`;
158
- const controllerClass = classMatch ? classMatch[1] : commandName;
159
- const handlerLine =
160
- handlerMatch && handlerMatch.index !== undefined
161
- ? content.slice(0, handlerMatch.index).split("\n").length
162
- : 1;
163
-
164
- // Detect options / args
165
- const optionsMatch = /(?:export\s+)?interface\s+([a-zA-Z0-9_]+(?:Options|Args))\b/.exec(content);
166
- const formRequestClass = optionsMatch ? optionsMatch[1] : null;
167
- const optionsLine =
168
- optionsMatch && optionsMatch.index !== undefined
169
- ? content.slice(0, optionsMatch.index).split("\n").length
170
- : undefined;
171
-
172
- const serviceDep = this.resolveServiceDependency(projectRoot, fullPath, content);
173
-
174
- const cliChain: Array<{
175
- stage: string;
176
- symbol: string;
177
- file?: string;
178
- line?: number;
179
- description?: string;
180
- }> = [
181
- {
182
- stage: "ingress",
183
- symbol: `${cliBinName} ${commandName}`,
184
- file: relPath,
185
- line: handlerLine,
186
- description: `CLI Command Entrypoint: ${cliBinName} ${commandName}`,
187
- },
188
- ];
189
-
190
- if (formRequestClass) {
191
- cliChain.push({
192
- stage: "validation",
193
- symbol: formRequestClass,
194
- file: relPath,
195
- ...(optionsLine ? { line: optionsLine } : {}),
196
- description: "CLI Options Interface",
197
- });
198
- }
199
-
200
- cliChain.push({
201
- stage: "controller",
202
- symbol: classMatch ? `${controllerClass}::${actionName}` : actionName,
203
- file: relPath,
204
- line: handlerLine,
205
- description: "Command Execution Handler",
206
- });
207
-
208
- if (serviceDep) {
209
- cliChain.push({
210
- stage: "service",
211
- symbol: serviceDep.symbol,
212
- ...(serviceDep.file ? { file: serviceDep.file } : {}),
213
- description: "Core Engine Service",
214
- });
215
- }
216
-
217
- slices.push({
218
- domain_id: domainId ?? null,
219
- feature_key: `cli.${commandName}`,
220
- http_method: "CLI",
221
- route_uri: `command:${commandName}`,
222
- route_name: `${cliBinName} ${commandName}`,
223
- controller_class: controllerClass,
224
- action_name: actionName,
225
- controller_file: relPath,
226
- controller_line: handlerLine,
227
- architecture_style: "cli",
228
- entry_kind: "cli_command",
229
- execution_chain_json: JSON.stringify(cliChain),
230
- });
46
+ const bins = new Map<string, string>();
47
+ try {
48
+ const manifest = path.join(root, "package.json");
49
+ const text = probeSourcePath(manifest) === "file" ? readOptionalSourceFile(manifest, "utf-8") : "";
50
+ const pkg = text ? JSON.parse(text) : {};
51
+ const name = typeof pkg.name === "string" ? pkg.name.split("/").at(-1)! : "cli";
52
+ const entries = typeof pkg.bin === "string" ? [[name, pkg.bin]] : Object.entries(pkg.bin ?? {});
53
+ for (const [bin, file] of entries) {
54
+ if (typeof bin !== "string" || typeof file !== "string") continue;
55
+ const target = path.resolve(root, file);
56
+ if (/\.[cm]?[jt]sx?$/.test(target) && isPathContained(target, root) &&
57
+ probeSourcePath(target) === "file" && !bins.has(target)) {
58
+ bins.set(target, bin);
59
+ // package.json is explicit entrypoint evidence even under the default bin/ exclusion.
60
+ files.add(target);
231
61
  }
232
- } catch {}
62
+ }
63
+ } catch (error) {
64
+ if (!(error instanceof SyntaxError)) throw error;
233
65
  }
234
-
235
- return slices;
236
- }
237
-
238
- private resolveProjectNaming(projectRoot: string): { toolPrefix: string; cliBinName: string } {
239
- const pkgPath = path.join(projectRoot, "package.json");
240
- if (fs.existsSync(pkgPath)) {
241
- try {
242
- const pkg = JSON.parse(readOptionalSourceFile(pkgPath, "utf-8"));
243
- const rawName = typeof pkg.name === "string" ? pkg.name.replace(/^@[^/]+\//, "") : "mcp";
244
- const toolPrefix = rawName.replace(/[^a-zA-Z0-9_]/g, "_").toLowerCase() || "mcp";
245
- let cliBinName = rawName;
246
- if (typeof pkg.bin === "string") {
247
- cliBinName = rawName;
248
- } else if (pkg.bin && typeof pkg.bin === "object") {
249
- const firstBin = Object.keys(pkg.bin)[0];
250
- if (firstBin) cliBinName = firstBin;
251
- }
252
- return { toolPrefix, cliBinName };
253
- } catch {}
66
+ for (const [file, bin] of bins) {
67
+ const unit = bindings.load(file);
68
+ if (unit) registrations.push(...cliRegistrations(unit.source, bin, bindings));
254
69
  }
255
- return { toolPrefix: "mcp", cliBinName: "cli" };
256
- }
257
70
 
258
- private scanRegisteredMcpToolNames(projectRoot: string): Map<string, string> {
259
- const map = new Map<string, string>();
260
- const serverPath = path.join(projectRoot, "src/mcp/server.ts");
261
- if (!fs.existsSync(serverPath)) return map;
262
-
263
- try {
264
- const content = readOptionalSourceFile(serverPath, "utf-8");
265
- const masked = blankCommentsAndStrings(content);
266
- const scan = (start: number, namesInArray: boolean) => {
267
- let depth = 0;
268
- const names: string[] = [];
269
- const property = /\b([A-Za-z_]\w*)\s*:\s*(['"])?/y;
270
- for (let i = start; i < masked.length; i++) {
271
- const ch = masked[i];
272
- if ("[{(".includes(ch)) depth++;
273
- else if ("]})".includes(ch)) {
274
- if (--depth === 0) break;
71
+ return registrations.map(entry => {
72
+ signal?.throwIfAborted();
73
+ const cli = entry.bin !== undefined;
74
+ const uri = (cli ? "command:" : "tool:") + entry.name;
75
+ const name = cli ? entry.bin + " " + entry.name : entry.name;
76
+ const ingress = { file: relative(entry.node.getSourceFile().fileName), line: lineOf(entry.node) };
77
+ const handler = entry.handler;
78
+ const handlerFile = handler ? relative(handler.node.getSourceFile().fileName) : null;
79
+ const chain: ExecutionChainNode[] = [{
80
+ stage: "ingress", symbol: cli ? name : uri, ...ingress,
81
+ description: cli ? "CLI command case" : "MCP tool registration",
82
+ relationship: { kind: "declares", derivation: "source", source: ingress, reason: "Literal public name" },
83
+ }];
84
+ if (handler && handlerFile) {
85
+ const typeNodes = handler.node.parameters.flatMap(param => param.type ? nodes(param.type).filter(ts.isIdentifier) : []);
86
+ for (const ref of typeNodes) {
87
+ const declaration = bindings.declaration(ref);
88
+ if (declaration && ts.isInterfaceDeclaration(declaration) && /(?:Options|Args)$/.test(ref.text)) {
89
+ chain.push({ stage: "validation", symbol: ref.text, file: handlerFile, line: lineOf(declaration),
90
+ description: "Handler parameter interface",
91
+ relationship: { kind: "schema_binding", derivation: "source", reason: "Parameter type reference" } });
275
92
  }
276
- if (depth !== (namesInArray ? 2 : 1) || (i > 0 && /\w/.test(masked[i - 1]))) continue;
277
- property.lastIndex = i;
278
- const match = property.exec(masked);
279
- if (!match) continue;
280
- if (!namesInArray) names.push(match[1]);
281
- else if (match[1] === "name" && match[2]) {
282
- const quoteStart = i + match[0].lastIndexOf(match[2]);
283
- const quoteEnd = masked.indexOf(match[2], quoteStart + 1);
284
- const name = content.slice(quoteStart + 1, quoteEnd);
285
- if (/^[A-Za-z0-9_-]+$/.test(name)) names.push(name);
286
- }
287
- }
288
- // Unbalanced declarations are not evidence of public registration.
289
- if (depth !== 0) return;
290
- for (const fullToolName of names) {
291
- const normalized = fullToolName.replace(/-/g, "_");
292
- map.set(normalized, fullToolName);
293
- const withoutPrefix = normalized.replace(/^[a-z0-9]+_/, "");
294
- if (withoutPrefix) map.set(withoutPrefix, fullToolName);
295
93
  }
296
- };
297
- const listCall = /setRequestHandler\s*\(\s*ListToolsRequestSchema\b/.exec(masked);
298
- if (listCall) {
299
- const callStart = masked.indexOf("(", listCall.index);
300
- let depth = 1;
301
- let end = callStart + 1;
302
- for (; end < masked.length && depth; end++) {
303
- if (masked[end] === "(") depth++;
304
- else if (masked[end] === ")") depth--;
305
- }
306
- const tools = /\btools\s*:\s*\[/.exec(masked.slice(callStart, end));
307
- if (tools && depth === 0) scan(callStart + tools.index + tools[0].length - 1, true);
308
- } else {
309
- const schemas = /\bTOOL_SCHEMAS\s*=\s*\{/.exec(masked);
310
- if (schemas) scan(schemas.index + schemas[0].length - 1, false);
94
+ chain.push({
95
+ stage: "controller", symbol: handler.name, file: handlerFile, line: lineOf(handler.node),
96
+ description: "Statically bound handler",
97
+ relationship: { kind: "route_binding", derivation: "source", source: ingress, reason: "Registration or dispatcher binding" },
98
+ });
311
99
  }
312
- } catch {}
313
- return map;
314
- }
315
-
316
- private resolveServiceDependency(
317
- projectRoot: string,
318
- sourceFile: string,
319
- content: string
320
- ): { symbol: string; file?: string } | null {
321
- const importRegex = /import\s+(?:type\s+)?\{([^}]+)\}\s+from\s+['"]([^'"]+)['"]/g;
322
- let match: RegExpExecArray | null;
323
-
324
- while ((match = importRegex.exec(content)) !== null) {
325
- const clause = match[1];
326
- const spec = match[2];
327
- const identifiers = clause
328
- .split(",")
329
- .map((s) => s.trim().split(/\s+as\s+/)[0].trim())
330
- .filter(Boolean);
331
-
332
- for (const id of identifiers) {
333
- if (
334
- /(?:Repository|Service|Manager|Loader|Pipeline|Evaluator|Tracer|Detector|Engine|Store)$/.test(
335
- id
336
- )
337
- ) {
338
- const resolvedFile = this.resolveImportFile(projectRoot, sourceFile, spec);
339
- return {
340
- symbol: id,
341
- ...(resolvedFile ? { file: resolvedFile } : {}),
342
- };
343
- }
344
- }
345
- }
346
-
347
- return null;
348
- }
349
-
350
- private resolveImportFile(projectRoot: string, sourceFile: string, importSpec: string): string | undefined {
351
- if (!importSpec.startsWith(".")) return undefined;
352
- const baseResolved = path.resolve(path.dirname(sourceFile), importSpec);
353
- const candidates = [
354
- baseResolved,
355
- `${baseResolved}.ts`,
356
- `${baseResolved}.js`,
357
- baseResolved.replace(/\.js$/, ".ts"),
358
- path.join(baseResolved, "index.ts"),
359
- ];
360
- for (const c of candidates) {
361
- if (fs.existsSync(c) && fs.statSync(c).isFile()) {
362
- return path.relative(projectRoot, c).replace(/\\/g, "/");
363
- }
364
- }
365
- return undefined;
100
+ return {
101
+ domain_id: domainId ?? null,
102
+ feature_key: (cli ? "cli." : "mcp.") + entry.name,
103
+ http_method: cli ? "CLI" : "MCP",
104
+ route_uri: uri,
105
+ route_name: name,
106
+ controller_class: handlerFile ? path.basename(handlerFile, path.extname(handlerFile)) : "Unresolved",
107
+ action_name: handler?.name ?? "unresolved_handler",
108
+ controller_file: handlerFile,
109
+ controller_line: handler ? lineOf(handler.node) : 0,
110
+ architecture_style: cli ? "cli" : "rpc",
111
+ entry_kind: cli ? "cli_command" : "mcp_tool",
112
+ execution_chain_json: JSON.stringify(chain),
113
+ };
114
+ });
366
115
  }
367
116
  }
368
-