mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-26 23:56:02 +08:00
2f18d6ec00
This commit finalizes the v3.7.0 unified MCP architecture migration by removing all deprecated code paths and eliminating compiler warnings. Frontend Changes (~950 lines removed): - Remove deprecated components: McpPanel, McpListItem, McpToggle - Remove deprecated hook: useMcpActions - Remove unused API methods: importFrom*, syncEnabledTo*, syncAllServers - Simplify McpFormModal by removing dual-mode logic (unified/legacy) - Remove syncOtherSide checkbox and conflict detection - Clean up unused imports and state variables - Delete associated test files Backend Changes (~400 lines cleaned): - Remove unused Tauri commands: import_mcp_from_*, sync_enabled_mcp_to_* - Delete unused Gemini MCP functions: get_mcp_status, upsert/delete_mcp_server - Add #[allow(deprecated)] to compatibility layer commands - Add #[allow(dead_code)] to legacy helper functions for future migration - Simplify boolean expression in mcp.rs per Clippy suggestion API Deprecation: - Mark legacy APIs with @deprecated JSDoc (getConfig, upsertServerInConfig, etc.) - Preserve backward compatibility for v3.x, planned removal in v4.0 Verification: - ✅ Zero TypeScript errors (pnpm typecheck) - ✅ Zero Clippy warnings (cargo clippy) - ✅ All code formatted (prettier + cargo fmt) - ✅ Builds successfully Total cleanup: ~1,350 lines of code removed/marked Breaking changes: None (all legacy APIs still functional)
124 lines
3.8 KiB
Rust
124 lines
3.8 KiB
Rust
use serde::{Deserialize, Serialize};
|
||
use serde_json::{Map, Value};
|
||
use std::fs;
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use crate::config::atomic_write;
|
||
use crate::error::AppError;
|
||
use crate::gemini_config::get_gemini_settings_path;
|
||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub struct McpStatus {
|
||
pub user_config_path: String,
|
||
pub user_config_exists: bool,
|
||
pub server_count: usize,
|
||
}
|
||
|
||
/// 获取 Gemini MCP 配置文件路径(~/.gemini/settings.json)
|
||
fn user_config_path() -> PathBuf {
|
||
get_gemini_settings_path()
|
||
}
|
||
|
||
fn read_json_value(path: &Path) -> Result<Value, AppError> {
|
||
if !path.exists() {
|
||
return Ok(serde_json::json!({}));
|
||
}
|
||
let content = fs::read_to_string(path).map_err(|e| AppError::io(path, e))?;
|
||
let value: Value = serde_json::from_str(&content).map_err(|e| AppError::json(path, e))?;
|
||
Ok(value)
|
||
}
|
||
|
||
fn write_json_value(path: &Path, value: &Value) -> Result<(), AppError> {
|
||
if let Some(parent) = path.parent() {
|
||
fs::create_dir_all(parent).map_err(|e| AppError::io(parent, e))?;
|
||
}
|
||
let json =
|
||
serde_json::to_string_pretty(value).map_err(|e| AppError::JsonSerialize { source: e })?;
|
||
atomic_write(path, json.as_bytes())
|
||
}
|
||
|
||
/// 读取 Gemini MCP 配置文件的完整 JSON 文本
|
||
pub fn read_mcp_json() -> Result<Option<String>, AppError> {
|
||
let path = user_config_path();
|
||
if !path.exists() {
|
||
return Ok(None);
|
||
}
|
||
let content = fs::read_to_string(&path).map_err(|e| AppError::io(&path, e))?;
|
||
Ok(Some(content))
|
||
}
|
||
|
||
|
||
|
||
/// 读取 Gemini settings.json 中的 mcpServers 映射
|
||
pub fn read_mcp_servers_map() -> Result<std::collections::HashMap<String, Value>, AppError> {
|
||
let path = user_config_path();
|
||
if !path.exists() {
|
||
return Ok(std::collections::HashMap::new());
|
||
}
|
||
|
||
let root = read_json_value(&path)?;
|
||
let servers = root
|
||
.get("mcpServers")
|
||
.and_then(|v| v.as_object())
|
||
.map(|obj| obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
|
||
.unwrap_or_default();
|
||
|
||
Ok(servers)
|
||
}
|
||
|
||
/// 将给定的启用 MCP 服务器映射写入到 Gemini settings.json 的 mcpServers 字段
|
||
/// 仅覆盖 mcpServers,其他字段保持不变
|
||
pub fn set_mcp_servers_map(
|
||
servers: &std::collections::HashMap<String, Value>,
|
||
) -> Result<(), AppError> {
|
||
let path = user_config_path();
|
||
let mut root = if path.exists() {
|
||
read_json_value(&path)?
|
||
} else {
|
||
serde_json::json!({})
|
||
};
|
||
|
||
// 构建 mcpServers 对象:移除 UI 辅助字段(enabled/source),仅保留实际 MCP 规范
|
||
let mut out: Map<String, Value> = Map::new();
|
||
for (id, spec) in servers.iter() {
|
||
let mut obj = if let Some(map) = spec.as_object() {
|
||
map.clone()
|
||
} else {
|
||
return Err(AppError::McpValidation(format!(
|
||
"MCP 服务器 '{id}' 不是对象"
|
||
)));
|
||
};
|
||
|
||
// 提取 server 字段(如果存在)
|
||
if let Some(server_val) = obj.remove("server") {
|
||
let server_obj = server_val.as_object().cloned().ok_or_else(|| {
|
||
AppError::McpValidation(format!("MCP 服务器 '{id}' server 字段不是对象"))
|
||
})?;
|
||
obj = server_obj;
|
||
}
|
||
|
||
// 移除 UI 辅助字段
|
||
obj.remove("enabled");
|
||
obj.remove("source");
|
||
obj.remove("id");
|
||
obj.remove("name");
|
||
obj.remove("description");
|
||
obj.remove("tags");
|
||
obj.remove("homepage");
|
||
obj.remove("docs");
|
||
|
||
out.insert(id.clone(), Value::Object(obj));
|
||
}
|
||
|
||
{
|
||
let obj = root
|
||
.as_object_mut()
|
||
.ok_or_else(|| AppError::Config("~/.gemini/settings.json 根必须是对象".into()))?;
|
||
obj.insert("mcpServers".into(), Value::Object(out));
|
||
}
|
||
|
||
write_json_value(&path, &root)?;
|
||
Ok(())
|
||
}
|