farion1231/cc-switch · warning · Error

TOML 内容不能为空

Error message

TOML 内容不能为空

What it means

tomlToMcpServer rejects empty or whitespace-only input before any parsing ('TOML content cannot be empty'). It is the first guard on the MCP server import path that converts pasted TOML text into an McpServerSpec.

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 a2e22f3302)

Solutions

  1. Guard with trim() and show a 'paste a config first' hint instead of calling
  2. Disable the import button while the input is blank
  3. Skip the conversion entirely for empty strings

Example fix

// before
const server = tomlToMcpServer(text); // throws on empty input

// after
if (!text.trim()) {
  setImportError("Paste an MCP server TOML config first");
  return;
}
const server = tomlToMcpServer(text);
Defensive patterns

Strategy: validation

Validate before calling

if (!tomlText.trim()) {
  setImportError("Paste an MCP server TOML config first");
  return;
}
const server = tomlToMcpServer(tomlText);

Try / catch

try {
  const server = tomlToMcpServer(tomlText);
} catch (e) {
  if (e instanceof Error && e.message === "TOML 内容不能为空") {
    setImportError("Paste a config first");
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: tomlToMcpServer('') or tomlToMcpServer('\n\t') - importing from an empty textarea, or a clipboard/file read that returned nothing.

Common situations: User clicks import before pasting; the selected file is empty; an upstream copy step failed silently and produced an empty string.

Related errors


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