#!/usr/bin/env node
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { config, validateConfig } from './config/environment.js';
import { BusinessMapMcpServer } from './server/mcp-server.js';
async function main() {
try {
// Validate configuration
validateConfig();
// Create and setup the MCP server
const businessMapServer = new BusinessMapMcpServer();
console.error(`π Starting ${config.server.name} v${config.server.version}`);
console.error(`π‘ BusinessMap API: ${config.businessMap.apiUrl}`);
console.error(`π Read-only mode: ${config.businessMap.readOnlyMode ? 'enabled' : 'disabled'}`);
// Initialize BusinessMap client with retry logic
console.error('π Initializing connection to BusinessMap API...');
let retryCount = 0;
const maxRetries = 3;
const retryDelay = 2000; // 2 seconds
while (retryCount < maxRetries) {
try {
await businessMapServer.initialize();
console.error('β
Successfully connected to BusinessMap API');
break;
} catch (error) {
retryCount++;
const message = error instanceof Error ? error.message : 'Unknown error';
if (retryCount < maxRetries) {
console.error(`β οΈ Connection attempt ${retryCount} failed: ${message}`);
console.error(
`π Retrying in ${retryDelay / 1000} seconds... (${retryCount}/${maxRetries})`
);
await new Promise((resolve) => setTimeout(resolve, retryDelay));
} else {
console.error(
`β Failed to connect to BusinessMap API after ${maxRetries} attempts: ${message}`
);
console.error('π‘ Please check your API URL and token configuration');
throw error;
}
}
}
// Setup transport
const transport = new StdioServerTransport();
// Connect server to transport
await businessMapServer.server.connect(transport);
console.error('β
BusinessMap MCP Server is running');
console.error('π‘ Use Ctrl+C to stop the server');
} catch (error) {
console.error('β Failed to start BusinessMap MCP Server:', error);
process.exit(1);
}
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.error('\nπ Shutting down BusinessMap MCP Server...');
process.exit(0);
});
process.on('SIGTERM', () => {
console.error('\nπ Shutting down BusinessMap MCP Server...');
process.exit(0);
});
// Start the server
main().catch((error) => {
console.error('π₯ Unhandled error:', error);
process.exit(1);
});