feat(mcp): add automatic key normalization for server entries

- Add normalize_server_keys() to ensure MCP server map keys match internal id fields
- Auto-normalize on all read/write operations (get, upsert, delete, import, sync)
- Handle edge cases: empty/whitespace ids, key renaming, conflict resolution
- Auto-save config when normalization detects changes
- Apply cargo fmt for code formatting consistency

This enhancement improves data integrity by automatically fixing inconsistencies
between server entry keys and their id fields, especially after manual config edits.
This commit is contained in:
Jason
2025-10-12 16:21:32 +08:00
parent 036d41b774
commit e92d99b758
7 changed files with 222 additions and 69 deletions

View File

@@ -66,17 +66,21 @@ pub fn write_codex_live_atomic(auth: &Value, config_text_opt: Option<&str>) -> R
// 读取旧内容用于回滚
let old_auth = if auth_path.exists() {
Some(fs::read(&auth_path)
.map_err(|e| format!("读取旧 auth.json 失败: {}: {}", auth_path.display(), e))?)
} else {
None
};
let _old_config = if config_path.exists() {
Some(fs::read(&config_path)
.map_err(|e| format!("读取旧 config.toml 失败: {}: {}", config_path.display(), e))?)
Some(
fs::read(&auth_path)
.map_err(|e| format!("读取旧 auth.json 失败: {}: {}", auth_path.display(), e))?,
)
} else {
None
};
let _old_config =
if config_path.exists() {
Some(fs::read(&config_path).map_err(|e| {
format!("读取旧 config.toml 失败: {}: {}", config_path.display(), e)
})?)
} else {
None
};
// 准备写入内容
let cfg_text = match config_text_opt {