feat(mcp): use project config as SSOT and sync enabled servers to ~/.claude.json

- Add McpConfig to MultiAppConfig and persist MCP servers in ~/.cc-switch/config.json
- Add Tauri commands: get_mcp_config, upsert_mcp_server_in_config, delete_mcp_server_in_config, set_mcp_enabled, sync_enabled_mcp_to_claude, import_mcp_from_claude
- Only write enabled MCPs to ~/.claude.json (mcpServers) and strip UI-only fields (enabled/source)
- Frontend: update API wrappers and MCP panel to read/write via config.json; seed presets on first open; import from ~/.claude.json
- Fix warnings (remove unused mut, dead code)
- Verified with cargo check and pnpm typecheck
This commit is contained in:
Jason
2025-10-09 21:08:42 +08:00
parent 0be596afb5
commit f6bf8611cd
9 changed files with 417 additions and 31 deletions

View File

@@ -1,6 +1,14 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// MCP 配置:集中存放于 ~/.cc-switch/config.json
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct McpConfig {
/// 以 id 为键的服务器定义(宽松 JSON 对象,包含 enabled/source 等 UI 辅助字段)
#[serde(default)]
pub servers: HashMap<String, serde_json::Value>,
}
use crate::config::{copy_file, get_app_config_dir, get_app_config_path, write_json_file};
use crate::provider::ProviderManager;
@@ -35,8 +43,12 @@ impl From<&str> for AppType {
pub struct MultiAppConfig {
#[serde(default = "default_version")]
pub version: u32,
/// 应用管理器claude/codex
#[serde(flatten)]
pub apps: HashMap<String, ProviderManager>,
/// MCP 配置
#[serde(default)]
pub mcp: McpConfig,
}
fn default_version() -> u32 {
@@ -49,7 +61,11 @@ impl Default for MultiAppConfig {
apps.insert("claude".to_string(), ProviderManager::default());
apps.insert("codex".to_string(), ProviderManager::default());
Self { version: 2, apps }
Self {
version: 2,
apps,
mcp: McpConfig::default(),
}
}
}
@@ -76,7 +92,11 @@ impl MultiAppConfig {
apps.insert("claude".to_string(), v1_config);
apps.insert("codex".to_string(), ProviderManager::default());
let config = Self { version: 2, apps };
let config = Self {
version: 2,
apps,
mcp: McpConfig::default(),
};
// 迁移前备份旧版(v1)配置文件
let backup_dir = get_app_config_dir();
@@ -136,4 +156,9 @@ impl MultiAppConfig {
.insert(app.as_str().to_string(), ProviderManager::default());
}
}
/// 获取 MCP 配置(可变引用)
pub fn mcp_mut(&mut self) -> &mut McpConfig {
&mut self.mcp
}
}