| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- /* eslint-disable max-lines -- MCP store 集中维护配置加载、持久化、状态刷新与运行时状态合并,拆分会增加跨状态同步复杂度。 */
- /**
- * MCP (Model Context Protocol) UI State Store
- */
- import { create } from "zustand";
- import type {
- ZCodeAgentMcpServer,
- CliMcpSource,
- McpConfig,
- McpScope,
- McpServerConfig,
- McpServerStatus,
- McpSource,
- NativeMcpServerRecord,
- ZCodeMcpListMode,
- ZCodeMcpServerStatusSnapshot,
- ZCodeMcpServer,
- } from "@zcode/shared";
- import { convertToZCodeAgentMcpServer } from "@zcode/shared";
- import { logger } from "@/logger.js";
- import {
- fetchNativeMcpServers,
- migrateLegacyCommonMcpFromDesktop,
- persistCliMcpToUserDirectory,
- type McpDirectoryService,
- type McpPlatformService,
- type MigrateLegacyResult,
- } from "@/store/mcpStoreDesktop.js";
- import {
- importLegacyCommonServersToZCodeAgent,
- migrateStoredCommonMcpToZCodeAgent,
- } from "@/store/mcpStoreMigration.js";
- import {
- buildServerList,
- DEFAULT_MCP_CONFIG,
- getServerPriority,
- loadPersistedConfig,
- makeServerId,
- MCP_DELETED_PRELOAD_KEY,
- safeReadJson,
- safeWriteJson,
- } from "@/store/mcpStoreHelpers.js";
- import { isRemoteWorkspaceDisconnectedError } from "@/lib/remoteWorkspaceServiceError.js";
- import { mergeMcpServerStatusSnapshots } from "@/store/mcpStoreStatusList.js";
- let mcpPlatformService: McpPlatformService | null = null;
- let mcpDirectoryService: McpDirectoryService | null = null;
- export function setMcpStorePlatform(platform: McpPlatformService | null): void {
- mcpPlatformService = platform;
- }
- export function setMcpStoreDirectoryService(service: McpDirectoryService | null): void {
- mcpDirectoryService = service;
- }
- interface UpdateServerStatusOptions {
- invalidateStatusListRequests?: boolean;
- }
- interface McpStoreState {
- config: McpConfig;
- nativeServers: NativeMcpServerRecord[];
- servers: ZCodeMcpServer[];
- statusSnapshots: Record<string, ZCodeMcpServerStatusSnapshot>;
- currentProjectPath: string;
- currentWorkspaceIdentity?: string;
- currentAuthorityKey?: string;
- enabledStates: Record<string, boolean>;
- deletedPreloadMcpServers: Set<string>;
- isConfigLoaded: boolean;
- currentSessionId: string | null;
- loadConfig: () => void;
- loadMcpFromUserDirectory: (
- directoryService?: McpDirectoryService | null,
- workspaceIdentity?: string,
- authorityKey?: string,
- ) => Promise<boolean>;
- ensureLoadedForWorkspace: (
- workspacePath?: string,
- directoryService?: McpDirectoryService | null,
- workspaceIdentity?: string,
- authorityKey?: string,
- ) => Promise<boolean>;
- saveConfig: () => void;
- addMcpServer: (name: string, config: McpServerConfig) => void;
- updateMcpServer: (name: string, config: McpServerConfig) => void;
- deleteMcpServer: (name: string) => void;
- addScopedMcpServer: (source: McpSource, name: string, config: McpServerConfig, projectPath?: string) => Promise<void>;
- updateScopedMcpServer: (source: McpSource, name: string, config: McpServerConfig, projectPath?: string) => Promise<void>;
- deleteScopedMcpServer: (source: McpSource, name: string, projectPath?: string) => void;
- addZCodeAgentMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- updateZCodeAgentMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- deleteZCodeAgentMcpServer: (name: string, projectPath?: string) => void;
- addClaudeCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- updateClaudeCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- deleteClaudeCliMcpServer: (name: string, projectPath?: string) => void;
- addGeminiCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- updateGeminiCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- deleteGeminiCliMcpServer: (name: string, projectPath?: string) => void;
- addCodexCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- updateCodexCliMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- deleteCodexCliMcpServer: (name: string, projectPath?: string) => void;
- addOpenCodeMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- updateOpenCodeMcpServer: (name: string, config: McpServerConfig, projectPath?: string) => void;
- deleteOpenCodeMcpServer: (name: string, projectPath?: string) => void;
- toggleServer: (id: string, enabled: boolean) => Promise<void>;
- updateServerStatus: (
- id: string,
- status: McpServerStatus,
- error?: string,
- options?: UpdateServerStatusOptions,
- ) => void;
- beginServerStatusListRefresh: (mode?: ZCodeMcpListMode) => number;
- markServerStatusListRefreshFailed: (
- error: string,
- requestEpoch: number,
- mode?: ZCodeMcpListMode,
- ) => void;
- mergeServerStatusSnapshots: (
- statuses: Record<string, ZCodeMcpServerStatusSnapshot>,
- requestEpoch: number,
- mode?: "connect" | "status",
- ) => void;
- getServer: (id: string) => ZCodeMcpServer | undefined;
- setCurrentProjectPath: (
- path: string,
- workspaceIdentity?: string,
- directoryService?: McpDirectoryService | null,
- authorityKey?: string,
- ) => void;
- getEnabledMcpServersForZCode: (provider: string) => ZCodeAgentMcpServer[];
- checkAllServerStatus: (tester: (config: McpServerConfig) => Promise<{ success: boolean; error?: string }>) => Promise<void>;
- mergePreloadedMcpServers: (preloaded: Record<string, McpServerConfig>) => void;
- deletePreloadedMcpServer: (source: McpSource, name: string) => void;
- setCurrentSessionId: (sessionId: string | null) => void;
- migrateLegacyCommonMcp: () => Promise<MigrateLegacyResult>;
- }
- export const useMcpStore = create<McpStoreState>((set, get) => {
- let loadMcpPromise: Promise<boolean> | null = null;
- let loadMcpWorkspaceKey: string | null = null;
- const statusListEpochs: Record<ZCodeMcpListMode, number> = {
- connect: 0,
- status: 0,
- };
- function invalidateStatusListRequests(): void {
- statusListEpochs.connect += 1;
- statusListEpochs.status += 1;
- }
- function resolveMcpDirectoryService(
- directoryService?: McpDirectoryService | null,
- workspaceIdentity?: string | null,
- ): McpDirectoryService | null {
- const effectiveWorkspaceIdentity = workspaceIdentity ?? get().currentWorkspaceIdentity;
- if (!effectiveWorkspaceIdentity?.trim()) {
- // 本地 workspace 仍要走 desktop platform 路径,才能执行旧 common MCP
- // 到用户级 ZCode Agent MCP 的迁移;目录服务只用于远端 workspace 覆盖路由。
- return null;
- }
- return directoryService ?? mcpDirectoryService;
- }
- function commitState(partial: { config?: McpConfig; nativeServers?: NativeMcpServerRecord[] }): Partial<McpStoreState> {
- const state = get();
- const nextConfig = partial.config ?? state.config;
- const nextNativeServers = partial.nativeServers ?? state.nativeServers;
- return {
- config: nextConfig,
- nativeServers: nextNativeServers,
- servers: buildServerList(nextConfig, nextNativeServers, state.enabledStates, state.deletedPreloadMcpServers, state.servers),
- };
- }
- function updateNativeServer(source: CliMcpSource, name: string, config: McpServerConfig, projectPath?: string) {
- const nativeServers = get().nativeServers.slice();
- const targetIndex = nativeServers.findIndex((server) => server.source === source && server.name === name && server.projectPath === projectPath);
- if (targetIndex >= 0) {
- const existing = nativeServers[targetIndex];
- if (!existing) {
- return;
- }
- nativeServers[targetIndex] = {
- ...existing,
- config,
- };
- } else {
- // 添加新的服务器记录
- const scope: McpScope = projectPath ? "workspace" : "user";
- nativeServers.push({
- source,
- scope,
- name,
- config,
- enabled: true,
- projectPath,
- });
- }
- set(commitState({ nativeServers }));
- }
- async function persistScopedChange(
- source: McpSource,
- payload: { action: "upsert" | "delete"; source: CliMcpSource; name: string; config?: McpServerConfig; projectPath?: string },
- ): Promise<void> {
- if (source === "mcp") {
- return;
- }
- await persistCliMcpToUserDirectory(
- mcpPlatformService,
- payload,
- resolveMcpDirectoryService(),
- ).catch((error) => {
- logger.warn(`[mcpStore] persist ${source} MCP failed`, String(error));
- });
- }
- return {
- config: { ...DEFAULT_MCP_CONFIG },
- nativeServers: [],
- servers: [],
- statusSnapshots: {},
- currentProjectPath: "",
- currentWorkspaceIdentity: undefined,
- currentAuthorityKey: undefined,
- enabledStates: {},
- deletedPreloadMcpServers: new Set(),
- isConfigLoaded: false,
- currentSessionId: null,
- loadConfig: () => {
- const config = loadPersistedConfig();
- // MCP 启停状态已经迁移到 ~/.zcode/cli/config.json,不能再读取旧 localStorage,
- // 否则旧的本地开关会覆盖新的 ZCode Agent 配置来源。
- const enabledStates: Record<string, boolean> = {};
- const deletedPreload = new Set<string>(safeReadJson<string[]>(MCP_DELETED_PRELOAD_KEY, []));
- const servers = buildServerList(config, [], enabledStates, deletedPreload, []);
- set({ config, nativeServers: [], enabledStates, deletedPreloadMcpServers: deletedPreload, servers, statusSnapshots: {}, isConfigLoaded: true });
- },
- loadMcpFromUserDirectory: async (directoryService, workspaceIdentity, authorityKey) => {
- if (typeof window === "undefined") {
- return false;
- }
- const requestWorkspacePath = get().currentProjectPath || undefined;
- const requestWorkspaceIdentity = workspaceIdentity ?? get().currentWorkspaceIdentity;
- const requestAuthorityKey = authorityKey ?? get().currentAuthorityKey;
- const requestWorkspaceKey = `${requestAuthorityKey?.trim() || "workspace"}\0${requestWorkspaceIdentity?.trim() || requestWorkspacePath || ""}`;
- if (loadMcpPromise && loadMcpWorkspaceKey === requestWorkspaceKey) {
- return await loadMcpPromise;
- }
- if (loadMcpPromise) {
- await loadMcpPromise;
- }
- const latestWorkspacePath = get().currentProjectPath || undefined;
- const latestWorkspaceIdentity = get().currentWorkspaceIdentity;
- const latestAuthorityKey = get().currentAuthorityKey;
- const latestWorkspaceKey = `${latestAuthorityKey?.trim() || "workspace"}\0${latestWorkspaceIdentity?.trim() || latestWorkspacePath || ""}`;
- if (latestWorkspaceKey !== requestWorkspaceKey) {
- // 该调用等待上一轮 load 时已从 B 切到 C;B 调用持有的 remote
- // directoryService 不得继续读取 C,交给 C 自己发起的 ensure/load 调用处理。
- return false;
- }
- // 等待上一 workspace load 期间可能再次切换;目录服务必须按等待后的
- // 最新 identity 重新选择,不能拿 B 的 remote service 去读取 C 的路径。
- const activeDirectoryService = resolveMcpDirectoryService(
- directoryService,
- latestWorkspaceIdentity,
- );
- if (loadMcpPromise && loadMcpWorkspaceKey === latestWorkspaceKey) {
- return await loadMcpPromise;
- }
- loadMcpWorkspaceKey = latestWorkspaceKey;
- loadMcpPromise = (async () => {
- try {
- logger.info(
- `[mcpStore] loadMcpFromUserDirectory workspace=${latestWorkspacePath ?? "<none>"} identity=${latestWorkspaceIdentity ?? "<none>"}`,
- );
- let servers = await fetchNativeMcpServers(
- mcpPlatformService,
- { workspacePath: latestWorkspacePath },
- activeDirectoryService,
- );
- if (!activeDirectoryService) {
- servers = await migrateStoredCommonMcpToZCodeAgent(
- mcpPlatformService,
- servers,
- latestWorkspacePath,
- );
- }
- const currentState = get();
- const currentWorkspaceKey = `${currentState.currentAuthorityKey?.trim() || "workspace"}\0${currentState.currentWorkspaceIdentity?.trim() || currentState.currentProjectPath}`;
- if (currentWorkspaceKey !== latestWorkspaceKey) {
- // workspace A 的异步目录读取可能晚于切换到 B 才返回;
- // 旧结果包含 env/header/OAuth secret,不能写回共享 store 后被 B 的 Agent 消费。
- return false;
- }
- set(commitState({ nativeServers: servers }));
- return true;
- } catch (e) {
- // 读取失败不是“配置为空”;调用方必须保持 workspace not-ready,
- // 否则会显式下发空 mcpServers 并触发 replace,断开仍在运行的 MCP。
- // remote session attachment 绑定前只能拿到断连代理;这是初始化时序,不是
- // MCP 配置读取失败。只过滤该精确错误码,避免吞掉真实的目录或 RPC 故障。
- if (!isRemoteWorkspaceDisconnectedError(e)) {
- logger.warn("[mcpStore] loadMcpFromUserDirectory failed", String(e));
- }
- return false;
- } finally {
- loadMcpPromise = null;
- loadMcpWorkspaceKey = null;
- }
- })();
- return await loadMcpPromise;
- },
- ensureLoadedForWorkspace: async (workspacePath, directoryService, workspaceIdentity, authorityKey) => {
- if (!get().isConfigLoaded) {
- get().loadConfig();
- }
- const normalizedWorkspacePath = workspacePath ?? "";
- const normalizedWorkspaceIdentity = workspaceIdentity?.trim() || undefined;
- const normalizedAuthorityKey = authorityKey?.trim() || undefined;
- if (
- normalizedWorkspacePath !== get().currentProjectPath ||
- normalizedWorkspaceIdentity !== get().currentWorkspaceIdentity ||
- normalizedAuthorityKey !== get().currentAuthorityKey
- ) {
- get().setCurrentProjectPath(
- normalizedWorkspacePath,
- normalizedWorkspaceIdentity,
- directoryService,
- normalizedAuthorityKey,
- );
- }
- return await get().loadMcpFromUserDirectory(
- directoryService,
- normalizedWorkspaceIdentity,
- normalizedAuthorityKey,
- );
- },
- saveConfig: () => {
- const { deletedPreloadMcpServers } = get();
- safeWriteJson(MCP_DELETED_PRELOAD_KEY, Array.from(deletedPreloadMcpServers));
- },
- addMcpServer: (name, config) => get().addZCodeAgentMcpServer(name, config),
- updateMcpServer: (name, config) => get().updateZCodeAgentMcpServer(name, config),
- deleteMcpServer: (name) => get().deleteZCodeAgentMcpServer(name),
- addScopedMcpServer: async (source, name, config, projectPath) => {
- invalidateStatusListRequests();
- const targetSource: CliMcpSource = source === "mcp" ? "zcodeagentmcp" : source;
- // 设置页保存后会马上触发 agent 侧 mcp/list 重连。
- // 先等配置落盘,再更新本地 store,避免 agent 读到旧 timeoutMs 后展示旧健康状态。
- await persistScopedChange(targetSource, { action: "upsert", source: targetSource, name, config, projectPath });
- updateNativeServer(targetSource, name, config, projectPath);
- },
- updateScopedMcpServer: async (source, name, config, projectPath) => {
- invalidateStatusListRequests();
- const targetSource: CliMcpSource = source === "mcp" ? "zcodeagentmcp" : source;
- // 修复原因同新增:本地状态变化会驱动健康状态刷新,必须在磁盘配置更新后发生。
- await persistScopedChange(targetSource, { action: "upsert", source: targetSource, name, config, projectPath });
- updateNativeServer(targetSource, name, config, projectPath);
- },
- deleteScopedMcpServer: (source, name, projectPath) => {
- invalidateStatusListRequests();
- const targetSource: CliMcpSource = source === "mcp" ? "zcodeagentmcp" : source;
- persistScopedChange(targetSource, { action: "delete", source: targetSource, name, projectPath });
- set(commitState({
- nativeServers: get().nativeServers.filter((server) => !(server.source === targetSource && server.name === name && server.projectPath === projectPath)),
- }));
- },
- addClaudeCliMcpServer: (name, config, projectPath) => get().addScopedMcpServer("claudeclimcp", name, config, projectPath),
- updateClaudeCliMcpServer: (name, config, projectPath) => get().updateScopedMcpServer("claudeclimcp", name, config, projectPath),
- deleteClaudeCliMcpServer: (name, projectPath) => get().deleteScopedMcpServer("claudeclimcp", name, projectPath),
- addGeminiCliMcpServer: (name, config, projectPath) => get().addScopedMcpServer("geminiclimcp", name, config, projectPath),
- updateGeminiCliMcpServer: (name, config, projectPath) => get().updateScopedMcpServer("geminiclimcp", name, config, projectPath),
- deleteGeminiCliMcpServer: (name, projectPath) => get().deleteScopedMcpServer("geminiclimcp", name, projectPath),
- addCodexCliMcpServer: (name, config, projectPath) => get().addScopedMcpServer("codexclimcp", name, config, projectPath),
- updateCodexCliMcpServer: (name, config, projectPath) => get().updateScopedMcpServer("codexclimcp", name, config, projectPath),
- deleteCodexCliMcpServer: (name, projectPath) => get().deleteScopedMcpServer("codexclimcp", name, projectPath),
- addOpenCodeMcpServer: (name, config, projectPath) => get().addScopedMcpServer("opencodemcp", name, config, projectPath),
- updateOpenCodeMcpServer: (name, config, projectPath) => get().updateScopedMcpServer("opencodemcp", name, config, projectPath),
- deleteOpenCodeMcpServer: (name, projectPath) => get().deleteScopedMcpServer("opencodemcp", name, projectPath),
- addZCodeAgentMcpServer: (name, config, projectPath) => get().addScopedMcpServer("zcodeagentmcp", name, config, projectPath),
- updateZCodeAgentMcpServer: (name, config, projectPath) => get().updateScopedMcpServer("zcodeagentmcp", name, config, projectPath),
- deleteZCodeAgentMcpServer: (name, projectPath) => get().deleteScopedMcpServer("zcodeagentmcp", name, projectPath),
- toggleServer: async (id, enabled) => {
- invalidateStatusListRequests();
- const targetServer = get().servers.find((server) => server.id === id);
- if (targetServer && targetServer.source !== "mcp") {
- // 开关变化会触发 agent 侧 mcp/list 读取磁盘配置做真实连接。
- // 必须先等 enable 状态落盘,再更新本地列表驱动刷新,否则 agent 会读到旧开关。
- await persistCliMcpToUserDirectory(mcpPlatformService, {
- action: "set-enabled",
- source: targetServer.source,
- name: targetServer.name,
- enabled,
- projectPath: targetServer.projectPath,
- location: targetServer.location,
- }, resolveMcpDirectoryService()).catch((error) => {
- logger.warn("[mcpStore] persist MCP enabled override failed", String(error));
- });
- }
- set((state) => {
- const nextEnabledStates = { ...state.enabledStates, [id]: enabled };
- return {
- enabledStates: nextEnabledStates,
- servers: state.servers.map((s) => (s.id === id ? { ...s, enabled } : s)),
- };
- });
- },
- updateServerStatus: (id, status, error, options) => {
- if (options?.invalidateStatusListRequests !== false) {
- invalidateStatusListRequests();
- }
- set((state) => ({
- servers: state.servers.map((s) =>
- s.id === id
- ? {
- ...s,
- status,
- authorization: undefined,
- error,
- failureKind: status === "error" ? "connection_failed" : undefined,
- serverRequestId: undefined,
- toolCount: status === "connected" ? s.toolCount : undefined,
- changed: status === "error" ? s.changed : false,
- lastConnected: status === "connected" ? new Date() : s.lastConnected,
- }
- : s,
- ),
- }));
- },
- beginServerStatusListRefresh: (mode = "connect") => {
- statusListEpochs[mode] += 1;
- const requestEpoch = statusListEpochs[mode];
- if (mode === "status") {
- // OAuth 1s status-only 轮询只读取 runtime snapshot;不能反复清空
- // authorization/toolCount 或把用户同时编辑的其他 MCP 投影成 connecting。
- return requestEpoch;
- }
- // agent 侧 mcp/list 会等待所有 MCP 连接完成才返回;如果插件 MCP 还在 30s 超时中,
- // 修改 timeoutMs 后本地列表会长时间停在 unknown,看起来像没有立即重新检查。
- set((state) => ({
- servers: state.servers.map((server) => {
- const enabled = state.enabledStates[server.id] ?? server.enabled;
- if (
- server.source !== "zcodeagentmcp" ||
- !enabled ||
- (!server.changed && server.status !== "unknown")
- ) {
- return server;
- }
- return {
- ...server,
- status: "connecting",
- authorization: undefined,
- error: undefined,
- failureKind: undefined,
- serverRequestId: undefined,
- toolCount: undefined,
- };
- }),
- }));
- return requestEpoch;
- },
- markServerStatusListRefreshFailed: (error, requestEpoch, mode = "connect") => {
- if (requestEpoch !== statusListEpochs[mode] || mode === "status") {
- // OAuth 轮询是只读的 best-effort status 请求;临时失败不能清空已有
- // authorization snapshot,也不能把同一列表中仍在连接的 MCP 批量标红。
- return;
- }
- set((state) => ({
- statusSnapshots: {},
- servers: state.servers.map((server) => {
- const enabled = state.enabledStates[server.id] ?? server.enabled;
- if (
- server.source !== "zcodeagentmcp" ||
- !enabled ||
- server.status !== "connecting"
- ) {
- return server;
- }
- return {
- ...server,
- status: "error",
- authorization: undefined,
- error,
- failureKind: "status_unavailable",
- serverRequestId: undefined,
- toolCount: undefined,
- };
- }),
- }));
- },
- mergeServerStatusSnapshots: (statuses, requestEpoch, mode = "connect") => {
- if (requestEpoch !== statusListEpochs[mode]) {
- return;
- }
- set((state) => {
- const nextStatusSnapshots =
- mode === "status"
- ? { ...state.statusSnapshots, ...statuses }
- : statuses;
- return {
- statusSnapshots: nextStatusSnapshots,
- servers: mergeMcpServerStatusSnapshots(
- state.servers,
- nextStatusSnapshots,
- {
- // Bugfix:OAuth 轮询的 status-only 响应可能只包含 pending 子集。
- // 缺失项不能按全量 mcp/list 处理,否则会把其他 MCP 误标为 agent 未返回。
- markMissingConnectingAsError: mode !== "status",
- },
- ),
- };
- });
- },
- getServer: (id) => get().servers.find((s) => s.id === id),
- setCurrentProjectPath: (path, workspaceIdentity, directoryService, authorityKey) => {
- invalidateStatusListRequests();
- const {
- config,
- currentProjectPath,
- currentWorkspaceIdentity,
- currentAuthorityKey,
- deletedPreloadMcpServers,
- enabledStates,
- isConfigLoaded,
- nativeServers,
- servers,
- } = get();
- const normalizedWorkspaceIdentity = workspaceIdentity?.trim() || undefined;
- const normalizedAuthorityKey = authorityKey?.trim() || undefined;
- const workspaceChanged =
- path !== currentProjectPath ||
- normalizedWorkspaceIdentity !== currentWorkspaceIdentity ||
- normalizedAuthorityKey !== currentAuthorityKey;
- const nextNativeServers = workspaceChanged ? [] : nativeServers;
- set({
- currentProjectPath: path,
- currentWorkspaceIdentity: normalizedWorkspaceIdentity,
- currentAuthorityKey: normalizedAuthorityKey,
- nativeServers: nextNativeServers,
- statusSnapshots: {},
- servers: buildServerList(
- config,
- nextNativeServers,
- enabledStates,
- deletedPreloadMcpServers,
- workspaceChanged ? [] : servers,
- ),
- });
- if (isConfigLoaded && workspaceChanged) {
- // 切换到远程 workspace 后配置加载是异步的;等待期间必须先清空 A 的
- // user-level MCP,不能把其 env/header/OAuth clientSecret 暂存为 B 的可发送配置。
- void get().loadMcpFromUserDirectory(directoryService, normalizedWorkspaceIdentity, normalizedAuthorityKey);
- }
- },
- getEnabledMcpServersForZCode: (_provider) => {
- const { servers, enabledStates, currentProjectPath } = get();
- const candidates = new Map<string, ZCodeMcpServer>();
- for (const server of servers) {
- const isEnabled = enabledStates[server.id] ?? server.enabled;
- if (!isEnabled) continue;
- if (server.source !== "zcodeagentmcp") continue;
- if (server.scope === "workspace" && server.projectPath !== currentProjectPath) continue;
- const existing = candidates.get(server.name);
- if (!existing || getServerPriority(server) > getServerPriority(existing)) {
- candidates.set(server.name, server);
- }
- }
- const result: ZCodeAgentMcpServer[] = [];
- for (const server of candidates.values()) {
- const zcodeAgentServer = convertToZCodeAgentMcpServer(server.name, server.config);
- if (zcodeAgentServer) result.push(zcodeAgentServer);
- }
- return result;
- },
- checkAllServerStatus: async (tester) => {
- for (const server of get().servers) {
- const isEnabled = get().enabledStates[server.id] ?? server.enabled;
- if (!isEnabled || (!server.changed && server.status !== "unknown")) continue;
- get().updateServerStatus(server.id, "connecting");
- try {
- const result = await tester(server.config);
- get().updateServerStatus(server.id, result.success ? "connected" : "error", result.success ? undefined : (result.error ?? "Connection failed"));
- } catch (e) {
- get().updateServerStatus(server.id, "error", String(e));
- }
- }
- },
- mergePreloadedMcpServers: (preloaded) => {
- const { deletedPreloadMcpServers, nativeServers } = get();
- const targetSource: CliMcpSource = "zcodeagentmcp";
- const nextNativeServers = nativeServers.slice();
- let changed = false;
- for (const [name, cfg] of Object.entries(preloaded)) {
- const serverId = makeServerId(targetSource, name);
- const exists = nextNativeServers.some((server) => server.source === targetSource && server.name === name && !server.projectPath);
- if (deletedPreloadMcpServers.has(serverId) || exists) continue;
- nextNativeServers.push({
- source: targetSource,
- scope: "user",
- name,
- config: cfg,
- enabled: true,
- });
- persistScopedChange(targetSource, { action: "upsert", source: targetSource, name, config: cfg });
- changed = true;
- }
- if (!changed) return;
- set(commitState({ nativeServers: nextNativeServers }));
- },
- deletePreloadedMcpServer: (source, name) => {
- const targetSource: CliMcpSource = source === "mcp" ? "zcodeagentmcp" : source;
- const key = makeServerId(targetSource, name);
- persistScopedChange(targetSource, { action: "delete", source: targetSource, name });
- set((state) => {
- const nextDeleted = new Set(state.deletedPreloadMcpServers);
- nextDeleted.add(key);
- safeWriteJson(MCP_DELETED_PRELOAD_KEY, Array.from(nextDeleted));
- const nextNativeServers = state.nativeServers.filter((server) => !(server.source === targetSource && server.name === name && !server.projectPath));
- return {
- deletedPreloadMcpServers: nextDeleted,
- nativeServers: nextNativeServers,
- servers: buildServerList(state.config, nextNativeServers, state.enabledStates, nextDeleted, state.servers),
- };
- });
- },
- setCurrentSessionId: (sessionId) => set({ currentSessionId: sessionId }),
- migrateLegacyCommonMcp: async () => {
- const result = await migrateLegacyCommonMcpFromDesktop(mcpPlatformService);
- const latestWorkspacePath = get().currentProjectPath || undefined;
- const currentNativeServers = get().nativeServers.length > 0
- ? get().nativeServers
- : await fetchNativeMcpServers(mcpPlatformService, { workspacePath: latestWorkspacePath });
- const migration = await importLegacyCommonServersToZCodeAgent(mcpPlatformService, result.servers ?? {}, currentNativeServers, result.sourcePath);
- if (migration.changed) {
- const servers = await fetchNativeMcpServers(mcpPlatformService, { workspacePath: latestWorkspacePath });
- set(commitState({ nativeServers: servers }));
- }
- return migration;
- },
- };
- });
|