start static method
Starts the HTTP server and configures platform connectivity.
If already running, does nothing. Logs available endpoints and how to configure TESTBRIDGE_URL.
Implementation
static Future<void> start({
int? customPort,
String? customHost,
int? customForwardPort,
}) async {
Log.init(level: Level.SEVERE);
// Production guard: the server must not run in release builds.
// In release, inspection mechanisms (debugLayer, visitChildElements
// for debug info) are not available or return incomplete data.
if (!kDebugMode && !kProfileMode) {
Log.i(
'[Server] ⚠️ McpEventServer must not run in production. Aborting.',
);
return;
}
if (_isRunning) {
Log.i('[Server] Already running at http://$host:$port');
return;
}
if (customPort != null) port = customPort;
if (customHost != null) host = customHost;
if (customForwardPort != null) forwardPort = customForwardPort;
try {
_server = await HttpServer.bind(host, port, shared: true);
_isRunning = true;
// Register lifecycle observer — auto-stops when the app closes
WidgetsBinding.instance.addObserver(_observer);
// Configure platform connectivity (ADB forward, iproxy, etc.)
final connectivity = await McpConnectivity.setup(
appPort: port,
forwardPort: forwardPort,
);
_logStartup(connectivity);
_server!.listen(
_handleRequest,
onError: (e) {
Log.i('[Server] Error: $e');
},
);
} catch (e) {
Log.i('[Server] ❌ Failed to start: $e');
_isRunning = false;
rethrow;
}
}