BigPizzaV3/CodexPlusPlus · error · anyhow::Error

请先粘贴 MCP 配置 JSON

Error message

请先粘贴 MCP 配置 JSON

What it means

parse_mcp_servers_json rejects empty input before attempting to parse JSON. Pasting nothing (or only whitespace) means there is nothing to import, so the function fails fast with this user-facing message instead of a confusing JSON syntax error.

Solutions

  1. Paste a valid MCP servers JSON document before calling import.
  2. Guard the call site: if input.trim().is_empty(), show the prompt/error without calling the parser.
  3. If the JSON is read from a file, check the file is non-empty before reading it into the argument.

Example fix

// before
parse_mcp_servers_json(userInput)
// after
if (userInput.trim().length === 0) { showError("请先粘贴 MCP 配置 JSON"); return; }
parse_mcp_servers_json(userInput);
Defensive patterns

Strategy: validation

Validate before calling

if (!jsonText || jsonText.trim().length === 0) {
  throw new Error("请先粘贴 MCP 配置 JSON");
}

Try / catch

match parse_mcp_servers_json(&input) {
    Ok(import) => apply(import),
    Err(e) => ui.show_import_error(&e.to_string()), // shows '请先粘贴 MCP 配置 JSON'
}

Prevention

When it happens

Trigger: Calling parse_mcp_servers_json("") or with a whitespace-only string (" \n\t").

Common situations: User clicks Import in the manager UI without pasting config; clipboard copy failed silently; a script passes an unset/empty variable holding the JSON.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/ac70e2b4e829813b. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-core/src/mcp_config.rs:261

        }
    }

    let text = doc.to_string();
    Ok(if text.trim().is_empty() {
        String::new()
    } else {
        ensure_trailing_newline(text)
    })
}

/// 解析社区文档里常见的 MCP JSON。
///
/// 接受四种外层形状:`{"mcpServers":{…}}`(Claude 标准)、`{"servers":{…}}`
/// (VS Code)、裸的 `{id: cfg}`,以及单个 server 对象(此时 id 留空由调用方填)。
pub fn parse_mcp_servers_json(json: &str) -> anyhow::Result<McpJsonImport> {
    let trimmed = json.trim();
    if trimmed.is_empty() {
        anyhow::bail!("请先粘贴 MCP 配置 JSON");
    }
    let root: Value = serde_json::from_str(trimmed).context("不是合法的 JSON")?;
    let Value::Object(root) = root else {
        anyhow::bail!("JSON 顶层必须是对象");
    };

    let servers = if let Some(Value::Object(map)) = root.get("mcpServers") {
        map.clone()
    } else if let Some(Value::Object(map)) = root.get("servers") {
        map.clone()
    } else if looks_like_single_server(&root) {
        // 单个 server 对象,没有 id,用占位 id 让用户在界面上改
        let mut map = Map::new();
        map.insert("mcp-server".to_string(), Value::Object(root));
        map
    } else {
        root
    };

View on GitHub (pinned to b1ed92e5e4)