adoptClient method

  1. @override
Future<KernelClientConnection> adoptClient({
  1. required String id,
  2. required Client client,
})
override

Adopt a client the HOST already holds, under id.

One device, one connection. A host that opens a device for its own screens and then also names it as a composition origin must not dial it twice: many embedded boards serve a single peer, so the second dial is refused and the user sees a device that "sometimes will not open". Even where a second link is allowed it splits subscriptions and health tracking across two links to the same device.

The adopted connection does NOT own client — closing it deregisters only. Whoever opened the client keeps the right to end it, which for a user-facing host is an explicit action (an app's Close), not a side effect of leaving a screen.

Implementation

@override
Future<KernelClientConnection> adoptClient({
  required String id,
  required cli.Client client,
}) async {
  final existing = _connections[id];
  if (existing != null && existing.isConnected) return existing;
  final conn = _McpClientConnection(id: id, client: client, owned: false);
  _connections[id] = conn;
  return conn;
}