McpAddOptions.parse constructor

McpAddOptions.parse(
  1. String rawArgs
)

Parse from a raw argument string. Expected format mirrors the CLI: /mcp add [--scope <scope>] [--transport <t>] [-e KEY=val] [-H "Header: val"] [--client-id <id>] [--client-secret] [--callback-port <port>] [--xaa] <name> <command> [args...]

Implementation

factory McpAddOptions.parse(String rawArgs) {
  final tokens = _tokenize(rawArgs);
  String? scope;
  String? transport;
  final envVars = <String>[];
  final headers = <String>[];
  String? clientId;
  bool clientSecret = false;
  int? callbackPort;
  bool xaa = false;
  final positional = <String>[];

  var i = 0;
  while (i < tokens.length) {
    final tok = tokens[i];
    switch (tok) {
      case '-s':
      case '--scope':
        scope = (++i < tokens.length) ? tokens[i] : null;
        break;
      case '-t':
      case '--transport':
        transport = (++i < tokens.length) ? tokens[i] : null;
        break;
      case '-e':
      case '--env':
        if (++i < tokens.length) envVars.add(tokens[i]);
        break;
      case '-H':
      case '--header':
        if (++i < tokens.length) headers.add(tokens[i]);
        break;
      case '--client-id':
        clientId = (++i < tokens.length) ? tokens[i] : null;
        break;
      case '--client-secret':
        clientSecret = true;
        break;
      case '--callback-port':
        final raw = (++i < tokens.length) ? tokens[i] : null;
        callbackPort = raw != null ? int.tryParse(raw) : null;
        break;
      case '--xaa':
        xaa = true;
        break;
      case '--':
        // Everything after -- is positional
        i++;
        while (i < tokens.length) {
          positional.add(tokens[i++]);
        }
        continue;
      default:
        positional.add(tok);
    }
    i++;
  }

  if (positional.isEmpty) {
    throw ArgumentError(
      'Error: Server name is required.\n'
      'Usage: /mcp add <name> <command> [args...]',
    );
  }
  if (positional.length < 2) {
    throw ArgumentError(
      'Error: Command is required when server name is provided.\n'
      'Usage: /mcp add <name> <command> [args...]',
    );
  }

  return McpAddOptions(
    name: positional[0],
    commandOrUrl: positional[1],
    args: positional.length > 2 ? positional.sublist(2) : const [],
    scope: scope ?? 'local',
    transport: transport,
    envVars: envVars.isEmpty ? null : envVars,
    headers: headers.isEmpty ? null : headers,
    clientId: clientId,
    clientSecret: clientSecret,
    callbackPort: callbackPort,
    xaa: xaa,
  );
}