farion1231/cc-switch · error · Error

TOML 内容不能为空

Error message

TOML 内容不能为空

What it means

tomlToMcpServer converts pasted TOML into a single McpServerSpec and rejects blank input up front: if tomlText.trim() is empty it throws 'TOML 内容不能为空' ('TOML content cannot be empty') before any parsing. Every later step — parseToml, direct-config detection, [mcp_servers.<id>] and [mcp.servers.<id>] extraction — assumes at least one non-whitespace character.

Solutions

  1. Disable the import action while the TOML textarea is blank
  2. Trim and early-return with an inline message instead of relying on the throw
  3. Pre-check with the same trim() condition the function uses

Example fix

// before
const server = tomlToMcpServer(tomlText); // throws on ''

// after
if (!tomlText.trim()) {
  showImportError('Paste a TOML server config first');
  return;
}
const server = tomlToMcpServer(tomlText);
Defensive patterns

Strategy: validation

Validate before calling

if (!tomlText.trim()) {
  showImportError('Paste a TOML server config first');
  return;
}
const server = tomlToMcpServer(tomlText);

Try / catch

try {
  const server = tomlToMcpServer(tomlText);
} catch (error) {
  if (error instanceof Error && error.message === 'TOML 内容不能为空') {
    // keep the import dialog open with an inline hint
  } else {
    throw error;
  }
}

Prevention

When it happens

Trigger: The MCP import/paste dialog calls tomlToMcpServer('') or with whitespace-only text — e.g. submit fired before the paste happened, or the textarea was cleared.

Common situations: Import button enabled on an empty dialog; clipboard paste failed silently; user presses submit to 'see what happens'.

Related errors


AI-assisted analysis of farion1231/cc-switch@0b5da51016 (2026-08-20). Data as JSON: /api/errors/b1ccd7e89215d7df. Report an issue: GitHub.

Appendix: source

Thrown at src/utils/tomlUtils.ts:55

  }

  // stringify 默认会带换行,做一次 trim 以适配文本框展示
  return stringifyToml(obj).trim();
};

/**
 * 将 TOML 文本转换为 McpServerSpec 对象(单个服务器配置)
 * 支持两种格式:
 * 1. 直接的服务器配置(type, command, args 等)
 * 2. [mcp_servers.<id>] 格式(推荐,取第一个服务器)
 * 3. [mcp.servers.<id>] 错误格式(容错解析,同样取第一个服务器)
 * @param tomlText TOML 文本
 * @returns McpServer 对象
 * @throws 解析或转换失败时抛出错误
 */
export const tomlToMcpServer = (tomlText: string): McpServerSpec => {
  if (!tomlText.trim()) {
    throw new Error("TOML 内容不能为空");
  }

  const parsed = parseToml(normalizeTomlText(tomlText));

  // 情况 1: 直接是服务器配置(包含 type/command/url 等字段)
  if (
    parsed.type ||
    parsed.command ||
    parsed.url ||
    parsed.args ||
    parsed.env
  ) {
    return normalizeServerConfig(parsed);
  }

  // 情况 2: [mcp_servers.<id>] 格式(推荐)
  if (parsed.mcp_servers && typeof parsed.mcp_servers === "object") {
    const serverIds = Object.keys(parsed.mcp_servers);

View on GitHub (pinned to 0b5da51016)