11#!/usr/bin/env node
22import { execa } from "execa" ;
33import { readFile , writeFile } from "fs/promises" ;
4- import { join } from "path" ;
5-
6- process . stderr . write ( "MCP Build Script Starting...\n" ) ;
4+ import { join , dirname } from "path" ;
5+ import { findUp } from 'find-up' ;
76
87export async function buildFramework ( ) {
8+ process . stderr . write ( "MCP Build Script Starting...\n" ) ;
99 process . stderr . write ( "Finding project root...\n" ) ;
1010
1111 const startDir = process . cwd ( ) ;
1212 process . stderr . write ( `Starting search from: ${ startDir } \n` ) ;
1313
14- if ( process . argv . includes ( 'create' ) ) {
15- process . stderr . write ( `Skipping build for create command\n` ) ;
16- return ;
14+ const skipValidation = process . env . MCP_SKIP_VALIDATION === 'true' ;
15+ if ( skipValidation ) {
16+ process . stderr . write ( `Skipping dependency validation\n` ) ;
1717 }
1818
1919 try {
20- const pkgPath = join ( startDir , 'package.json' ) ;
21- const pkgContent = await readFile ( pkgPath , 'utf8' ) ;
22- const pkg = JSON . parse ( pkgContent ) ;
20+ const pkgPath = await findUp ( 'package.json' ) ;
21+ if ( ! pkgPath ) {
22+ throw new Error ( "Could not find package.json in current directory or any parent directories" ) ;
23+ }
2324
24- if ( ! pkg . dependencies ?. [ "mcp-framework" ] ) {
25- throw new Error ( "This directory is not an MCP project (mcp-framework not found in dependencies)" ) ;
25+ const projectRoot = dirname ( pkgPath ) ;
26+
27+ if ( ! skipValidation ) {
28+ const pkgContent = await readFile ( pkgPath , 'utf8' ) ;
29+ const pkg = JSON . parse ( pkgContent ) ;
30+
31+ if ( ! pkg . dependencies ?. [ "mcp-framework" ] ) {
32+ throw new Error ( "This directory is not an MCP project (mcp-framework not found in dependencies)" ) ;
33+ }
2634 }
2735
28- process . stderr . write ( `Running tsc in ${ startDir } \n` ) ;
36+ process . stderr . write ( `Running tsc in ${ projectRoot } \n` ) ;
2937
3038 const tscCommand = process . platform === 'win32' ? [ 'npx.cmd' , 'tsc' ] : [ 'npx' , 'tsc' ] ;
3139
3240 await execa ( tscCommand [ 0 ] , [ tscCommand [ 1 ] ] , {
33- cwd : startDir ,
41+ cwd : projectRoot ,
3442 stdio : "inherit" ,
3543 env : {
3644 ...process . env ,
@@ -39,7 +47,7 @@ export async function buildFramework() {
3947 }
4048 } ) ;
4149
42- const distPath = join ( startDir , "dist" ) ;
50+ const distPath = join ( projectRoot , "dist" ) ;
4351 const projectIndexPath = join ( distPath , "index.js" ) ;
4452 const shebang = "#!/usr/bin/env node\n" ;
4553
@@ -61,12 +69,4 @@ export async function buildFramework() {
6169 }
6270}
6371
64- if ( import . meta. url . startsWith ( 'file:' ) ) {
65- process . stderr . write ( "Script running as main module\n" ) ;
66- buildFramework ( ) . catch ( error => {
67- process . stderr . write ( `Fatal error: ${ error instanceof Error ? error . message : String ( error ) } \n` ) ;
68- process . exit ( 1 ) ;
69- } ) ;
70- }
71-
7272export default buildFramework ;
0 commit comments