register method

void register({
  1. required Client client,
  2. required MCPUIRuntime runtime,
  3. String? serverId,
  4. McpLogMessageHandler? onMcpLogMessage,
})

FR-RES-005 + MCP logging spec.

onMcpLogMessage receives the raw MCP notifications/message payload ({level, logger?, data} per spec). The router itself does not interpret level; mapping into the host's log model is the caller's responsibility.

Implementation

void register({
  required Client client,
  required MCPUIRuntime runtime,
  String? serverId,
  McpLogMessageHandler? onMcpLogMessage,
}) {
  _logger.debug('Registering notification handlers',
      {'serverId': serverId});

  client.onNotification('notifications/resources/updated',
      (params) async {
    try {
      _logger.debug('Resource update notification', {'params': params});
      if (!runtime.isInitialized) return;

      await runtime.handleNotification(
        {
          'method': 'notifications/resources/updated',
          'params': params,
        },
        resourceReader: (uri) async {
          final res = await client.readResource(uri);
          if (res.contents.isEmpty) return '{}';
          return res.contents.first.text ?? '{}';
        },
      );
    } catch (e, st) {
      _logger.logError('handleNotification failed', e, st);
    }
  });

  if (onMcpLogMessage != null && serverId != null) {
    client.onNotification('notifications/message', (params) async {
      try {
        onMcpLogMessage(serverId, params);
      } catch (e, st) {
        _logger.logError(
            'mcp log notification handler failed', e, st, {'serverId': serverId});
      }
    });
  }
}