sendLog method

void sendLog(
  1. McpLogLevel level,
  2. String message, {
  3. String? logger,
  4. dynamic data,
})

Send a logging notification to the client

Implementation

void sendLog(McpLogLevel level, String message, {String? logger, dynamic data}) {
  if (!isConnected) return;

  // Convert level enum to string as per MCP 2025-03-26 specification
  final levelString = switch (level) {
    McpLogLevel.debug => 'debug',
    McpLogLevel.info => 'info',
    McpLogLevel.notice => 'notice',
    McpLogLevel.warning => 'warning',
    McpLogLevel.error => 'error',
    McpLogLevel.critical => 'critical',
    McpLogLevel.alert => 'alert',
    McpLogLevel.emergency => 'emergency',
  };

  final params = <String, dynamic>{
    'level': levelString,
  };

  if (logger != null) {
    params['logger'] = logger;
  }

  // According to MCP 2025-03-26 spec, message goes in data object
  final logData = <String, dynamic>{
    'message': message,
  };

  if (data != null) {
    // Merge additional data if provided
    if (data is Map<String, dynamic>) {
      logData.addAll(data);
    } else {
      logData['additional'] = data;
    }
  }

  params['data'] = logData;

  // Use correct method name as per MCP 2025-03-26 specification
  _broadcastNotification('notifications/message', params);
}