start method

Future<void> start({
  1. String? apiKey,
})

Starts the MCP server

⚠️ Security Warning: Server exposes logs via HTTP. Consider providing apiKey for authentication.

Implementation

Future<void> start({String? apiKey}) async {
  if (_isRunning) {
    throw StateError('MCP Server is already running');
  }

  // Update API key if provided
  if (apiKey != null) {
    _apiKey = apiKey;
  }

  try {
    _server = await HttpServer.bind(_host, _port);
    _isRunning = true;

    if (_apiKey != null) {
      developer.log(
        '🚀 MCP Server started on $_host:$_port (with authentication)',
        name: 'MCPServer',
      );
    } else {
      developer.log(
        '⚠️ MCP Server started on $_host:$_port (NO AUTHENTICATION - Security Risk!)',
        name: 'MCPServer',
      );
      developer.log(
        '⚠️ Consider providing apiKey for production use',
        name: 'MCPServer',
      );
    }

    // Handle incoming requests
    _server!.listen(_handleRequest);

    // Set up log queue listener
    _setupLogQueueListener();
  } catch (e) {
    developer.log('❌ Failed to start MCP Server: $e', name: 'MCPServer');
    rethrow;
  }
}