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

@@ -118,11 +118,10 @@ pub fn read_json_file<T: for<'a> Deserialize<'a>>(path: &Path) -> Result<T, Stri
return Err(format!("文件不存在: {}", path.display()));
}
let content = fs::read_to_string(path)
.map_err(|e| format!("读取文件失败: {}: {}", path.display(), e))?;
let content =
fs::read_to_string(path).map_err(|e| format!("读取文件失败: {}: {}", path.display(), e))?;
serde_json::from_str(&content)
.map_err(|e| format!("解析 JSON 失败: {}: {}", path.display(), e))
serde_json::from_str(&content).map_err(|e| format!("解析 JSON 失败: {}: {}", path.display(), e))
}
/// 写入 JSON 配置文件
@@ -192,14 +191,26 @@ pub fn atomic_write(path: &Path, data: &[u8]) -> Result<(), String> {
if path.exists() {
let _ = fs::remove_file(path);
}
fs::rename(&tmp, path)
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
fs::rename(&tmp, path).map_err(|e| {
format!(
"原子替换失败: {} -> {}: {}",
tmp.display(),
path.display(),
e
)
})?;
}
#[cfg(not(windows))]
{
fs::rename(&tmp, path)
.map_err(|e| format!("原子替换失败: {} -> {}: {}", tmp.display(), path.display(), e))?;
fs::rename(&tmp, path).map_err(|e| {
format!(
"原子替换失败: {} -> {}: {}",
tmp.display(),
path.display(),
e
)
})?;
}
Ok(())
}