Introduce the "--generate-lib" to make it possible to generate a library instead of running the "main" method.
This commit is contained in:
parent
3e16df2630
commit
2ac9124267
@ -82,6 +82,25 @@ export function generateMcpServerCode(
|
||||
break;
|
||||
}
|
||||
|
||||
const mainDeclaration= options.generateLib === true ? '' : `
|
||||
/**
|
||||
* Cleanup function for graceful shutdown
|
||||
*/
|
||||
async function cleanup() {
|
||||
console.error("Shutting down MCP server...");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Register signal handlers
|
||||
process.on('SIGINT', cleanup);
|
||||
process.on('SIGTERM', cleanup);
|
||||
|
||||
// Start the server
|
||||
main().catch((error) => {
|
||||
console.error("Fatal error in main execution:", error);
|
||||
process.exit(1);
|
||||
});`;
|
||||
|
||||
// Generate the full server code
|
||||
return `#!/usr/bin/env node
|
||||
/**
|
||||
@ -160,27 +179,11 @@ ${executeApiToolFunctionCode}
|
||||
/**
|
||||
* Main function to start the server
|
||||
*/
|
||||
async function main() {
|
||||
${options.generateLib === true ? 'export ' : ''}async function main() {
|
||||
${transportCode}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup function for graceful shutdown
|
||||
*/
|
||||
async function cleanup() {
|
||||
console.error("Shutting down MCP server...");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// Register signal handlers
|
||||
process.on('SIGINT', cleanup);
|
||||
process.on('SIGTERM', cleanup);
|
||||
|
||||
// Start the server
|
||||
main().catch((error) => {
|
||||
console.error("Fatal error in main execution:", error);
|
||||
process.exit(1);
|
||||
});
|
||||
${mainDeclaration}
|
||||
|
||||
/**
|
||||
* Formats API errors for better readability
|
||||
|
||||
@ -73,6 +73,11 @@ program
|
||||
'Port for web or streamable-http transport (default: 3000)',
|
||||
(val) => parseInt(val, 10)
|
||||
)
|
||||
.option(
|
||||
'-l, --generate-lib <number>',
|
||||
'Indicates whether a library should be generated and hence the \'main\' method not invoked (default: false)',
|
||||
(val) => val === 'true'
|
||||
)
|
||||
.option(
|
||||
'--default-include <boolean>',
|
||||
'Default behavior for x-mcp filtering (true|false, case-insensitive). Default: true (include by default), false = exclude by default',
|
||||
|
||||
@ -29,6 +29,8 @@ export interface CliOptions {
|
||||
transport?: TransportType;
|
||||
/** Server port (for web and streamable-http transports) */
|
||||
port?: number;
|
||||
/** Whether the generated code should invoke the main method or expose the main method as a library */
|
||||
generateLib?: boolean;
|
||||
/**
|
||||
* Default behavior for x-mcp filtering.
|
||||
* true (default) = include by default when x-mcp is missing or invalid;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user