webmcp-nexus-core 0.1.9
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/LICENSE +21 -0
- package/dist/index.cjs +609 -0
- package/dist/index.d.cts +129 -0
- package/dist/index.d.ts +129 -0
- package/dist/index.js +568 -0
- package/package.json +38 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alibaba
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,609 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
extractProperties: () => extractProperties,
|
|
34
|
+
extractToolsFromFile: () => extractToolsFromFile,
|
|
35
|
+
generateSchema: () => generateSchema,
|
|
36
|
+
generateSchemaInjectionCode: () => generateSchemaInjectionCode,
|
|
37
|
+
mapType: () => mapType,
|
|
38
|
+
mapTypeToSchema: () => mapTypeToSchema,
|
|
39
|
+
resolveWithAlias: () => resolveWithAlias,
|
|
40
|
+
transformCode: () => transformCode
|
|
41
|
+
});
|
|
42
|
+
module.exports = __toCommonJS(index_exports);
|
|
43
|
+
|
|
44
|
+
// src/ts-extractor.ts
|
|
45
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
46
|
+
var import_ts_morph = require("ts-morph");
|
|
47
|
+
var REGISTRATION_FUNCTIONS = ["registerGlobalTools", "useWebMcpTools"];
|
|
48
|
+
var isDebug = () => (process.env.DEBUG ?? "").toLowerCase().includes("webmcp");
|
|
49
|
+
function resolveWithAlias(spec, alias) {
|
|
50
|
+
if (!alias) return null;
|
|
51
|
+
const keys = Object.keys(alias).sort((a, b) => b.length - a.length);
|
|
52
|
+
for (const key of keys) {
|
|
53
|
+
const target = alias[key];
|
|
54
|
+
if (typeof target !== "string") continue;
|
|
55
|
+
if (key.endsWith("$")) {
|
|
56
|
+
if (spec === key.slice(0, -1)) return target;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (spec === key) return target;
|
|
60
|
+
if (spec.startsWith(key + "/")) {
|
|
61
|
+
return import_node_path.default.join(target, spec.slice(key.length + 1));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
function mapType(type) {
|
|
67
|
+
if (type.isString() || type.isStringLiteral()) return "string";
|
|
68
|
+
if (type.isNumber() || type.isNumberLiteral()) return "number";
|
|
69
|
+
if (type.isBoolean() || type.isBooleanLiteral()) return "boolean";
|
|
70
|
+
if (type.isArray()) return "array";
|
|
71
|
+
if (type.isUnion()) {
|
|
72
|
+
const filtered = type.getUnionTypes().filter((t) => !t.isUndefined() && !t.isNull());
|
|
73
|
+
if (filtered.length === 0) return "string";
|
|
74
|
+
if (filtered.length === 1) return mapType(filtered[0]);
|
|
75
|
+
if (filtered.every((t) => t.isStringLiteral())) return "string";
|
|
76
|
+
if (filtered.every((t) => t.isNumberLiteral())) return "number";
|
|
77
|
+
if (filtered.every((t) => t.isBooleanLiteral())) return "boolean";
|
|
78
|
+
return "string";
|
|
79
|
+
}
|
|
80
|
+
if (type.isObject() && !type.isArray()) return "object";
|
|
81
|
+
if (type.isNull() || type.isUndefined()) return "string";
|
|
82
|
+
if (type.isAny() || type.isUnknown()) return "string";
|
|
83
|
+
return "string";
|
|
84
|
+
}
|
|
85
|
+
function getArrayItemType(type) {
|
|
86
|
+
if (!type.isArray()) return void 0;
|
|
87
|
+
const typeArgs = type.getTypeArguments();
|
|
88
|
+
if (typeArgs.length > 0) {
|
|
89
|
+
return mapType(typeArgs[0]);
|
|
90
|
+
}
|
|
91
|
+
return "string";
|
|
92
|
+
}
|
|
93
|
+
function extractEnumValues(type) {
|
|
94
|
+
if (!type.isUnion()) return void 0;
|
|
95
|
+
const unionTypes = type.getUnionTypes().filter((t) => !t.isUndefined() && !t.isNull());
|
|
96
|
+
if (unionTypes.length === 0) return void 0;
|
|
97
|
+
if (unionTypes.every((t) => t.isStringLiteral())) {
|
|
98
|
+
return unionTypes.map((t) => t.getLiteralValue());
|
|
99
|
+
}
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
function getPropertyDescription(symbol) {
|
|
103
|
+
const declarations = symbol.getDeclarations();
|
|
104
|
+
for (const decl of declarations) {
|
|
105
|
+
const jsDocs = decl.getJsDocs?.();
|
|
106
|
+
if (jsDocs && jsDocs.length > 0) {
|
|
107
|
+
const comment = jsDocs[0].getDescription?.();
|
|
108
|
+
if (comment) return comment.trim();
|
|
109
|
+
}
|
|
110
|
+
const fullText = decl.getFullText();
|
|
111
|
+
const match = fullText.match(/\/\*\*\s*(.*?)\s*\*\//s);
|
|
112
|
+
if (match) {
|
|
113
|
+
const raw = match[1].split("\n").map((line) => line.replace(/^\s*\*\s?/, "").trim()).filter(Boolean).join(" ");
|
|
114
|
+
if (raw) return raw;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return void 0;
|
|
118
|
+
}
|
|
119
|
+
function extractProperties(type, depth = 0) {
|
|
120
|
+
const properties = [];
|
|
121
|
+
for (const prop of type.getProperties()) {
|
|
122
|
+
const propType = prop.getValueDeclaration() ? prop.getTypeAtLocation(prop.getValueDeclaration()) : prop.getDeclaredType();
|
|
123
|
+
const isOptional = prop.isOptional();
|
|
124
|
+
const enumValues = extractEnumValues(propType);
|
|
125
|
+
const mappedType = mapType(propType);
|
|
126
|
+
let nestedProperties;
|
|
127
|
+
if (mappedType === "object" && depth < 3) {
|
|
128
|
+
nestedProperties = extractProperties(propType, depth + 1);
|
|
129
|
+
}
|
|
130
|
+
properties.push({
|
|
131
|
+
name: prop.getName(),
|
|
132
|
+
type: mappedType,
|
|
133
|
+
description: getPropertyDescription(prop),
|
|
134
|
+
required: !isOptional,
|
|
135
|
+
enumValues,
|
|
136
|
+
itemType: getArrayItemType(propType),
|
|
137
|
+
properties: nestedProperties
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return properties;
|
|
141
|
+
}
|
|
142
|
+
function extractFunctionMetadata(node, _sourceFile) {
|
|
143
|
+
let description = "";
|
|
144
|
+
let readOnly = false;
|
|
145
|
+
let properties = [];
|
|
146
|
+
if (node.getKind() === import_ts_morph.SyntaxKind.FunctionDeclaration) {
|
|
147
|
+
const funcDecl = node;
|
|
148
|
+
const jsDocs = funcDecl.getJsDocs();
|
|
149
|
+
if (jsDocs.length > 0) {
|
|
150
|
+
description = jsDocs[0].getDescription()?.trim() ?? "";
|
|
151
|
+
const tags = jsDocs[0].getTags();
|
|
152
|
+
readOnly = tags.some((tag) => tag.getTagName() === "readonly");
|
|
153
|
+
}
|
|
154
|
+
const params = funcDecl.getParameters();
|
|
155
|
+
if (params.length > 0) {
|
|
156
|
+
const paramType = params[0].getType();
|
|
157
|
+
properties = extractProperties(paramType);
|
|
158
|
+
}
|
|
159
|
+
return { description, readOnly, properties };
|
|
160
|
+
}
|
|
161
|
+
if (node.getKind() === import_ts_morph.SyntaxKind.ArrowFunction || node.getKind() === import_ts_morph.SyntaxKind.FunctionExpression) {
|
|
162
|
+
const funcNode = node;
|
|
163
|
+
const parent = funcNode.getParent();
|
|
164
|
+
if (parent) {
|
|
165
|
+
let varStatement;
|
|
166
|
+
if (parent.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
167
|
+
varStatement = parent.getParent()?.getParent();
|
|
168
|
+
} else if (parent.getKind() === import_ts_morph.SyntaxKind.CallExpression) {
|
|
169
|
+
const callParent = parent.getParent();
|
|
170
|
+
if (callParent?.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
171
|
+
varStatement = callParent.getParent()?.getParent();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (varStatement) {
|
|
175
|
+
const jsDocs = varStatement.getJsDocs?.();
|
|
176
|
+
if (jsDocs && jsDocs.length > 0) {
|
|
177
|
+
description = jsDocs[0].getDescription?.()?.trim() ?? "";
|
|
178
|
+
const tags = jsDocs[0].getTags?.() ?? [];
|
|
179
|
+
readOnly = tags.some((tag) => tag.getTagName() === "readonly");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const params = funcNode.getParameters();
|
|
184
|
+
if (params.length > 0) {
|
|
185
|
+
const paramType = params[0].getType();
|
|
186
|
+
properties = extractProperties(paramType);
|
|
187
|
+
}
|
|
188
|
+
return { description, readOnly, properties };
|
|
189
|
+
}
|
|
190
|
+
return { description, readOnly, properties };
|
|
191
|
+
}
|
|
192
|
+
function extractToolFromBindingElement(bindingElement, toolName, relPath) {
|
|
193
|
+
const be = bindingElement;
|
|
194
|
+
const nameNode = be.getNameNode?.();
|
|
195
|
+
if (!nameNode) return null;
|
|
196
|
+
const bindingType = nameNode.getType();
|
|
197
|
+
const callSignatures = bindingType.getCallSignatures();
|
|
198
|
+
if (callSignatures.length === 0) return null;
|
|
199
|
+
let properties = [];
|
|
200
|
+
const sig = callSignatures[0];
|
|
201
|
+
const sigParams = sig.getParameters();
|
|
202
|
+
if (sigParams.length > 0) {
|
|
203
|
+
const paramSym = sigParams[0];
|
|
204
|
+
const paramDecls = paramSym.getDeclarations();
|
|
205
|
+
if (paramDecls.length > 0) {
|
|
206
|
+
const paramType = paramSym.getTypeAtLocation(paramDecls[0]);
|
|
207
|
+
properties = extractProperties(paramType);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
const propertyNameNode = be.getPropertyNameNode?.();
|
|
211
|
+
const propertyName = propertyNameNode ? propertyNameNode.getText() : toolName;
|
|
212
|
+
let description = "";
|
|
213
|
+
let readOnly = false;
|
|
214
|
+
const objectBindingPattern = bindingElement.getParent();
|
|
215
|
+
const variableDecl = objectBindingPattern?.getParent();
|
|
216
|
+
if (variableDecl && variableDecl.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
217
|
+
const initializer = variableDecl.getInitializer?.();
|
|
218
|
+
if (initializer) {
|
|
219
|
+
const initType = initializer.getType();
|
|
220
|
+
let propSym = initType.getProperty(propertyName);
|
|
221
|
+
if (!propSym && initType.isUnion?.()) {
|
|
222
|
+
for (const t of initType.getUnionTypes()) {
|
|
223
|
+
if (t.isNull?.() || t.isUndefined?.()) continue;
|
|
224
|
+
propSym = t.getProperty(propertyName);
|
|
225
|
+
if (propSym) break;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
if (propSym) {
|
|
229
|
+
const propDecls = propSym.getDeclarations();
|
|
230
|
+
for (const pdecl of propDecls) {
|
|
231
|
+
const jsDocs = pdecl.getJsDocs?.();
|
|
232
|
+
if (jsDocs && jsDocs.length > 0) {
|
|
233
|
+
description = jsDocs[0].getDescription?.()?.trim() ?? "";
|
|
234
|
+
const tags = jsDocs[0].getTags?.() ?? [];
|
|
235
|
+
readOnly = tags.some((t) => t.getTagName() === "readonly");
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
const fullText = pdecl.getFullText();
|
|
239
|
+
const m = fullText.match(/\/\*\*\s*([\s\S]*?)\s*\*\//);
|
|
240
|
+
if (m) {
|
|
241
|
+
const raw = m[1].split("\n").map((line) => line.replace(/^\s*\*\s?/, "").trim()).filter(Boolean).join(" ");
|
|
242
|
+
if (raw) {
|
|
243
|
+
description = raw;
|
|
244
|
+
if (/@readonly\b/.test(m[1])) readOnly = true;
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
name: toolName,
|
|
254
|
+
description,
|
|
255
|
+
properties,
|
|
256
|
+
readOnly,
|
|
257
|
+
sourceFile: relPath,
|
|
258
|
+
injectionTarget: toolName
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function resolveObjectLiteralArg(arg, sourceFile, project, projectRoot) {
|
|
262
|
+
const tools = [];
|
|
263
|
+
if (arg.getKind() !== import_ts_morph.SyntaxKind.ObjectLiteralExpression) return tools;
|
|
264
|
+
const objLiteral = arg;
|
|
265
|
+
const filePath = sourceFile.getFilePath();
|
|
266
|
+
const baseDir = projectRoot ?? process.cwd();
|
|
267
|
+
const relPath = "./" + import_node_path.default.relative(baseDir, filePath).replace(/\.tsx?$/, "");
|
|
268
|
+
for (const prop of objLiteral.getProperties()) {
|
|
269
|
+
let toolName;
|
|
270
|
+
let valueNode;
|
|
271
|
+
if (prop.getKind() === import_ts_morph.SyntaxKind.ShorthandPropertyAssignment) {
|
|
272
|
+
toolName = prop.getName();
|
|
273
|
+
const tc = project.getTypeChecker().compilerObject;
|
|
274
|
+
const valueSym = tc.getShorthandAssignmentValueSymbol(prop.compilerNode);
|
|
275
|
+
if (!valueSym || !valueSym.declarations || valueSym.declarations.length === 0) continue;
|
|
276
|
+
const decl = sourceFile._getNodeFromCompilerNode(valueSym.declarations[0]);
|
|
277
|
+
if (decl.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
278
|
+
valueNode = decl.getInitializer?.();
|
|
279
|
+
} else if (decl.getKind() === import_ts_morph.SyntaxKind.FunctionDeclaration) {
|
|
280
|
+
valueNode = decl;
|
|
281
|
+
} else if (decl.getKind() === import_ts_morph.SyntaxKind.Parameter) {
|
|
282
|
+
continue;
|
|
283
|
+
} else if (decl.getKind() === import_ts_morph.SyntaxKind.BindingElement) {
|
|
284
|
+
const tool = extractToolFromBindingElement(decl, toolName, relPath);
|
|
285
|
+
if (tool) tools.push(tool);
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
} else if (prop.getKind() === import_ts_morph.SyntaxKind.PropertyAssignment) {
|
|
289
|
+
toolName = prop.getName();
|
|
290
|
+
valueNode = prop.getInitializer?.();
|
|
291
|
+
} else {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
if (!valueNode) continue;
|
|
295
|
+
let funcNode = valueNode;
|
|
296
|
+
if (valueNode.getKind() === import_ts_morph.SyntaxKind.CallExpression) {
|
|
297
|
+
const callExpr = valueNode;
|
|
298
|
+
const callName = callExpr.getExpression().getText();
|
|
299
|
+
if (callName === "useCallback") {
|
|
300
|
+
const args = callExpr.getArguments();
|
|
301
|
+
if (args.length > 0) {
|
|
302
|
+
funcNode = args[0];
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
if (funcNode.getKind() === import_ts_morph.SyntaxKind.Identifier) {
|
|
307
|
+
const symbol = funcNode.getSymbol();
|
|
308
|
+
if (symbol) {
|
|
309
|
+
const declarations = symbol.getDeclarations();
|
|
310
|
+
if (declarations.length > 0) {
|
|
311
|
+
const decl = declarations[0];
|
|
312
|
+
if (decl.getKind() === import_ts_morph.SyntaxKind.FunctionDeclaration) {
|
|
313
|
+
funcNode = decl;
|
|
314
|
+
} else if (decl.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
315
|
+
const init = decl.getInitializer?.();
|
|
316
|
+
if (init) funcNode = init;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const metadata = extractFunctionMetadata(funcNode, sourceFile);
|
|
322
|
+
tools.push({
|
|
323
|
+
name: toolName,
|
|
324
|
+
description: metadata.description,
|
|
325
|
+
properties: metadata.properties,
|
|
326
|
+
readOnly: metadata.readOnly,
|
|
327
|
+
sourceFile: relPath,
|
|
328
|
+
injectionTarget: toolName
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
return tools;
|
|
332
|
+
}
|
|
333
|
+
function resolveNamespaceImportArg(arg, sourceFile, project, projectRoot, alias) {
|
|
334
|
+
const tools = [];
|
|
335
|
+
if (arg.getKind() !== import_ts_morph.SyntaxKind.Identifier) return tools;
|
|
336
|
+
const identifier = arg;
|
|
337
|
+
const symbol = identifier.getSymbol();
|
|
338
|
+
if (!symbol) return tools;
|
|
339
|
+
const declarations = symbol.getDeclarations();
|
|
340
|
+
if (declarations.length === 0) return tools;
|
|
341
|
+
const decl = declarations[0];
|
|
342
|
+
if (decl.getKind() !== import_ts_morph.SyntaxKind.NamespaceImport) return tools;
|
|
343
|
+
const importDecl = decl.getParent()?.getParent();
|
|
344
|
+
if (!importDecl) return tools;
|
|
345
|
+
const moduleSpecifier = importDecl.getModuleSpecifierValue?.();
|
|
346
|
+
if (!moduleSpecifier) return tools;
|
|
347
|
+
const currentDir = import_node_path.default.dirname(sourceFile.getFilePath());
|
|
348
|
+
const aliasResolved = resolveWithAlias(moduleSpecifier, alias);
|
|
349
|
+
let resolvedPath = null;
|
|
350
|
+
if (aliasResolved) {
|
|
351
|
+
resolvedPath = import_node_path.default.isAbsolute(aliasResolved) ? aliasResolved : import_node_path.default.resolve(projectRoot ?? currentDir, aliasResolved);
|
|
352
|
+
} else if (moduleSpecifier.startsWith(".") || import_node_path.default.isAbsolute(moduleSpecifier)) {
|
|
353
|
+
resolvedPath = import_node_path.default.resolve(currentDir, moduleSpecifier);
|
|
354
|
+
}
|
|
355
|
+
if (!resolvedPath) {
|
|
356
|
+
if (isDebug()) {
|
|
357
|
+
console.warn(
|
|
358
|
+
`[webmcp] skip bare module specifier "${moduleSpecifier}" in ${sourceFile.getFilePath()} (no alias match)`
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
return tools;
|
|
362
|
+
}
|
|
363
|
+
const possibleExtensions = [".ts", ".tsx", "/index.ts", "/index.tsx"];
|
|
364
|
+
let targetFile;
|
|
365
|
+
for (const ext of possibleExtensions) {
|
|
366
|
+
const tryPath = resolvedPath + ext;
|
|
367
|
+
targetFile = project.getSourceFile(tryPath);
|
|
368
|
+
if (targetFile) break;
|
|
369
|
+
}
|
|
370
|
+
if (!targetFile) {
|
|
371
|
+
for (const ext of possibleExtensions) {
|
|
372
|
+
const tryPath = resolvedPath + ext;
|
|
373
|
+
try {
|
|
374
|
+
targetFile = project.addSourceFileAtPath(tryPath);
|
|
375
|
+
if (targetFile) break;
|
|
376
|
+
} catch (err) {
|
|
377
|
+
if (isDebug()) {
|
|
378
|
+
console.warn(`[webmcp] failed to add source file: ${tryPath}`, err);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
if (!targetFile) {
|
|
384
|
+
console.warn(
|
|
385
|
+
`[webmcp] cannot resolve module "${moduleSpecifier}" from ${sourceFile.getFilePath()}. Tried extensions: ${possibleExtensions.join(", ")}. Resolved base: ${resolvedPath}`
|
|
386
|
+
);
|
|
387
|
+
return tools;
|
|
388
|
+
}
|
|
389
|
+
const namespaceName = identifier.getText();
|
|
390
|
+
const baseDir = projectRoot ?? process.cwd();
|
|
391
|
+
const relPath = "./" + import_node_path.default.relative(baseDir, targetFile.getFilePath()).replace(/\.tsx?$/, "");
|
|
392
|
+
for (const exportedDecl of targetFile.getExportedDeclarations()) {
|
|
393
|
+
const [exportName, declarations2] = exportedDecl;
|
|
394
|
+
if (declarations2.length === 0) continue;
|
|
395
|
+
const exportNode = declarations2[0];
|
|
396
|
+
let funcNode;
|
|
397
|
+
if (exportNode.getKind() === import_ts_morph.SyntaxKind.FunctionDeclaration) {
|
|
398
|
+
funcNode = exportNode;
|
|
399
|
+
} else if (exportNode.getKind() === import_ts_morph.SyntaxKind.VariableDeclaration) {
|
|
400
|
+
const init = exportNode.getInitializer?.();
|
|
401
|
+
if (init && (init.getKind() === import_ts_morph.SyntaxKind.ArrowFunction || init.getKind() === import_ts_morph.SyntaxKind.FunctionExpression)) {
|
|
402
|
+
funcNode = init;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (!funcNode) continue;
|
|
406
|
+
const metadata = extractFunctionMetadata(funcNode, targetFile);
|
|
407
|
+
tools.push({
|
|
408
|
+
name: exportName,
|
|
409
|
+
description: metadata.description,
|
|
410
|
+
properties: metadata.properties,
|
|
411
|
+
readOnly: metadata.readOnly,
|
|
412
|
+
sourceFile: relPath,
|
|
413
|
+
injectionTarget: `${namespaceName}.${exportName}`
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
return tools;
|
|
417
|
+
}
|
|
418
|
+
function extractToolsFromFile(fileContent, filePath, projectRoot, alias) {
|
|
419
|
+
const hasRegistration = REGISTRATION_FUNCTIONS.some((fn) => fileContent.includes(fn));
|
|
420
|
+
if (!hasRegistration) return null;
|
|
421
|
+
if (isDebug()) {
|
|
422
|
+
console.log(
|
|
423
|
+
`[webmcp] extractToolsFromFile: ${filePath}, projectRoot=${projectRoot ?? "(none)"}`
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
try {
|
|
427
|
+
const project = new import_ts_morph.Project({
|
|
428
|
+
compilerOptions: {
|
|
429
|
+
strict: true,
|
|
430
|
+
target: 99,
|
|
431
|
+
// ESNext
|
|
432
|
+
module: 99,
|
|
433
|
+
// ESNext
|
|
434
|
+
jsx: 4,
|
|
435
|
+
// ReactJSX
|
|
436
|
+
moduleResolution: 100,
|
|
437
|
+
// Bundler
|
|
438
|
+
esModuleInterop: true,
|
|
439
|
+
...projectRoot ? { rootDir: projectRoot, baseUrl: projectRoot } : {}
|
|
440
|
+
},
|
|
441
|
+
skipAddingFilesFromTsConfig: true,
|
|
442
|
+
useInMemoryFileSystem: false
|
|
443
|
+
});
|
|
444
|
+
const sourceFile = project.createSourceFile(filePath, fileContent, {
|
|
445
|
+
overwrite: true
|
|
446
|
+
});
|
|
447
|
+
const result = {
|
|
448
|
+
tools: [],
|
|
449
|
+
registrationCalls: []
|
|
450
|
+
};
|
|
451
|
+
sourceFile.forEachDescendant((node) => {
|
|
452
|
+
if (node.getKind() !== import_ts_morph.SyntaxKind.CallExpression) return;
|
|
453
|
+
const callExpr = node;
|
|
454
|
+
const exprText = callExpr.getExpression().getText();
|
|
455
|
+
if (!REGISTRATION_FUNCTIONS.includes(exprText)) return;
|
|
456
|
+
result.registrationCalls.push({
|
|
457
|
+
type: exprText,
|
|
458
|
+
start: callExpr.getStart()
|
|
459
|
+
});
|
|
460
|
+
const args = callExpr.getArguments();
|
|
461
|
+
for (const arg of args) {
|
|
462
|
+
if (arg.getKind() === import_ts_morph.SyntaxKind.ObjectLiteralExpression) {
|
|
463
|
+
const tools = resolveObjectLiteralArg(arg, sourceFile, project, projectRoot);
|
|
464
|
+
result.tools.push(...tools);
|
|
465
|
+
} else if (arg.getKind() === import_ts_morph.SyntaxKind.Identifier) {
|
|
466
|
+
const tools = resolveNamespaceImportArg(arg, sourceFile, project, projectRoot, alias);
|
|
467
|
+
result.tools.push(...tools);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
});
|
|
471
|
+
if (result.tools.length === 0) {
|
|
472
|
+
if (isDebug()) {
|
|
473
|
+
console.warn(`[webmcp] no tools extracted from ${filePath}`);
|
|
474
|
+
}
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
return result;
|
|
478
|
+
} catch (err) {
|
|
479
|
+
console.warn(
|
|
480
|
+
`[webmcp] extractToolsFromFile failed for ${filePath}:`,
|
|
481
|
+
err instanceof Error ? err.message : err
|
|
482
|
+
);
|
|
483
|
+
if (isDebug()) {
|
|
484
|
+
console.warn(`[webmcp] full error:`, err);
|
|
485
|
+
}
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// src/schema-generator.ts
|
|
491
|
+
function generateSchema(properties, description) {
|
|
492
|
+
const schema = {
|
|
493
|
+
type: "object",
|
|
494
|
+
properties: {},
|
|
495
|
+
required: []
|
|
496
|
+
};
|
|
497
|
+
if (description) {
|
|
498
|
+
schema.description = description;
|
|
499
|
+
}
|
|
500
|
+
for (const prop of properties) {
|
|
501
|
+
const propSchema = mapTypeToSchema(prop);
|
|
502
|
+
schema.properties[prop.name] = propSchema;
|
|
503
|
+
if (prop.required) {
|
|
504
|
+
schema.required.push(prop.name);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
if (schema.required.length === 0) {
|
|
508
|
+
delete schema.required;
|
|
509
|
+
}
|
|
510
|
+
return schema;
|
|
511
|
+
}
|
|
512
|
+
function generateSchemaInjectionCode(injectionTarget, description, properties, readOnly) {
|
|
513
|
+
const inputSchema = generateSchema(properties);
|
|
514
|
+
delete inputSchema.description;
|
|
515
|
+
const schemaObj = {
|
|
516
|
+
description,
|
|
517
|
+
inputSchema,
|
|
518
|
+
readOnly
|
|
519
|
+
};
|
|
520
|
+
return `${injectionTarget}.__webmcpSchema = ${JSON.stringify(schemaObj, null, 2)};`;
|
|
521
|
+
}
|
|
522
|
+
function mapTypeToSchema(prop) {
|
|
523
|
+
const schema = { type: "string" };
|
|
524
|
+
if (prop.description) {
|
|
525
|
+
schema.description = prop.description;
|
|
526
|
+
}
|
|
527
|
+
if (prop.enumValues && prop.enumValues.length > 0) {
|
|
528
|
+
schema.type = "string";
|
|
529
|
+
schema.enum = prop.enumValues;
|
|
530
|
+
return schema;
|
|
531
|
+
}
|
|
532
|
+
switch (prop.type) {
|
|
533
|
+
case "string":
|
|
534
|
+
schema.type = "string";
|
|
535
|
+
break;
|
|
536
|
+
case "number":
|
|
537
|
+
schema.type = "number";
|
|
538
|
+
break;
|
|
539
|
+
case "boolean":
|
|
540
|
+
schema.type = "boolean";
|
|
541
|
+
break;
|
|
542
|
+
case "array":
|
|
543
|
+
schema.type = "array";
|
|
544
|
+
if (prop.itemType) {
|
|
545
|
+
schema.items = { type: prop.itemType };
|
|
546
|
+
}
|
|
547
|
+
break;
|
|
548
|
+
case "object":
|
|
549
|
+
schema.type = "object";
|
|
550
|
+
if (prop.properties && prop.properties.length > 0) {
|
|
551
|
+
schema.properties = {};
|
|
552
|
+
const required = [];
|
|
553
|
+
for (const subProp of prop.properties) {
|
|
554
|
+
schema.properties[subProp.name] = mapTypeToSchema(subProp);
|
|
555
|
+
if (subProp.required) required.push(subProp.name);
|
|
556
|
+
}
|
|
557
|
+
if (required.length > 0) schema.required = required;
|
|
558
|
+
}
|
|
559
|
+
break;
|
|
560
|
+
default:
|
|
561
|
+
schema.type = "string";
|
|
562
|
+
}
|
|
563
|
+
return schema;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// src/transform.ts
|
|
567
|
+
var isDebug2 = () => (process.env.DEBUG ?? "").toLowerCase().includes("webmcp");
|
|
568
|
+
function transformCode(code, filePath, options = {}) {
|
|
569
|
+
if (!code.includes("registerGlobalTools") && !code.includes("useWebMcpTools")) {
|
|
570
|
+
return { code, transformed: false };
|
|
571
|
+
}
|
|
572
|
+
if (isDebug2()) {
|
|
573
|
+
console.log(
|
|
574
|
+
`[webmcp] transformCode: ${filePath}, projectRoot=${options.projectRoot ?? "(none)"}`
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
const result = extractToolsFromFile(code, filePath, options.projectRoot, options.alias);
|
|
578
|
+
if (!result || result.tools.length === 0) {
|
|
579
|
+
return { code, transformed: false };
|
|
580
|
+
}
|
|
581
|
+
const injections = [];
|
|
582
|
+
for (const tool of result.tools) {
|
|
583
|
+
const injectionCode = generateSchemaInjectionCode(
|
|
584
|
+
tool.injectionTarget,
|
|
585
|
+
tool.description,
|
|
586
|
+
tool.properties,
|
|
587
|
+
tool.readOnly
|
|
588
|
+
);
|
|
589
|
+
injections.push(injectionCode);
|
|
590
|
+
}
|
|
591
|
+
if (injections.length === 0) {
|
|
592
|
+
return { code, transformed: false };
|
|
593
|
+
}
|
|
594
|
+
const firstCall = result.registrationCalls.sort((a, b) => a.start - b.start)[0];
|
|
595
|
+
const injectionBlock = "\n// [webmcp-nexus-core] \u6784\u5EFA\u65F6\u6CE8\u5165\u7684 schema \u5143\u6570\u636E\n" + injections.join("\n") + "\n";
|
|
596
|
+
const newCode = code.slice(0, firstCall.start) + injectionBlock + code.slice(firstCall.start);
|
|
597
|
+
return { code: newCode, transformed: true };
|
|
598
|
+
}
|
|
599
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
600
|
+
0 && (module.exports = {
|
|
601
|
+
extractProperties,
|
|
602
|
+
extractToolsFromFile,
|
|
603
|
+
generateSchema,
|
|
604
|
+
generateSchemaInjectionCode,
|
|
605
|
+
mapType,
|
|
606
|
+
mapTypeToSchema,
|
|
607
|
+
resolveWithAlias,
|
|
608
|
+
transformCode
|
|
609
|
+
});
|