index.ts•2.71 kB
#!/usr/bin/env node
/**
* Google Drive MCP Server
* Entry point for the Model Context Protocol server
*/
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { GoogleDriveMCPServer } from './mcp/index.js';
import { ConfigManager } from './config/config-manager.js';
import { AuthService } from './auth/auth-service.js';
import { serverConfigToMCPConfig } from './config/config-adapter.js';
import { registerCleanupHandler } from './utils/process-cleanup.js';
let server: GoogleDriveMCPServer | null = null;
let isShuttingDown = false;
/**
* Main entry point for the MCP server
*/
async function main(): Promise<void> {
try {
// Load configuration
const configManager = new ConfigManager();
const config = await configManager.loadConfig();
// Initialize authentication
const authService = new AuthService();
await authService.initialize();
const status = await authService.validateAuthentication();
if (!status.isValid) {
console.error('Authentication failed:', status.error);
if (status.needsSetup) {
console.error('Run "google-drive-mcp auth setup" to configure authentication');
} else if (status.needsReauth) {
console.error('Run "google-drive-mcp auth refresh" to refresh tokens');
}
process.exit(1);
}
// Create and initialize the server
const mcpConfig = serverConfigToMCPConfig(config);
server = new GoogleDriveMCPServer(mcpConfig);
await server.start();
// Register cleanup handler for the server
registerCleanupHandler('mcp-server', async () => {
if (server) {
await server.stop();
server = null;
}
}, 10); // High priority
// Connect to stdio transport for MCP communication
const transport = new StdioServerTransport();
await server.getServer().connect(transport);
console.error('Google Drive MCP Server started successfully'); // Use stderr for logging
// Keep the process alive
await waitForShutdown();
} catch (error) {
console.error('Failed to start Google Drive MCP Server:', error);
process.exit(1);
}
}
/**
* Wait for shutdown signal
*/
async function waitForShutdown(): Promise<void> {
return new Promise((resolve) => {
const checkShutdown = () => {
if (isShuttingDown) {
resolve();
} else {
setTimeout(checkShutdown, 100);
}
};
checkShutdown();
});
}
// Start the server if this file is run directly
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch((error) => {
console.error('Fatal error:', error);
process.exit(1);
});
}
export { GoogleDriveMCPServer };