2025-11-14 15:24:48 +08:00
|
|
|
|
import React, { useMemo, useState } from "react";
|
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-11-21 09:32:39 +08:00
|
|
|
|
import { Server } from "lucide-react";
|
2025-11-14 15:24:48 +08:00
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-11-16 11:31:27 +08:00
|
|
|
|
import { Switch } from "@/components/ui/switch";
|
2025-11-14 15:52:01 +08:00
|
|
|
|
import { useAllMcpServers, useToggleMcpApp } from "@/hooks/useMcp";
|
2025-11-14 15:24:48 +08:00
|
|
|
|
import type { McpServer } from "@/types";
|
|
|
|
|
|
import type { AppId } from "@/lib/api/types";
|
|
|
|
|
|
import McpFormModal from "./McpFormModal";
|
|
|
|
|
|
import { ConfirmDialog } from "../ConfirmDialog";
|
|
|
|
|
|
import { useDeleteMcpServer } from "@/hooks/useMcp";
|
|
|
|
|
|
import { Edit3, Trash2 } from "lucide-react";
|
|
|
|
|
|
import { settingsApi } from "@/lib/api";
|
|
|
|
|
|
import { mcpPresets } from "@/config/mcpPresets";
|
|
|
|
|
|
import { toast } from "sonner";
|
|
|
|
|
|
|
|
|
|
|
|
interface UnifiedMcpPanelProps {
|
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一 MCP 管理面板
|
|
|
|
|
|
* v3.7.0 新架构:所有 MCP 服务器统一管理,每个服务器通过复选框控制应用到哪些客户端
|
|
|
|
|
|
*/
|
2025-11-21 09:32:39 +08:00
|
|
|
|
export interface UnifiedMcpPanelHandle {
|
|
|
|
|
|
openAdd: () => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-21 11:09:24 +08:00
|
|
|
|
const UnifiedMcpPanel = React.forwardRef<
|
|
|
|
|
|
UnifiedMcpPanelHandle,
|
|
|
|
|
|
UnifiedMcpPanelProps
|
|
|
|
|
|
>(({ onOpenChange: _onOpenChange }, ref) => {
|
2025-11-14 15:24:48 +08:00
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
const [isFormOpen, setIsFormOpen] = useState(false);
|
|
|
|
|
|
const [editingId, setEditingId] = useState<string | null>(null);
|
|
|
|
|
|
const [confirmDialog, setConfirmDialog] = useState<{
|
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
message: string;
|
|
|
|
|
|
onConfirm: () => void;
|
|
|
|
|
|
} | null>(null);
|
|
|
|
|
|
|
|
|
|
|
|
// Queries and Mutations
|
2025-11-14 15:47:04 +08:00
|
|
|
|
const { data: serversMap, isLoading } = useAllMcpServers();
|
2025-11-14 15:24:48 +08:00
|
|
|
|
const toggleAppMutation = useToggleMcpApp();
|
|
|
|
|
|
const deleteServerMutation = useDeleteMcpServer();
|
|
|
|
|
|
|
|
|
|
|
|
// Convert serversMap to array for easier rendering
|
|
|
|
|
|
const serverEntries = useMemo((): Array<[string, McpServer]> => {
|
|
|
|
|
|
if (!serversMap) return [];
|
|
|
|
|
|
return Object.entries(serversMap);
|
|
|
|
|
|
}, [serversMap]);
|
|
|
|
|
|
|
|
|
|
|
|
// Count enabled servers per app
|
|
|
|
|
|
const enabledCounts = useMemo(() => {
|
|
|
|
|
|
const counts = { claude: 0, codex: 0, gemini: 0 };
|
|
|
|
|
|
serverEntries.forEach(([_, server]) => {
|
|
|
|
|
|
if (server.apps.claude) counts.claude++;
|
|
|
|
|
|
if (server.apps.codex) counts.codex++;
|
|
|
|
|
|
if (server.apps.gemini) counts.gemini++;
|
|
|
|
|
|
});
|
|
|
|
|
|
return counts;
|
|
|
|
|
|
}, [serverEntries]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleToggleApp = async (
|
|
|
|
|
|
serverId: string,
|
|
|
|
|
|
app: AppId,
|
|
|
|
|
|
enabled: boolean,
|
|
|
|
|
|
) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await toggleAppMutation.mutateAsync({ serverId, app, enabled });
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
toast.error(t("common.error"), {
|
|
|
|
|
|
description: String(error),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleEdit = (id: string) => {
|
|
|
|
|
|
setEditingId(id);
|
|
|
|
|
|
setIsFormOpen(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleAdd = () => {
|
|
|
|
|
|
setEditingId(null);
|
|
|
|
|
|
setIsFormOpen(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-11-21 09:32:39 +08:00
|
|
|
|
React.useImperativeHandle(ref, () => ({
|
2025-11-21 11:09:24 +08:00
|
|
|
|
openAdd: handleAdd,
|
2025-11-21 09:32:39 +08:00
|
|
|
|
}));
|
|
|
|
|
|
|
2025-11-14 15:24:48 +08:00
|
|
|
|
const handleDelete = (id: string) => {
|
|
|
|
|
|
setConfirmDialog({
|
|
|
|
|
|
isOpen: true,
|
|
|
|
|
|
title: t("mcp.unifiedPanel.deleteServer"),
|
|
|
|
|
|
message: t("mcp.unifiedPanel.deleteConfirm", { id }),
|
|
|
|
|
|
onConfirm: async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await deleteServerMutation.mutateAsync(id);
|
|
|
|
|
|
setConfirmDialog(null);
|
|
|
|
|
|
toast.success(t("common.success"));
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
toast.error(t("common.error"), {
|
|
|
|
|
|
description: String(error),
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleCloseForm = () => {
|
|
|
|
|
|
setIsFormOpen(false);
|
|
|
|
|
|
setEditingId(null);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="mx-auto max-w-[60rem] px-6 flex flex-col h-[calc(100vh-8rem)] overflow-hidden">
|
2025-11-21 09:32:39 +08:00
|
|
|
|
{/* Info Section */}
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="flex-shrink-0 py-4 glass rounded-xl border border-white/10 mb-4 px-6">
|
2025-11-21 09:32:39 +08:00
|
|
|
|
<div className="text-sm text-muted-foreground">
|
|
|
|
|
|
{t("mcp.serverCount", { count: serverEntries.length })} ·{" "}
|
|
|
|
|
|
{t("mcp.unifiedPanel.apps.claude")}: {enabledCounts.claude} ·{" "}
|
|
|
|
|
|
{t("mcp.unifiedPanel.apps.codex")}: {enabledCounts.codex} ·{" "}
|
|
|
|
|
|
{t("mcp.unifiedPanel.apps.gemini")}: {enabledCounts.gemini}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* Content - Scrollable */}
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="flex-1 overflow-y-auto overflow-x-hidden pb-24">
|
2025-11-21 09:32:39 +08:00
|
|
|
|
{isLoading ? (
|
|
|
|
|
|
<div className="text-center py-12 text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("mcp.loading")}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : serverEntries.length === 0 ? (
|
|
|
|
|
|
<div className="text-center py-12">
|
|
|
|
|
|
<div className="w-16 h-16 mx-auto mb-4 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center">
|
2025-11-21 11:09:24 +08:00
|
|
|
|
<Server size={24} className="text-gray-400 dark:text-gray-500" />
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
2025-11-21 09:32:39 +08:00
|
|
|
|
<h3 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-2">
|
|
|
|
|
|
{t("mcp.unifiedPanel.noServers")}
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<p className="text-gray-500 dark:text-gray-400 text-sm">
|
|
|
|
|
|
{t("mcp.emptyDescription")}
|
|
|
|
|
|
</p>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
2025-11-21 09:32:39 +08:00
|
|
|
|
) : (
|
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
|
{serverEntries.map(([id, server]) => (
|
|
|
|
|
|
<UnifiedMcpListItem
|
|
|
|
|
|
key={id}
|
|
|
|
|
|
id={id}
|
|
|
|
|
|
server={server}
|
|
|
|
|
|
onToggleApp={handleToggleApp}
|
|
|
|
|
|
onEdit={handleEdit}
|
|
|
|
|
|
onDelete={handleDelete}
|
|
|
|
|
|
/>
|
|
|
|
|
|
))}
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
2025-11-21 09:32:39 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
|
|
|
|
|
|
{/* Form Modal */}
|
|
|
|
|
|
{isFormOpen && (
|
|
|
|
|
|
<McpFormModal
|
|
|
|
|
|
editingId={editingId || undefined}
|
refactor(mcp): complete v3.7.0 cleanup - remove legacy code and warnings
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)
2025-11-14 22:43:25 +08:00
|
|
|
|
initialData={
|
|
|
|
|
|
editingId && serversMap ? serversMap[editingId] : undefined
|
|
|
|
|
|
}
|
2025-11-14 15:24:48 +08:00
|
|
|
|
existingIds={serversMap ? Object.keys(serversMap) : []}
|
2025-11-15 23:47:35 +08:00
|
|
|
|
defaultFormat="json"
|
2025-11-14 15:24:48 +08:00
|
|
|
|
onSave={async () => {
|
|
|
|
|
|
setIsFormOpen(false);
|
|
|
|
|
|
setEditingId(null);
|
|
|
|
|
|
}}
|
|
|
|
|
|
onClose={handleCloseForm}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* Confirm Dialog */}
|
|
|
|
|
|
{confirmDialog && (
|
|
|
|
|
|
<ConfirmDialog
|
|
|
|
|
|
isOpen={confirmDialog.isOpen}
|
|
|
|
|
|
title={confirmDialog.title}
|
|
|
|
|
|
message={confirmDialog.message}
|
|
|
|
|
|
onConfirm={confirmDialog.onConfirm}
|
|
|
|
|
|
onCancel={() => setConfirmDialog(null)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-11-21 09:32:39 +08:00
|
|
|
|
</div>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
);
|
2025-11-21 09:32:39 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
UnifiedMcpPanel.displayName = "UnifiedMcpPanel";
|
2025-11-14 15:24:48 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 统一 MCP 列表项组件
|
|
|
|
|
|
* 展示服务器名称、描述,以及三个应用的复选框
|
|
|
|
|
|
*/
|
|
|
|
|
|
interface UnifiedMcpListItemProps {
|
|
|
|
|
|
id: string;
|
|
|
|
|
|
server: McpServer;
|
|
|
|
|
|
onToggleApp: (serverId: string, app: AppId, enabled: boolean) => void;
|
|
|
|
|
|
onEdit: (id: string) => void;
|
|
|
|
|
|
onDelete: (id: string) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const UnifiedMcpListItem: React.FC<UnifiedMcpListItemProps> = ({
|
|
|
|
|
|
id,
|
|
|
|
|
|
server,
|
|
|
|
|
|
onToggleApp,
|
|
|
|
|
|
onEdit,
|
|
|
|
|
|
onDelete,
|
|
|
|
|
|
}) => {
|
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
const name = server.name || id;
|
|
|
|
|
|
const description = server.description || "";
|
|
|
|
|
|
|
|
|
|
|
|
// 匹配预设元信息
|
|
|
|
|
|
const meta = mcpPresets.find((p) => p.id === id);
|
|
|
|
|
|
const docsUrl = server.docs || meta?.docs;
|
|
|
|
|
|
const homepageUrl = server.homepage || meta?.homepage;
|
|
|
|
|
|
const tags = server.tags || meta?.tags;
|
|
|
|
|
|
|
|
|
|
|
|
const openDocs = async () => {
|
|
|
|
|
|
const url = docsUrl || homepageUrl;
|
|
|
|
|
|
if (!url) return;
|
|
|
|
|
|
try {
|
|
|
|
|
|
await settingsApi.openExternal(url);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
// ignore
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="group relative flex items-center gap-4 p-4 rounded-xl border border-white/10 bg-gray-900/40 hover:bg-gray-900/60 transition-all duration-300">
|
|
|
|
|
|
{/* 左侧:服务器信息 */}
|
|
|
|
|
|
<div className="flex-1 min-w-0">
|
|
|
|
|
|
<div className="flex items-center gap-2 mb-1">
|
|
|
|
|
|
<h3 className="font-medium text-gray-900 dark:text-gray-100">
|
|
|
|
|
|
{name}
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
{docsUrl && (
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={openDocs}
|
|
|
|
|
|
title={t("mcp.presets.docs")}
|
|
|
|
|
|
>
|
|
|
|
|
|
{t("mcp.presets.docs")}
|
|
|
|
|
|
</Button>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-11-22 02:41:17 +08:00
|
|
|
|
{description && (
|
|
|
|
|
|
<p className="text-sm text-gray-500 dark:text-gray-400 line-clamp-2">
|
|
|
|
|
|
{description}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
{!description && tags && tags.length > 0 && (
|
|
|
|
|
|
<p className="text-xs text-gray-400 dark:text-gray-500 truncate">
|
|
|
|
|
|
{tags.join(", ")}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
|
2025-11-22 02:41:17 +08:00
|
|
|
|
{/* 中间:应用开关 */}
|
|
|
|
|
|
<div className="flex flex-col gap-2 flex-shrink-0 min-w-[120px]">
|
|
|
|
|
|
<div className="flex items-center justify-between gap-3">
|
|
|
|
|
|
<label
|
|
|
|
|
|
htmlFor={`${id}-claude`}
|
|
|
|
|
|
className="text-sm text-gray-700 dark:text-gray-300 cursor-pointer"
|
|
|
|
|
|
>
|
|
|
|
|
|
{t("mcp.unifiedPanel.apps.claude")}
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<Switch
|
|
|
|
|
|
id={`${id}-claude`}
|
|
|
|
|
|
checked={server.apps.claude}
|
|
|
|
|
|
onCheckedChange={(checked: boolean) =>
|
|
|
|
|
|
onToggleApp(id, "claude", checked)
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="flex items-center justify-between gap-3">
|
|
|
|
|
|
<label
|
|
|
|
|
|
htmlFor={`${id}-codex`}
|
|
|
|
|
|
className="text-sm text-gray-700 dark:text-gray-300 cursor-pointer"
|
2025-11-14 15:24:48 +08:00
|
|
|
|
>
|
2025-11-22 02:41:17 +08:00
|
|
|
|
{t("mcp.unifiedPanel.apps.codex")}
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<Switch
|
|
|
|
|
|
id={`${id}-codex`}
|
|
|
|
|
|
checked={server.apps.codex}
|
|
|
|
|
|
onCheckedChange={(checked: boolean) =>
|
|
|
|
|
|
onToggleApp(id, "codex", checked)
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
|
2025-11-22 02:41:17 +08:00
|
|
|
|
<div className="flex items-center justify-between gap-3">
|
|
|
|
|
|
<label
|
|
|
|
|
|
htmlFor={`${id}-gemini`}
|
|
|
|
|
|
className="text-sm text-gray-700 dark:text-gray-300 cursor-pointer"
|
2025-11-14 15:24:48 +08:00
|
|
|
|
>
|
2025-11-22 02:41:17 +08:00
|
|
|
|
{t("mcp.unifiedPanel.apps.gemini")}
|
|
|
|
|
|
</label>
|
|
|
|
|
|
<Switch
|
|
|
|
|
|
id={`${id}-gemini`}
|
|
|
|
|
|
checked={server.apps.gemini}
|
|
|
|
|
|
onCheckedChange={(checked: boolean) =>
|
|
|
|
|
|
onToggleApp(id, "gemini", checked)
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-11-22 02:41:17 +08:00
|
|
|
|
|
|
|
|
|
|
{/* 右侧:操作按钮 */}
|
|
|
|
|
|
<div className="flex items-center gap-2 flex-shrink-0">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => onEdit(id)}
|
|
|
|
|
|
title={t("common.edit")}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Edit3 size={16} />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon"
|
|
|
|
|
|
onClick={() => onDelete(id)}
|
|
|
|
|
|
className="hover:text-red-500 hover:bg-red-100 dark:hover:text-red-400 dark:hover:bg-red-500/10"
|
|
|
|
|
|
title={t("common.delete")}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Trash2 size={16} />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2025-11-14 15:24:48 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default UnifiedMcpPanel;
|