11#!/usr/bin/env node
22
3- import { startServer } from '../build/index.js' ;
3+ import { fileURLToPath } from 'url' ;
4+ import { dirname , resolve } from 'path' ;
5+ import { spawn } from 'child_process' ;
6+ import { createRequire } from 'module' ;
47
8+ const __filename = fileURLToPath ( import . meta. url ) ;
9+ const __dirname = dirname ( __filename ) ;
10+ const require = createRequire ( import . meta. url ) ;
11+
12+ // Parse command line arguments
513const args = process . argv . slice ( 2 ) ;
6- const mode = args [ 0 ] ?. toLowerCase ( ) ;
7-
8- if ( mode === 'http' ) {
9- // If the HTTP server module exists, import and run it
10- import ( '../build/http-server.js' )
11- . catch ( error => {
12- console . error ( 'Error starting HTTP server:' , error ) ;
13- process . exit ( 1 ) ;
14- } ) ;
15- } else {
16- // Default to stdio server
17- startServer ( )
18- . catch ( error => {
19- console . error ( 'Error starting stdio server:' , error ) ;
20- process . exit ( 1 ) ;
21- } ) ;
14+ const httpMode = args . includes ( '--http' ) || args . includes ( '-h' ) ;
15+
16+ console . log ( `Starting Starknet MCP Server in ${ httpMode ? 'HTTP' : 'stdio' } mode...` ) ;
17+
18+ // Determine which file to execute
19+ const scriptPath = resolve ( __dirname , '../build' , httpMode ? 'http-server.js' : 'index.js' ) ;
20+
21+ try {
22+ // Check if the built files exist
23+ require . resolve ( scriptPath ) ;
24+
25+ // Execute the server
26+ const server = spawn ( 'node' , [ scriptPath ] , {
27+ stdio : 'inherit' ,
28+ shell : false
29+ } ) ;
30+
31+ server . on ( 'error' , ( err ) => {
32+ console . error ( 'Failed to start server:' , err ) ;
33+ process . exit ( 1 ) ;
34+ } ) ;
35+
36+ // Handle clean shutdown
37+ const cleanup = ( ) => {
38+ if ( ! server . killed ) {
39+ server . kill ( ) ;
40+ }
41+ } ;
42+
43+ process . on ( 'SIGINT' , cleanup ) ;
44+ process . on ( 'SIGTERM' , cleanup ) ;
45+ process . on ( 'exit' , cleanup ) ;
46+
47+ } catch ( error ) {
48+ console . error ( 'Error: Server files not found. The package may not be built correctly.' ) ;
49+ console . error ( 'Please try reinstalling the package or contact the maintainers.' ) ;
50+ console . error ( error ) ;
51+ process . exit ( 1 ) ;
2252}
0 commit comments