Skip to content

Commit c810436

Browse files
committed
fix: fixed binary to be executable with npx
1 parent 19921b3 commit c810436

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

bin/cli.js

+47-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
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
513
const 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
}

src/index.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
2-
import startServerImpl from "./server/server.js";
2+
import startServer from "./server/server.js";
33

4-
export const startServer = async () => {
4+
// Start the server
5+
async function main() {
56
try {
6-
const server = await startServerImpl();
7+
const server = await startServer();
78
const transport = new StdioServerTransport();
89
await server.connect(transport);
910
console.error("Starknet MCP Server running on stdio");
10-
return server;
1111
} catch (error) {
1212
console.error("Error starting Starknet MCP server:", error);
1313
process.exit(1);
1414
}
15-
};
15+
}
1616

17-
// Start the server when this file is run directly
18-
if (import.meta.url === `file://${process.argv[1]}`) {
19-
startServer().catch((error) => {
20-
console.error("Fatal error in main():", error);
21-
process.exit(1);
22-
});
23-
}
17+
main().catch((error) => {
18+
console.error("Fatal error in main():", error);
19+
process.exit(1);
20+
});

0 commit comments

Comments
 (0)