@aashari/boilerplate-mcp-server
Version:
TypeScript Model Context Protocol (MCP) server boilerplate providing IP lookup tools/resources. Includes CLI support and extensible structure for connecting AI systems (LLMs) to external data sources like ip-api.com. Ideal template for creating new MCP in
117 lines (116 loc) • 5.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.startServer = startServer;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const logger_util_js_1 = require("./utils/logger.util.js");
const config_util_js_1 = require("./utils/config.util.js");
const error_util_js_1 = require("./utils/error.util.js");
const constants_util_js_1 = require("./utils/constants.util.js");
const index_js_1 = require("./cli/index.js");
// Import tools
const ipaddress_tool_js_1 = __importDefault(require("./tools/ipaddress.tool.js"));
// Import resources
const ipaddress_resource_js_1 = __importDefault(require("./resources/ipaddress.resource.js"));
/**
* Boilerplate MCP Server
*
* A template project for building MCP servers that follow best practices.
* Demonstrates proper structure, logging, error handling, and MCP protocol integration.
*/
// Create file-level logger
const indexLogger = logger_util_js_1.Logger.forContext('index.ts');
// Log initialization at debug level
indexLogger.debug('Boilerplate MCP server module loaded');
let serverInstance = null;
let transportInstance = null;
/**
* Start the MCP server with the specified transport mode
*
* @param mode The transport mode to use (stdio or sse)
* @returns Promise that resolves to the server instance when started successfully
*/
async function startServer(mode = 'stdio') {
const serverLogger = logger_util_js_1.Logger.forContext('index.ts', 'startServer');
// Load configuration
serverLogger.info('Starting MCP server initialization...');
config_util_js_1.config.load();
serverLogger.info('Configuration loaded successfully');
// Enable debug logging if DEBUG is set to true
if (config_util_js_1.config.getBoolean('DEBUG')) {
serverLogger.debug('Debug mode enabled');
}
// Log the DEBUG value to verify configuration loading
serverLogger.debug(`DEBUG environment variable: ${process.env.DEBUG}`);
serverLogger.debug(`IPAPI_API_TOKEN value exists: ${Boolean(process.env.IPAPI_API_TOKEN)}`);
serverLogger.debug(`Config DEBUG value: ${config_util_js_1.config.get('DEBUG')}`);
serverLogger.info(`Initializing Boilerplate MCP server v${constants_util_js_1.VERSION}`);
serverInstance = new mcp_js_1.McpServer({
name: constants_util_js_1.PACKAGE_NAME,
version: constants_util_js_1.VERSION,
});
if (mode === 'stdio') {
serverLogger.info('Using STDIO transport for MCP communication');
transportInstance = new stdio_js_1.StdioServerTransport();
}
else {
throw (0, error_util_js_1.createUnexpectedError)('SSE mode is not supported yet');
}
// Register tools
serverLogger.info('Registering MCP tools...');
ipaddress_tool_js_1.default.registerTools(serverInstance);
serverLogger.debug('Registered IP address tools');
// Register resources
serverLogger.info('Registering MCP resources...');
ipaddress_resource_js_1.default.registerResources(serverInstance);
serverLogger.debug('Registered IP address resources');
serverLogger.info('All tools and resources registered successfully');
try {
serverLogger.info(`Connecting to ${mode.toUpperCase()} transport...`);
await serverInstance.connect(transportInstance);
serverLogger.info('MCP server started successfully and ready to process requests');
return serverInstance;
}
catch (err) {
serverLogger.error(`Failed to start server`, err);
process.exit(1);
}
}
/**
* Main entry point - this will run when executed directly
* Determines whether to run in CLI or server mode based on command-line arguments
*/
async function main() {
const mainLogger = logger_util_js_1.Logger.forContext('index.ts', 'main');
// Load configuration
config_util_js_1.config.load();
// Log the DEBUG value to verify configuration loading
mainLogger.debug(`DEBUG environment variable: ${process.env.DEBUG}`);
mainLogger.debug(`IPAPI_API_TOKEN value exists: ${Boolean(process.env.IPAPI_API_TOKEN)}`);
mainLogger.debug(`Config DEBUG value: ${config_util_js_1.config.get('DEBUG')}`);
// Check if arguments are provided (CLI mode)
if (process.argv.length > 2) {
// CLI mode: Pass arguments to CLI runner
mainLogger.info('Starting in CLI mode');
await (0, index_js_1.runCli)(process.argv.slice(2));
mainLogger.info('CLI execution completed');
}
else {
// MCP Server mode: Start server with default STDIO
mainLogger.info('Starting in server mode');
await startServer();
mainLogger.info('Server is now running');
}
}
// If this file is being executed directly (not imported), run the main function
if (require.main === module) {
main().catch((err) => {
const indexLogger = logger_util_js_1.Logger.forContext('index.ts'); // Re-create logger for catch
indexLogger.error('Unhandled error in main process', err);
process.exit(1);
});
}