parseToolsSpec function

ToolsConfig parseToolsSpec(
  1. String csv
)

Parses the --tools flag / FA_TOOLS env CSV spec: 'web_search=off,dap=off,mcp:my-server=on'.

Values accept on/off/true/false, case-insensitively; tokens are trimmed. An empty string yields an empty config. A malformed token throws ConfigException naming it.

Implementation

ToolsConfig parseToolsSpec(String csv) {
  final trimmed = csv.trim();
  if (trimmed.isEmpty) return const ToolsConfig();
  final tools = <String, bool>{};
  for (final raw in trimmed.split(',')) {
    final token = raw.trim();
    if (token.isEmpty) continue;
    final separator = token.indexOf('=');
    final id = separator < 0 ? '' : token.substring(0, separator).trim();
    final value = separator < 0
        ? ''
        : token.substring(separator + 1).trim().toLowerCase();
    final enabled = switch (value) {
      'on' || 'true' => true,
      'off' || 'false' => false,
      _ => null,
    };
    if (id.isEmpty || enabled == null) {
      throw ConfigException('invalid tools spec token: "$token"');
    }
    tools[id] = enabled;
  }
  return ToolsConfig(tools: tools);
}