UNPKG

@siva-sub/mcp-public-transport

Version:

A Model Context Protocol server for Singapore transport data with real-time information and routing

135 lines (127 loc) 4.62 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SingaporeTransportServer = void 0; const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js"); const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js"); const server_js_1 = require("./server.js"); Object.defineProperty(exports, "SingaporeTransportServer", { enumerable: true, get: function () { return server_js_1.SingaporeTransportServer; } }); const environment_js_1 = require("./config/environment.js"); const logger_js_1 = require("./utils/logger.js"); const PACKAGE_VERSION = '1.0.0'; async function main() { try { // Load and validate environment configuration const config = (0, environment_js_1.loadEnvironment)(); logger_js_1.logger.info('Starting Singapore Transport MCP Server', { version: PACKAGE_VERSION, logLevel: config.logLevel, cacheDuration: config.cacheDuration, }); // Create MCP server instance const server = new index_js_1.Server({ name: 'singapore-transport-mcp', version: PACKAGE_VERSION, }, { capabilities: { tools: {}, }, }); // Initialize Singapore Transport server with tools const transportServer = new server_js_1.SingaporeTransportServer(config); await transportServer.setupTools(server); // Perform health check const healthStatus = await transportServer.healthCheck(); logger_js_1.logger.info('Health check completed', healthStatus); // Create stdio transport and connect const transport = new stdio_js_1.StdioServerTransport(); await server.connect(transport); logger_js_1.logger.info('Singapore Transport MCP Server started successfully'); // Keep the process alive process.stdin.resume(); } catch (error) { logger_js_1.logger.error('Failed to start Singapore Transport MCP Server', error); process.exit(1); } } // Handle graceful shutdown function setupGracefulShutdown() { const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT']; signals.forEach((signal) => { process.on(signal, () => { logger_js_1.logger.info(`Received ${signal}, shutting down gracefully`); process.exit(0); }); }); process.on('uncaughtException', (error) => { logger_js_1.logger.error('Uncaught exception', error); process.exit(1); }); process.on('unhandledRejection', (reason, promise) => { logger_js_1.logger.error('Unhandled rejection', { reason, promise }); process.exit(1); }); } // CLI argument handling function handleCliArgs() { const args = process.argv.slice(2); if (args.includes('--version') || args.includes('-v')) { console.log(`Singapore Transport MCP v${PACKAGE_VERSION}`); process.exit(0); } if (args.includes('--help') || args.includes('-h')) { console.log(` Singapore Transport MCP Server v${PACKAGE_VERSION} A Model Context Protocol server for Singapore's transport system. Usage: singapore-transport-mcp [options] npx @siva-sub/mcp-public-transport [options] Options: -h, --help Show this help message -v, --version Show version number Environment Variables: LTA_ACCOUNT_KEY LTA DataMall API key (required) ONEMAP_TOKEN OneMap API token (optional) CACHE_DURATION Cache duration in seconds (default: 300) LOG_LEVEL Logging level (default: info) MAX_WALK_DISTANCE Max walking distance in meters (default: 1000) Examples: # Start the server singapore-transport-mcp # Use with Claude Desktop (add to claude_desktop_config.json): { "mcpServers": { "singapore-transport": { "command": "npx", "args": ["-y", "@siva-sub/mcp-public-transport"], "env": { "LTA_ACCOUNT_KEY": "your_api_key_here" } } } } For more information: https://github.com/siva-sub/MCP-Public-Transport `); process.exit(0); } } // Main execution - check if this file is being run directly function isMainModule() { // For CommonJS try { return require.main === module; } catch { // For ES modules - fallback return true; // Assume main module for ES builds } } if (isMainModule()) { handleCliArgs(); setupGracefulShutdown(); main().catch((error) => { logger_js_1.logger.error('Fatal error during startup', error); process.exit(1); }); }