Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
mobile: stop a second Start tap from killing the embedded MCP server
Tomas Tormo6e49e1515180
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dartindex a006218b..f5030e45 100644--- a/mobile/sam-node-app/lib/main.dart+++ b/mobile/sam-node-app/lib/main.dart@@ -102,6 +102,7 @@ class _NodeControlPageState extends State<NodeControlPage> {final _externalMcpDescController = TextEditingController();late SamDartMcpServer _embeddedMcpServer;+ bool _starting = false;int _selectedTab = 0; // 0 = Dashboard, 1 = Services@override@@ -627,13 +628,37 @@ class _NodeControlPageState extends State<NodeControlPage> {}Future<void> _start() async {+ // Backstop for the disabled button while a start is in flight.+ if (_starting) return;+ setState(() {+ _starting = true;+ });+ try {+ await _startNode();+ } finally {+ if (mounted) {+ setState(() {+ _starting = false;+ });+ }+ }+ }++ Future<void> _startNode() async {final appDir = await getApplicationDocumentsDirectory();final dataDir = '${appDir.path}/sam_data';// The embedded MCP backend must be listening before the node starts:// services are declared in the start configuration and probed at startup,// there is no runtime registration.- await _embeddedMcpServer.start(port: 9090);+ try {+ await _embeddedMcpServer.start(port: 9090);+ } catch (e) {+ setState(() {+ _status = 'Start failed: embedded MCP server: $e';+ });+ return;+ }final services = <Map<String, String>>[{@@ -1114,9 +1139,9 @@ class _NodeControlPageState extends State<NodeControlPage> {children: [Expanded(child: ElevatedButton.icon(- onPressed: isRunning ? null : _start,+ onPressed: (isRunning || _starting) ? null : _start,icon: const Icon(Icons.play_arrow),- label: const Text('Start'),+ label: Text(_starting ? 'Starting…' : 'Start'),style: ElevatedButton.styleFrom(backgroundColor: Colors.green,foregroundColor: Colors.white,diff --git a/mobile/sam-node-app/lib/mcp_server.dart b/mobile/sam-node-app/lib/mcp_server.dartindex fd8b32f8..a111e8d0 100644--- a/mobile/sam-node-app/lib/mcp_server.dart+++ b/mobile/sam-node-app/lib/mcp_server.dart@@ -21,30 +21,30 @@ class SamDartMcpServer {/// backs is declared in the node's start configuration; there is no/// runtime registration, so this server must be listening before the node/// starts and probes it.+ ///+ /// Throws if the port cannot be bound.Future<void> start({int port = 9090}) async {- try {- _server = await HttpServer.bind(InternetAddress.loopbackIPv4, port);- debugPrint('SAM Dart MCP Server listening on port $port');-- _server!.listen((HttpRequest request) async {- // Handle CORS if needed, but since it's loopback and called by Go, maybe not strict- if (request.method == 'GET') {- _handleSse(request);- } else if (request.method == 'POST') {- _handlePost(request);- } else {- request.response.statusCode = HttpStatus.methodNotAllowed;- await request.response.close();- }- });- } catch (e) {- debugPrint('Failed to start Dart MCP Server: $e');- }+ if (_server != null) throw StateError('already started');+ _server = await HttpServer.bind(InternetAddress.loopbackIPv4, port);+ debugPrint('SAM Dart MCP Server listening on port $port');++ _server!.listen((HttpRequest request) async {+ // Handle CORS if needed, but since it's loopback and called by Go, maybe not strict+ if (request.method == 'GET') {+ _handleSse(request);+ } else if (request.method == 'POST') {+ _handlePost(request);+ } else {+ request.response.statusCode = HttpStatus.methodNotAllowed;+ await request.response.close();+ }+ });}/// Stops the serverFuture<void> stop() async {await _server?.close(force: true);+ _server = null;_sseClients.clear();debugPrint('SAM Dart MCP Server stopped');}