#!/usr/bin/env node

/**
 * MCP Server Entry Point
 * Runs the MCP server in standalone mode via stdio
 * This can be added to LM Studio, Claude Desktop, or other MCP clients
 *
 * The MCP server will automatically start the Electron app if it's not already running.
 */

const McpServer = require('./src/services/McpServer');

let mcpServer = null;

async function startMcpServer() {
  try {
    // Create and start MCP server
    // The MCP server will auto-start the Electron app if needed
    mcpServer = new McpServer();
    await mcpServer.start();
  } catch (error) {
    console.error('Failed to start MCP server:', error);
    process.exit(1);
  }
}

async function cleanup() {
  if (mcpServer) {
    await mcpServer.stop();
  }
}

// Start MCP server
startMcpServer().catch((error) => {
  console.error('MCP server startup error:', error);
  process.exit(1);
});

// Handle cleanup
process.on('SIGINT', async () => {
  await cleanup();
  process.exit(0);
});

process.on('SIGTERM', async () => {
  await cleanup();
  process.exit(0);
});

process.on('exit', async () => {
  await cleanup();
});
