@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.
- package/README.md +46 -45
- package/package.json +8 -6
- package/src/cli/commands/check.ts +41 -26
- package/src/cli/commands/hook.ts +77 -105
- package/src/cli/commands/init.ts +35 -9
- package/src/cli/commands/stats.ts +33 -0
- package/src/cli/commands/sync-progress.ts +71 -0
- package/src/cli/commands/sync.ts +35 -9
- package/src/cli/hook-payload.ts +43 -0
- package/src/cli/index.ts +15 -51
- package/src/cli/scaffold/harness-config.ts +48 -8
- package/src/cli/templates/hooks.ts +51 -27
- package/src/cli/templates/rules.ts +34 -49
- package/src/cli/templates/skill.ts +46 -35
- package/src/core/boundary/audit.ts +383 -0
- package/src/core/boundary/evaluator.ts +299 -178
- package/src/core/boundary/staged-auditor.ts +188 -34
- package/src/core/config/domain-ownership.ts +47 -0
- package/src/core/config/domain-registration.ts +141 -0
- package/src/core/config/loader.ts +82 -54
- package/src/core/config/schema.ts +24 -46
- package/src/core/database/client.ts +2 -0
- package/src/core/database/repositories/dependency.repository.ts +326 -543
- package/src/core/database/repositories/domain.repository.ts +48 -4
- package/src/core/database/repositories/file.repository.ts +10 -0
- package/src/core/database/repositories/symbol.repository.ts +97 -259
- package/src/core/database/repository.ts +138 -24
- package/src/core/database/schema.ts +28 -33
- package/src/core/discovery/declarative-routes.ts +69 -0
- package/src/core/discovery/framework-detector.ts +149 -0
- package/src/core/discovery/navigation.ts +269 -0
- package/src/core/discovery/topology-detector.ts +153 -170
- package/src/core/errors.ts +19 -0
- package/src/core/ingestion/pipeline.ts +378 -212
- package/src/core/parser/behavioral-analyzer.ts +26 -22
- package/src/core/parser/boundary-tracker.ts +15 -0
- package/src/core/parser/extractors/base.ts +16 -4
- package/src/core/parser/extractors/csharp.ts +9 -5
- package/src/core/parser/extractors/django-routing.ts +163 -0
- package/src/core/parser/extractors/django-semantic.ts +400 -516
- package/src/core/parser/extractors/elysia-semantic.ts +172 -496
- package/src/core/parser/extractors/express-semantic.ts +195 -398
- package/src/core/parser/extractors/fastapi-semantic.ts +182 -399
- package/src/core/parser/extractors/fastify-semantic.ts +216 -732
- package/src/core/parser/extractors/flask-semantic.ts +92 -388
- package/src/core/parser/extractors/go-routing.ts +344 -0
- package/src/core/parser/extractors/go-semantic.ts +210 -508
- package/src/core/parser/extractors/go.ts +6 -3
- package/src/core/parser/extractors/graphql-entrypoint.ts +146 -0
- package/src/core/parser/extractors/hono-semantic.ts +132 -530
- package/src/core/parser/extractors/java.ts +7 -5
- package/src/core/parser/extractors/laravel-routing.ts +263 -0
- package/src/core/parser/extractors/laravel-semantic.ts +169 -333
- package/src/core/parser/extractors/manifest-helpers.ts +64 -0
- package/src/core/parser/extractors/mcp-cli-dispatch.ts +268 -0
- package/src/core/parser/extractors/mcp-cli-semantic.ts +84 -336
- package/src/core/parser/extractors/nestjs-semantic.ts +238 -510
- package/src/core/parser/extractors/nextjs-semantic.ts +164 -0
- package/src/core/parser/extractors/php-ast-helpers.ts +51 -0
- package/src/core/parser/extractors/php.ts +17 -1
- package/src/core/parser/extractors/python-route-ast.ts +249 -0
- package/src/core/parser/extractors/python.ts +9 -8
- package/src/core/parser/extractors/registry.ts +28 -5
- package/src/core/parser/extractors/route-prefixes.ts +43 -0
- package/src/core/parser/extractors/ruby.ts +10 -7
- package/src/core/parser/extractors/rust.ts +35 -17
- package/src/core/parser/extractors/semantic-extractor.interface.ts +3 -1
- package/src/core/parser/extractors/symfony-routing.ts +114 -0
- package/src/core/parser/extractors/symfony-semantic.ts +75 -262
- package/src/core/parser/extractors/typescript-bindings.ts +333 -0
- package/src/core/parser/extractors/typescript-compiler-fallback.ts +380 -0
- package/src/core/parser/extractors/typescript-fallback.ts +207 -0
- package/src/core/parser/extractors/typescript-route-context.ts +58 -0
- package/src/core/parser/extractors/typescript.ts +36 -216
- package/src/core/parser/implementation-fingerprint.ts +97 -0
- package/src/core/parser/language-capabilities.ts +23 -7
- package/src/core/parser/schema/providers/laravel-migration-provider.ts +77 -51
- package/src/core/parser/schema/schema-provider-registry.ts +1 -3
- package/src/core/parser/schema/schema-provider.interface.ts +2 -1
- package/src/core/parser/semantic-revision.ts +3 -0
- package/src/core/parser/static-string.ts +51 -0
- package/src/core/parser/wasm-loader.ts +6 -2
- package/src/core/resolver/ast-method-tracer.ts +230 -0
- package/src/core/resolver/call-graph-helpers.ts +72 -0
- package/src/core/resolver/call-graph-tracer.ts +375 -311
- package/src/core/resolver/dependency-analysis.ts +593 -0
- package/src/core/resolver/import-candidates.ts +120 -0
- package/src/core/resolver/import-evidence.ts +88 -0
- package/src/core/resolver/import-resolver.ts +162 -181
- package/src/core/resolver/interpretation-inputs.ts +65 -0
- package/src/core/resolver/languages/go.ts +103 -0
- package/src/core/resolver/languages/managed.ts +200 -0
- package/src/core/resolver/languages/python.ts +133 -0
- package/src/core/resolver/languages/ruby.ts +40 -0
- package/src/core/resolver/languages/rust.ts +127 -0
- package/src/core/resolver/languages/shared.ts +74 -0
- package/src/core/resolver/module-resolver.ts +18 -7
- package/src/core/resolver/path-resolver.ts +6 -3
- package/src/core/resolver/polyglot-slice-tracer.ts +197 -0
- package/src/core/resolver/slice-catalog.ts +94 -0
- package/src/core/resolver/slice-format.ts +22 -0
- package/src/core/resolver/slice-selection.ts +237 -0
- package/src/core/resolver/symbol-impact.ts +477 -0
- package/src/core/resolver/symbol-locator.ts +82 -15
- package/src/core/resolver/vertical-slice-tracer.ts +57 -278
- package/src/core/resources/ignore.ts +21 -1
- package/src/core/resources/limits.ts +2 -1
- package/src/core/resources/source-files.ts +143 -20
- package/src/core/resources/source-inventory.ts +55 -0
- package/src/core/resources/work-queue.ts +167 -4
- package/src/core/retrieval/context-page.ts +116 -0
- package/src/core/retrieval/context.ts +457 -0
- package/src/core/retrieval/resolution-warnings.ts +21 -0
- package/src/core/retrieval/source-context.ts +448 -0
- package/src/core/retrieval/symbol-details.ts +453 -0
- package/src/core/retrieval/symbol-evidence.ts +97 -0
- package/src/core/retrieval/typescript-references.ts +293 -0
- package/src/core/retrieval/universal-context.ts +182 -0
- package/src/core/telemetry/summary.ts +114 -0
- package/src/core/telemetry/telemetry.ts +225 -37
- package/src/index.ts +0 -3
- package/src/mcp/formatters/compact-text.ts +439 -0
- package/src/mcp/resources-and-prompts.ts +253 -0
- package/src/mcp/schemas/boundary.ts +145 -0
- package/src/mcp/schemas/map.ts +250 -0
- package/src/mcp/schemas/shared.ts +52 -0
- package/src/mcp/schemas/slice.ts +60 -0
- package/src/mcp/schemas/symbol.ts +268 -0
- package/src/mcp/schemas.ts +5 -1105
- package/src/mcp/server.ts +263 -597
- package/src/mcp/tool-catalog.ts +331 -0
- package/src/mcp/tools/boundary.ts +69 -4
- package/src/mcp/tools/map.ts +126 -0
- package/src/mcp/tools/slice.ts +101 -3
- package/src/mcp/tools/symbol.ts +480 -26
- package/src/mcp/ui/app-client.ts +899 -0
- package/src/mcp/ui/app.html +478 -0
- package/src/mcp/workflow-playbooks.ts +181 -0
- package/src/mcp/workspace-boundary.ts +50 -0
- package/src/mcp/workspace-manager.ts +102 -18
- package/src/mcp/workspace-watcher.ts +38 -19
- package/src/types/index.ts +47 -53
- package/src/cli/commands/topology.ts +0 -161
- package/src/core/database/repositories/environment.repository.ts +0 -207
- package/src/core/parser/schema/providers/prisma-schema-provider.ts +0 -90
- package/src/core/session/session-manager.ts +0 -107
- package/src/core/topology/environment-detector.ts +0 -973
- package/src/core/topology/types.ts +0 -83
- package/src/mcp/tools/check-boundary.ts +0 -387
- package/src/mcp/tools/domain.ts +0 -40
- package/src/mcp/tools/feature.ts +0 -67
- package/src/mcp/tools/get-domain-catalog.ts +0 -226
- package/src/mcp/tools/get-environment-topology.ts +0 -138
- package/src/mcp/tools/get-feature-context.ts +0 -224
- package/src/mcp/tools/get-symbol-hotspots.ts +0 -54
- package/src/mcp/tools/get-symbol-impact.ts +0 -64
- package/src/mcp/tools/get-symbol.ts +0 -175
- package/src/mcp/tools/locate-symbol.ts +0 -196
- package/src/mcp/tools/register-domain.ts +0 -75
- package/src/mcp/tools/register-feature.ts +0 -110
- package/src/mcp/tools/trace-vertical-slice.ts +0 -80
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { probeSourcePath, readOptionalSourceFile } from "../../resources/source-files.ts";
|
|
4
|
+
|
|
5
|
+
export function warnIfUnexpectedParserError(context: string, err: unknown): void {
|
|
6
|
+
const errno = (err as NodeJS.ErrnoException | undefined)?.code;
|
|
7
|
+
if (errno === "ENOENT" || errno === "ENOTDIR") return;
|
|
8
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
9
|
+
console.warn(`[Septum Extractor] ${context}: ${msg}`);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function readPackageJson(projectRoot: string): Record<string, any> | null {
|
|
13
|
+
const manifestPath = path.join(projectRoot, "package.json");
|
|
14
|
+
if (probeSourcePath(manifestPath) !== "file") return null;
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(readOptionalSourceFile(manifestPath, "utf-8"));
|
|
17
|
+
} catch (err: unknown) {
|
|
18
|
+
warnIfUnexpectedParserError(`Failed to parse ${manifestPath}`, err);
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function hasPackageJsonDependency(projectRoot: string, depNames: string[]): boolean {
|
|
24
|
+
const pkg = readPackageJson(projectRoot);
|
|
25
|
+
if (!pkg) return false;
|
|
26
|
+
return depNames.some(
|
|
27
|
+
(name) => Boolean(pkg.dependencies?.[name] || pkg.devDependencies?.[name])
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function hasComposerDependency(projectRoot: string, depNames: string[]): boolean {
|
|
32
|
+
const composerJson = path.join(projectRoot, "composer.json");
|
|
33
|
+
if (probeSourcePath(composerJson) !== "file") return false;
|
|
34
|
+
try {
|
|
35
|
+
const composer = JSON.parse(readOptionalSourceFile(composerJson, "utf-8"));
|
|
36
|
+
const reqs = { ...(composer.require ?? {}), ...(composer["require-dev"] ?? {}) };
|
|
37
|
+
return depNames.some((name) => Boolean(reqs[name]));
|
|
38
|
+
} catch (err: unknown) {
|
|
39
|
+
warnIfUnexpectedParserError(`Failed to parse ${composerJson}`, err);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function anyFileContains(
|
|
45
|
+
rootDir: string,
|
|
46
|
+
relativePaths: string[],
|
|
47
|
+
needles: string[],
|
|
48
|
+
caseInsensitive = false
|
|
49
|
+
): boolean {
|
|
50
|
+
for (const rel of relativePaths) {
|
|
51
|
+
const fullPath = path.join(rootDir, rel);
|
|
52
|
+
if (probeSourcePath(fullPath) !== "file") continue;
|
|
53
|
+
try {
|
|
54
|
+
const raw = readOptionalSourceFile(fullPath, "utf-8");
|
|
55
|
+
const content = caseInsensitive ? raw.toLowerCase() : raw;
|
|
56
|
+
if (needles.some((needle) => content.includes(needle))) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
} catch (err: unknown) {
|
|
60
|
+
warnIfUnexpectedParserError(`Failed to read ${fullPath}`, err);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { type Handler, literal, TypeScriptBindings, nameText, nodes, unwrap } from "./typescript-bindings.ts";
|
|
3
|
+
|
|
4
|
+
export type Registration = { name: string; node: ts.Node; handler?: Handler; bin?: string };
|
|
5
|
+
|
|
6
|
+
function returns(handler: Handler): Array<ts.Expression | undefined> {
|
|
7
|
+
const body = handler.node.body;
|
|
8
|
+
if (!body) return [];
|
|
9
|
+
if (!ts.isBlock(body)) return [body];
|
|
10
|
+
return nodes(body, true).filter(ts.isReturnStatement).map(node => node.expression);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function property(object: ts.ObjectLiteralExpression, name: string): ts.Expression | undefined {
|
|
14
|
+
const matches = object.properties.filter(item => (ts.isPropertyAssignment(item) || ts.isShorthandPropertyAssignment(item)) &&
|
|
15
|
+
nameText(item.name) === name);
|
|
16
|
+
if (matches.length !== 1 || object.properties.some(item => ts.isSpreadAssignment(item) || item.name && ts.isComputedPropertyName(item.name))) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const item = matches[0]!;
|
|
20
|
+
return ts.isPropertyAssignment(item) ? item.initializer : ts.isShorthandPropertyAssignment(item) ? item.name : undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function constant(expression: ts.Expression | undefined, bindings: TypeScriptBindings, depth = 0): ts.Expression | undefined {
|
|
24
|
+
if (!expression || depth > 16) return undefined;
|
|
25
|
+
const node = unwrap(expression);
|
|
26
|
+
if (ts.isIdentifier(node)) return constant(bindings.initializer(bindings.declaration(node)), bindings, depth + 1);
|
|
27
|
+
return node;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function unique(handlers: Array<Handler | undefined>): Handler | undefined {
|
|
31
|
+
const first = handlers[0];
|
|
32
|
+
return first && handlers.every(item => item?.node === first.node) ? first : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function endsCase(statement: ts.Statement | undefined): boolean {
|
|
36
|
+
if (!statement) return false;
|
|
37
|
+
if (ts.isBlock(statement)) return endsCase(statement.statements.at(-1));
|
|
38
|
+
return ts.isReturnStatement(statement) || ts.isThrowStatement(statement) ||
|
|
39
|
+
ts.isBreakStatement(statement) && !statement.label;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class Dispatch {
|
|
43
|
+
private readonly assignments = new Map<ts.Declaration, Array<ts.Expression | undefined>>();
|
|
44
|
+
|
|
45
|
+
constructor(private readonly bindings: TypeScriptBindings, private readonly body: ts.Node | undefined, private readonly seeds: Map<ts.Declaration, string>) {
|
|
46
|
+
if (!body) return;
|
|
47
|
+
for (const node of nodes(body)) {
|
|
48
|
+
if (!ts.isBinaryExpression(node) || !ts.isIdentifier(node.left) ||
|
|
49
|
+
node.operatorToken.kind < ts.SyntaxKind.FirstAssignment || node.operatorToken.kind > ts.SyntaxKind.LastAssignment) continue;
|
|
50
|
+
const declaration = bindings.declaration(node.left);
|
|
51
|
+
if (!declaration) continue;
|
|
52
|
+
const writes = this.assignments.get(declaration) ?? [];
|
|
53
|
+
writes.push(node.operatorToken.kind === ts.SyntaxKind.EqualsToken ? node.right : undefined);
|
|
54
|
+
this.assignments.set(declaration, writes);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private origin(expression: ts.Expression | undefined, depth = 0): string | undefined {
|
|
59
|
+
if (!expression || depth > 20) return undefined;
|
|
60
|
+
const node = unwrap(expression);
|
|
61
|
+
const from = (value: ts.Expression | undefined) => this.origin(value, depth + 1);
|
|
62
|
+
if (ts.isIdentifier(node)) {
|
|
63
|
+
if (node.text === "process" && !this.bindings.declaration(node)) return "process";
|
|
64
|
+
const declaration = this.bindings.declaration(node);
|
|
65
|
+
if (!declaration) return undefined;
|
|
66
|
+
const seed = this.seeds.get(declaration);
|
|
67
|
+
if (seed) return seed;
|
|
68
|
+
const writes = this.assignments.get(declaration);
|
|
69
|
+
if (ts.isVariableDeclaration(declaration)) {
|
|
70
|
+
if (writes) return writes.length === 1 && !declaration.initializer && (writes[0]?.getStart() ?? Infinity) < node.getStart()
|
|
71
|
+
? from(writes[0]) : undefined;
|
|
72
|
+
return from(declaration.initializer);
|
|
73
|
+
}
|
|
74
|
+
if (ts.isBindingElement(declaration) && ts.isObjectBindingPattern(declaration.parent) && !declaration.initializer && !declaration.dotDotDotToken) {
|
|
75
|
+
const field = nameText(declaration.propertyName ?? declaration.name);
|
|
76
|
+
const owner = declaration.parent.parent;
|
|
77
|
+
const parent = ts.isVariableDeclaration(owner) ? from(owner.initializer) : this.seeds.get(owner);
|
|
78
|
+
if (parent && field && !writes) return parent + "." + field;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) {
|
|
82
|
+
const base = from(node.expression);
|
|
83
|
+
const field = ts.isPropertyAccessExpression(node) ? node.name.text
|
|
84
|
+
: ts.isNumericLiteral(node.argumentExpression) || ts.isStringLiteralLike(node.argumentExpression) ? node.argumentExpression.text : undefined;
|
|
85
|
+
if (base && field !== undefined) return base + "." + field;
|
|
86
|
+
}
|
|
87
|
+
if (ts.isCallExpression(node)) {
|
|
88
|
+
if (ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "slice" &&
|
|
89
|
+
from(node.expression.expression) === "process.argv" && node.arguments[0]?.getText() === "2") return "argv";
|
|
90
|
+
if (this.bindings.sdk(node.expression, "parseArgs", "node:util") || this.bindings.sdk(node.expression, "parseArgs", "util")) {
|
|
91
|
+
const options = node.arguments[0] && unwrap(node.arguments[0]);
|
|
92
|
+
if (options && ts.isObjectLiteralExpression(options) && from(property(options, "args")) === "argv") return "cli";
|
|
93
|
+
}
|
|
94
|
+
if (from(node.arguments[0]) === "request.params.name") {
|
|
95
|
+
const handler = this.bindings.handler(node.expression);
|
|
96
|
+
const param = handler?.node.parameters[0];
|
|
97
|
+
if (handler && param) {
|
|
98
|
+
const values = returns(handler);
|
|
99
|
+
// Support a local validation helper only when each return preserves its input name.
|
|
100
|
+
if (values.length && values.every(value => {
|
|
101
|
+
const object = value && unwrap(value);
|
|
102
|
+
const name = object && ts.isObjectLiteralExpression(object) ? property(object, "name") : undefined;
|
|
103
|
+
return name && ts.isIdentifier(name) && this.bindings.declaration(name) === param;
|
|
104
|
+
})) return "validated";
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
cases(origins: string[], cli = false): Registration[] {
|
|
112
|
+
const entries: Registration[] = [];
|
|
113
|
+
const body = this.body;
|
|
114
|
+
if (!body) return entries;
|
|
115
|
+
const visited = new Set<ts.Node>();
|
|
116
|
+
const visit = (node: ts.Node): void => {
|
|
117
|
+
if (visited.has(node) || visited.size > 24) return;
|
|
118
|
+
visited.add(node);
|
|
119
|
+
for (const child of nodes(node, true)) {
|
|
120
|
+
if (ts.isSwitchStatement(child) && origins.includes(this.origin(child.expression) ?? "")) {
|
|
121
|
+
let labels: Array<{ name: string; node: ts.CaseClause }> = [];
|
|
122
|
+
const clauses = child.caseBlock.clauses;
|
|
123
|
+
for (const [index, clause] of clauses.entries()) {
|
|
124
|
+
if (!ts.isCaseClause(clause)) { labels = []; continue; }
|
|
125
|
+
const name = literal(clause.expression);
|
|
126
|
+
if (name) labels.push({ name, node: clause });
|
|
127
|
+
if (!clause.statements.length) continue;
|
|
128
|
+
const statements = nodes(clause, true);
|
|
129
|
+
const values = statements.filter(ts.isReturnStatement).map(statement => statement.expression);
|
|
130
|
+
if (cli) values.push(...statements.filter(ts.isExpressionStatement).map(statement => statement.expression));
|
|
131
|
+
const handlers = values.flatMap(expression => {
|
|
132
|
+
if (!expression) return cli ? [] : [undefined];
|
|
133
|
+
const call = unwrap(expression);
|
|
134
|
+
if (!ts.isCallExpression(call)) return cli ? [] : [undefined];
|
|
135
|
+
return [this.bindings.handler(call.expression)];
|
|
136
|
+
});
|
|
137
|
+
const fallsThrough = index < clauses.length - 1 && !endsCase(clause.statements.at(-1));
|
|
138
|
+
for (const label of labels) entries.push({ ...label, handler: fallsThrough ? undefined : unique(handlers) });
|
|
139
|
+
labels = [];
|
|
140
|
+
}
|
|
141
|
+
} else if (ts.isCallExpression(child) && !cli) {
|
|
142
|
+
const callee = child.expression;
|
|
143
|
+
const callbacks = ts.isPropertyAccessExpression(callee) && callee.name.text === "runTool" ? child.arguments : [];
|
|
144
|
+
for (const expression of [callee, ...callbacks]) {
|
|
145
|
+
const handler = this.bindings.handler(expression);
|
|
146
|
+
if (handler?.node.body && handler.node.getSourceFile() === node.getSourceFile() &&
|
|
147
|
+
handler.node.pos >= body.pos && handler.node.end <= body.end) visit(handler.node.body);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
visit(body);
|
|
153
|
+
return entries;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function sdkServer(expression: ts.Expression, bindings: TypeScriptBindings, depth = 0): { node: ts.NewExpression; highLevel: boolean } | undefined {
|
|
158
|
+
if (depth > 16) return undefined;
|
|
159
|
+
const node = unwrap(expression);
|
|
160
|
+
if (ts.isIdentifier(node)) {
|
|
161
|
+
const init = bindings.initializer(bindings.declaration(node));
|
|
162
|
+
return init && sdkServer(init, bindings, depth + 1);
|
|
163
|
+
}
|
|
164
|
+
if (!ts.isNewExpression(node)) return undefined;
|
|
165
|
+
if (bindings.sdk(node.expression, "McpServer")) return { node, highLevel: true };
|
|
166
|
+
if (bindings.sdk(node.expression, "Server")) return { node, highLevel: false };
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Recognize a literal tool catalog projected directly by a tools/list handler. */
|
|
171
|
+
function projectedToolCatalog(expression: ts.Expression | undefined, bindings: TypeScriptBindings): Registration[] {
|
|
172
|
+
if (!expression || !ts.isCallExpression(expression) || !ts.isPropertyAccessExpression(expression.expression) ||
|
|
173
|
+
expression.expression.name.text !== "map" || expression.arguments.length !== 1) return [];
|
|
174
|
+
const projection = expression.arguments[0];
|
|
175
|
+
if (!ts.isArrowFunction(projection) || !ts.isParenthesizedExpression(projection.body)) return [];
|
|
176
|
+
const body = unwrap(projection.body);
|
|
177
|
+
if (!ts.isObjectLiteralExpression(body) || !property(body, "name") || !property(body, "inputSchema")) return [];
|
|
178
|
+
const array = bindings.value(expression.expression.expression);
|
|
179
|
+
if (!array || !ts.isArrayLiteralExpression(array)) return [];
|
|
180
|
+
return array.elements.flatMap(element => {
|
|
181
|
+
if (!ts.isCallExpression(element) || !ts.isIdentifier(element.expression) ||
|
|
182
|
+
element.expression.text !== "defineTool" || element.arguments.length !== 1) return [];
|
|
183
|
+
const definition = element.arguments[0];
|
|
184
|
+
if (!ts.isObjectLiteralExpression(definition)) return [];
|
|
185
|
+
const name = literal(property(definition, "name"));
|
|
186
|
+
if (!name || !property(definition, "schema") || !property(definition, "inputSchema")) return [];
|
|
187
|
+
const execute = bindings.handler(property(definition, "execute"));
|
|
188
|
+
const results = execute ? returns(execute) : [];
|
|
189
|
+
const call = results.length === 1 ? results[0] : undefined;
|
|
190
|
+
const handler = call && ts.isCallExpression(call) ? bindings.handler(call.expression) : undefined;
|
|
191
|
+
return [{ name, node: definition, handler }];
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function mcpRegistrations(source: ts.SourceFile, bindings: TypeScriptBindings): Registration[] {
|
|
196
|
+
const lists = new Map<ts.NewExpression, Registration[]>();
|
|
197
|
+
const dispatches = new Map<ts.NewExpression, Registration[]>();
|
|
198
|
+
const direct: Registration[] = [];
|
|
199
|
+
for (const node of nodes(source)) {
|
|
200
|
+
if (!ts.isCallExpression(node) || !ts.isPropertyAccessExpression(node.expression)) continue;
|
|
201
|
+
const method = node.expression.name.text;
|
|
202
|
+
if (!["registerTool", "tool", "setRequestHandler"].includes(method)) continue;
|
|
203
|
+
const server = sdkServer(node.expression.expression, bindings);
|
|
204
|
+
if (!server) continue;
|
|
205
|
+
if (method !== "setRequestHandler") {
|
|
206
|
+
if (!server.highLevel || node.arguments.length < (method === "registerTool" ? 3 : 2)) continue;
|
|
207
|
+
const name = literal(constant(node.arguments[0], bindings));
|
|
208
|
+
if (name) direct.push({ name, node, handler: bindings.handler(node.arguments.at(-1)) });
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (server.highLevel) continue;
|
|
212
|
+
const schema = node.arguments[0];
|
|
213
|
+
const callback = bindings.handler(node.arguments[1]);
|
|
214
|
+
if (!callback) continue;
|
|
215
|
+
if (bindings.sdk(schema, "ListToolsRequestSchema")) {
|
|
216
|
+
const entries = lists.get(server.node) ?? [];
|
|
217
|
+
const results = returns(callback);
|
|
218
|
+
if (results.length !== 1) continue;
|
|
219
|
+
const object = constant(results[0], bindings);
|
|
220
|
+
const array = object && ts.isObjectLiteralExpression(object) ? constant(property(object, "tools"), bindings) : undefined;
|
|
221
|
+
if (!array || !ts.isArrayLiteralExpression(array)) {
|
|
222
|
+
entries.push(...projectedToolCatalog(array, bindings));
|
|
223
|
+
lists.set(server.node, entries);
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
for (const element of array.elements) {
|
|
227
|
+
if (!ts.isObjectLiteralExpression(element)) continue;
|
|
228
|
+
const name = literal(constant(property(element, "name"), bindings));
|
|
229
|
+
if (name) entries.push({ name, node: element });
|
|
230
|
+
}
|
|
231
|
+
lists.set(server.node, entries);
|
|
232
|
+
} else if (bindings.sdk(schema, "CallToolRequestSchema")) {
|
|
233
|
+
const param = callback.node.parameters[0];
|
|
234
|
+
if (!param) continue;
|
|
235
|
+
const dispatch = new Dispatch(bindings, callback.node.body, new Map([[param, "request"]]));
|
|
236
|
+
const entries = dispatches.get(server.node) ?? [];
|
|
237
|
+
entries.push(...dispatch.cases(["request.params.name", "validated.name"]));
|
|
238
|
+
dispatches.set(server.node, entries);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
for (const [server, published] of lists) {
|
|
242
|
+
const registered = dispatches.get(server) ?? [];
|
|
243
|
+
for (const entry of published) direct.push({
|
|
244
|
+
...entry, handler: entry.handler ?? unique(registered.filter(item => item.name === entry.name).map(item => item.handler)),
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return direct;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export function cliRegistrations(source: ts.SourceFile, bin: string, bindings: TypeScriptBindings): Registration[] {
|
|
251
|
+
const entries: Registration[] = [];
|
|
252
|
+
const argv = (expr?: ts.Expression): boolean => Boolean(expr && ts.isPropertyAccessExpression(expr) &&
|
|
253
|
+
expr.name.text === "argv" && ts.isIdentifier(expr.expression) && expr.expression.text === "process" && !bindings.declaration(expr.expression));
|
|
254
|
+
// A bin may dispatch directly or invoke a statically bound function with process.argv.
|
|
255
|
+
const origins = ["process.argv.2", "argv.0", "cli.positionals.0"];
|
|
256
|
+
entries.push(...new Dispatch(bindings, source, new Map()).cases(origins, true));
|
|
257
|
+
for (const statement of source.statements) {
|
|
258
|
+
if (!ts.isExpressionStatement(statement)) continue;
|
|
259
|
+
const call = unwrap(statement.expression);
|
|
260
|
+
if (!ts.isCallExpression(call)) continue;
|
|
261
|
+
const handler = bindings.handler(call.expression);
|
|
262
|
+
if (!handler) continue;
|
|
263
|
+
const seeds = new Map<ts.Declaration, string>();
|
|
264
|
+
handler.node.parameters.forEach((param, index) => { if (argv(call.arguments[index])) seeds.set(param, "process.argv"); });
|
|
265
|
+
if (seeds.size) entries.push(...new Dispatch(bindings, handler.node.body, seeds).cases(origins, true));
|
|
266
|
+
}
|
|
267
|
+
return entries.map(entry => ({ ...entry, bin }));
|
|
268
|
+
}
|