mcpStoreMigration.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import type { McpServerConfig, NativeMcpServerRecord } from "@zcode/shared";
  2. import { logger } from "@/logger.js";
  3. import {
  4. fetchNativeMcpServers,
  5. persistCliMcpToUserDirectory,
  6. type McpPlatformService,
  7. type MigrateLegacyResult,
  8. } from "@/store/mcpStoreDesktop.js";
  9. import {
  10. clearLegacyCommonMcpServers,
  11. readLegacyCommonMcpServers,
  12. } from "@/store/mcpStoreHelpers.js";
  13. interface CommonMcpMigrationResult extends MigrateLegacyResult {
  14. completed: boolean;
  15. changed: boolean;
  16. }
  17. function isZCodeAgentUserServer(server: NativeMcpServerRecord): boolean {
  18. return (
  19. server.source === "zcodeagentmcp" &&
  20. server.scope === "user" &&
  21. (!server.location || server.location.source === "zcode")
  22. );
  23. }
  24. export async function importLegacyCommonServersToZCodeAgent(
  25. platform: McpPlatformService | null,
  26. legacyServers: Record<string, McpServerConfig>,
  27. nativeServers: NativeMcpServerRecord[],
  28. sourcePath?: string,
  29. ): Promise<CommonMcpMigrationResult> {
  30. const entries = Object.entries(legacyServers);
  31. const totalCount = entries.length;
  32. if (totalCount === 0) {
  33. return {
  34. totalCount: 0,
  35. importedCount: 0,
  36. skippedCount: 0,
  37. sourcePath,
  38. completed: true,
  39. changed: false,
  40. };
  41. }
  42. const existingNames = new Set(
  43. nativeServers.filter(isZCodeAgentUserServer).map((server) => server.name),
  44. );
  45. let importedCount = 0;
  46. let skippedCount = 0;
  47. for (const [name, config] of entries) {
  48. if (existingNames.has(name)) {
  49. skippedCount += 1;
  50. continue;
  51. }
  52. try {
  53. const persisted = await persistCliMcpToUserDirectory(platform, {
  54. action: "upsert",
  55. source: "zcodeagentmcp",
  56. name,
  57. config,
  58. });
  59. if (!persisted) {
  60. return {
  61. totalCount,
  62. importedCount,
  63. skippedCount,
  64. sourcePath,
  65. completed: false,
  66. changed: importedCount > 0,
  67. };
  68. }
  69. } catch (error) {
  70. logger.warn(`[mcpStore] migrate legacy common MCP ${name} failed`, String(error));
  71. return {
  72. totalCount,
  73. importedCount,
  74. skippedCount,
  75. sourcePath,
  76. completed: false,
  77. changed: importedCount > 0,
  78. };
  79. }
  80. existingNames.add(name);
  81. importedCount += 1;
  82. }
  83. return {
  84. totalCount,
  85. importedCount,
  86. skippedCount,
  87. sourcePath,
  88. completed: true,
  89. changed: importedCount > 0,
  90. };
  91. }
  92. export async function migrateStoredCommonMcpToZCodeAgent(
  93. platform: McpPlatformService | null,
  94. nativeServers: NativeMcpServerRecord[],
  95. workspacePath?: string,
  96. ): Promise<NativeMcpServerRecord[]> {
  97. const legacyServers = readLegacyCommonMcpServers();
  98. if (Object.keys(legacyServers).length === 0) {
  99. return nativeServers;
  100. }
  101. // 旧通用 MCP 保存在 localStorage,不迁移就直接去掉 common 读取会让用户配置从设置页和运行时消失。
  102. const migration = await importLegacyCommonServersToZCodeAgent(
  103. platform,
  104. legacyServers,
  105. nativeServers,
  106. "localStorage:zcode-mcp-config",
  107. );
  108. if (migration.completed) {
  109. // 只有确认写入 zcode agent 目录后才清理旧数据,避免 Web 端没有 desktop bridge 时丢配置。
  110. clearLegacyCommonMcpServers();
  111. }
  112. if (!migration.changed) {
  113. return nativeServers;
  114. }
  115. logger.info(
  116. `[mcpStore] migrated ${migration.importedCount} legacy common MCP servers to zcode agent config`,
  117. );
  118. return fetchNativeMcpServers(platform, { workspacePath });
  119. }