@easynet/agent-tool 1.0.53 → 1.0.55
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/dist/api/adapters/LangChainToolsHub.d.ts.map +1 -1
- package/dist/api/expose/mcp-build/generator.d.ts.map +1 -1
- package/dist/api/main.cjs +13 -13
- package/dist/api/main.js +2 -2
- package/dist/api/runtimeFromConfig.d.ts.map +1 -1
- package/dist/{chunk-WFPHIYB6.cjs → chunk-D2T3NTFN.cjs} +1726 -51
- package/dist/chunk-D2T3NTFN.cjs.map +1 -0
- package/dist/chunk-TFT4RYFZ.cjs +353 -0
- package/dist/chunk-TFT4RYFZ.cjs.map +1 -0
- package/dist/chunk-W4AGLV2U.js +325 -0
- package/dist/chunk-W4AGLV2U.js.map +1 -0
- package/dist/{chunk-DDTTGZLU.cjs → chunk-WAGGOFT3.cjs} +17 -16
- package/dist/chunk-WAGGOFT3.cjs.map +1 -0
- package/dist/{chunk-C56WRG4H.js → chunk-WC54NBDE.js} +5 -4
- package/dist/chunk-WC54NBDE.js.map +1 -0
- package/dist/{chunk-6VU6PRO3.js → chunk-XT2CXR6A.js} +1703 -52
- package/dist/chunk-XT2CXR6A.js.map +1 -0
- package/dist/index.cjs +30 -30
- package/dist/index.js +4 -4
- package/dist/utils/cli/index.cjs +16 -16
- package/dist/utils/cli/index.js +2 -2
- package/package.json +2 -2
- package/dist/chunk-6VU6PRO3.js.map +0 -1
- package/dist/chunk-C56WRG4H.js.map +0 -1
- package/dist/chunk-DDTTGZLU.cjs.map +0 -1
- package/dist/chunk-K45PQLZI.cjs +0 -1868
- package/dist/chunk-K45PQLZI.cjs.map +0 -1
- package/dist/chunk-WFPHIYB6.cjs.map +0 -1
- package/dist/chunk-Z5RKNQQB.js +0 -1835
- package/dist/chunk-Z5RKNQQB.js.map +0 -1
package/dist/chunk-K45PQLZI.cjs
DELETED
|
@@ -1,1868 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkWFPHIYB6_cjs = require('./chunk-WFPHIYB6.cjs');
|
|
4
|
-
var chunkQEJF3KDV_cjs = require('./chunk-QEJF3KDV.cjs');
|
|
5
|
-
var fs3 = require('fs/promises');
|
|
6
|
-
var path9 = require('path');
|
|
7
|
-
var fs2 = require('fs');
|
|
8
|
-
var ts2 = require('typescript');
|
|
9
|
-
var yaml = require('js-yaml');
|
|
10
|
-
var url = require('url');
|
|
11
|
-
var child_process = require('child_process');
|
|
12
|
-
|
|
13
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
14
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
|
-
|
|
16
|
-
function _interopNamespace(e) {
|
|
17
|
-
if (e && e.__esModule) return e;
|
|
18
|
-
var n = Object.create(null);
|
|
19
|
-
if (e) {
|
|
20
|
-
Object.keys(e).forEach(function (k) {
|
|
21
|
-
if (k !== 'default') {
|
|
22
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
-
enumerable: true,
|
|
25
|
-
get: function () { return e[k]; }
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
n.default = e;
|
|
31
|
-
return Object.freeze(n);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var fs3__namespace = /*#__PURE__*/_interopNamespace(fs3);
|
|
35
|
-
var path9__namespace = /*#__PURE__*/_interopNamespace(path9);
|
|
36
|
-
var fs2__namespace = /*#__PURE__*/_interopNamespace(fs2);
|
|
37
|
-
var ts2__namespace = /*#__PURE__*/_interopNamespace(ts2);
|
|
38
|
-
var yaml__default = /*#__PURE__*/_interopDefault(yaml);
|
|
39
|
-
|
|
40
|
-
var TEMPLATES = {
|
|
41
|
-
"package.json": `{
|
|
42
|
-
"name": "my-mcp-tools",
|
|
43
|
-
"version": "1.0.0",
|
|
44
|
-
"private": true,
|
|
45
|
-
"type": "module",
|
|
46
|
-
"scripts": {
|
|
47
|
-
"build": "node build.mjs",
|
|
48
|
-
"start": "node start.mjs"
|
|
49
|
-
},
|
|
50
|
-
"devDependencies": {
|
|
51
|
-
"@easynet/agent-tool": "latest"
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
`,
|
|
55
|
-
"build.mjs": `import { buildMcpPackage } from "@easynet/agent-tool";
|
|
56
|
-
|
|
57
|
-
const result = await buildMcpPackage({ outDir: "dist" });
|
|
58
|
-
console.log("Built", result.toolCount, "tool(s) ->", result.outDir);
|
|
59
|
-
`,
|
|
60
|
-
"start.mjs": `import { runMcpServer } from "@easynet/agent-tool";
|
|
61
|
-
|
|
62
|
-
const { process: child } = await runMcpServer({ path: "./dist" });
|
|
63
|
-
child.stdin?.pipe(process.stdin);
|
|
64
|
-
child.stdout?.pipe(process.stdout);
|
|
65
|
-
child.stderr?.pipe(process.stderr);
|
|
66
|
-
process.stdin?.ref();
|
|
67
|
-
child.on("close", (code) => process.exit(code ?? 0));
|
|
68
|
-
`,
|
|
69
|
-
"tsconfig.json": `{
|
|
70
|
-
"compilerOptions": {
|
|
71
|
-
"target": "ES2022",
|
|
72
|
-
"module": "ESNext",
|
|
73
|
-
"strict": true,
|
|
74
|
-
"skipLibCheck": true,
|
|
75
|
-
"noEmit": true
|
|
76
|
-
},
|
|
77
|
-
"include": ["src/**/*.ts"]
|
|
78
|
-
}
|
|
79
|
-
`,
|
|
80
|
-
"src/tools/example.ts": `/**
|
|
81
|
-
* Add two numbers.
|
|
82
|
-
* @tool
|
|
83
|
-
*/
|
|
84
|
-
export async function add(a: number, b: number): Promise<number> {
|
|
85
|
-
return a + b;
|
|
86
|
-
}
|
|
87
|
-
`,
|
|
88
|
-
"skills/hello-skill/SKILL.md": `---
|
|
89
|
-
name: hello-skill
|
|
90
|
-
description: Say hello with a name.
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
# Hello Skill
|
|
94
|
-
|
|
95
|
-
Returns a greeting for the given name.
|
|
96
|
-
`,
|
|
97
|
-
"skills/hello-skill/handler.js": `export default async function handler(args, ctx) {
|
|
98
|
-
const name = args?.name ?? "World";
|
|
99
|
-
return { message: \`Hello, \${name}!\` };
|
|
100
|
-
}
|
|
101
|
-
`,
|
|
102
|
-
"README.md": `# MCP Tool Project
|
|
103
|
-
|
|
104
|
-
Scaffolded by \`agent-tool init\`. Workflow:
|
|
105
|
-
|
|
106
|
-
1. **init** \u2014 create project template (you are here)
|
|
107
|
-
2. **npm install** \u2014 install dependencies
|
|
108
|
-
3. **npm run build** \u2014 generate MCP package under \`dist/\`
|
|
109
|
-
4. **npm start** \u2014 start the MCP server for testing
|
|
110
|
-
|
|
111
|
-
## Quick start
|
|
112
|
-
|
|
113
|
-
\`\`\`bash
|
|
114
|
-
npm install
|
|
115
|
-
npm run build
|
|
116
|
-
npm start
|
|
117
|
-
\`\`\`
|
|
118
|
-
|
|
119
|
-
Edit \`src/tools/*.ts\` (add \`@tool\` JSDoc) and/or \`skills/*/SKILL.md\`, then \`npm run build\` again.
|
|
120
|
-
`
|
|
121
|
-
};
|
|
122
|
-
async function initProject(options = {}) {
|
|
123
|
-
const targetPath = path9__namespace.resolve(options.targetPath ?? process.cwd());
|
|
124
|
-
const filesCreated = [];
|
|
125
|
-
await fs3__namespace.mkdir(targetPath, { recursive: true });
|
|
126
|
-
for (const [relPath, content] of Object.entries(TEMPLATES)) {
|
|
127
|
-
const fullPath = path9__namespace.join(targetPath, relPath);
|
|
128
|
-
await fs3__namespace.mkdir(path9__namespace.dirname(fullPath), { recursive: true });
|
|
129
|
-
try {
|
|
130
|
-
await fs3__namespace.access(fullPath);
|
|
131
|
-
if (relPath === "package.json") continue;
|
|
132
|
-
} catch {
|
|
133
|
-
}
|
|
134
|
-
await fs3__namespace.writeFile(fullPath, content, "utf8");
|
|
135
|
-
filesCreated.push(relPath);
|
|
136
|
-
}
|
|
137
|
-
return { targetPath, filesCreated };
|
|
138
|
-
}
|
|
139
|
-
async function findDirsContainingFile(rootPath, fileName) {
|
|
140
|
-
const found = [];
|
|
141
|
-
await collectDirsWithFile(rootPath, fileName, found);
|
|
142
|
-
return found;
|
|
143
|
-
}
|
|
144
|
-
async function collectDirsWithFile(dir, fileName, acc) {
|
|
145
|
-
let entries;
|
|
146
|
-
try {
|
|
147
|
-
const e = await fs3.readdir(dir, { withFileTypes: true });
|
|
148
|
-
entries = e.map((x) => ({
|
|
149
|
-
name: x.name,
|
|
150
|
-
isDirectory: x.isDirectory(),
|
|
151
|
-
isFile: x.isFile()
|
|
152
|
-
}));
|
|
153
|
-
} catch {
|
|
154
|
-
return;
|
|
155
|
-
}
|
|
156
|
-
if (entries.some((x) => x.isFile && x.name === fileName)) acc.push(dir);
|
|
157
|
-
for (const entry of entries) {
|
|
158
|
-
if (!entry.isDirectory || entry.name === "node_modules" || entry.name.startsWith(".")) continue;
|
|
159
|
-
await collectDirsWithFile(path9.join(dir, entry.name), fileName, acc);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
function pathToToolName(sourcePath, programName) {
|
|
163
|
-
const normalized = sourcePath.replace(/\\/g, "/").replace(/\.(ts|tsx|js|mjs|json)$/i, "");
|
|
164
|
-
const segments = normalized.split("/").filter(Boolean);
|
|
165
|
-
return segments.length === 0 ? programName : `${segments.join(".")}.${programName}`;
|
|
166
|
-
}
|
|
167
|
-
function buildOutputSchemaFromReturnType(node, typeChecker, onWarn) {
|
|
168
|
-
const sig = typeChecker.getSignatureFromDeclaration(node);
|
|
169
|
-
if (!sig) {
|
|
170
|
-
onWarn?.("Could not get signature for return type, using object");
|
|
171
|
-
return { type: "object", additionalProperties: true };
|
|
172
|
-
}
|
|
173
|
-
let returnType = typeChecker.getReturnTypeOfSignature(sig);
|
|
174
|
-
if (returnType.getSymbol?.()?.getName() === "Promise") {
|
|
175
|
-
const typeArgs = returnType.typeArguments;
|
|
176
|
-
if (typeArgs?.[0]) returnType = typeArgs[0];
|
|
177
|
-
}
|
|
178
|
-
const schema = typeToJsonSchema(returnType, typeChecker, onWarn);
|
|
179
|
-
const hasProps = typeof schema === "object" && schema.type === "object" && Object.keys(schema.properties ?? {}).length > 0;
|
|
180
|
-
return hasProps ? schema : { type: "object", additionalProperties: true };
|
|
181
|
-
}
|
|
182
|
-
function buildInputSchemaFromParams(node, typeChecker, onWarn) {
|
|
183
|
-
const properties = {};
|
|
184
|
-
const required = [];
|
|
185
|
-
if (!node.parameters.length) {
|
|
186
|
-
return { schema: { type: "object", properties: {} }, required: [] };
|
|
187
|
-
}
|
|
188
|
-
for (const param of node.parameters) {
|
|
189
|
-
const name = param.name.getText();
|
|
190
|
-
if (name.startsWith("_") && name.length <= 2) continue;
|
|
191
|
-
const sym = param.symbol;
|
|
192
|
-
const paramType = sym ? typeChecker.getTypeOfSymbolAtLocation(sym, param) : typeChecker.getTypeAtLocation(param);
|
|
193
|
-
const isOptional = !!param.questionToken || param.initializer !== void 0;
|
|
194
|
-
const propSchema = typeToJsonSchema(paramType, typeChecker, onWarn);
|
|
195
|
-
properties[name] = propSchema;
|
|
196
|
-
if (!isOptional) required.push(name);
|
|
197
|
-
}
|
|
198
|
-
const paramNames = Object.keys(properties);
|
|
199
|
-
if (paramNames.length === 1) {
|
|
200
|
-
const soleName = paramNames[0];
|
|
201
|
-
const inner = properties[soleName];
|
|
202
|
-
if (inner && typeof inner === "object" && inner.type === "object" && inner.properties && typeof inner.properties === "object") {
|
|
203
|
-
return {
|
|
204
|
-
schema: {
|
|
205
|
-
type: "object",
|
|
206
|
-
properties: inner.properties,
|
|
207
|
-
...Array.isArray(inner.required) && inner.required.length > 0 ? { required: inner.required } : {},
|
|
208
|
-
...inner.additionalProperties !== void 0 ? { additionalProperties: inner.additionalProperties } : {}
|
|
209
|
-
},
|
|
210
|
-
required: inner.required ?? []
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
schema: {
|
|
216
|
-
type: "object",
|
|
217
|
-
properties,
|
|
218
|
-
...required.length > 0 ? { required } : {}
|
|
219
|
-
},
|
|
220
|
-
required
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
function typeToJsonSchema(type, typeChecker, onWarn) {
|
|
224
|
-
const flags = type.flags;
|
|
225
|
-
if (flags & ts2__namespace.TypeFlags.String) return { type: "string" };
|
|
226
|
-
if (flags & ts2__namespace.TypeFlags.Number) return { type: "number" };
|
|
227
|
-
if (flags & ts2__namespace.TypeFlags.Boolean) return { type: "boolean" };
|
|
228
|
-
if (flags & ts2__namespace.TypeFlags.BooleanLiteral) return { type: "boolean" };
|
|
229
|
-
if (flags & ts2__namespace.TypeFlags.Null) return { type: "null" };
|
|
230
|
-
if (flags & ts2__namespace.TypeFlags.Undefined || flags & ts2__namespace.TypeFlags.Void) return {};
|
|
231
|
-
if (flags & ts2__namespace.TypeFlags.Any || flags & ts2__namespace.TypeFlags.Unknown) {
|
|
232
|
-
onWarn?.(`Unsupported type: any/unknown, using empty schema`);
|
|
233
|
-
return {};
|
|
234
|
-
}
|
|
235
|
-
if (type.isUnion?.()) {
|
|
236
|
-
const union = type;
|
|
237
|
-
const types = union.types;
|
|
238
|
-
const withoutUndef = types.filter(
|
|
239
|
-
(t) => !(t.flags & ts2__namespace.TypeFlags.Undefined) && !(t.flags & ts2__namespace.TypeFlags.Void)
|
|
240
|
-
);
|
|
241
|
-
if (withoutUndef.length === 1) return typeToJsonSchema(withoutUndef[0], typeChecker, onWarn);
|
|
242
|
-
if (withoutUndef.length === 0) return {};
|
|
243
|
-
const stringEnumValues = [];
|
|
244
|
-
let allStringLiterals = true;
|
|
245
|
-
for (const t of withoutUndef) {
|
|
246
|
-
if (t.flags & ts2__namespace.TypeFlags.StringLiteral) {
|
|
247
|
-
const lit = t;
|
|
248
|
-
if (typeof lit.value === "string") stringEnumValues.push(lit.value);
|
|
249
|
-
} else {
|
|
250
|
-
allStringLiterals = false;
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
if (allStringLiterals && stringEnumValues.length > 0) {
|
|
255
|
-
return { type: "string", enum: [...new Set(stringEnumValues)] };
|
|
256
|
-
}
|
|
257
|
-
let allBooleanLiterals = true;
|
|
258
|
-
for (const t of withoutUndef) {
|
|
259
|
-
if (!(t.flags & ts2__namespace.TypeFlags.BooleanLiteral)) {
|
|
260
|
-
allBooleanLiterals = false;
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
if (allBooleanLiterals) return { type: "boolean" };
|
|
265
|
-
}
|
|
266
|
-
if (flags & ts2__namespace.TypeFlags.StringLiteral) {
|
|
267
|
-
const lit = type;
|
|
268
|
-
if (typeof lit.value === "string") {
|
|
269
|
-
return { type: "string", enum: [lit.value] };
|
|
270
|
-
}
|
|
271
|
-
return { type: "string" };
|
|
272
|
-
}
|
|
273
|
-
if (typeChecker.isArrayType(type)) {
|
|
274
|
-
const typeRef = type;
|
|
275
|
-
const typeArgs = typeRef.typeArguments;
|
|
276
|
-
const itemType = typeArgs?.[0];
|
|
277
|
-
const items = itemType ? typeToJsonSchema(itemType, typeChecker, onWarn) : {};
|
|
278
|
-
return { type: "array", items: Object.keys(items).length ? items : {} };
|
|
279
|
-
}
|
|
280
|
-
const str = typeChecker.typeToString(type);
|
|
281
|
-
if (str === "string") return { type: "string" };
|
|
282
|
-
if (str === "number") return { type: "number" };
|
|
283
|
-
if (str === "boolean") return { type: "boolean" };
|
|
284
|
-
if (str.endsWith("[]")) {
|
|
285
|
-
const inner = str.slice(0, -2).trim();
|
|
286
|
-
const itemType = inner === "string" ? { type: "string" } : inner === "number" ? { type: "number" } : {};
|
|
287
|
-
return { type: "array", items: itemType };
|
|
288
|
-
}
|
|
289
|
-
if (type.getProperties && type.getProperties().length >= 0) {
|
|
290
|
-
const props = type.getProperties();
|
|
291
|
-
const properties = {};
|
|
292
|
-
const required = [];
|
|
293
|
-
for (const p of props) {
|
|
294
|
-
const decl = p.valueDeclaration;
|
|
295
|
-
const propType = decl ? typeChecker.getTypeAtLocation(decl) : typeChecker.getTypeOfSymbolAtLocation(p, p.valueDeclaration);
|
|
296
|
-
const optional = decl && ts2__namespace.isPropertySignature(decl) ? !!decl.questionToken : false;
|
|
297
|
-
properties[p.name] = typeToJsonSchema(propType, typeChecker, onWarn);
|
|
298
|
-
if (!optional) required.push(p.name);
|
|
299
|
-
}
|
|
300
|
-
return { type: "object", properties, ...required.length ? { required } : {} };
|
|
301
|
-
}
|
|
302
|
-
onWarn?.(`Unsupported type: ${str}, using object`);
|
|
303
|
-
return { type: "object" };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// src/tools/function/types.ts
|
|
307
|
-
var FUNCTION_KIND = "function";
|
|
308
|
-
|
|
309
|
-
// src/tools/skill/types.ts
|
|
310
|
-
var SKILL_KIND = "skill";
|
|
311
|
-
var SKILL_DIR_NAME = "skill";
|
|
312
|
-
|
|
313
|
-
// src/tools/n8n/types.ts
|
|
314
|
-
var N8N_KIND = "n8n";
|
|
315
|
-
|
|
316
|
-
// src/tools/mcp/mcpSpecToToolSpec.ts
|
|
317
|
-
var DEFAULT_OUTPUT = { type: "object", additionalProperties: true };
|
|
318
|
-
function mcpSpecToToolSpec(spec, projectPath) {
|
|
319
|
-
const base = {
|
|
320
|
-
name: spec.name,
|
|
321
|
-
version: "1.0.0",
|
|
322
|
-
kind: spec.kind,
|
|
323
|
-
description: spec.description,
|
|
324
|
-
inputSchema: spec.inputSchema ?? DEFAULT_OUTPUT,
|
|
325
|
-
outputSchema: "outputSchema" in spec && spec.outputSchema ? spec.outputSchema : chunkQEJF3KDV_cjs.DEFAULT_OUTPUT_SCHEMA,
|
|
326
|
-
capabilities: [],
|
|
327
|
-
_meta: spec._meta,
|
|
328
|
-
...spec.kind === N8N_KIND && "webhookUrl" in spec && spec.webhookUrl ? { endpoint: spec.webhookUrl } : {}
|
|
329
|
-
};
|
|
330
|
-
if (spec.kind === FUNCTION_KIND && "sourcePath" in spec && "exportName" in spec) {
|
|
331
|
-
base._meta = {
|
|
332
|
-
...base._meta,
|
|
333
|
-
sourcePath: spec.sourcePath,
|
|
334
|
-
exportName: spec.exportName,
|
|
335
|
-
...projectPath && { projectPath }
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
|
-
if (spec.kind === SKILL_KIND && "sourcePath" in spec && projectPath) {
|
|
339
|
-
base._meta = { ...base._meta, sourcePath: spec.sourcePath, projectPath };
|
|
340
|
-
}
|
|
341
|
-
if (spec.kind === N8N_KIND && "sourcePath" in spec && projectPath) {
|
|
342
|
-
base._meta = { ...base._meta, sourcePath: spec.sourcePath, projectPath };
|
|
343
|
-
}
|
|
344
|
-
return base;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// src/tools/function/scanner.ts
|
|
348
|
-
var TOOL_TAG = "@tool";
|
|
349
|
-
var EFFECT_VALUES = ["none", "local_write", "external_write", "destructive"];
|
|
350
|
-
function scanForTools(options) {
|
|
351
|
-
const projectPath = path9__namespace.resolve(options.projectPath);
|
|
352
|
-
const tsconfigPath = options.tsconfigPath ?? path9__namespace.join(projectPath, "tsconfig.json");
|
|
353
|
-
const include = options.include ?? ["**/*.ts"];
|
|
354
|
-
const errors = [];
|
|
355
|
-
const warnings = [];
|
|
356
|
-
let config;
|
|
357
|
-
let configPathResolved = path9__namespace.resolve(projectPath, tsconfigPath);
|
|
358
|
-
if (!fs2__namespace.existsSync(configPathResolved)) {
|
|
359
|
-
configPathResolved = path9__namespace.join(projectPath, "tsconfig.json");
|
|
360
|
-
}
|
|
361
|
-
if (fs2__namespace.existsSync(configPathResolved)) {
|
|
362
|
-
const configFile = ts2__namespace.readConfigFile(configPathResolved, ts2__namespace.sys.readFile);
|
|
363
|
-
if (configFile.error) {
|
|
364
|
-
errors.push({ file: configPathResolved, message: String(configFile.error.messageText) });
|
|
365
|
-
return { specs: [], errors, warnings };
|
|
366
|
-
}
|
|
367
|
-
const parsed = ts2__namespace.parseJsonConfigFileContent(
|
|
368
|
-
configFile.config,
|
|
369
|
-
ts2__namespace.sys,
|
|
370
|
-
path9__namespace.dirname(configPathResolved)
|
|
371
|
-
);
|
|
372
|
-
if (parsed.errors.length) {
|
|
373
|
-
for (const e of parsed.errors) {
|
|
374
|
-
errors.push({ file: e.file?.fileName ?? "tsconfig", message: String(e.messageText) });
|
|
375
|
-
}
|
|
376
|
-
return { specs: [], errors, warnings };
|
|
377
|
-
}
|
|
378
|
-
config = parsed;
|
|
379
|
-
} else {
|
|
380
|
-
config = {
|
|
381
|
-
options: {
|
|
382
|
-
target: ts2__namespace.ScriptTarget.ES2022,
|
|
383
|
-
module: ts2__namespace.ModuleKind.ESNext,
|
|
384
|
-
moduleResolution: ts2__namespace.ModuleResolutionKind.NodeNext,
|
|
385
|
-
strict: true,
|
|
386
|
-
skipLibCheck: true,
|
|
387
|
-
noEmit: true
|
|
388
|
-
},
|
|
389
|
-
fileNames: resolveGlob(projectPath, include),
|
|
390
|
-
errors: []
|
|
391
|
-
};
|
|
392
|
-
}
|
|
393
|
-
const program = ts2__namespace.createProgram(config.fileNames, config.options);
|
|
394
|
-
const typeChecker = program.getTypeChecker();
|
|
395
|
-
const specs = [];
|
|
396
|
-
for (const sourceFile of program.getSourceFiles()) {
|
|
397
|
-
const fileName = sourceFile.fileName;
|
|
398
|
-
if (fileName.includes("node_modules") || fileName.endsWith(".d.ts")) continue;
|
|
399
|
-
if (!config.fileNames.some((f) => path9__namespace.resolve(f) === path9__namespace.resolve(fileName))) continue;
|
|
400
|
-
ts2__namespace.forEachChild(sourceFile, (node) => {
|
|
401
|
-
const decl = getExportedFunctionDeclaration(node);
|
|
402
|
-
if (!decl) return;
|
|
403
|
-
const func = decl.func;
|
|
404
|
-
const name = decl.name;
|
|
405
|
-
if (!name) return;
|
|
406
|
-
const host = getJSDocHost(func);
|
|
407
|
-
if (!hasToolTag(host)) return;
|
|
408
|
-
const jsDoc = getJSDocComments(host);
|
|
409
|
-
const description = getDescription(jsDoc);
|
|
410
|
-
if (!description) {
|
|
411
|
-
warnings.push({ file: fileName, message: `Tool ${name}: missing description, using humanized name` });
|
|
412
|
-
}
|
|
413
|
-
const sideEffect = getEffect(host);
|
|
414
|
-
const onWarn = (msg) => warnings.push({ file: fileName, message: `${name}: ${msg}` });
|
|
415
|
-
const { schema } = buildInputSchemaFromParams(func, typeChecker, onWarn);
|
|
416
|
-
const inputSchema = Object.keys(schema.properties ?? {}).length > 0 ? schema : { type: "object", properties: {} };
|
|
417
|
-
const outputSchema = buildOutputSchemaFromReturnType(func, typeChecker, onWarn);
|
|
418
|
-
const sourcePath = path9__namespace.relative(projectPath, fileName) || path9__namespace.basename(fileName);
|
|
419
|
-
const toolName = pathToToolName(sourcePath, name);
|
|
420
|
-
specs.push({
|
|
421
|
-
kind: FUNCTION_KIND,
|
|
422
|
-
name: toolName,
|
|
423
|
-
description: description || humanize(name),
|
|
424
|
-
inputSchema,
|
|
425
|
-
outputSchema,
|
|
426
|
-
_meta: { hitl: { sideEffect } },
|
|
427
|
-
sourcePath,
|
|
428
|
-
exportName: name
|
|
429
|
-
});
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
return { specs, errors, warnings };
|
|
433
|
-
}
|
|
434
|
-
function resolveGlob(projectPath, patterns) {
|
|
435
|
-
const result = [];
|
|
436
|
-
const seen = /* @__PURE__ */ new Set();
|
|
437
|
-
const add = (f) => {
|
|
438
|
-
const abs = path9__namespace.resolve(f);
|
|
439
|
-
if (f.endsWith(".ts") && !f.endsWith(".d.ts") && !seen.has(abs)) {
|
|
440
|
-
seen.add(abs);
|
|
441
|
-
result.push(abs);
|
|
442
|
-
}
|
|
443
|
-
};
|
|
444
|
-
for (const p of patterns) {
|
|
445
|
-
const full = path9__namespace.join(projectPath, p);
|
|
446
|
-
if (full.includes("*")) {
|
|
447
|
-
const baseDir = full.replace(/\*\*\/.*$/, "").replace(/\*.*$/, "").replace(/\/?$/, "") || ".";
|
|
448
|
-
const dir = path9__namespace.resolve(projectPath, baseDir);
|
|
449
|
-
if (fs2__namespace.existsSync(dir)) walk(dir, add);
|
|
450
|
-
} else {
|
|
451
|
-
const resolved = path9__namespace.resolve(projectPath, full);
|
|
452
|
-
if (fs2__namespace.existsSync(resolved)) {
|
|
453
|
-
if (fs2__namespace.statSync(resolved).isFile()) add(resolved);
|
|
454
|
-
else walk(resolved, add);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
if (result.length > 0) return result;
|
|
459
|
-
const srcDir = path9__namespace.join(projectPath, "src");
|
|
460
|
-
if (fs2__namespace.existsSync(srcDir)) return walkCollect(srcDir);
|
|
461
|
-
return [];
|
|
462
|
-
}
|
|
463
|
-
function walkCollect(dir) {
|
|
464
|
-
const out = [];
|
|
465
|
-
walk(dir, (fullPath) => {
|
|
466
|
-
if (fullPath.endsWith(".ts") && !fullPath.endsWith(".d.ts")) out.push(path9__namespace.resolve(fullPath));
|
|
467
|
-
});
|
|
468
|
-
return out;
|
|
469
|
-
}
|
|
470
|
-
var SKIP_DIRS = /* @__PURE__ */ new Set(["node_modules", "generated", "dist"]);
|
|
471
|
-
function walk(dir, visit) {
|
|
472
|
-
try {
|
|
473
|
-
const entries = fs2__namespace.readdirSync(dir, { withFileTypes: true });
|
|
474
|
-
for (const e of entries) {
|
|
475
|
-
const full = path9__namespace.join(dir, e.name);
|
|
476
|
-
if (e.isDirectory() && !SKIP_DIRS.has(e.name)) walk(full, visit);
|
|
477
|
-
else if (e.isFile()) visit(full);
|
|
478
|
-
}
|
|
479
|
-
} catch {
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
function getExportedFunctionDeclaration(node, _sourceFile) {
|
|
483
|
-
if (ts2__namespace.isFunctionDeclaration(node) && node.name) {
|
|
484
|
-
const exported = (ts2__namespace.getModifiers(node) ?? []).some((m) => m.kind === ts2__namespace.SyntaxKind.ExportKeyword);
|
|
485
|
-
if (exported) return { func: node, name: node.name.getText() };
|
|
486
|
-
return null;
|
|
487
|
-
}
|
|
488
|
-
if (ts2__namespace.isVariableStatement(node)) {
|
|
489
|
-
const exported = (ts2__namespace.getModifiers(node) ?? []).some((m) => m.kind === ts2__namespace.SyntaxKind.ExportKeyword);
|
|
490
|
-
if (!exported) return null;
|
|
491
|
-
for (const decl of node.declarationList.declarations) {
|
|
492
|
-
let init = decl.initializer;
|
|
493
|
-
while (init && (ts2__namespace.isParenthesizedExpression(init) || ts2__namespace.isAsExpression(init)))
|
|
494
|
-
init = init.expression;
|
|
495
|
-
if (init && ts2__namespace.isArrowFunction(init)) {
|
|
496
|
-
const name = decl.name.getText();
|
|
497
|
-
return { func: init, name };
|
|
498
|
-
}
|
|
499
|
-
if (init && ts2__namespace.isFunctionExpression(init)) {
|
|
500
|
-
const name = decl.name.getText();
|
|
501
|
-
return { func: init, name };
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
return null;
|
|
506
|
-
}
|
|
507
|
-
function getJSDocHost(node) {
|
|
508
|
-
const parent = node.parent;
|
|
509
|
-
if (ts2__namespace.isVariableDeclaration(parent)) {
|
|
510
|
-
const gp = parent.parent;
|
|
511
|
-
if (ts2__namespace.isVariableDeclarationList(gp) && gp.parent && ts2__namespace.isVariableStatement(gp.parent)) return gp.parent;
|
|
512
|
-
}
|
|
513
|
-
return node;
|
|
514
|
-
}
|
|
515
|
-
function getJSDocComments(host) {
|
|
516
|
-
const all = ts2__namespace.getJSDocCommentsAndTags(host);
|
|
517
|
-
return all.filter((t) => ts2__namespace.isJSDoc(t));
|
|
518
|
-
}
|
|
519
|
-
function hasToolTag(host) {
|
|
520
|
-
const tags = ts2__namespace.getJSDocTags(host);
|
|
521
|
-
for (const tag of tags) {
|
|
522
|
-
const name = tag.tagName?.getText() ?? "";
|
|
523
|
-
if (name === "tool") return true;
|
|
524
|
-
}
|
|
525
|
-
const all = ts2__namespace.getJSDocCommentsAndTags(host);
|
|
526
|
-
for (const t of all) {
|
|
527
|
-
if (ts2__namespace.isJSDoc(t)) {
|
|
528
|
-
const full = t.getFullText();
|
|
529
|
-
if (full.includes(TOOL_TAG)) return true;
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
return false;
|
|
533
|
-
}
|
|
534
|
-
function getDescription(jsDocs, fallbackName) {
|
|
535
|
-
for (const doc of jsDocs) {
|
|
536
|
-
const comment = doc.comment;
|
|
537
|
-
if (typeof comment === "string") {
|
|
538
|
-
const first = comment.split(/\n/)[0]?.trim() ?? "";
|
|
539
|
-
if (first && !first.startsWith("@")) return first;
|
|
540
|
-
}
|
|
541
|
-
if (Array.isArray(comment)) {
|
|
542
|
-
const first = comment[0];
|
|
543
|
-
if (first && typeof first === "object" && "text" in first) {
|
|
544
|
-
const t = first.text.trim();
|
|
545
|
-
if (t && !t.startsWith("@")) return t;
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
const full = doc.getFullText();
|
|
549
|
-
const match = full.match(/\*\s*@tool\s+(.+?)(?=\n|$|\*\/)/s);
|
|
550
|
-
if (match?.[1]) return match[1].trim();
|
|
551
|
-
}
|
|
552
|
-
return "";
|
|
553
|
-
}
|
|
554
|
-
function getEffect(host) {
|
|
555
|
-
const tags = ts2__namespace.getJSDocTags(host);
|
|
556
|
-
for (const tag of tags) {
|
|
557
|
-
const name = tag.tagName?.getText() ?? "";
|
|
558
|
-
if (name === "effect") {
|
|
559
|
-
const comment = tag.comment;
|
|
560
|
-
const v = (typeof comment === "string" ? comment : "").trim().toLowerCase();
|
|
561
|
-
if (EFFECT_VALUES.includes(v)) return v;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
const all = ts2__namespace.getJSDocCommentsAndTags(host);
|
|
565
|
-
for (const t of all) {
|
|
566
|
-
if (ts2__namespace.isJSDoc(t)) {
|
|
567
|
-
const full = t.getFullText();
|
|
568
|
-
const match = full.match(/\*\s*@effect\s+(\w+)/);
|
|
569
|
-
if (match && EFFECT_VALUES.includes(match[1])) return match[1];
|
|
570
|
-
}
|
|
571
|
-
}
|
|
572
|
-
return "none";
|
|
573
|
-
}
|
|
574
|
-
function humanize(name) {
|
|
575
|
-
return name.replace(/([A-Z])/g, " $1").replace(/^./, (s) => s.toUpperCase()).trim();
|
|
576
|
-
}
|
|
577
|
-
function scan(projectPath, options = {}) {
|
|
578
|
-
const root = path9__namespace.resolve(projectPath);
|
|
579
|
-
const result = scanForTools({
|
|
580
|
-
projectPath: root,
|
|
581
|
-
include: options.include ?? ["**/*.ts"],
|
|
582
|
-
tsconfigPath: options.tsconfigPath
|
|
583
|
-
});
|
|
584
|
-
const specs = result.specs.map((s) => mcpSpecToToolSpec(s, root));
|
|
585
|
-
return Promise.resolve({
|
|
586
|
-
specs,
|
|
587
|
-
errors: result.errors,
|
|
588
|
-
warnings: result.warnings
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
var DEFAULT_EXTENSIONS = [".js", ".mjs"];
|
|
592
|
-
async function resolveEntryPoint(dirPath, baseName, extensions = DEFAULT_EXTENSIONS) {
|
|
593
|
-
if (extensions.some((ext) => baseName.endsWith(ext))) {
|
|
594
|
-
const fullPath = path9.join(dirPath, baseName);
|
|
595
|
-
await fs3.stat(fullPath);
|
|
596
|
-
return fullPath;
|
|
597
|
-
}
|
|
598
|
-
for (const ext of extensions) {
|
|
599
|
-
const fullPath = path9.join(dirPath, `${baseName}${ext}`);
|
|
600
|
-
try {
|
|
601
|
-
await fs3.stat(fullPath);
|
|
602
|
-
return fullPath;
|
|
603
|
-
} catch {
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
throw new Error(
|
|
607
|
-
`Could not find entry point in ${dirPath}. Tried: ${extensions.map((e) => baseName + e).join(", ")}`
|
|
608
|
-
);
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
// src/tools/skill/SkillManifest.ts
|
|
612
|
-
var SkillManifestError = class extends Error {
|
|
613
|
-
constructor(path12, field, message) {
|
|
614
|
-
super(`SKILL.md error in ${path12}: ${message}`);
|
|
615
|
-
this.path = path12;
|
|
616
|
-
this.field = field;
|
|
617
|
-
this.name = "SkillManifestError";
|
|
618
|
-
}
|
|
619
|
-
};
|
|
620
|
-
var NAME_PATTERN = /^[a-z0-9-]+$/;
|
|
621
|
-
var NAME_MAX_LENGTH = 64;
|
|
622
|
-
var DESCRIPTION_MAX_LENGTH = 1024;
|
|
623
|
-
var COMPATIBILITY_MAX_LENGTH = 500;
|
|
624
|
-
var RESERVED_WORDS = ["anthropic", "claude"];
|
|
625
|
-
var XML_TAG_PATTERN = /<\/?[a-zA-Z][^>]*>/;
|
|
626
|
-
function validateFrontmatter(fm, filePath) {
|
|
627
|
-
if (!fm.name || typeof fm.name !== "string") {
|
|
628
|
-
throw new SkillManifestError(filePath, "name", "name is required");
|
|
629
|
-
}
|
|
630
|
-
if (fm.name.length > NAME_MAX_LENGTH) {
|
|
631
|
-
throw new SkillManifestError(
|
|
632
|
-
filePath,
|
|
633
|
-
"name",
|
|
634
|
-
`name must be at most ${NAME_MAX_LENGTH} characters (got ${fm.name.length})`
|
|
635
|
-
);
|
|
636
|
-
}
|
|
637
|
-
if (!NAME_PATTERN.test(fm.name)) {
|
|
638
|
-
throw new SkillManifestError(
|
|
639
|
-
filePath,
|
|
640
|
-
"name",
|
|
641
|
-
"name must contain only lowercase letters, numbers, and hyphens"
|
|
642
|
-
);
|
|
643
|
-
}
|
|
644
|
-
if (fm.name.startsWith("-") || fm.name.endsWith("-")) {
|
|
645
|
-
throw new SkillManifestError(
|
|
646
|
-
filePath,
|
|
647
|
-
"name",
|
|
648
|
-
"name must not start or end with a hyphen"
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
if (fm.name.includes("--")) {
|
|
652
|
-
throw new SkillManifestError(
|
|
653
|
-
filePath,
|
|
654
|
-
"name",
|
|
655
|
-
"name must not contain consecutive hyphens"
|
|
656
|
-
);
|
|
657
|
-
}
|
|
658
|
-
if (XML_TAG_PATTERN.test(fm.name)) {
|
|
659
|
-
throw new SkillManifestError(filePath, "name", "name cannot contain XML tags");
|
|
660
|
-
}
|
|
661
|
-
for (const reserved of RESERVED_WORDS) {
|
|
662
|
-
if (fm.name.includes(reserved)) {
|
|
663
|
-
throw new SkillManifestError(
|
|
664
|
-
filePath,
|
|
665
|
-
"name",
|
|
666
|
-
`name cannot contain reserved word "${reserved}"`
|
|
667
|
-
);
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
if (!fm.description || typeof fm.description !== "string") {
|
|
671
|
-
throw new SkillManifestError(
|
|
672
|
-
filePath,
|
|
673
|
-
"description",
|
|
674
|
-
"description is required and must be non-empty"
|
|
675
|
-
);
|
|
676
|
-
}
|
|
677
|
-
if (fm.description.length > DESCRIPTION_MAX_LENGTH) {
|
|
678
|
-
throw new SkillManifestError(
|
|
679
|
-
filePath,
|
|
680
|
-
"description",
|
|
681
|
-
`description must be at most ${DESCRIPTION_MAX_LENGTH} characters (got ${fm.description.length})`
|
|
682
|
-
);
|
|
683
|
-
}
|
|
684
|
-
if (XML_TAG_PATTERN.test(fm.description)) {
|
|
685
|
-
throw new SkillManifestError(
|
|
686
|
-
filePath,
|
|
687
|
-
"description",
|
|
688
|
-
"description cannot contain XML tags"
|
|
689
|
-
);
|
|
690
|
-
}
|
|
691
|
-
if (fm.compatibility != null && typeof fm.compatibility === "string") {
|
|
692
|
-
if (fm.compatibility.length > COMPATIBILITY_MAX_LENGTH) {
|
|
693
|
-
throw new SkillManifestError(
|
|
694
|
-
filePath,
|
|
695
|
-
"compatibility",
|
|
696
|
-
`compatibility must be at most ${COMPATIBILITY_MAX_LENGTH} characters (got ${fm.compatibility.length})`
|
|
697
|
-
);
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
// src/tools/skill/SkillMdParser.ts
|
|
703
|
-
var CODE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
704
|
-
".py",
|
|
705
|
-
".js",
|
|
706
|
-
".mjs",
|
|
707
|
-
".ts",
|
|
708
|
-
".sh",
|
|
709
|
-
".bash",
|
|
710
|
-
".rb",
|
|
711
|
-
".go"
|
|
712
|
-
]);
|
|
713
|
-
var INSTRUCTION_EXTENSIONS = /* @__PURE__ */ new Set([".md", ".markdown", ".txt"]);
|
|
714
|
-
var EXCLUDED_FILES = /* @__PURE__ */ new Set(["SKILL.md", "tool.json"]);
|
|
715
|
-
function parseSkillMd(content, filePath) {
|
|
716
|
-
const trimmed = content.trimStart();
|
|
717
|
-
if (!trimmed.startsWith("---")) {
|
|
718
|
-
throw new SkillManifestError(
|
|
719
|
-
filePath,
|
|
720
|
-
"frontmatter",
|
|
721
|
-
"SKILL.md must start with YAML frontmatter (---)"
|
|
722
|
-
);
|
|
723
|
-
}
|
|
724
|
-
const endIndex = trimmed.indexOf("\n---", 3);
|
|
725
|
-
if (endIndex === -1) {
|
|
726
|
-
throw new SkillManifestError(
|
|
727
|
-
filePath,
|
|
728
|
-
"frontmatter",
|
|
729
|
-
"SKILL.md frontmatter is not closed (missing closing ---)"
|
|
730
|
-
);
|
|
731
|
-
}
|
|
732
|
-
const yamlBlock = trimmed.slice(4, endIndex).trim();
|
|
733
|
-
const body = trimmed.slice(endIndex + 4).trim();
|
|
734
|
-
let raw;
|
|
735
|
-
try {
|
|
736
|
-
const parsed = yaml__default.default.load(yamlBlock);
|
|
737
|
-
if (parsed == null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
738
|
-
throw new SkillManifestError(
|
|
739
|
-
filePath,
|
|
740
|
-
"frontmatter",
|
|
741
|
-
"YAML frontmatter must be an object (key: value)"
|
|
742
|
-
);
|
|
743
|
-
}
|
|
744
|
-
raw = parsed;
|
|
745
|
-
} catch (err) {
|
|
746
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
747
|
-
throw new SkillManifestError(
|
|
748
|
-
filePath,
|
|
749
|
-
"frontmatter",
|
|
750
|
-
`Invalid YAML frontmatter: ${message}`
|
|
751
|
-
);
|
|
752
|
-
}
|
|
753
|
-
const name = stringField(raw, "name", filePath);
|
|
754
|
-
const description = stringField(raw, "description", filePath);
|
|
755
|
-
if (!name || !description) {
|
|
756
|
-
throw new SkillManifestError(
|
|
757
|
-
filePath,
|
|
758
|
-
"frontmatter",
|
|
759
|
-
!name ? "name is required" : "description is required"
|
|
760
|
-
);
|
|
761
|
-
}
|
|
762
|
-
const license = stringField(raw, "license");
|
|
763
|
-
const compatibility = stringField(raw, "compatibility");
|
|
764
|
-
const allowedTools = stringField(raw, "allowed-tools");
|
|
765
|
-
const metadata = normalizeMetadata(raw.metadata);
|
|
766
|
-
const frontmatter = {
|
|
767
|
-
name,
|
|
768
|
-
description,
|
|
769
|
-
...license && { license },
|
|
770
|
-
...compatibility && { compatibility },
|
|
771
|
-
...allowedTools && { allowedTools },
|
|
772
|
-
...metadata && Object.keys(metadata).length > 0 && { metadata }
|
|
773
|
-
};
|
|
774
|
-
validateFrontmatter(frontmatter, filePath);
|
|
775
|
-
return { frontmatter, instructions: body };
|
|
776
|
-
}
|
|
777
|
-
function stringField(raw, key, filePath) {
|
|
778
|
-
const v = raw[key];
|
|
779
|
-
if (v == null) return "";
|
|
780
|
-
if (typeof v === "string") return v;
|
|
781
|
-
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
782
|
-
if (Array.isArray(v)) {
|
|
783
|
-
return v.map((x) => typeof x === "string" ? x : String(x)).join("\n");
|
|
784
|
-
}
|
|
785
|
-
if (filePath) {
|
|
786
|
-
throw new SkillManifestError(
|
|
787
|
-
filePath,
|
|
788
|
-
"frontmatter",
|
|
789
|
-
`Frontmatter field "${key}" must be a string, number, boolean, or array`
|
|
790
|
-
);
|
|
791
|
-
}
|
|
792
|
-
return String(v);
|
|
793
|
-
}
|
|
794
|
-
function normalizeMetadata(val) {
|
|
795
|
-
if (val == null) return void 0;
|
|
796
|
-
if (typeof val === "object" && !Array.isArray(val)) {
|
|
797
|
-
const out = {};
|
|
798
|
-
for (const [k, v] of Object.entries(val)) {
|
|
799
|
-
if (typeof k === "string" && v !== void 0 && v !== null) {
|
|
800
|
-
out[k] = typeof v === "object" ? JSON.stringify(v) : String(v);
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
return Object.keys(out).length ? out : void 0;
|
|
804
|
-
}
|
|
805
|
-
if (typeof val === "string" || typeof val === "number" || typeof val === "boolean") {
|
|
806
|
-
return { value: String(val) };
|
|
807
|
-
}
|
|
808
|
-
return void 0;
|
|
809
|
-
}
|
|
810
|
-
async function scanSkillResources(dirPath) {
|
|
811
|
-
const resources = [];
|
|
812
|
-
await scanDir(dirPath, dirPath, resources);
|
|
813
|
-
return resources;
|
|
814
|
-
}
|
|
815
|
-
async function scanDir(basePath, currentPath, resources) {
|
|
816
|
-
let entries;
|
|
817
|
-
try {
|
|
818
|
-
entries = await fs3.readdir(currentPath, { withFileTypes: true });
|
|
819
|
-
} catch {
|
|
820
|
-
return;
|
|
821
|
-
}
|
|
822
|
-
for (const entry of entries) {
|
|
823
|
-
const fullPath = path9.join(currentPath, entry.name);
|
|
824
|
-
if (entry.isDirectory()) {
|
|
825
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules") {
|
|
826
|
-
continue;
|
|
827
|
-
}
|
|
828
|
-
await scanDir(basePath, fullPath, resources);
|
|
829
|
-
} else if (entry.isFile()) {
|
|
830
|
-
if (EXCLUDED_FILES.has(entry.name)) {
|
|
831
|
-
continue;
|
|
832
|
-
}
|
|
833
|
-
const ext = path9.extname(entry.name).toLowerCase();
|
|
834
|
-
const relPath = path9.relative(basePath, fullPath);
|
|
835
|
-
resources.push({
|
|
836
|
-
relativePath: relPath,
|
|
837
|
-
absolutePath: fullPath,
|
|
838
|
-
extension: ext,
|
|
839
|
-
type: inferResourceType(ext)
|
|
840
|
-
});
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
function inferResourceType(ext) {
|
|
845
|
-
if (CODE_EXTENSIONS.has(ext)) return "code";
|
|
846
|
-
if (INSTRUCTION_EXTENSIONS.has(ext)) return "instructions";
|
|
847
|
-
return "data";
|
|
848
|
-
}
|
|
849
|
-
async function loadSkillDefinition(dirPath) {
|
|
850
|
-
const skillMdPath = path9.join(dirPath, "SKILL.md");
|
|
851
|
-
let content;
|
|
852
|
-
try {
|
|
853
|
-
content = await fs3.readFile(skillMdPath, "utf-8");
|
|
854
|
-
} catch (err) {
|
|
855
|
-
throw new SkillManifestError(
|
|
856
|
-
skillMdPath,
|
|
857
|
-
"file",
|
|
858
|
-
`Cannot read SKILL.md: ${err.message}`
|
|
859
|
-
);
|
|
860
|
-
}
|
|
861
|
-
const { frontmatter, instructions } = parseSkillMd(content, skillMdPath);
|
|
862
|
-
const resources = await scanSkillResources(dirPath);
|
|
863
|
-
return {
|
|
864
|
-
frontmatter,
|
|
865
|
-
instructions,
|
|
866
|
-
resources,
|
|
867
|
-
dirPath,
|
|
868
|
-
skillMdPath
|
|
869
|
-
};
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// src/tools/skill/scanSkill.ts
|
|
873
|
-
var defaultInputSchema = { type: "object", properties: {}, additionalProperties: true };
|
|
874
|
-
async function scanForSkill(projectPath) {
|
|
875
|
-
const projectRoot = path9__namespace.resolve(projectPath);
|
|
876
|
-
const dirs = await findDirsContainingFile(projectRoot, "SKILL.md");
|
|
877
|
-
const skills = [];
|
|
878
|
-
const errors = [];
|
|
879
|
-
for (const dirPath of dirs) {
|
|
880
|
-
const relativePath = path9__namespace.relative(projectRoot, dirPath) || path9__namespace.basename(dirPath);
|
|
881
|
-
try {
|
|
882
|
-
const skillDef = await loadSkillDefinition(dirPath);
|
|
883
|
-
const name = pathToToolName(relativePath, skillDef.frontmatter.name);
|
|
884
|
-
skills.push({
|
|
885
|
-
kind: SKILL_KIND,
|
|
886
|
-
name,
|
|
887
|
-
description: skillDef.frontmatter.description,
|
|
888
|
-
inputSchema: defaultInputSchema,
|
|
889
|
-
_meta: { hitl: { sideEffect: "none" } },
|
|
890
|
-
sourcePath: relativePath.replace(/\\/g, "/")
|
|
891
|
-
});
|
|
892
|
-
} catch (err) {
|
|
893
|
-
errors.push({ dir: relativePath, message: err instanceof Error ? err.message : String(err) });
|
|
894
|
-
}
|
|
895
|
-
}
|
|
896
|
-
return { skills, errors };
|
|
897
|
-
}
|
|
898
|
-
async function scan2(projectPath, _options = {}) {
|
|
899
|
-
const root = path9__namespace.resolve(projectPath);
|
|
900
|
-
const result = await scanForSkill(root);
|
|
901
|
-
const specs = result.skills.map((s) => mcpSpecToToolSpec(s, root));
|
|
902
|
-
return {
|
|
903
|
-
specs,
|
|
904
|
-
errors: result.errors.map((e) => ({ file: e.dir, message: e.message }))
|
|
905
|
-
};
|
|
906
|
-
}
|
|
907
|
-
async function readWorkflowMeta(dirPath, workflowFileName = "workflow.json") {
|
|
908
|
-
const workflowPath = path9.join(dirPath, workflowFileName);
|
|
909
|
-
let raw;
|
|
910
|
-
try {
|
|
911
|
-
raw = await fs3.readFile(workflowPath, "utf-8");
|
|
912
|
-
} catch (err) {
|
|
913
|
-
throw new DiscoveryError(
|
|
914
|
-
dirPath,
|
|
915
|
-
"load",
|
|
916
|
-
`Failed to read workflow: ${workflowPath}`,
|
|
917
|
-
err
|
|
918
|
-
);
|
|
919
|
-
}
|
|
920
|
-
let workflowDef;
|
|
921
|
-
try {
|
|
922
|
-
workflowDef = JSON.parse(raw);
|
|
923
|
-
} catch (err) {
|
|
924
|
-
throw new DiscoveryError(
|
|
925
|
-
dirPath,
|
|
926
|
-
"load",
|
|
927
|
-
`Invalid JSON in ${workflowPath}`,
|
|
928
|
-
err
|
|
929
|
-
);
|
|
930
|
-
}
|
|
931
|
-
if (!workflowDef.nodes || !Array.isArray(workflowDef.nodes)) {
|
|
932
|
-
throw new DiscoveryError(
|
|
933
|
-
dirPath,
|
|
934
|
-
"validate",
|
|
935
|
-
`workflow.json must have a "nodes" array`
|
|
936
|
-
);
|
|
937
|
-
}
|
|
938
|
-
const meta = workflowDef.meta;
|
|
939
|
-
const name = workflowDef.name || meta?.name || path9.basename(dirPath);
|
|
940
|
-
const description = workflowDef.description || meta?.description || `n8n workflow: ${name}`;
|
|
941
|
-
let webhookUrl;
|
|
942
|
-
const nodes = workflowDef.nodes;
|
|
943
|
-
if (Array.isArray(nodes)) {
|
|
944
|
-
const webhookNode = nodes.find(
|
|
945
|
-
(n) => n.type === "n8n-nodes-base.webhook" || n.type?.includes("webhook")
|
|
946
|
-
);
|
|
947
|
-
if (webhookNode?.parameters && typeof webhookNode.parameters === "object") {
|
|
948
|
-
const params = webhookNode.parameters;
|
|
949
|
-
const pathVal = params.path ?? params.webhookPath;
|
|
950
|
-
if (typeof pathVal === "string" && pathVal.startsWith("http")) {
|
|
951
|
-
webhookUrl = pathVal;
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
return { name, description, webhookUrl, workflowDef };
|
|
956
|
-
}
|
|
957
|
-
async function loadN8nTool(dirPath, manifest) {
|
|
958
|
-
const { workflowDef } = await readWorkflowMeta(
|
|
959
|
-
dirPath,
|
|
960
|
-
manifest.entryPoint ?? "workflow.json"
|
|
961
|
-
);
|
|
962
|
-
return { manifest, dirPath, workflowDef };
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
// src/tools/n8n/scanN8n.ts
|
|
966
|
-
var defaultInputSchema2 = { type: "object", properties: {}, additionalProperties: true };
|
|
967
|
-
async function scanForN8n(projectPath) {
|
|
968
|
-
const projectRoot = path9__namespace.resolve(projectPath);
|
|
969
|
-
const dirs = await findDirsContainingFile(projectRoot, "workflow.json");
|
|
970
|
-
const n8n = [];
|
|
971
|
-
const errors = [];
|
|
972
|
-
for (const dirPath of dirs) {
|
|
973
|
-
const relativePath = path9__namespace.relative(projectRoot, dirPath) || path9__namespace.basename(dirPath);
|
|
974
|
-
try {
|
|
975
|
-
const { name: wfName, description: wfDesc, webhookUrl } = await readWorkflowMeta(dirPath);
|
|
976
|
-
const toolName = pathToToolName(relativePath, wfName);
|
|
977
|
-
n8n.push({
|
|
978
|
-
kind: N8N_KIND,
|
|
979
|
-
name: toolName,
|
|
980
|
-
description: wfDesc,
|
|
981
|
-
inputSchema: defaultInputSchema2,
|
|
982
|
-
_meta: { hitl: { sideEffect: "external_write" } },
|
|
983
|
-
sourcePath: relativePath.replace(/\\/g, "/"),
|
|
984
|
-
webhookUrl
|
|
985
|
-
});
|
|
986
|
-
} catch (err) {
|
|
987
|
-
errors.push({ dir: relativePath, message: err instanceof Error ? err.message : String(err) });
|
|
988
|
-
}
|
|
989
|
-
}
|
|
990
|
-
return { n8n, errors };
|
|
991
|
-
}
|
|
992
|
-
async function scan3(projectPath, _options = {}) {
|
|
993
|
-
const root = path9__namespace.resolve(projectPath);
|
|
994
|
-
const result = await scanForN8n(root);
|
|
995
|
-
const specs = result.n8n.map((s) => mcpSpecToToolSpec(s, root));
|
|
996
|
-
return {
|
|
997
|
-
specs,
|
|
998
|
-
errors: result.errors.map((e) => ({ file: e.dir, message: e.message }))
|
|
999
|
-
};
|
|
1000
|
-
}
|
|
1001
|
-
async function scan4(projectPath, options = {}) {
|
|
1002
|
-
const root = path9__namespace.resolve(projectPath);
|
|
1003
|
-
const namespace = options.namespace ?? "dir";
|
|
1004
|
-
const errors = [];
|
|
1005
|
-
const scanner = new DirectoryScanner({
|
|
1006
|
-
roots: [root],
|
|
1007
|
-
namespace,
|
|
1008
|
-
extensions: options.extensions,
|
|
1009
|
-
onError: (dir, err) => {
|
|
1010
|
-
errors.push({ file: dir, message: err.message });
|
|
1011
|
-
options.onError?.(dir, err);
|
|
1012
|
-
}
|
|
1013
|
-
});
|
|
1014
|
-
let specs;
|
|
1015
|
-
try {
|
|
1016
|
-
specs = await scanner.scan();
|
|
1017
|
-
} catch (err) {
|
|
1018
|
-
errors.push({
|
|
1019
|
-
file: root,
|
|
1020
|
-
message: err instanceof Error ? err.message : String(err)
|
|
1021
|
-
});
|
|
1022
|
-
return { specs: [], errors };
|
|
1023
|
-
}
|
|
1024
|
-
const filtered = specs.filter((s) => s.kind === chunkWFPHIYB6_cjs.MCP_KIND);
|
|
1025
|
-
return { specs: filtered, errors };
|
|
1026
|
-
}
|
|
1027
|
-
async function scan5(projectPath, options = {}) {
|
|
1028
|
-
const root = path9__namespace.resolve(projectPath);
|
|
1029
|
-
const namespace = options.namespace ?? "dir";
|
|
1030
|
-
const errors = [];
|
|
1031
|
-
const scanner = new DirectoryScanner({
|
|
1032
|
-
roots: [root],
|
|
1033
|
-
namespace,
|
|
1034
|
-
extensions: options.extensions,
|
|
1035
|
-
onError: (dir, err) => {
|
|
1036
|
-
errors.push({ file: dir, message: err.message });
|
|
1037
|
-
options.onError?.(dir, err);
|
|
1038
|
-
}
|
|
1039
|
-
});
|
|
1040
|
-
let specs;
|
|
1041
|
-
try {
|
|
1042
|
-
specs = await scanner.scan();
|
|
1043
|
-
} catch (err) {
|
|
1044
|
-
errors.push({
|
|
1045
|
-
file: root,
|
|
1046
|
-
message: err instanceof Error ? err.message : String(err)
|
|
1047
|
-
});
|
|
1048
|
-
return { specs: [], errors };
|
|
1049
|
-
}
|
|
1050
|
-
const filtered = specs.filter((s) => s.kind === chunkWFPHIYB6_cjs.LANGCHAIN_KIND);
|
|
1051
|
-
return { specs: filtered, errors };
|
|
1052
|
-
}
|
|
1053
|
-
async function loadLangChainTool(dirPath, manifest, extensions) {
|
|
1054
|
-
let entryFile;
|
|
1055
|
-
try {
|
|
1056
|
-
entryFile = await resolveEntryPoint(
|
|
1057
|
-
dirPath,
|
|
1058
|
-
manifest.entryPoint ?? "index",
|
|
1059
|
-
extensions
|
|
1060
|
-
);
|
|
1061
|
-
} catch (err) {
|
|
1062
|
-
throw new DiscoveryError(
|
|
1063
|
-
dirPath,
|
|
1064
|
-
"load",
|
|
1065
|
-
`Cannot find LangChain entry point`,
|
|
1066
|
-
err
|
|
1067
|
-
);
|
|
1068
|
-
}
|
|
1069
|
-
let mod;
|
|
1070
|
-
try {
|
|
1071
|
-
mod = await import(url.pathToFileURL(entryFile).href);
|
|
1072
|
-
} catch (err) {
|
|
1073
|
-
throw new DiscoveryError(
|
|
1074
|
-
dirPath,
|
|
1075
|
-
"load",
|
|
1076
|
-
`Failed to import ${entryFile}`,
|
|
1077
|
-
err
|
|
1078
|
-
);
|
|
1079
|
-
}
|
|
1080
|
-
const tool = mod.default ?? mod.tool ?? mod;
|
|
1081
|
-
if (!tool || typeof tool.invoke !== "function") {
|
|
1082
|
-
throw new DiscoveryError(
|
|
1083
|
-
dirPath,
|
|
1084
|
-
"validate",
|
|
1085
|
-
`Entry point must export an object with invoke() method (LangChainToolLike)`
|
|
1086
|
-
);
|
|
1087
|
-
}
|
|
1088
|
-
return { manifest, dirPath, impl: tool };
|
|
1089
|
-
}
|
|
1090
|
-
var DEFAULT_EXTENSIONS2 = [".js", ".mjs"];
|
|
1091
|
-
async function listSkillProgramFiles(dirPath, extensions = DEFAULT_EXTENSIONS2) {
|
|
1092
|
-
let entries;
|
|
1093
|
-
try {
|
|
1094
|
-
const dirEntries = await fs3.readdir(dirPath, { withFileTypes: true });
|
|
1095
|
-
entries = dirEntries.map((entry) => ({
|
|
1096
|
-
name: entry.name,
|
|
1097
|
-
isFile: entry.isFile()
|
|
1098
|
-
}));
|
|
1099
|
-
} catch {
|
|
1100
|
-
return [];
|
|
1101
|
-
}
|
|
1102
|
-
return entries.filter((e) => e.isFile).map((e) => e.name).filter((name) => {
|
|
1103
|
-
if (name.startsWith(".") || name.startsWith("_")) return false;
|
|
1104
|
-
if (name.includes(".test.") || name.includes(".spec.")) return false;
|
|
1105
|
-
return extensions.some((ext) => name.endsWith(ext));
|
|
1106
|
-
}).sort((a, b) => {
|
|
1107
|
-
if (a === "handler.js" || a === "index.js") return -1;
|
|
1108
|
-
if (b === "handler.js" || b === "index.js") return 1;
|
|
1109
|
-
return a.localeCompare(b);
|
|
1110
|
-
});
|
|
1111
|
-
}
|
|
1112
|
-
function isLangChainLikeTool(val) {
|
|
1113
|
-
return val != null && typeof val === "object" && "invoke" in val && typeof val.invoke === "function";
|
|
1114
|
-
}
|
|
1115
|
-
function isConstructable(val) {
|
|
1116
|
-
return typeof val === "function" && typeof val.prototype === "object";
|
|
1117
|
-
}
|
|
1118
|
-
async function loadOneSkillProgram(dirPath, manifest, entryFile, skillDef, programKey, extensions) {
|
|
1119
|
-
let impl;
|
|
1120
|
-
try {
|
|
1121
|
-
const fullPath = await resolveEntryPoint(dirPath, entryFile, extensions ?? [".js", ".mjs"]);
|
|
1122
|
-
const mod = await import(url.pathToFileURL(fullPath).href);
|
|
1123
|
-
const fn = mod.default ?? mod.handler ?? mod.Tool;
|
|
1124
|
-
if (isLangChainLikeTool(fn)) {
|
|
1125
|
-
impl = fn;
|
|
1126
|
-
} else if (isConstructable(fn)) {
|
|
1127
|
-
const instance = new fn();
|
|
1128
|
-
if (isLangChainLikeTool(instance)) impl = instance;
|
|
1129
|
-
} else if (typeof fn === "function") {
|
|
1130
|
-
impl = fn;
|
|
1131
|
-
}
|
|
1132
|
-
} catch {
|
|
1133
|
-
}
|
|
1134
|
-
return {
|
|
1135
|
-
manifest,
|
|
1136
|
-
dirPath,
|
|
1137
|
-
impl,
|
|
1138
|
-
skillDefinition: skillDef,
|
|
1139
|
-
programKey
|
|
1140
|
-
};
|
|
1141
|
-
}
|
|
1142
|
-
async function loadSkillTools(dirPath, manifest, extensions) {
|
|
1143
|
-
let skillDef;
|
|
1144
|
-
try {
|
|
1145
|
-
skillDef = await loadSkillDefinition(dirPath);
|
|
1146
|
-
} catch (err) {
|
|
1147
|
-
throw new DiscoveryError(
|
|
1148
|
-
dirPath,
|
|
1149
|
-
"load",
|
|
1150
|
-
`Failed to parse SKILL.md: ${err.message}`,
|
|
1151
|
-
err
|
|
1152
|
-
);
|
|
1153
|
-
}
|
|
1154
|
-
const programs = manifest.programs;
|
|
1155
|
-
if (programs && typeof programs === "object" && Object.keys(programs).length > 0) {
|
|
1156
|
-
const result = [];
|
|
1157
|
-
for (const [programKey, entryFile2] of Object.entries(programs)) {
|
|
1158
|
-
const loaded2 = await loadOneSkillProgram(
|
|
1159
|
-
dirPath,
|
|
1160
|
-
manifest,
|
|
1161
|
-
entryFile2,
|
|
1162
|
-
skillDef,
|
|
1163
|
-
programKey,
|
|
1164
|
-
extensions
|
|
1165
|
-
);
|
|
1166
|
-
result.push(loaded2);
|
|
1167
|
-
}
|
|
1168
|
-
return result;
|
|
1169
|
-
}
|
|
1170
|
-
const exts = extensions ?? DEFAULT_EXTENSIONS2;
|
|
1171
|
-
const files = await listSkillProgramFiles(dirPath, exts);
|
|
1172
|
-
if (files.length >= 2) {
|
|
1173
|
-
const result = [];
|
|
1174
|
-
for (let i = 0; i < files.length; i++) {
|
|
1175
|
-
const file = files[i];
|
|
1176
|
-
const programKey = i === 0 ? "default" : file.replace(/\.[^.]+$/, "");
|
|
1177
|
-
const loaded2 = await loadOneSkillProgram(
|
|
1178
|
-
dirPath,
|
|
1179
|
-
manifest,
|
|
1180
|
-
file,
|
|
1181
|
-
skillDef,
|
|
1182
|
-
programKey,
|
|
1183
|
-
extensions
|
|
1184
|
-
);
|
|
1185
|
-
result.push(loaded2);
|
|
1186
|
-
}
|
|
1187
|
-
return result;
|
|
1188
|
-
}
|
|
1189
|
-
const entryFile = manifest.entryPoint ?? files[0] ?? "handler";
|
|
1190
|
-
const loaded = await loadOneSkillProgram(
|
|
1191
|
-
dirPath,
|
|
1192
|
-
manifest,
|
|
1193
|
-
entryFile,
|
|
1194
|
-
skillDef,
|
|
1195
|
-
void 0,
|
|
1196
|
-
extensions
|
|
1197
|
-
);
|
|
1198
|
-
return [loaded];
|
|
1199
|
-
}
|
|
1200
|
-
async function listLangchainEntryFiles(dirPath, extensions) {
|
|
1201
|
-
let entries;
|
|
1202
|
-
try {
|
|
1203
|
-
const dirEntries = await fs3.readdir(dirPath, { withFileTypes: true });
|
|
1204
|
-
entries = dirEntries.map((entry) => ({
|
|
1205
|
-
name: entry.name,
|
|
1206
|
-
isFile: entry.isFile()
|
|
1207
|
-
}));
|
|
1208
|
-
} catch {
|
|
1209
|
-
return [];
|
|
1210
|
-
}
|
|
1211
|
-
return entries.filter((e) => e.isFile).map((e) => e.name).filter((name) => {
|
|
1212
|
-
if (name.startsWith(".") || name.startsWith("_")) return false;
|
|
1213
|
-
if (name.endsWith(".d.ts")) return false;
|
|
1214
|
-
if (name.includes(".test.") || name.includes(".spec.")) return false;
|
|
1215
|
-
return extensions.some((ext) => name.endsWith(ext));
|
|
1216
|
-
});
|
|
1217
|
-
}
|
|
1218
|
-
async function loadLangChainToolsFromDir(dirPath, dirName, manifest, strict, namespace, extensions, langchainDirName, toSpec, onError) {
|
|
1219
|
-
const entryFiles = await listLangchainEntryFiles(dirPath, extensions);
|
|
1220
|
-
if (entryFiles.length === 0) {
|
|
1221
|
-
if (strict) {
|
|
1222
|
-
throw new DiscoveryError(dirPath, "load", "No LangChain entry files found");
|
|
1223
|
-
}
|
|
1224
|
-
return [];
|
|
1225
|
-
}
|
|
1226
|
-
const specs = [];
|
|
1227
|
-
const useDirNameForSingle = dirName !== langchainDirName;
|
|
1228
|
-
for (const entryFile of entryFiles) {
|
|
1229
|
-
const fileManifest = { ...manifest, entryPoint: entryFile };
|
|
1230
|
-
try {
|
|
1231
|
-
const loaded = await loadLangChainTool(dirPath, fileManifest, extensions);
|
|
1232
|
-
const fileBase = path9.basename(entryFile).replace(/\.[^.]+$/, "");
|
|
1233
|
-
const nameHint = entryFiles.length === 1 && useDirNameForSingle ? dirName : fileBase;
|
|
1234
|
-
specs.push(toSpec(loaded, nameHint, dirPath, namespace));
|
|
1235
|
-
} catch (error) {
|
|
1236
|
-
const err = error;
|
|
1237
|
-
if (err instanceof DiscoveryError && err.phase === "validate") {
|
|
1238
|
-
if (strict) throw err;
|
|
1239
|
-
continue;
|
|
1240
|
-
}
|
|
1241
|
-
onError?.(path9.join(dirPath, entryFile), err);
|
|
1242
|
-
if (strict) throw err;
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
return specs;
|
|
1246
|
-
}
|
|
1247
|
-
function isCursorFormat(obj) {
|
|
1248
|
-
return typeof obj === "object" && obj !== null && "mcpServers" in obj && typeof obj.mcpServers === "object" && obj.mcpServers !== null;
|
|
1249
|
-
}
|
|
1250
|
-
function extractMCPConfig(parsed, toolName) {
|
|
1251
|
-
if (isCursorFormat(parsed)) {
|
|
1252
|
-
const servers = parsed.mcpServers;
|
|
1253
|
-
const keys = Object.keys(servers);
|
|
1254
|
-
if (keys.length === 0) {
|
|
1255
|
-
return {};
|
|
1256
|
-
}
|
|
1257
|
-
const name = toolName && keys.includes(toolName) ? toolName : keys[0];
|
|
1258
|
-
return servers[name];
|
|
1259
|
-
}
|
|
1260
|
-
return parsed;
|
|
1261
|
-
}
|
|
1262
|
-
async function loadMCPTool(dirPath, manifest) {
|
|
1263
|
-
const mcpPath = path9.join(dirPath, manifest.entryPoint ?? "mcp.json");
|
|
1264
|
-
let raw;
|
|
1265
|
-
try {
|
|
1266
|
-
raw = await fs3.readFile(mcpPath, "utf-8");
|
|
1267
|
-
} catch (err) {
|
|
1268
|
-
throw new DiscoveryError(
|
|
1269
|
-
dirPath,
|
|
1270
|
-
"load",
|
|
1271
|
-
`Failed to read MCP config: ${mcpPath}`,
|
|
1272
|
-
err
|
|
1273
|
-
);
|
|
1274
|
-
}
|
|
1275
|
-
let parsed;
|
|
1276
|
-
try {
|
|
1277
|
-
parsed = JSON.parse(raw);
|
|
1278
|
-
} catch (err) {
|
|
1279
|
-
throw new DiscoveryError(
|
|
1280
|
-
dirPath,
|
|
1281
|
-
"load",
|
|
1282
|
-
`Invalid JSON in ${mcpPath}`,
|
|
1283
|
-
err
|
|
1284
|
-
);
|
|
1285
|
-
}
|
|
1286
|
-
const baseName = manifest.name?.split("/").pop();
|
|
1287
|
-
const config = extractMCPConfig(parsed, baseName);
|
|
1288
|
-
if (!config.command && !config.url) {
|
|
1289
|
-
throw new DiscoveryError(
|
|
1290
|
-
dirPath,
|
|
1291
|
-
"validate",
|
|
1292
|
-
`mcp.json must have either "command" or "url" field`
|
|
1293
|
-
);
|
|
1294
|
-
}
|
|
1295
|
-
return { manifest, dirPath, mcpConfig: config };
|
|
1296
|
-
}
|
|
1297
|
-
|
|
1298
|
-
// src/tools/mcp/directoryApply.ts
|
|
1299
|
-
function applyLoadedToSpec(spec, loaded, _manifest, _defaultDirName, _namespace) {
|
|
1300
|
-
if (loaded.mcpConfig?.url) spec.endpoint = loaded.mcpConfig.url;
|
|
1301
|
-
spec.impl = loaded.mcpConfig;
|
|
1302
|
-
}
|
|
1303
|
-
var directoryMarker = {
|
|
1304
|
-
kind: "mcp",
|
|
1305
|
-
markerFile: "mcp.json",
|
|
1306
|
-
defaultEntryPoint: "mcp.json"
|
|
1307
|
-
};
|
|
1308
|
-
|
|
1309
|
-
// src/tools/langchain/directoryApply.ts
|
|
1310
|
-
function applyLoadedToSpec2(spec, loaded, manifest, _defaultDirName, namespace) {
|
|
1311
|
-
spec.impl = loaded.impl;
|
|
1312
|
-
if (!manifest.name && loaded.impl) {
|
|
1313
|
-
const toolName = loaded.impl.name;
|
|
1314
|
-
if (toolName) spec.name = `${namespace}/${toolName}`;
|
|
1315
|
-
}
|
|
1316
|
-
if (!manifest.description && loaded.impl) {
|
|
1317
|
-
const d = loaded.impl.description;
|
|
1318
|
-
if (d) spec.description = d;
|
|
1319
|
-
}
|
|
1320
|
-
if (!manifest.inputSchema && loaded.impl) {
|
|
1321
|
-
const schema = loaded.impl.schema;
|
|
1322
|
-
if (schema) spec.inputSchema = schema;
|
|
1323
|
-
}
|
|
1324
|
-
}
|
|
1325
|
-
|
|
1326
|
-
// src/tools/skill/directoryApply.ts
|
|
1327
|
-
function applyLoadedToSpec3(spec, loaded, manifest, defaultDirName, namespace) {
|
|
1328
|
-
const skillDef = loaded.skillDefinition;
|
|
1329
|
-
if (skillDef) {
|
|
1330
|
-
spec.name = manifest.name ?? skillDef.frontmatter.name;
|
|
1331
|
-
spec.description = skillDef.frontmatter.description;
|
|
1332
|
-
if (loaded.programKey === "default") {
|
|
1333
|
-
spec.name = `${namespace}/${defaultDirName}`;
|
|
1334
|
-
} else if (loaded.programKey) {
|
|
1335
|
-
spec.name = `${namespace}/${defaultDirName}/${loaded.programKey}`;
|
|
1336
|
-
}
|
|
1337
|
-
const impl = loaded.impl;
|
|
1338
|
-
if (impl && typeof impl === "object" && typeof impl.invoke === "function") {
|
|
1339
|
-
if (impl.description != null && impl.description !== "")
|
|
1340
|
-
spec.description = impl.description;
|
|
1341
|
-
if (impl.schema != null && typeof impl.schema === "object")
|
|
1342
|
-
spec.inputSchema = impl.schema;
|
|
1343
|
-
}
|
|
1344
|
-
spec.impl = { ...skillDef, handler: loaded.impl };
|
|
1345
|
-
} else {
|
|
1346
|
-
spec.impl = loaded.impl;
|
|
1347
|
-
}
|
|
1348
|
-
}
|
|
1349
|
-
var directoryMarker2 = {
|
|
1350
|
-
kind: "skill",
|
|
1351
|
-
markerFile: "SKILL.md",
|
|
1352
|
-
defaultEntryPoint: "handler"
|
|
1353
|
-
};
|
|
1354
|
-
|
|
1355
|
-
// src/tools/n8n/directoryApply.ts
|
|
1356
|
-
function applyLoadedToSpec4(spec, loaded, manifest, _defaultDirName, _namespace) {
|
|
1357
|
-
const workflow = loaded.workflowDef;
|
|
1358
|
-
if (workflow?.id) spec.resourceId = String(workflow.id);
|
|
1359
|
-
if (!manifest.description && workflow) {
|
|
1360
|
-
const d = workflow.description ?? workflow.meta?.description ?? (typeof workflow.name === "string" ? workflow.name : void 0);
|
|
1361
|
-
if (d) spec.description = d;
|
|
1362
|
-
}
|
|
1363
|
-
spec.impl = loaded.workflowDef;
|
|
1364
|
-
}
|
|
1365
|
-
var directoryMarker3 = {
|
|
1366
|
-
kind: "n8n",
|
|
1367
|
-
markerFile: "workflow.json",
|
|
1368
|
-
defaultEntryPoint: "workflow.json"
|
|
1369
|
-
};
|
|
1370
|
-
|
|
1371
|
-
// src/tools/discoveryFactory.ts
|
|
1372
|
-
var DiscoveryError = class extends Error {
|
|
1373
|
-
toolDir;
|
|
1374
|
-
phase;
|
|
1375
|
-
cause;
|
|
1376
|
-
constructor(toolDir, phase, message, cause) {
|
|
1377
|
-
super(`[${phase}] ${toolDir}: ${message}`);
|
|
1378
|
-
this.name = "DiscoveryError";
|
|
1379
|
-
this.toolDir = toolDir;
|
|
1380
|
-
this.phase = phase;
|
|
1381
|
-
this.cause = cause;
|
|
1382
|
-
}
|
|
1383
|
-
};
|
|
1384
|
-
var DIRECTORY_KINDS = ["mcp", "langchain", "skill", "n8n"];
|
|
1385
|
-
var DIRECTORY_DISCOVERABLE_KINDS = DIRECTORY_KINDS;
|
|
1386
|
-
var DIRECTORY_KIND_MARKERS = [
|
|
1387
|
-
directoryMarker2,
|
|
1388
|
-
directoryMarker3,
|
|
1389
|
-
directoryMarker
|
|
1390
|
-
];
|
|
1391
|
-
var DIRECTORY_LOADERS = {
|
|
1392
|
-
mcp: async (dirPath, manifest) => [await loadMCPTool(dirPath, manifest)],
|
|
1393
|
-
langchain: async (dirPath, manifest, ext) => [await loadLangChainTool(dirPath, manifest, ext)],
|
|
1394
|
-
skill: (dirPath, manifest, ext) => loadSkillTools(dirPath, manifest, ext),
|
|
1395
|
-
n8n: async (dirPath, manifest) => [await loadN8nTool(dirPath, manifest)]
|
|
1396
|
-
};
|
|
1397
|
-
function getDirectoryLoader(kind) {
|
|
1398
|
-
const loader = DIRECTORY_LOADERS[kind];
|
|
1399
|
-
if (!loader) {
|
|
1400
|
-
throw new DiscoveryError("", "manifest", `Unknown directory tool kind: "${kind}"`);
|
|
1401
|
-
}
|
|
1402
|
-
return loader;
|
|
1403
|
-
}
|
|
1404
|
-
function applyDirectoryLoadedToSpec(spec, loaded, manifest, defaultDirName, namespace) {
|
|
1405
|
-
switch (manifest.kind) {
|
|
1406
|
-
case "mcp":
|
|
1407
|
-
return applyLoadedToSpec(spec, loaded);
|
|
1408
|
-
case "langchain":
|
|
1409
|
-
return applyLoadedToSpec2(spec, loaded, manifest, defaultDirName, namespace);
|
|
1410
|
-
case "skill":
|
|
1411
|
-
return applyLoadedToSpec3(spec, loaded, manifest, defaultDirName, namespace);
|
|
1412
|
-
case "n8n":
|
|
1413
|
-
return applyLoadedToSpec4(spec, loaded, manifest);
|
|
1414
|
-
}
|
|
1415
|
-
}
|
|
1416
|
-
async function discoverTools(type, projectPath, options = {}) {
|
|
1417
|
-
const root = path9__namespace.resolve(projectPath);
|
|
1418
|
-
switch (type) {
|
|
1419
|
-
case "function":
|
|
1420
|
-
return scan(root, options);
|
|
1421
|
-
case "skill":
|
|
1422
|
-
return scan2(root, options);
|
|
1423
|
-
case "n8n":
|
|
1424
|
-
return scan3(root, options);
|
|
1425
|
-
case "mcp":
|
|
1426
|
-
return scan4(root, options);
|
|
1427
|
-
case "langchain":
|
|
1428
|
-
return scan5(root, options);
|
|
1429
|
-
default: {
|
|
1430
|
-
const _ = type;
|
|
1431
|
-
return _;
|
|
1432
|
-
}
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
var DEFAULT_EXTENSIONS3 = [".js", ".mjs"];
|
|
1436
|
-
var DirectoryScanner = class {
|
|
1437
|
-
roots;
|
|
1438
|
-
extensions;
|
|
1439
|
-
onError;
|
|
1440
|
-
constructor(options) {
|
|
1441
|
-
const defaultNamespace = options.namespace ?? "dir";
|
|
1442
|
-
this.roots = options.roots.map((root) => {
|
|
1443
|
-
if (typeof root === "string") {
|
|
1444
|
-
return { path: root, namespace: defaultNamespace };
|
|
1445
|
-
}
|
|
1446
|
-
return {
|
|
1447
|
-
path: root.path,
|
|
1448
|
-
namespace: root.namespace ?? defaultNamespace
|
|
1449
|
-
};
|
|
1450
|
-
});
|
|
1451
|
-
this.extensions = options.extensions ?? DEFAULT_EXTENSIONS3;
|
|
1452
|
-
this.onError = options.onError;
|
|
1453
|
-
}
|
|
1454
|
-
async scan() {
|
|
1455
|
-
const specs = [];
|
|
1456
|
-
for (const root of this.roots) {
|
|
1457
|
-
const rootSpecs = await this.scanRoot(root.path, root.namespace);
|
|
1458
|
-
specs.push(...rootSpecs);
|
|
1459
|
-
}
|
|
1460
|
-
return specs;
|
|
1461
|
-
}
|
|
1462
|
-
async scanRoot(rootPath, namespace) {
|
|
1463
|
-
return this.scanRecursive(rootPath, namespace);
|
|
1464
|
-
}
|
|
1465
|
-
async scanRecursive(dirPath, namespace) {
|
|
1466
|
-
const specs = [];
|
|
1467
|
-
let dirEntries;
|
|
1468
|
-
try {
|
|
1469
|
-
const entries = await fs3.readdir(dirPath, { withFileTypes: true });
|
|
1470
|
-
dirEntries = entries.map((entry) => ({
|
|
1471
|
-
name: entry.name,
|
|
1472
|
-
isDirectory: entry.isDirectory()
|
|
1473
|
-
}));
|
|
1474
|
-
} catch (error) {
|
|
1475
|
-
this.onError?.(dirPath, error);
|
|
1476
|
-
return specs;
|
|
1477
|
-
}
|
|
1478
|
-
const dirName = path9.basename(dirPath);
|
|
1479
|
-
try {
|
|
1480
|
-
const loadedSpecs = await this.loadToolDir(dirPath, dirName, namespace);
|
|
1481
|
-
if (loadedSpecs.length > 0) specs.push(...loadedSpecs);
|
|
1482
|
-
} catch (error) {
|
|
1483
|
-
this.onError?.(dirPath, error);
|
|
1484
|
-
}
|
|
1485
|
-
for (const entry of dirEntries) {
|
|
1486
|
-
if (!entry.isDirectory) continue;
|
|
1487
|
-
const childPath = path9.join(dirPath, entry.name);
|
|
1488
|
-
try {
|
|
1489
|
-
const childSpecs = await this.scanRecursive(childPath, namespace);
|
|
1490
|
-
specs.push(...childSpecs);
|
|
1491
|
-
} catch (error) {
|
|
1492
|
-
this.onError?.(childPath, error);
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
return specs;
|
|
1496
|
-
}
|
|
1497
|
-
async loadToolDir(dirPath, dirName, namespace) {
|
|
1498
|
-
const manifestPath = path9.join(dirPath, "tool.json");
|
|
1499
|
-
let manifestRaw;
|
|
1500
|
-
try {
|
|
1501
|
-
manifestRaw = await fs3.readFile(manifestPath, "utf-8");
|
|
1502
|
-
} catch {
|
|
1503
|
-
const inferred = await this.inferManifest(dirPath, dirName);
|
|
1504
|
-
if (!inferred) return [];
|
|
1505
|
-
if (inferred.kind === "langchain") {
|
|
1506
|
-
if (inferred.entryPoint) {
|
|
1507
|
-
const loaded3 = await loadLangChainTool(dirPath, inferred, this.extensions);
|
|
1508
|
-
return [this.toToolSpec(loaded3, dirName, dirPath, namespace)];
|
|
1509
|
-
}
|
|
1510
|
-
return loadLangChainToolsFromDir(
|
|
1511
|
-
dirPath,
|
|
1512
|
-
dirName,
|
|
1513
|
-
inferred,
|
|
1514
|
-
false,
|
|
1515
|
-
namespace,
|
|
1516
|
-
this.extensions,
|
|
1517
|
-
chunkWFPHIYB6_cjs.LANGCHAIN_DIR_NAME,
|
|
1518
|
-
(loaded3, nameHint, dp, ns) => this.toToolSpec(loaded3, nameHint, dp, ns),
|
|
1519
|
-
this.onError
|
|
1520
|
-
);
|
|
1521
|
-
}
|
|
1522
|
-
if (inferred.kind === "skill") {
|
|
1523
|
-
const loadedList = await loadSkillTools(dirPath, inferred, this.extensions);
|
|
1524
|
-
return loadedList.map(
|
|
1525
|
-
(loaded3) => this.toToolSpec(loaded3, dirName, dirPath, namespace)
|
|
1526
|
-
);
|
|
1527
|
-
}
|
|
1528
|
-
const loaded2 = await this.loadByKind(dirPath, inferred);
|
|
1529
|
-
return [this.toToolSpec(loaded2, dirName, dirPath, namespace)];
|
|
1530
|
-
}
|
|
1531
|
-
let manifest;
|
|
1532
|
-
try {
|
|
1533
|
-
manifest = JSON.parse(manifestRaw);
|
|
1534
|
-
} catch (err) {
|
|
1535
|
-
throw new DiscoveryError(dirPath, "manifest", "Invalid JSON in tool.json", err);
|
|
1536
|
-
}
|
|
1537
|
-
if (!manifest.kind) {
|
|
1538
|
-
throw new DiscoveryError(dirPath, "manifest", `tool.json must have a "kind" field`);
|
|
1539
|
-
}
|
|
1540
|
-
if (manifest.enabled === false) return [];
|
|
1541
|
-
if (manifest.kind === "langchain") {
|
|
1542
|
-
if (manifest.entryPoint) {
|
|
1543
|
-
const loaded2 = await loadLangChainTool(dirPath, manifest, this.extensions);
|
|
1544
|
-
return [this.toToolSpec(loaded2, dirName, dirPath, namespace)];
|
|
1545
|
-
}
|
|
1546
|
-
return loadLangChainToolsFromDir(
|
|
1547
|
-
dirPath,
|
|
1548
|
-
dirName,
|
|
1549
|
-
manifest,
|
|
1550
|
-
true,
|
|
1551
|
-
namespace,
|
|
1552
|
-
this.extensions,
|
|
1553
|
-
chunkWFPHIYB6_cjs.LANGCHAIN_DIR_NAME,
|
|
1554
|
-
(loaded2, nameHint, dp, ns) => this.toToolSpec(loaded2, nameHint, dp, ns),
|
|
1555
|
-
this.onError
|
|
1556
|
-
);
|
|
1557
|
-
}
|
|
1558
|
-
if (manifest.kind === "skill") {
|
|
1559
|
-
const loadedList = await loadSkillTools(dirPath, manifest, this.extensions);
|
|
1560
|
-
return loadedList.map(
|
|
1561
|
-
(loaded2) => this.toToolSpec(loaded2, dirName, dirPath, namespace)
|
|
1562
|
-
);
|
|
1563
|
-
}
|
|
1564
|
-
const loaded = await this.loadByKind(dirPath, manifest);
|
|
1565
|
-
return [this.toToolSpec(loaded, dirName, dirPath, namespace)];
|
|
1566
|
-
}
|
|
1567
|
-
async inferManifest(dirPath, dirName) {
|
|
1568
|
-
const kinds = [];
|
|
1569
|
-
for (const m of DIRECTORY_KIND_MARKERS) {
|
|
1570
|
-
if (await this.fileExists(path9.join(dirPath, m.markerFile))) kinds.push(m.kind);
|
|
1571
|
-
}
|
|
1572
|
-
const isLangchainDir = dirName === chunkWFPHIYB6_cjs.LANGCHAIN_DIR_NAME;
|
|
1573
|
-
const hasLangchain = isLangchainDir ? (await listLangchainEntryFiles(dirPath, this.extensions)).length > 0 : dirName !== SKILL_DIR_NAME && await this.hasEntryPoint(dirPath, "index");
|
|
1574
|
-
if (hasLangchain) kinds.push("langchain");
|
|
1575
|
-
if (kinds.length === 0) return null;
|
|
1576
|
-
if (kinds.length > 1) {
|
|
1577
|
-
throw new DiscoveryError(
|
|
1578
|
-
dirPath,
|
|
1579
|
-
"manifest",
|
|
1580
|
-
`Ambiguous tool kind (found ${kinds.join(", ")}). Add tool.json to disambiguate.`
|
|
1581
|
-
);
|
|
1582
|
-
}
|
|
1583
|
-
const kind = kinds[0];
|
|
1584
|
-
const manifest = { kind };
|
|
1585
|
-
const marker = DIRECTORY_KIND_MARKERS.find((m) => m.kind === kind);
|
|
1586
|
-
if (marker) {
|
|
1587
|
-
manifest.entryPoint = marker.defaultEntryPoint;
|
|
1588
|
-
}
|
|
1589
|
-
if (kind === "langchain" && !isLangchainDir) manifest.entryPoint = "index";
|
|
1590
|
-
return manifest;
|
|
1591
|
-
}
|
|
1592
|
-
async fileExists(path12) {
|
|
1593
|
-
try {
|
|
1594
|
-
await fs3.access(path12);
|
|
1595
|
-
return true;
|
|
1596
|
-
} catch {
|
|
1597
|
-
return false;
|
|
1598
|
-
}
|
|
1599
|
-
}
|
|
1600
|
-
async hasEntryPoint(dirPath, baseName) {
|
|
1601
|
-
try {
|
|
1602
|
-
await resolveEntryPoint(dirPath, baseName, this.extensions);
|
|
1603
|
-
return true;
|
|
1604
|
-
} catch {
|
|
1605
|
-
return false;
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
async loadByKind(dirPath, manifest) {
|
|
1609
|
-
const kind = manifest.kind;
|
|
1610
|
-
const loader = getDirectoryLoader(kind);
|
|
1611
|
-
const result = await loader(dirPath, manifest, this.extensions);
|
|
1612
|
-
const list = Array.isArray(result) ? result : [result];
|
|
1613
|
-
if (list.length === 0) {
|
|
1614
|
-
throw new DiscoveryError(dirPath, "load", "No tools loaded", new Error("empty"));
|
|
1615
|
-
}
|
|
1616
|
-
return list[0];
|
|
1617
|
-
}
|
|
1618
|
-
toToolSpec(loaded, dirName, dirPath, namespace) {
|
|
1619
|
-
const { manifest } = loaded;
|
|
1620
|
-
const kindDirNames = new Set(DIRECTORY_DISCOVERABLE_KINDS);
|
|
1621
|
-
const parentName = path9.basename(path9.join(dirPath, ".."));
|
|
1622
|
-
const isKindDir = kindDirNames.has(dirName);
|
|
1623
|
-
const defaultDirName = isKindDir ? parentName : dirName;
|
|
1624
|
-
const inferredName = isKindDir ? `${namespace}/${defaultDirName}-${dirName}` : `${namespace}/${defaultDirName}`;
|
|
1625
|
-
const name = manifest.name ?? inferredName;
|
|
1626
|
-
const spec = this.buildBaseSpec(manifest, name, dirName);
|
|
1627
|
-
applyDirectoryLoadedToSpec(spec, loaded, manifest, defaultDirName, namespace);
|
|
1628
|
-
return spec;
|
|
1629
|
-
}
|
|
1630
|
-
buildBaseSpec(manifest, name, dirName) {
|
|
1631
|
-
return {
|
|
1632
|
-
name,
|
|
1633
|
-
version: manifest.version ?? "1.0.0",
|
|
1634
|
-
kind: manifest.kind,
|
|
1635
|
-
description: manifest.description ?? `${manifest.kind} tool: ${dirName}`,
|
|
1636
|
-
tags: manifest.tags,
|
|
1637
|
-
inputSchema: manifest.inputSchema ?? { type: "object", additionalProperties: true },
|
|
1638
|
-
outputSchema: manifest.outputSchema ?? { type: "object", additionalProperties: true },
|
|
1639
|
-
capabilities: manifest.capabilities ?? [],
|
|
1640
|
-
costHints: manifest.costHints
|
|
1641
|
-
};
|
|
1642
|
-
}
|
|
1643
|
-
};
|
|
1644
|
-
|
|
1645
|
-
// src/tools/scanPackage.ts
|
|
1646
|
-
async function scanForAllTools(projectPath, options = {}) {
|
|
1647
|
-
const include = options.include ?? ["**/*.ts"];
|
|
1648
|
-
const tsconfigPath = options.tsconfigPath;
|
|
1649
|
-
const includeN8n = options.includeN8n === true;
|
|
1650
|
-
const opts = { include, tsconfigPath };
|
|
1651
|
-
const results = await Promise.all([
|
|
1652
|
-
discoverTools("function", projectPath, opts),
|
|
1653
|
-
discoverTools("skill", projectPath, opts),
|
|
1654
|
-
...includeN8n ? [discoverTools("n8n", projectPath, opts)] : []
|
|
1655
|
-
]);
|
|
1656
|
-
const specs = results.flatMap((r) => r.specs);
|
|
1657
|
-
const errors = results.flatMap((r) => r.errors);
|
|
1658
|
-
const warnings = results.flatMap((r) => r.warnings ?? []);
|
|
1659
|
-
return { specs, errors, warnings };
|
|
1660
|
-
}
|
|
1661
|
-
var __dirname$1 = path9__namespace.dirname(url.fileURLToPath((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('chunk-K45PQLZI.cjs', document.baseURI).href))));
|
|
1662
|
-
async function loadTemplate(name) {
|
|
1663
|
-
for (const dir of [
|
|
1664
|
-
path9__namespace.join(__dirname$1, "templates"),
|
|
1665
|
-
path9__namespace.join(__dirname$1, "..", "templates")
|
|
1666
|
-
]) {
|
|
1667
|
-
try {
|
|
1668
|
-
return await fs3__namespace.readFile(path9__namespace.join(dir, name), "utf-8");
|
|
1669
|
-
} catch {
|
|
1670
|
-
continue;
|
|
1671
|
-
}
|
|
1672
|
-
}
|
|
1673
|
-
throw new Error(`Template not found: ${name}. Tried templates/ next to generator and ../templates.`);
|
|
1674
|
-
}
|
|
1675
|
-
var TEMPLATE_NAMES = {
|
|
1676
|
-
mcpServer: "mcp-server.js",
|
|
1677
|
-
toolIndex: "tool-index.js",
|
|
1678
|
-
skillInvoker: "skill-invoker.js",
|
|
1679
|
-
n8nInvoker: "n8n-invoker.js"
|
|
1680
|
-
};
|
|
1681
|
-
function buildToolIndexCases(specs, fromGeneratedToProject) {
|
|
1682
|
-
return specs.map((s) => {
|
|
1683
|
-
const modPath = path9__namespace.join(fromGeneratedToProject, s._meta.sourcePath).replace(/\\/g, "/");
|
|
1684
|
-
return ` case "${s.name}": return (await import("${modPath}")).${s._meta.exportName};`;
|
|
1685
|
-
}).join("\n");
|
|
1686
|
-
}
|
|
1687
|
-
function buildSkillInvokerCases(specs, fromGeneratedToProject) {
|
|
1688
|
-
return specs.map((s) => {
|
|
1689
|
-
const handlerPath = path9__namespace.join(fromGeneratedToProject, s._meta.sourcePath, "handler").replace(/\\/g, "/");
|
|
1690
|
-
const descEscaped = (s.description ?? "").replace(/"/g, '\\"');
|
|
1691
|
-
return ` case "${s.name}": {
|
|
1692
|
-
const mod = await import("${handlerPath}.js").catch(() => import("${handlerPath}.mjs"));
|
|
1693
|
-
const fn = mod.default ?? mod.handler;
|
|
1694
|
-
if (typeof fn !== "function") return { result: null, error: "No handler" };
|
|
1695
|
-
const ctx = { requestId: "mcp", taskId: "mcp", skill: { name: "${s.name}", description: "${descEscaped}", instructions: "", resources: [], readResource: async () => "", getResourcesByType: () => [], dirPath: "" } };
|
|
1696
|
-
const out = await fn(args, ctx);
|
|
1697
|
-
return out?.result !== undefined ? out.result : out;
|
|
1698
|
-
}`;
|
|
1699
|
-
}).join("\n");
|
|
1700
|
-
}
|
|
1701
|
-
function buildN8nInvokerCases(specs) {
|
|
1702
|
-
return specs.map((s) => {
|
|
1703
|
-
const webhookUrl = s.endpoint;
|
|
1704
|
-
const url = webhookUrl ? `"${webhookUrl}"` : "process.env.N8N_WEBHOOK_" + s.name.replace(/[^a-zA-Z0-9]/g, "_").toUpperCase() + " ?? null";
|
|
1705
|
-
return ` case "${s.name}": {
|
|
1706
|
-
const url = ${url};
|
|
1707
|
-
if (!url) throw new Error("n8n webhook not configured for ${s.name}. Set N8N_WEBHOOK_* or add webhook to workflow.");
|
|
1708
|
-
const res = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(args) });
|
|
1709
|
-
if (!res.ok) throw new Error("n8n request failed: " + res.status);
|
|
1710
|
-
return res.json().catch(() => ({}));
|
|
1711
|
-
}`;
|
|
1712
|
-
}).join("\n");
|
|
1713
|
-
}
|
|
1714
|
-
async function generate(options) {
|
|
1715
|
-
const { specs, outDir, projectPath } = options;
|
|
1716
|
-
await fs3__namespace.mkdir(outDir, { recursive: true });
|
|
1717
|
-
const functionSpecs = specs.filter(
|
|
1718
|
-
(s) => s.kind === FUNCTION_KIND && s._meta?.sourcePath != null && s._meta?.exportName != null
|
|
1719
|
-
);
|
|
1720
|
-
const skillSpecs = specs.filter(
|
|
1721
|
-
(s) => s.kind === SKILL_KIND && s._meta?.sourcePath != null
|
|
1722
|
-
);
|
|
1723
|
-
const n8nSpecs = specs.filter((s) => s.kind === N8N_KIND);
|
|
1724
|
-
const toolSpecsJson = specs.map((s) => ({
|
|
1725
|
-
kind: s.kind,
|
|
1726
|
-
name: s.name,
|
|
1727
|
-
description: s.description,
|
|
1728
|
-
inputSchema: s.inputSchema,
|
|
1729
|
-
...s.kind === FUNCTION_KIND && s._meta?.sourcePath != null && s._meta?.exportName != null && {
|
|
1730
|
-
outputSchema: s.outputSchema ?? { type: "object", additionalProperties: true },
|
|
1731
|
-
sourcePath: s._meta.sourcePath,
|
|
1732
|
-
exportName: s._meta.exportName
|
|
1733
|
-
},
|
|
1734
|
-
_meta: s._meta,
|
|
1735
|
-
...s.kind === SKILL_KIND && s._meta?.sourcePath != null && { sourcePath: s._meta.sourcePath },
|
|
1736
|
-
...s.kind === N8N_KIND && {
|
|
1737
|
-
sourcePath: s._meta?.sourcePath,
|
|
1738
|
-
webhookUrl: s.endpoint
|
|
1739
|
-
}
|
|
1740
|
-
}));
|
|
1741
|
-
await fs3__namespace.writeFile(
|
|
1742
|
-
path9__namespace.join(outDir, "tool-specs.json"),
|
|
1743
|
-
JSON.stringify(toolSpecsJson, null, 2),
|
|
1744
|
-
"utf-8"
|
|
1745
|
-
);
|
|
1746
|
-
const configJson = { projectPath: path9__namespace.resolve(projectPath) };
|
|
1747
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "config.json"), JSON.stringify(configJson, null, 2), "utf-8");
|
|
1748
|
-
const rel = path9__namespace.relative(outDir, projectPath) || ".";
|
|
1749
|
-
const fromGeneratedToProject = rel.split(path9__namespace.sep).length ? rel : ".";
|
|
1750
|
-
const [mcpServerTemplate, toolIndexTemplate, skillInvokerTemplate, n8nInvokerTemplate] = await Promise.all([
|
|
1751
|
-
loadTemplate(TEMPLATE_NAMES.mcpServer),
|
|
1752
|
-
loadTemplate(TEMPLATE_NAMES.toolIndex),
|
|
1753
|
-
loadTemplate(TEMPLATE_NAMES.skillInvoker),
|
|
1754
|
-
loadTemplate(TEMPLATE_NAMES.n8nInvoker)
|
|
1755
|
-
]);
|
|
1756
|
-
const toolIndexTs = toolIndexTemplate.replace("{{CASES}}", buildToolIndexCases(functionSpecs, fromGeneratedToProject));
|
|
1757
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "tool-index.ts"), toolIndexTs, "utf-8");
|
|
1758
|
-
const skillCases = buildSkillInvokerCases(skillSpecs, fromGeneratedToProject);
|
|
1759
|
-
const skillDefaultCase = skillSpecs.length === 0 ? "default: throw new Error('Unknown skill: ' + name);" : 'default: throw new Error("Unknown skill: " + name);';
|
|
1760
|
-
const skillInvokerTs = skillInvokerTemplate.replace("{{CASES}}", skillCases).replace("{{DEFAULT_CASE}}", skillDefaultCase);
|
|
1761
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "skill-invoker.ts"), skillInvokerTs, "utf-8");
|
|
1762
|
-
const n8nCases = buildN8nInvokerCases(n8nSpecs);
|
|
1763
|
-
const n8nDefaultCase = n8nSpecs.length === 0 ? "default: throw new Error('Unknown n8n tool: ' + name);" : 'default: throw new Error("Unknown n8n tool: " + name);';
|
|
1764
|
-
const n8nInvokerTs = n8nInvokerTemplate.replace("{{CASES}}", n8nCases).replace("{{DEFAULT_CASE}}", n8nDefaultCase);
|
|
1765
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "n8n-invoker.ts"), n8nInvokerTs, "utf-8");
|
|
1766
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "mcp-server.ts"), mcpServerTemplate, "utf-8");
|
|
1767
|
-
const mcpJson = {
|
|
1768
|
-
command: "npx",
|
|
1769
|
-
args: ["-y", "tsx", path9__namespace.join(outDir, "mcp-server.ts")]
|
|
1770
|
-
};
|
|
1771
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "mcp.json"), JSON.stringify(mcpJson, null, 2), "utf-8");
|
|
1772
|
-
const packageJson = {
|
|
1773
|
-
name: "function-tools-mcp",
|
|
1774
|
-
version: "1.0.0",
|
|
1775
|
-
type: "module",
|
|
1776
|
-
description: "MCP server generated from @tool functions, SKILL.md, and workflow.json",
|
|
1777
|
-
main: "mcp-server.ts",
|
|
1778
|
-
scripts: { start: "npx tsx mcp-server.ts" },
|
|
1779
|
-
dependencies: {
|
|
1780
|
-
"@modelcontextprotocol/sdk": "^1.25.3",
|
|
1781
|
-
typescript: ">=5.0.0",
|
|
1782
|
-
tsx: ">=4.0.0"
|
|
1783
|
-
}
|
|
1784
|
-
};
|
|
1785
|
-
await fs3__namespace.writeFile(path9__namespace.join(outDir, "package.json"), JSON.stringify(packageJson, null, 2), "utf-8");
|
|
1786
|
-
return {
|
|
1787
|
-
entryPath: path9__namespace.join(outDir, "mcp-server.ts"),
|
|
1788
|
-
mcpJsonPath: path9__namespace.join(outDir, "mcp.json")
|
|
1789
|
-
};
|
|
1790
|
-
}
|
|
1791
|
-
|
|
1792
|
-
// src/api/expose/mcp-build/build.ts
|
|
1793
|
-
async function buildMcpPackage(options = {}) {
|
|
1794
|
-
const projectPath = path9__namespace.resolve(options.projectPath ?? process.cwd());
|
|
1795
|
-
const outDir = path9__namespace.resolve(projectPath, options.outDir ?? "dist");
|
|
1796
|
-
const include = options.include ?? ["**/*.ts"];
|
|
1797
|
-
const tsconfigPath = options.tsconfigPath;
|
|
1798
|
-
const scanResult = await scanForAllTools(projectPath, {
|
|
1799
|
-
include,
|
|
1800
|
-
tsconfigPath,
|
|
1801
|
-
includeN8n: options.includeN8n
|
|
1802
|
-
});
|
|
1803
|
-
if (scanResult.errors.length > 0) {
|
|
1804
|
-
const msg = scanResult.errors.map((e) => `${e.file}: ${e.message}`).join("; ");
|
|
1805
|
-
throw new Error(`Scan failed: ${msg}`);
|
|
1806
|
-
}
|
|
1807
|
-
const allSpecs = scanResult.specs;
|
|
1808
|
-
if (allSpecs.length === 0) {
|
|
1809
|
-
throw new Error(
|
|
1810
|
-
"No tools found. Add @tool functions (JSDoc), SKILL.md directories, or workflow.json directories."
|
|
1811
|
-
);
|
|
1812
|
-
}
|
|
1813
|
-
const { entryPath, mcpJsonPath } = await generate({
|
|
1814
|
-
specs: allSpecs,
|
|
1815
|
-
outDir,
|
|
1816
|
-
projectPath
|
|
1817
|
-
});
|
|
1818
|
-
return {
|
|
1819
|
-
outDir,
|
|
1820
|
-
toolCount: allSpecs.length,
|
|
1821
|
-
entryPath,
|
|
1822
|
-
mcpJsonPath
|
|
1823
|
-
};
|
|
1824
|
-
}
|
|
1825
|
-
var buildFunctionToTool = buildMcpPackage;
|
|
1826
|
-
async function runMcpServer(options = {}) {
|
|
1827
|
-
const base = options.path ?? process.cwd();
|
|
1828
|
-
const candidates = [
|
|
1829
|
-
path9__namespace.join(base, "mcp-server.ts"),
|
|
1830
|
-
path9__namespace.join(base, "mcp-server.js"),
|
|
1831
|
-
path9__namespace.join(base, "dist", "mcp-server.ts"),
|
|
1832
|
-
path9__namespace.join(base, "dist", "mcp-server.js"),
|
|
1833
|
-
path9__namespace.join(base, "generated", "mcp-server.ts"),
|
|
1834
|
-
path9__namespace.join(base, "generated", "mcp-server.js")
|
|
1835
|
-
];
|
|
1836
|
-
let entry = "";
|
|
1837
|
-
for (const p of candidates) {
|
|
1838
|
-
try {
|
|
1839
|
-
await fs3__namespace.access(p);
|
|
1840
|
-
entry = p;
|
|
1841
|
-
break;
|
|
1842
|
-
} catch {
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
if (!entry) {
|
|
1846
|
-
throw new Error(
|
|
1847
|
-
`MCP entrypoint not found. Run "agent-tool build" first, or pass --path to a directory containing mcp-server.ts. Tried: ${candidates.join(", ")}`
|
|
1848
|
-
);
|
|
1849
|
-
}
|
|
1850
|
-
const dir = path9__namespace.dirname(entry);
|
|
1851
|
-
const child = child_process.spawn("npx", ["-y", "tsx", path9__namespace.basename(entry)], {
|
|
1852
|
-
cwd: dir,
|
|
1853
|
-
stdio: ["pipe", "pipe", "inherit"],
|
|
1854
|
-
shell: false
|
|
1855
|
-
});
|
|
1856
|
-
return { process: child };
|
|
1857
|
-
}
|
|
1858
|
-
var runGeneratedMCP = runMcpServer;
|
|
1859
|
-
|
|
1860
|
-
exports.buildFunctionToTool = buildFunctionToTool;
|
|
1861
|
-
exports.buildMcpPackage = buildMcpPackage;
|
|
1862
|
-
exports.initProject = initProject;
|
|
1863
|
-
exports.runGeneratedMCP = runGeneratedMCP;
|
|
1864
|
-
exports.runMcpServer = runMcpServer;
|
|
1865
|
-
exports.scan = scan;
|
|
1866
|
-
exports.scanForTools = scanForTools;
|
|
1867
|
-
//# sourceMappingURL=chunk-K45PQLZI.cjs.map
|
|
1868
|
-
//# sourceMappingURL=chunk-K45PQLZI.cjs.map
|