frontmcp 0.2.5 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +30 -16
- package/src/cli.d.ts +6 -0
- package/src/cli.js +642 -0
- package/src/cli.js.map +1 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
- package/src/version.d.ts +1 -0
- package/src/version.js +11 -0
- package/src/version.js.map +1 -0
- package/dist/cli.js +0 -667
- package/dist/index.js +0 -2
- package/dist/version.js +0 -7
- package/src/cli.ts +0 -677
- package/src/index.ts +0 -1
- package/src/version.ts +0 -5
- package/tsconfig.json +0 -14
package/src/cli.ts
DELETED
|
@@ -1,677 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* frontmcp - FrontMCP command line interface
|
|
4
|
-
* Save as bin/frontmcp.ts (compile to JS with shebang preserved) or run with tsx.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import * as fs from 'fs';
|
|
8
|
-
import {promises as fsp} from 'fs';
|
|
9
|
-
import * as path from 'path';
|
|
10
|
-
import {spawn, ChildProcess} from 'child_process';
|
|
11
|
-
import {getSelfVersion} from "./version";
|
|
12
|
-
|
|
13
|
-
/* ----------------------------- Types & Helpers ---------------------------- */
|
|
14
|
-
|
|
15
|
-
type Command = 'dev' | 'build' | 'init' | 'doctor' | 'inspector' | 'create' | 'help';
|
|
16
|
-
|
|
17
|
-
interface ParsedArgs {
|
|
18
|
-
_: string[];
|
|
19
|
-
outDir?: string;
|
|
20
|
-
entry?: string;
|
|
21
|
-
help?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const COLORS = {
|
|
25
|
-
reset: '\x1b[0m',
|
|
26
|
-
bold: '\x1b[1m',
|
|
27
|
-
dim: '\x1b[2m',
|
|
28
|
-
red: '\x1b[31m',
|
|
29
|
-
green: '\x1b[32m',
|
|
30
|
-
yellow: '\x1b[33m',
|
|
31
|
-
blue: '\x1b[34m',
|
|
32
|
-
cyan: '\x1b[36m',
|
|
33
|
-
gray: '\x1b[90m',
|
|
34
|
-
} as const;
|
|
35
|
-
|
|
36
|
-
const c = (color: keyof typeof COLORS, s: string) => COLORS[color] + s + COLORS.reset;
|
|
37
|
-
|
|
38
|
-
function showHelp(): void {
|
|
39
|
-
console.log(`
|
|
40
|
-
${c('bold', 'frontmcp')} — FrontMCP command line interface
|
|
41
|
-
|
|
42
|
-
${c('bold', 'Usage')}
|
|
43
|
-
frontmcp <command> [options]
|
|
44
|
-
|
|
45
|
-
${c('bold', 'Commands')}
|
|
46
|
-
dev Start in development mode (tsx --watch <entry> + async type-check)
|
|
47
|
-
build Compile entry with TypeScript (tsc)
|
|
48
|
-
init Create or fix a tsconfig.json suitable for FrontMCP
|
|
49
|
-
doctor Check Node/npm versions and tsconfig requirements
|
|
50
|
-
inspector Launch MCP Inspector (npx @modelcontextprotocol/inspector)
|
|
51
|
-
create <name> Scaffold a new FrontMCP project in ./<name>
|
|
52
|
-
help Show this help message
|
|
53
|
-
|
|
54
|
-
${c('bold', 'Options')}
|
|
55
|
-
-o, --out-dir <dir> Output directory (default: ./dist)
|
|
56
|
-
-e, --entry <path> Manually specify entry file path
|
|
57
|
-
|
|
58
|
-
${c('bold', 'Examples')}
|
|
59
|
-
frontmcp dev
|
|
60
|
-
frontmcp build --out-dir build
|
|
61
|
-
frontmcp init
|
|
62
|
-
frontmcp doctor
|
|
63
|
-
frontmcp inspector
|
|
64
|
-
npx frontmcp create my-mcp
|
|
65
|
-
`);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function parseArgs(argv: string[]): ParsedArgs {
|
|
69
|
-
const out: ParsedArgs = {_: []};
|
|
70
|
-
for (let i = 0; i < argv.length; i++) {
|
|
71
|
-
const a = argv[i];
|
|
72
|
-
if (a === '--out-dir' || a === '-o') out.outDir = argv[++i];
|
|
73
|
-
else if (a === '--entry' || a === '-e') out.entry = argv[++i];
|
|
74
|
-
else if (a === '--help' || a === '-h') out.help = true;
|
|
75
|
-
else out._.push(a);
|
|
76
|
-
}
|
|
77
|
-
return out;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async function fileExists(p: string): Promise<boolean> {
|
|
81
|
-
try {
|
|
82
|
-
await fsp.access(p, fs.constants.F_OK);
|
|
83
|
-
return true;
|
|
84
|
-
} catch {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
async function readJSON<T = any>(jsonPath: string): Promise<T | null> {
|
|
90
|
-
try {
|
|
91
|
-
const buf = await fsp.readFile(jsonPath, 'utf8');
|
|
92
|
-
return JSON.parse(buf) as T;
|
|
93
|
-
} catch {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
async function writeJSON(p: string, obj: any) {
|
|
99
|
-
await fsp.writeFile(p, JSON.stringify(obj, null, 2) + '\n', 'utf8');
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function tryCandidates(base: string): string[] {
|
|
103
|
-
const exts = ['', '.ts', '.tsx', '.js', '.mjs', '.cjs'];
|
|
104
|
-
return exts.map((ext) => base + ext);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
async function resolveEntry(cwd: string, explicit?: string): Promise<string> {
|
|
108
|
-
if (explicit) {
|
|
109
|
-
const full = path.resolve(cwd, explicit);
|
|
110
|
-
if (await fileExists(full)) return full;
|
|
111
|
-
throw new Error(`Entry override not found: ${explicit}`);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const pkgPath = path.join(cwd, 'package.json');
|
|
115
|
-
if (await fileExists(pkgPath)) {
|
|
116
|
-
const pkg = await readJSON<any>(pkgPath);
|
|
117
|
-
if (pkg && typeof pkg.main === 'string' && pkg.main.trim()) {
|
|
118
|
-
const mainCandidates = tryCandidates(path.resolve(cwd, pkg.main));
|
|
119
|
-
for (const p of mainCandidates) {
|
|
120
|
-
if (await fileExists(p)) return p;
|
|
121
|
-
}
|
|
122
|
-
const asDir = path.resolve(cwd, pkg.main);
|
|
123
|
-
const idxCandidates = tryCandidates(path.join(asDir, 'index'));
|
|
124
|
-
for (const p of idxCandidates) {
|
|
125
|
-
if (await fileExists(p)) return p;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const fallback = path.join(cwd, 'src', 'main.ts');
|
|
131
|
-
if (await fileExists(fallback)) return fallback;
|
|
132
|
-
|
|
133
|
-
const msg = [
|
|
134
|
-
c('red', 'No entry file found.'),
|
|
135
|
-
'',
|
|
136
|
-
'I looked for:',
|
|
137
|
-
` • ${pkgPath} with a valid "main" field`,
|
|
138
|
-
` • ${path.relative(cwd, fallback)}`,
|
|
139
|
-
'',
|
|
140
|
-
'Please create an entry file (e.g. src/main.ts) or set "main" in package.json,',
|
|
141
|
-
'or run with an explicit path:',
|
|
142
|
-
` frontmcp dev --entry src/main.ts`,
|
|
143
|
-
].join('\n');
|
|
144
|
-
throw new Error(msg);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function runCmd(cmd: string, args: string[], opts: { cwd?: string } = {}): Promise<void> {
|
|
148
|
-
return new Promise((resolve, reject) => {
|
|
149
|
-
const child = spawn(cmd, args, {stdio: 'inherit', shell: true, ...opts});
|
|
150
|
-
child.on('close', (code) => (code === 0 ? resolve() : reject(new Error(`${cmd} exited with code ${code}`))));
|
|
151
|
-
child.on('error', reject);
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function ensureDir(p: string): Promise<void> {
|
|
156
|
-
await fsp.mkdir(p, {recursive: true});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async function isDirEmpty(dir: string): Promise<boolean> {
|
|
160
|
-
try {
|
|
161
|
-
const items = await fsp.readdir(dir);
|
|
162
|
-
return items.length === 0;
|
|
163
|
-
} catch (e: any) {
|
|
164
|
-
if (e?.code === 'ENOENT') return true;
|
|
165
|
-
throw e;
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function sanitizeForFolder(name: string): string {
|
|
170
|
-
const seg = name.startsWith('@') && name.includes('/') ? name.split('/')[1] : name;
|
|
171
|
-
return seg.replace(/[^a-zA-Z0-9._-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '').toLowerCase() || 'frontmcp-app';
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function sanitizeForNpm(name: string): string {
|
|
175
|
-
if (name.startsWith('@') && name.includes('/')) {
|
|
176
|
-
const [scope, pkg] = name.split('/');
|
|
177
|
-
const s = scope.replace(/[^a-z0-9-]/gi, '').toLowerCase();
|
|
178
|
-
const p = pkg.replace(/[^a-z0-9._-]/gi, '-').toLowerCase();
|
|
179
|
-
return `@${s}/${p || 'frontmcp-app'}`;
|
|
180
|
-
}
|
|
181
|
-
return name.replace(/[^a-z0-9._-]/gi, '-').toLowerCase() || 'frontmcp-app';
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
/* --------------------------------- Actions -------------------------------- */
|
|
186
|
-
|
|
187
|
-
function isTsLike(p: string): boolean {
|
|
188
|
-
return /\.tsx?$/i.test(p);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function killQuiet(proc?: ChildProcess) {
|
|
192
|
-
try {
|
|
193
|
-
proc && proc.kill('SIGINT');
|
|
194
|
-
} catch {
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async function runDev(opts: ParsedArgs): Promise<void> {
|
|
199
|
-
const cwd = process.cwd();
|
|
200
|
-
const entry = await resolveEntry(cwd, opts.entry);
|
|
201
|
-
|
|
202
|
-
console.log(`${c('cyan', '[dev]')} using entry: ${path.relative(cwd, entry)}`);
|
|
203
|
-
console.log(`${c('gray', '[dev]')} starting ${c('bold', 'tsx --watch')} and ${c('bold', 'tsc --noEmit --watch')} (async type-checker)`);
|
|
204
|
-
console.log(`${c('gray', 'hint:')} press Ctrl+C to stop`);
|
|
205
|
-
|
|
206
|
-
// Start tsx watcher (app run)
|
|
207
|
-
const app = spawn('npx', ['-y', 'tsx', '--watch', entry], {stdio: 'inherit', shell: true});
|
|
208
|
-
// Start tsc in watch mode for async type-checking (non-blocking)
|
|
209
|
-
const checker = spawn('npx', ['-y', 'tsc', '--noEmit', '--pretty', '--watch'], {stdio: 'inherit', shell: true});
|
|
210
|
-
|
|
211
|
-
const cleanup = () => {
|
|
212
|
-
killQuiet(checker);
|
|
213
|
-
killQuiet(app);
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
process.on('SIGINT', () => {
|
|
217
|
-
cleanup();
|
|
218
|
-
process.exit(0);
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
await new Promise<void>((resolve, reject) => {
|
|
222
|
-
app.on('close', (_code) => {
|
|
223
|
-
// When app exits, stop checker too.
|
|
224
|
-
cleanup();
|
|
225
|
-
resolve();
|
|
226
|
-
});
|
|
227
|
-
app.on('error', (err) => {
|
|
228
|
-
cleanup();
|
|
229
|
-
reject(err);
|
|
230
|
-
});
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async function runBuild(opts: ParsedArgs): Promise<void> {
|
|
235
|
-
const cwd = process.cwd();
|
|
236
|
-
const entry = await resolveEntry(cwd, opts.entry);
|
|
237
|
-
const outDir = path.resolve(cwd, opts.outDir || 'dist');
|
|
238
|
-
await ensureDir(outDir);
|
|
239
|
-
|
|
240
|
-
console.log(`${c('cyan', '[build]')} entry: ${path.relative(cwd, entry)}`);
|
|
241
|
-
console.log(`${c('cyan', '[build]')} outDir: ${path.relative(cwd, outDir)}`);
|
|
242
|
-
|
|
243
|
-
const args: string[] = [];
|
|
244
|
-
args.push('--outDir', outDir);
|
|
245
|
-
args.push('--skipLibCheck');
|
|
246
|
-
args.push('--rootDir', path.dirname(entry));
|
|
247
|
-
|
|
248
|
-
if (!isTsLike(entry)) {
|
|
249
|
-
args.push('--allowJs');
|
|
250
|
-
console.log(c('yellow', '[build] Entry is not TypeScript; enabling --allowJs'));
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
const tsconfigPath = path.join(cwd, 'tsconfig.json');
|
|
254
|
-
if (await fileExists(tsconfigPath)) {
|
|
255
|
-
console.log(c('gray', `[build] tsconfig.json detected (project options will be respected where applicable)`));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
await runCmd('npx', ['-y', 'tsc', entry, ...args]);
|
|
259
|
-
console.log(c('green', '✅ Build completed.'));
|
|
260
|
-
console.log(c('gray', `Output placed in ${path.relative(cwd, outDir)}`));
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/* --------------------------- tsconfig management --------------------------- */
|
|
264
|
-
|
|
265
|
-
const REQUIRED_DECORATOR_FIELDS = {
|
|
266
|
-
target: 'es2021',
|
|
267
|
-
module: 'esnext',
|
|
268
|
-
emitDecoratorMetadata: true,
|
|
269
|
-
experimentalDecorators: true,
|
|
270
|
-
strictFunctionTypes: true,
|
|
271
|
-
moduleResolution: 'node',
|
|
272
|
-
} as const;
|
|
273
|
-
|
|
274
|
-
const RECOMMENDED_TSCONFIG = {
|
|
275
|
-
compilerOptions: {
|
|
276
|
-
target: REQUIRED_DECORATOR_FIELDS.target,
|
|
277
|
-
module: REQUIRED_DECORATOR_FIELDS.module,
|
|
278
|
-
emitDecoratorMetadata: REQUIRED_DECORATOR_FIELDS.emitDecoratorMetadata,
|
|
279
|
-
experimentalDecorators: REQUIRED_DECORATOR_FIELDS.experimentalDecorators,
|
|
280
|
-
strictFunctionTypes: REQUIRED_DECORATOR_FIELDS.strictFunctionTypes,
|
|
281
|
-
moduleResolution: REQUIRED_DECORATOR_FIELDS.moduleResolution,
|
|
282
|
-
strict: true,
|
|
283
|
-
esModuleInterop: true,
|
|
284
|
-
resolveJsonModule: true,
|
|
285
|
-
skipLibCheck: true,
|
|
286
|
-
sourceMap: true,
|
|
287
|
-
outDir: 'dist',
|
|
288
|
-
rootDir: 'src',
|
|
289
|
-
types: ['node'],
|
|
290
|
-
},
|
|
291
|
-
include: ['src/**/*'],
|
|
292
|
-
} as const;
|
|
293
|
-
|
|
294
|
-
function deepMerge<T extends Record<string, any>, U extends Record<string, any>>(base: T, patch: U): T & U {
|
|
295
|
-
const out: Record<string, any> = {...base};
|
|
296
|
-
for (const [k, v] of Object.entries(patch)) {
|
|
297
|
-
if (v && typeof v === 'object' && !Array.isArray(v)) {
|
|
298
|
-
out[k] = deepMerge(base[k] ?? {}, v as Record<string, any>);
|
|
299
|
-
} else {
|
|
300
|
-
out[k] = v;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
return out as T & U;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
function ensureRequiredTsOptions(obj: Record<string, any>): Record<string, any> {
|
|
307
|
-
const next = {...obj};
|
|
308
|
-
next.compilerOptions = {...(next.compilerOptions || {})};
|
|
309
|
-
next.compilerOptions.target = REQUIRED_DECORATOR_FIELDS.target;
|
|
310
|
-
next.compilerOptions.module = REQUIRED_DECORATOR_FIELDS.module;
|
|
311
|
-
next.compilerOptions.emitDecoratorMetadata = REQUIRED_DECORATOR_FIELDS.emitDecoratorMetadata;
|
|
312
|
-
next.compilerOptions.experimentalDecorators = REQUIRED_DECORATOR_FIELDS.experimentalDecorators;
|
|
313
|
-
return next;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
function normalizeStr(x: unknown): string | undefined {
|
|
317
|
-
return typeof x === 'string' ? x.toLowerCase() : undefined;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
function checkRequiredTsOptions(compilerOptions: Record<string, any> | undefined) {
|
|
321
|
-
const issues: string[] = [];
|
|
322
|
-
const ok: string[] = [];
|
|
323
|
-
|
|
324
|
-
const target = normalizeStr(compilerOptions?.target);
|
|
325
|
-
const moduleVal = normalizeStr(compilerOptions?.module);
|
|
326
|
-
const edm = compilerOptions?.emitDecoratorMetadata;
|
|
327
|
-
const ed = compilerOptions?.experimentalDecorators;
|
|
328
|
-
|
|
329
|
-
if (target === REQUIRED_DECORATOR_FIELDS.target) ok.push(`compilerOptions.target = "${REQUIRED_DECORATOR_FIELDS.target}"`);
|
|
330
|
-
else issues.push(`compilerOptions.target should be "${REQUIRED_DECORATOR_FIELDS.target}"`);
|
|
331
|
-
|
|
332
|
-
if (moduleVal === REQUIRED_DECORATOR_FIELDS.module) ok.push(`compilerOptions.module = "${REQUIRED_DECORATOR_FIELDS.module}"`);
|
|
333
|
-
else issues.push(`compilerOptions.module should be "${REQUIRED_DECORATOR_FIELDS.module}"`);
|
|
334
|
-
|
|
335
|
-
if (edm === REQUIRED_DECORATOR_FIELDS.emitDecoratorMetadata) ok.push(`compilerOptions.emitDecoratorMetadata = true`);
|
|
336
|
-
else issues.push(`compilerOptions.emitDecoratorMetadata should be true`);
|
|
337
|
-
|
|
338
|
-
if (ed === REQUIRED_DECORATOR_FIELDS.experimentalDecorators) ok.push(`compilerOptions.experimentalDecorators = true`);
|
|
339
|
-
else issues.push(`compilerOptions.experimentalDecorators should be true`);
|
|
340
|
-
|
|
341
|
-
return {ok, issues};
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async function runInit(baseDir?: string): Promise<void> {
|
|
345
|
-
const cwd = baseDir ?? process.cwd();
|
|
346
|
-
const tsconfigPath = path.join(cwd, 'tsconfig.json');
|
|
347
|
-
const existing = await readJSON<Record<string, any>>(tsconfigPath);
|
|
348
|
-
|
|
349
|
-
if (!existing) {
|
|
350
|
-
console.log(c('yellow', `tsconfig.json not found — creating one in ${path.relative(process.cwd(), cwd) || '.'}.`));
|
|
351
|
-
await writeJSON(tsconfigPath, RECOMMENDED_TSCONFIG);
|
|
352
|
-
console.log(c('green', '✅ Created tsconfig.json with required decorator settings.'));
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
let merged = deepMerge(RECOMMENDED_TSCONFIG as any, existing);
|
|
357
|
-
merged = ensureRequiredTsOptions(merged);
|
|
358
|
-
|
|
359
|
-
await writeJSON(tsconfigPath, merged);
|
|
360
|
-
console.log(c('green', '✅ tsconfig.json verified and updated (required decorator settings enforced).'));
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
function cmpSemver(a: string, b: string): number {
|
|
364
|
-
const pa = a.split('.').map((n) => parseInt(n, 10) || 0);
|
|
365
|
-
const pb = b.split('.').map((n) => parseInt(n, 10) || 0);
|
|
366
|
-
for (let i = 0; i < 3; i++) {
|
|
367
|
-
if ((pa[i] || 0) > (pb[i] || 0)) return 1;
|
|
368
|
-
if ((pa[i] || 0) < (pb[i] || 0)) return -1;
|
|
369
|
-
}
|
|
370
|
-
return 0;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
async function runDoctor(): Promise<void> {
|
|
374
|
-
const MIN_NODE = '22.0.0';
|
|
375
|
-
const MIN_NPM = '10.0.0';
|
|
376
|
-
const cwd = process.cwd();
|
|
377
|
-
|
|
378
|
-
let ok = true;
|
|
379
|
-
|
|
380
|
-
const nodeVer = process.versions.node;
|
|
381
|
-
if (cmpSemver(nodeVer, MIN_NODE) >= 0) {
|
|
382
|
-
console.log(`✅ Node ${nodeVer} (min ${MIN_NODE})`);
|
|
383
|
-
} else {
|
|
384
|
-
ok = false;
|
|
385
|
-
console.log(`❌ Node ${nodeVer} — please upgrade to >= ${MIN_NODE}`);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
let npmVer = 'unknown';
|
|
389
|
-
try {
|
|
390
|
-
npmVer = await new Promise<string>((resolve, reject) => {
|
|
391
|
-
const child = spawn('npm', ['-v'], {shell: true});
|
|
392
|
-
let out = '';
|
|
393
|
-
child.stdout?.on('data', (d) => (out += String(d)));
|
|
394
|
-
child.on('close', () => resolve(out.trim()));
|
|
395
|
-
child.on('error', reject);
|
|
396
|
-
});
|
|
397
|
-
if (cmpSemver(npmVer, MIN_NPM) >= 0) {
|
|
398
|
-
console.log(`✅ npm ${npmVer} (min ${MIN_NPM})`);
|
|
399
|
-
} else {
|
|
400
|
-
ok = false;
|
|
401
|
-
console.log(`❌ npm ${npmVer} — please upgrade to >= ${MIN_NPM}`);
|
|
402
|
-
}
|
|
403
|
-
} catch {
|
|
404
|
-
ok = false;
|
|
405
|
-
console.log('❌ npm not found in PATH');
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const tsconfigPath = path.join(cwd, 'tsconfig.json');
|
|
409
|
-
if (await fileExists(tsconfigPath)) {
|
|
410
|
-
console.log(`✅ tsconfig.json found`);
|
|
411
|
-
const tsconfig = await readJSON<Record<string, any>>(tsconfigPath);
|
|
412
|
-
const {ok: oks, issues} = checkRequiredTsOptions(tsconfig?.compilerOptions);
|
|
413
|
-
for (const line of oks) console.log(c('green', ` ✓ ${line}`));
|
|
414
|
-
if (issues.length) {
|
|
415
|
-
ok = false;
|
|
416
|
-
for (const line of issues) console.log(c('yellow', ` • ${line}`));
|
|
417
|
-
console.log(c('cyan', ` -> Run "frontmcp init" to apply the required settings.`));
|
|
418
|
-
}
|
|
419
|
-
} else {
|
|
420
|
-
ok = false;
|
|
421
|
-
console.log(`❌ tsconfig.json not found — run ${c('cyan', 'frontmcp init')}`);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
try {
|
|
425
|
-
const entry = await resolveEntry(cwd);
|
|
426
|
-
console.log(`✅ entry detected: ${path.relative(cwd, entry)}`);
|
|
427
|
-
} catch (e: any) {
|
|
428
|
-
const firstLine = (e?.message as string | undefined)?.split('\n')?.[0] ?? 'entry not found';
|
|
429
|
-
console.log(`❌ entry not detected — ${firstLine}`);
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
if (ok) console.log(c('green', '\nAll checks passed. You are ready to go!'));
|
|
433
|
-
else console.log(c('yellow', '\nSome checks failed. See above for fixes.'));
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
/* ------------------------------- Inspector -------------------------------- */
|
|
437
|
-
|
|
438
|
-
async function runInspector(): Promise<void> {
|
|
439
|
-
console.log(`${c('cyan', '[inspector]')} launching MCP Inspector...`);
|
|
440
|
-
await runCmd('npx', ['-y', '@modelcontextprotocol/inspector']);
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/* --------------------------------- Create --------------------------------- */
|
|
444
|
-
|
|
445
|
-
function pkgNameFromCwd(cwd: string) {
|
|
446
|
-
return path.basename(cwd).replace(/[^a-zA-Z0-9._-]/g, '-').toLowerCase() || 'frontmcp-app';
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
async function upsertPackageJson(cwd: string, nameOverride: string | undefined, selfVersion: string) {
|
|
450
|
-
const pkgPath = path.join(cwd, 'package.json');
|
|
451
|
-
const existing = await readJSON<Record<string, any>>(pkgPath);
|
|
452
|
-
|
|
453
|
-
const base = {
|
|
454
|
-
name: nameOverride ?? pkgNameFromCwd(cwd),
|
|
455
|
-
version: '0.1.0',
|
|
456
|
-
private: true,
|
|
457
|
-
type: 'commonjs',
|
|
458
|
-
main: 'src/main.ts',
|
|
459
|
-
scripts: {
|
|
460
|
-
dev: 'frontmcp dev',
|
|
461
|
-
build: 'frontmcp build',
|
|
462
|
-
inspect: 'frontmcp inspector',
|
|
463
|
-
doctor: 'frontmcp doctor',
|
|
464
|
-
},
|
|
465
|
-
engines: {
|
|
466
|
-
node: '>=22',
|
|
467
|
-
npm: '>=10',
|
|
468
|
-
},
|
|
469
|
-
dependencies: {
|
|
470
|
-
'@frontmcp/sdk': '^0.2.5',
|
|
471
|
-
'@frontmcp/core': '^0.2.5',
|
|
472
|
-
zod: '^3.23.8',
|
|
473
|
-
'reflect-metadata': '^0.2.2',
|
|
474
|
-
},
|
|
475
|
-
devDependencies: {
|
|
476
|
-
frontmcp: selfVersion, // exact CLI version used by npx
|
|
477
|
-
tsx: '^4.20.6',
|
|
478
|
-
"@types/node": "^20.11.30",
|
|
479
|
-
typescript: '^5.5.3',
|
|
480
|
-
},
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
if (!existing) {
|
|
484
|
-
await writeJSON(pkgPath, base);
|
|
485
|
-
console.log(c('green', '✅ Created package.json (pinned versions + exact frontmcp)'));
|
|
486
|
-
return;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
const merged = {...base, ...existing};
|
|
490
|
-
|
|
491
|
-
merged.name = existing.name || base.name;
|
|
492
|
-
merged.main = existing.main || base.main;
|
|
493
|
-
merged.type = existing.type || base.type;
|
|
494
|
-
|
|
495
|
-
merged.scripts = {
|
|
496
|
-
...base.scripts,
|
|
497
|
-
...(existing.scripts || {}),
|
|
498
|
-
dev: existing.scripts?.dev ?? base.scripts.dev,
|
|
499
|
-
build: existing.scripts?.build ?? base.scripts.build,
|
|
500
|
-
inspect: existing.scripts?.inspect ?? base.scripts.inspect,
|
|
501
|
-
doctor: existing.scripts?.doctor ?? base.scripts.doctor,
|
|
502
|
-
};
|
|
503
|
-
|
|
504
|
-
merged.engines = {
|
|
505
|
-
...(existing.engines || {}),
|
|
506
|
-
node: existing.engines?.node || base.engines.node,
|
|
507
|
-
npm: existing.engines?.npm || base.engines.npm,
|
|
508
|
-
};
|
|
509
|
-
|
|
510
|
-
merged.dependencies = {
|
|
511
|
-
...base.dependencies,
|
|
512
|
-
...(existing.dependencies || {}),
|
|
513
|
-
zod: '^3.23.8',
|
|
514
|
-
'reflect-metadata': '^0.2.2',
|
|
515
|
-
};
|
|
516
|
-
|
|
517
|
-
merged.devDependencies = {
|
|
518
|
-
...base.devDependencies,
|
|
519
|
-
...(existing.devDependencies || {}),
|
|
520
|
-
frontmcp: selfVersion,
|
|
521
|
-
tsx: '^4.20.6',
|
|
522
|
-
typescript: '^5.5.3',
|
|
523
|
-
};
|
|
524
|
-
|
|
525
|
-
await writeJSON(pkgPath, merged);
|
|
526
|
-
console.log(c('green', '✅ Updated package.json (ensured exact frontmcp and pinned versions)'));
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
async function scaffoldFileIfMissing(baseDir: string, p: string, content: string) {
|
|
530
|
-
if (await fileExists(p)) {
|
|
531
|
-
console.log(c('gray', `skip: ${path.relative(baseDir, p)} already exists`));
|
|
532
|
-
return;
|
|
533
|
-
}
|
|
534
|
-
await ensureDir(path.dirname(p));
|
|
535
|
-
await fsp.writeFile(p, content.replace(/^\n/, ''), 'utf8');
|
|
536
|
-
console.log(c('green', `✓ created ${path.relative(baseDir, p)}`));
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
const TEMPLATE_MAIN_TS = `
|
|
540
|
-
import 'reflect-metadata';
|
|
541
|
-
import { FrontMcp } from '@frontmcp/sdk';
|
|
542
|
-
import { CalcApp } from './calc.app';
|
|
543
|
-
|
|
544
|
-
@FrontMcp({
|
|
545
|
-
info: { name: 'Demo 🚀', version: '0.1.0' },
|
|
546
|
-
apps: [CalcApp],
|
|
547
|
-
auth: {
|
|
548
|
-
type: 'remote',
|
|
549
|
-
name: 'my-remote-auth',
|
|
550
|
-
baseUrl: 'https://idp.example.com',
|
|
551
|
-
},
|
|
552
|
-
})
|
|
553
|
-
export default class Server {}
|
|
554
|
-
`;
|
|
555
|
-
|
|
556
|
-
const TEMPLATE_CALC_APP_TS = `
|
|
557
|
-
import { App } from '@frontmcp/sdk';
|
|
558
|
-
import AddTool from './tools/add.tool';
|
|
559
|
-
|
|
560
|
-
@App({
|
|
561
|
-
id: 'calc',
|
|
562
|
-
name: 'Calculator',
|
|
563
|
-
tools: [AddTool],
|
|
564
|
-
})
|
|
565
|
-
export class CalcApp {}
|
|
566
|
-
`;
|
|
567
|
-
|
|
568
|
-
const TEMPLATE_ADD_TOOL_TS = `
|
|
569
|
-
import { tool } from '@frontmcp/sdk';
|
|
570
|
-
import { z } from 'zod';
|
|
571
|
-
|
|
572
|
-
const AddTool = tool({
|
|
573
|
-
name: 'add',
|
|
574
|
-
description: 'Add two numbers',
|
|
575
|
-
inputSchema: { a: z.number(), b: z.number() },
|
|
576
|
-
outputSchema: { result: z.number() },
|
|
577
|
-
})(async (input, _ctx) => {
|
|
578
|
-
return { result: input.a + input.b };
|
|
579
|
-
});
|
|
580
|
-
|
|
581
|
-
export default AddTool;
|
|
582
|
-
`;
|
|
583
|
-
|
|
584
|
-
async function runCreate(projectArg?: string): Promise<void> {
|
|
585
|
-
if (!projectArg) {
|
|
586
|
-
console.error(c('red', 'Error: project name is required.\n'));
|
|
587
|
-
console.log(`Usage: ${c('bold', 'npx frontmcp create <project-name>')}`);
|
|
588
|
-
process.exit(1);
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
const folder = sanitizeForFolder(projectArg);
|
|
592
|
-
const pkgName = sanitizeForNpm(projectArg);
|
|
593
|
-
const targetDir = path.resolve(process.cwd(), folder);
|
|
594
|
-
|
|
595
|
-
if (await fileExists(targetDir)) {
|
|
596
|
-
if (!(await isDirEmpty(targetDir))) {
|
|
597
|
-
console.error(c('red', `Refusing to scaffold into non-empty directory: ${path.relative(process.cwd(), targetDir)}`));
|
|
598
|
-
console.log(c('gray', 'Pick a different name or start with an empty folder.'));
|
|
599
|
-
process.exit(1);
|
|
600
|
-
}
|
|
601
|
-
} else {
|
|
602
|
-
await ensureDir(targetDir);
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
console.log(`${c('cyan', '[create]')} Creating project in ${c('bold', './' + path.relative(process.cwd(), targetDir))}`);
|
|
606
|
-
process.chdir(targetDir);
|
|
607
|
-
|
|
608
|
-
// 1) tsconfig
|
|
609
|
-
await runInit(targetDir);
|
|
610
|
-
|
|
611
|
-
// 2) package.json (pinned deps + exact CLI version)
|
|
612
|
-
const selfVersion = getSelfVersion();
|
|
613
|
-
await upsertPackageJson(targetDir, pkgName, selfVersion);
|
|
614
|
-
|
|
615
|
-
// 3) files
|
|
616
|
-
await scaffoldFileIfMissing(targetDir, path.join(targetDir, 'src', 'main.ts'), TEMPLATE_MAIN_TS);
|
|
617
|
-
await scaffoldFileIfMissing(targetDir, path.join(targetDir, 'src', 'calc.app.ts'), TEMPLATE_CALC_APP_TS);
|
|
618
|
-
await scaffoldFileIfMissing(targetDir, path.join(targetDir, 'src', 'tools', 'add.tool.ts'), TEMPLATE_ADD_TOOL_TS);
|
|
619
|
-
|
|
620
|
-
console.log('\nNext steps:');
|
|
621
|
-
console.log(` 1) cd ${folder}`);
|
|
622
|
-
console.log(' 2) npm install');
|
|
623
|
-
console.log(' 3) npm run dev ', c('gray', '# tsx watcher + async tsc type-check'));
|
|
624
|
-
console.log(' 4) npm run inspect ', c('gray', '# launch MCP Inspector'));
|
|
625
|
-
console.log(' 5) npm run build ', c('gray', '# compile with tsc via frontmcp build'));
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
/* --------------------------------- Main ----------------------------------- */
|
|
629
|
-
|
|
630
|
-
async function main(): Promise<void> {
|
|
631
|
-
const argv = process.argv.slice(2);
|
|
632
|
-
const parsed = parseArgs(argv);
|
|
633
|
-
const cmd = parsed._[0] as Command | undefined;
|
|
634
|
-
|
|
635
|
-
if (parsed.help || !cmd) {
|
|
636
|
-
showHelp();
|
|
637
|
-
process.exit(0);
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
try {
|
|
641
|
-
switch (cmd) {
|
|
642
|
-
case 'dev':
|
|
643
|
-
await runDev(parsed);
|
|
644
|
-
break;
|
|
645
|
-
case 'build':
|
|
646
|
-
parsed.outDir = parsed.outDir || 'dist';
|
|
647
|
-
await runBuild(parsed);
|
|
648
|
-
break;
|
|
649
|
-
case 'init':
|
|
650
|
-
await runInit();
|
|
651
|
-
break;
|
|
652
|
-
case 'doctor':
|
|
653
|
-
await runDoctor();
|
|
654
|
-
break;
|
|
655
|
-
case 'inspector':
|
|
656
|
-
await runInspector();
|
|
657
|
-
break;
|
|
658
|
-
case 'create': {
|
|
659
|
-
const projectName = parsed._[1];
|
|
660
|
-
await runCreate(projectName);
|
|
661
|
-
break;
|
|
662
|
-
}
|
|
663
|
-
case 'help':
|
|
664
|
-
showHelp();
|
|
665
|
-
break;
|
|
666
|
-
default:
|
|
667
|
-
console.error(c('red', `Unknown command: ${cmd}`));
|
|
668
|
-
showHelp();
|
|
669
|
-
process.exitCode = 1;
|
|
670
|
-
}
|
|
671
|
-
} catch (err: any) {
|
|
672
|
-
console.error('\n' + c('red', err instanceof Error ? err.stack || err.message : String(err)));
|
|
673
|
-
process.exit(1);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
main();
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
console.log('Happy developing ✨')
|
package/src/version.ts
DELETED
package/tsconfig.json
DELETED