2025-10-08 22:35:07 +08:00
|
|
|
|
import React, { useEffect, useMemo, useState } from "react";
|
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-10-17 17:49:16 +08:00
|
|
|
|
import { toast } from "sonner";
|
2025-10-16 16:20:45 +08:00
|
|
|
|
import { Plus, Server, Check } from "lucide-react";
|
2025-10-16 12:13:51 +08:00
|
|
|
|
import { Button } from "@/components/ui/button";
|
2025-10-16 16:20:45 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
} from "@/components/ui/dialog";
|
2025-10-16 12:13:51 +08:00
|
|
|
|
import { mcpApi, type AppType } from "@/lib/api";
|
2025-10-17 16:35:12 +08:00
|
|
|
|
import { McpServer } from "@/types";
|
2025-10-09 11:04:36 +08:00
|
|
|
|
import McpListItem from "./McpListItem";
|
|
|
|
|
|
import McpFormModal from "./McpFormModal";
|
|
|
|
|
|
import { ConfirmDialog } from "../ConfirmDialog";
|
2025-10-17 17:49:16 +08:00
|
|
|
|
import {
|
|
|
|
|
|
extractErrorMessage,
|
|
|
|
|
|
translateMcpBackendError,
|
|
|
|
|
|
} from "@/utils/errorUtils";
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
|
|
|
|
|
interface McpPanelProps {
|
2025-10-16 16:20:45 +08:00
|
|
|
|
open: boolean;
|
|
|
|
|
|
onOpenChange: (open: boolean) => void;
|
2025-10-10 12:35:02 +08:00
|
|
|
|
appType: AppType;
|
2025-10-08 22:35:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* MCP 管理面板
|
|
|
|
|
|
* 采用与主界面一致的设计风格,右上角添加按钮,每个 MCP 占一行
|
|
|
|
|
|
*/
|
2025-10-16 16:20:45 +08:00
|
|
|
|
const McpPanel: React.FC<McpPanelProps> = ({
|
|
|
|
|
|
open,
|
|
|
|
|
|
onOpenChange,
|
|
|
|
|
|
appType,
|
|
|
|
|
|
}) => {
|
2025-10-08 22:35:07 +08:00
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
const [servers, setServers] = useState<Record<string, McpServer>>({});
|
|
|
|
|
|
const [loading, setLoading] = useState(true);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const [isFormOpen, setIsFormOpen] = useState(false);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
const [editingId, setEditingId] = useState<string | null>(null);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const [confirmDialog, setConfirmDialog] = useState<{
|
|
|
|
|
|
isOpen: boolean;
|
|
|
|
|
|
title: string;
|
|
|
|
|
|
message: string;
|
|
|
|
|
|
onConfirm: () => void;
|
|
|
|
|
|
} | null>(null);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
|
|
|
|
|
const reload = async () => {
|
|
|
|
|
|
setLoading(true);
|
|
|
|
|
|
try {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
const cfg = await mcpApi.getConfig(appType);
|
2025-10-09 21:08:42 +08:00
|
|
|
|
setServers(cfg.servers || {});
|
2025-10-08 22:35:07 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-10-09 21:08:42 +08:00
|
|
|
|
const setup = async () => {
|
|
|
|
|
|
try {
|
2025-10-10 22:34:38 +08:00
|
|
|
|
// 初始化:仅从对应客户端导入已有 MCP,不做“预设落库”
|
2025-10-10 12:35:02 +08:00
|
|
|
|
if (appType === "claude") {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
await mcpApi.importFromClaude();
|
2025-10-10 14:59:02 +08:00
|
|
|
|
} else if (appType === "codex") {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
await mcpApi.importFromCodex();
|
2025-10-10 12:35:02 +08:00
|
|
|
|
}
|
2025-10-09 21:08:42 +08:00
|
|
|
|
} catch (e) {
|
2025-10-10 22:34:38 +08:00
|
|
|
|
console.warn("MCP 初始化导入失败(忽略继续)", e);
|
2025-10-09 21:08:42 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
await reload();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
setup();
|
2025-10-10 12:35:02 +08:00
|
|
|
|
// appType 改变时重新初始化
|
|
|
|
|
|
}, [appType]);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const handleToggle = async (id: string, enabled: boolean) => {
|
2025-10-10 23:23:40 +08:00
|
|
|
|
// 乐观更新:立即更新 UI
|
|
|
|
|
|
const previousServers = servers;
|
|
|
|
|
|
setServers((prev) => ({
|
|
|
|
|
|
...prev,
|
|
|
|
|
|
[id]: {
|
|
|
|
|
|
...prev[id],
|
|
|
|
|
|
enabled,
|
|
|
|
|
|
},
|
|
|
|
|
|
}));
|
2025-10-10 22:34:38 +08:00
|
|
|
|
|
2025-10-10 23:23:40 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 后台调用 API
|
2025-10-16 12:13:51 +08:00
|
|
|
|
await mcpApi.setEnabled(appType, id, enabled);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.success(
|
2025-10-09 11:04:36 +08:00
|
|
|
|
enabled ? t("mcp.msg.enabled") : t("mcp.msg.disabled"),
|
2025-10-17 17:49:16 +08:00
|
|
|
|
{ duration: 1500 },
|
2025-10-09 11:04:36 +08:00
|
|
|
|
);
|
|
|
|
|
|
} catch (e: any) {
|
2025-10-10 23:23:40 +08:00
|
|
|
|
// 失败时回滚
|
|
|
|
|
|
setServers(previousServers);
|
2025-10-09 17:21:03 +08:00
|
|
|
|
const detail = extractErrorMessage(e);
|
2025-10-11 16:20:12 +08:00
|
|
|
|
const mapped = translateMcpBackendError(detail, t);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.error(
|
2025-10-11 16:20:12 +08:00
|
|
|
|
mapped || detail || t("mcp.error.saveFailed"),
|
2025-10-17 17:49:16 +08:00
|
|
|
|
{ duration: mapped || detail ? 6000 : 5000 },
|
2025-10-09 17:21:03 +08:00
|
|
|
|
);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
}
|
2025-10-08 22:35:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const handleEdit = (id: string) => {
|
2025-10-08 22:35:07 +08:00
|
|
|
|
setEditingId(id);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
setIsFormOpen(true);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const handleAdd = () => {
|
|
|
|
|
|
setEditingId(null);
|
|
|
|
|
|
setIsFormOpen(true);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const handleDelete = (id: string) => {
|
|
|
|
|
|
setConfirmDialog({
|
|
|
|
|
|
isOpen: true,
|
|
|
|
|
|
title: t("mcp.confirm.deleteTitle"),
|
|
|
|
|
|
message: t("mcp.confirm.deleteMessage", { id }),
|
|
|
|
|
|
onConfirm: async () => {
|
|
|
|
|
|
try {
|
2025-10-16 12:13:51 +08:00
|
|
|
|
await mcpApi.deleteServerInConfig(appType, id);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
await reload();
|
|
|
|
|
|
setConfirmDialog(null);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.success(t("mcp.msg.deleted"), { duration: 1500 });
|
2025-10-09 11:04:36 +08:00
|
|
|
|
} catch (e: any) {
|
2025-10-09 17:21:03 +08:00
|
|
|
|
const detail = extractErrorMessage(e);
|
2025-10-11 16:20:12 +08:00
|
|
|
|
const mapped = translateMcpBackendError(detail, t);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.error(
|
2025-10-11 16:20:12 +08:00
|
|
|
|
mapped || detail || t("mcp.error.deleteFailed"),
|
2025-10-17 17:49:16 +08:00
|
|
|
|
{ duration: mapped || detail ? 6000 : 5000 },
|
2025-10-09 17:21:03 +08:00
|
|
|
|
);
|
2025-10-09 11:04:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
2025-10-08 22:35:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-14 00:22:15 +08:00
|
|
|
|
const handleSave = async (
|
|
|
|
|
|
id: string,
|
|
|
|
|
|
server: McpServer,
|
|
|
|
|
|
options?: { syncOtherSide?: boolean },
|
|
|
|
|
|
) => {
|
2025-10-08 22:35:07 +08:00
|
|
|
|
try {
|
2025-10-12 00:08:37 +08:00
|
|
|
|
const payload: McpServer = { ...server, id };
|
2025-10-16 12:13:51 +08:00
|
|
|
|
await mcpApi.upsertServerInConfig(appType, id, payload, {
|
2025-10-14 00:22:15 +08:00
|
|
|
|
syncOtherSide: options?.syncOtherSide,
|
|
|
|
|
|
});
|
2025-10-08 22:35:07 +08:00
|
|
|
|
await reload();
|
2025-10-09 11:04:36 +08:00
|
|
|
|
setIsFormOpen(false);
|
|
|
|
|
|
setEditingId(null);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.success(t("mcp.msg.saved"), { duration: 1500 });
|
2025-10-08 22:35:07 +08:00
|
|
|
|
} catch (e: any) {
|
2025-10-09 17:21:03 +08:00
|
|
|
|
const detail = extractErrorMessage(e);
|
2025-10-11 16:20:12 +08:00
|
|
|
|
const mapped = translateMcpBackendError(detail, t);
|
2025-10-17 17:49:16 +08:00
|
|
|
|
toast.error(
|
2025-10-11 16:20:12 +08:00
|
|
|
|
mapped || detail || t("mcp.error.saveFailed"),
|
2025-10-17 17:49:16 +08:00
|
|
|
|
{ duration: mapped || detail ? 6000 : 5000 },
|
2025-10-09 17:21:03 +08:00
|
|
|
|
);
|
2025-10-09 16:44:28 +08:00
|
|
|
|
// 继续抛出错误,让表单层可以给到直观反馈(避免被更高层遮挡)
|
|
|
|
|
|
throw e;
|
2025-10-08 22:35:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-09 11:04:36 +08:00
|
|
|
|
const handleCloseForm = () => {
|
|
|
|
|
|
setIsFormOpen(false);
|
|
|
|
|
|
setEditingId(null);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-10-12 00:08:37 +08:00
|
|
|
|
const serverEntries = useMemo(
|
|
|
|
|
|
() => Object.entries(servers) as Array<[string, McpServer]>,
|
|
|
|
|
|
[servers],
|
|
|
|
|
|
);
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-12 12:16:15 +08:00
|
|
|
|
const enabledCount = useMemo(
|
|
|
|
|
|
() => serverEntries.filter(([_, server]) => server.enabled).length,
|
|
|
|
|
|
[serverEntries],
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-10-10 20:52:16 +08:00
|
|
|
|
const panelTitle =
|
|
|
|
|
|
appType === "claude" ? t("mcp.claudeTitle") : t("mcp.codexTitle");
|
|
|
|
|
|
|
2025-10-08 22:35:07 +08:00
|
|
|
|
return (
|
2025-10-16 16:20:45 +08:00
|
|
|
|
<>
|
|
|
|
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
|
|
|
|
<DialogContent className="max-w-3xl max-h-[85vh] min-h-[600px] flex flex-col">
|
|
|
|
|
|
<DialogHeader>
|
|
|
|
|
|
<div className="flex items-center justify-between pr-8">
|
|
|
|
|
|
<DialogTitle>{panelTitle}</DialogTitle>
|
|
|
|
|
|
<Button type="button" variant="mcp" size="sm" onClick={handleAdd}>
|
|
|
|
|
|
<Plus size={16} />
|
|
|
|
|
|
{t("mcp.add")}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</DialogHeader>
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-16 16:20:45 +08:00
|
|
|
|
{/* Info Section */}
|
|
|
|
|
|
<div className="flex-shrink-0 -mt-2">
|
|
|
|
|
|
<div className="text-sm text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("mcp.serverCount", { count: Object.keys(servers).length })} ·{" "}
|
|
|
|
|
|
{t("mcp.enabledCount", { count: enabledCount })}
|
|
|
|
|
|
</div>
|
2025-10-09 11:04:36 +08:00
|
|
|
|
</div>
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-16 16:20:45 +08:00
|
|
|
|
{/* Content - Scrollable */}
|
|
|
|
|
|
<div className="flex-1 overflow-y-auto -mx-6 px-6">
|
|
|
|
|
|
{loading ? (
|
|
|
|
|
|
<div className="text-center py-12 text-gray-500 dark:text-gray-400">
|
|
|
|
|
|
{t("mcp.loading")}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
(() => {
|
|
|
|
|
|
const hasAny = serverEntries.length > 0;
|
|
|
|
|
|
if (!hasAny) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<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">
|
|
|
|
|
|
<Server
|
|
|
|
|
|
size={24}
|
|
|
|
|
|
className="text-gray-400 dark:text-gray-500"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<h3 className="text-lg font-medium text-gray-900 dark:text-gray-100 mb-2">
|
|
|
|
|
|
{t("mcp.empty")}
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<p className="text-gray-500 dark:text-gray-400 text-sm">
|
|
|
|
|
|
{t("mcp.emptyDescription")}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-10-08 22:35:07 +08:00
|
|
|
|
|
2025-10-09 17:21:03 +08:00
|
|
|
|
return (
|
2025-10-16 16:20:45 +08:00
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
|
{/* 已安装 */}
|
|
|
|
|
|
{serverEntries.map(([id, server]) => (
|
|
|
|
|
|
<McpListItem
|
|
|
|
|
|
key={`installed-${id}`}
|
|
|
|
|
|
id={id}
|
|
|
|
|
|
server={server}
|
|
|
|
|
|
onToggle={handleToggle}
|
|
|
|
|
|
onEdit={handleEdit}
|
|
|
|
|
|
onDelete={handleDelete}
|
2025-10-09 17:21:03 +08:00
|
|
|
|
/>
|
2025-10-16 16:20:45 +08:00
|
|
|
|
))}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 预设已移至"新增 MCP"面板中展示与套用 */}
|
2025-10-09 17:21:03 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2025-10-16 16:20:45 +08:00
|
|
|
|
})()
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-10-10 23:57:38 +08:00
|
|
|
|
|
2025-10-16 16:20:45 +08:00
|
|
|
|
{/* Footer */}
|
|
|
|
|
|
<div className="flex-shrink-0 flex items-center justify-end pt-4 border-t border-gray-200 dark:border-gray-800">
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="button"
|
|
|
|
|
|
variant="mcp"
|
|
|
|
|
|
size="sm"
|
|
|
|
|
|
onClick={() => onOpenChange(false)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Check size={16} />
|
|
|
|
|
|
{t("common.done")}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
2025-10-09 11:04:36 +08:00
|
|
|
|
|
|
|
|
|
|
{/* Form Modal */}
|
|
|
|
|
|
{isFormOpen && (
|
|
|
|
|
|
<McpFormModal
|
2025-10-10 20:52:16 +08:00
|
|
|
|
appType={appType}
|
2025-10-09 11:04:36 +08:00
|
|
|
|
editingId={editingId || undefined}
|
|
|
|
|
|
initialData={editingId ? servers[editingId] : undefined}
|
2025-10-10 11:17:40 +08:00
|
|
|
|
existingIds={Object.keys(servers)}
|
2025-10-09 11:04:36 +08:00
|
|
|
|
onSave={handleSave}
|
|
|
|
|
|
onClose={handleCloseForm}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* Confirm Dialog */}
|
|
|
|
|
|
{confirmDialog && (
|
|
|
|
|
|
<ConfirmDialog
|
|
|
|
|
|
isOpen={confirmDialog.isOpen}
|
|
|
|
|
|
title={confirmDialog.title}
|
|
|
|
|
|
message={confirmDialog.message}
|
|
|
|
|
|
onConfirm={confirmDialog.onConfirm}
|
|
|
|
|
|
onCancel={() => setConfirmDialog(null)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-10-16 16:20:45 +08:00
|
|
|
|
</>
|
2025-10-08 22:35:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default McpPanel;
|