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

@@ -40,43 +40,56 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
const reload = async () => {
setLoading(true);
try {
const s = await window.api.getClaudeMcpStatus();
setStatus(s);
const text = await window.api.readClaudeMcpConfig();
if (text) {
try {
const obj = JSON.parse(text);
const list = (obj?.mcpServers || {}) as Record<string, McpServer>;
setServers(list);
} catch (e) {
console.error("Failed to parse mcp.json", e);
setServers({});
}
} else {
setServers({});
}
const cfg = await window.api.getMcpConfig();
setStatus({
userConfigPath: cfg.configPath,
userConfigExists: true,
serverCount: Object.keys(cfg.servers || {}).length,
});
setServers(cfg.servers || {});
} finally {
setLoading(false);
}
};
useEffect(() => {
reload();
const setup = async () => {
try {
// 先从 ~/.claude.json 导入已存在的 MCP设为 enabled=true
await window.api.importMcpFromClaude();
// 读取现有 config.json 内容
const cfg = await window.api.getMcpConfig();
const existing = cfg.servers || {};
// 将预设落库为禁用(若缺失)
const missing = mcpPresets.filter((p) => !existing[p.id]);
for (const p of missing) {
const seed: McpServer = {
...(p.server as McpServer),
enabled: false,
source: "preset",
} as unknown as McpServer;
await window.api.upsertMcpServerInConfig(p.id, seed);
}
} catch (e) {
console.warn("MCP 初始化导入/落库失败(忽略继续)", e);
} finally {
await reload();
}
};
setup();
}, []);
const handleToggle = async (id: string, enabled: boolean) => {
try {
const server = servers[id];
let updatedServer: McpServer | null = null;
if (server) {
updatedServer = { ...server, enabled };
} else {
if (!server) {
const preset = mcpPresets.find((p) => p.id === id);
if (!preset) return; // 既不是已安装项也不是预设,忽略
updatedServer = { ...(preset.server as McpServer), enabled };
if (!preset) return;
await window.api.upsertMcpServerInConfig(id, preset.server as McpServer);
}
await window.api.upsertClaudeMcpServer(id, updatedServer as McpServer);
await window.api.setMcpEnabled(id, enabled);
await reload();
onNotify?.(
enabled ? t("mcp.msg.enabled") : t("mcp.msg.disabled"),
@@ -110,7 +123,7 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
message: t("mcp.confirm.deleteMessage", { id }),
onConfirm: async () => {
try {
await window.api.deleteClaudeMcpServer(id);
await window.api.deleteMcpServerInConfig(id);
await reload();
setConfirmDialog(null);
onNotify?.(t("mcp.msg.deleted"), "success", 1500);
@@ -128,7 +141,7 @@ const McpPanel: React.FC<McpPanelProps> = ({ onClose, onNotify }) => {
const handleSave = async (id: string, server: McpServer) => {
try {
await window.api.upsertClaudeMcpServer(id, server);
await window.api.upsertMcpServerInConfig(id, server);
await reload();
setIsFormOpen(false);
setEditingId(null);

View File

@@ -6,6 +6,7 @@ import {
CustomEndpoint,
McpStatus,
McpServer,
McpConfigResponse,
} from "../types";
// 应用类型
@@ -338,6 +339,64 @@ export const tauriAPI = {
}
},
// 新config.json 为 SSOT 的 MCP API
getMcpConfig: async (): Promise<McpConfigResponse> => {
try {
return await invoke<McpConfigResponse>("get_mcp_config");
} catch (error) {
console.error("获取 MCP 配置失败:", error);
throw error;
}
},
upsertMcpServerInConfig: async (
id: string,
spec: McpServer | Record<string, any>,
): Promise<boolean> => {
try {
return await invoke<boolean>("upsert_mcp_server_in_config", { id, spec });
} catch (error) {
console.error("写入 MCPconfig.json失败:", error);
throw error;
}
},
deleteMcpServerInConfig: async (id: string): Promise<boolean> => {
try {
return await invoke<boolean>("delete_mcp_server_in_config", { id });
} catch (error) {
console.error("删除 MCPconfig.json失败:", error);
throw error;
}
},
setMcpEnabled: async (id: string, enabled: boolean): Promise<boolean> => {
try {
return await invoke<boolean>("set_mcp_enabled", { id, enabled });
} catch (error) {
console.error("设置 MCP 启用状态失败:", error);
throw error;
}
},
syncEnabledMcpToClaude: async (): Promise<boolean> => {
try {
return await invoke<boolean>("sync_enabled_mcp_to_claude");
} catch (error) {
console.error("同步启用 MCP 到 .claude.json 失败:", error);
throw error;
}
},
importMcpFromClaude: async (): Promise<number> => {
try {
return await invoke<number>("import_mcp_from_claude");
} catch (error) {
console.error("从 ~/.claude.json 导入 MCP 失败:", error);
throw error;
}
},
// ours: 第三方/自定义供应商——测速与端点管理
// 第三方/自定义供应商:批量测试端点延迟
testApiEndpoints: async (

View File

@@ -75,3 +75,9 @@ export interface McpStatus {
userConfigExists: boolean;
serverCount: number;
}
// 新:来自 config.json 的 MCP 列表响应
export interface McpConfigResponse {
configPath: string;
servers: Record<string, McpServer>;
}

12
src/vite-env.d.ts vendored
View File

@@ -1,6 +1,6 @@
/// <reference types="vite/client" />
import { Provider, Settings, CustomEndpoint, McpStatus } from "./types";
import { Provider, Settings, CustomEndpoint, McpStatus, McpConfigResponse } from "./types";
import { AppType } from "./lib/tauri-api";
import type { UnlistenFn } from "@tauri-apps/api/event";
@@ -70,6 +70,16 @@ declare global {
) => Promise<boolean>;
deleteClaudeMcpServer: (id: string) => Promise<boolean>;
validateMcpCommand: (cmd: string) => Promise<boolean>;
// 新config.json 为 SSOT 的 MCP API
getMcpConfig: () => Promise<McpConfigResponse>;
upsertMcpServerInConfig: (
id: string,
spec: Record<string, any>,
) => Promise<boolean>;
deleteMcpServerInConfig: (id: string) => Promise<boolean>;
setMcpEnabled: (id: string, enabled: boolean) => Promise<boolean>;
syncEnabledMcpToClaude: () => Promise<boolean>;
importMcpFromClaude: () => Promise<number>;
testApiEndpoints: (
urls: string[],
options?: { timeoutSecs?: number },