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