| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937 |
- /* eslint-disable max-lines -- MCP 同步服务集中维护用户目录读写、远端导入和 filesystem 路径改写,拆分会增加远端配置同步回归面。 */
- import { createHash } from "node:crypto";
- import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
- import { homedir } from "node:os";
- import { basename, dirname, join, posix, win32 } from "node:path";
- import type {
- LoadCliMcpFromUserDirectoryRequest,
- LoadCliMcpFromUserDirectoryResult,
- McpScope,
- McpServerConfig,
- McpSyncCandidate,
- McpSyncExportedServer,
- McpSyncImportResult,
- McpSyncRemoteStatus,
- McpSyncSource,
- NativeMcpServerRecord,
- SaveCliMcpToUserDirectoryRequest,
- SettingsDirectoryLocation,
- SettingsDirectorySource,
- } from "@zcode/shared";
- import type { IMcpSyncService } from "./mcpSync.js";
- import { checkRemoteSyncDirectoryWriteAccess } from "../remote-sync/remoteSyncWriteAccess.js";
- type McpConfigKeyName = "mcp.servers" | "mcpServers";
- interface DirectoryMcpDescriptor {
- source: McpSyncSource;
- directorySource: SettingsDirectorySource;
- userConfigDirSegments: string[];
- workspaceConfigDirSegments: string[];
- fileName: string;
- configKeyName: McpConfigKeyName;
- }
- interface UserMcpRecord {
- name: string;
- config: McpServerConfig;
- enabled: boolean;
- source: McpSyncSource;
- path: string;
- }
- const ZCODE_MCP_DESCRIPTOR: DirectoryMcpDescriptor = {
- source: "zcode",
- directorySource: "zcode",
- userConfigDirSegments: [".zcode", "cli"],
- workspaceConfigDirSegments: [".zcode"],
- fileName: "config.json",
- configKeyName: "mcp.servers",
- };
- const AGENTS_MCP_DESCRIPTOR: DirectoryMcpDescriptor = {
- source: "agents",
- directorySource: "agents",
- userConfigDirSegments: [".agents"],
- workspaceConfigDirSegments: [".agents"],
- fileName: "mcp.json",
- configKeyName: "mcpServers",
- };
- const ENABLED_KEY = "enabled";
- // 历史遗留:桌面端早期把停用状态写成 enable,而 CLI 契约字段(contracts McpServerConfigBase)
- // 一直是 enabled,导致同一条 server 出现两套口径、停用后仍被 agent 拉起。
- // 现在读写逻辑一律只认 enabled,这里只保留一次性迁移;存量配置清空后整块删除。
- const LEGACY_ENABLE_KEY = "enable";
- const SECRET_CONFIG_FILE_MODE = 0o600;
- const DIRECTORY_MCP_DESCRIPTORS: readonly DirectoryMcpDescriptor[] = [
- ZCODE_MCP_DESCRIPTOR,
- AGENTS_MCP_DESCRIPTOR,
- ];
- export interface McpSyncServiceDependencies {
- /**
- * MCP server 运行态状态检查的执行面。真实 connect/listTools 必须发生在
- * agent 进程(workspace 的 PATH/cwd 环境),host 侧没有可替代实现;无该依赖的
- * 构造(纯目录同步用途/单测)调用 listWorkspaceMcpServerStatuses 会显式抛错。
- */
- listMcpServerStatuses?: IMcpSyncService["listWorkspaceMcpServerStatuses"];
- }
- export function createMcpSyncService(
- dependencies: McpSyncServiceDependencies = {},
- ): IMcpSyncService {
- return {
- async loadMcpFromUserDirectory(request) {
- return loadMcpFromUserDirectory(request);
- },
- async listWorkspaceMcpServerStatuses(params) {
- if (!dependencies.listMcpServerStatuses) {
- throw new Error("MCP server status listing is unavailable in this runtime");
- }
- return dependencies.listMcpServerStatuses(params);
- },
- async saveMcpToUserDirectory(payload) {
- await saveMcpToUserDirectory(payload);
- },
- async listLocalUserMcpCandidates() {
- const localHomeDir = resolveUserHomeDir();
- return {
- candidates: (await collectEffectiveUserMcpRecords()).map(
- recordToCandidate,
- ),
- localHomeDir,
- };
- },
- async listRemoteUserMcpStatuses(params) {
- const remoteHomeDir = resolveUserHomeDir();
- const existingByName = await collectEffectiveUserMcpRecordByName();
- return {
- remoteHomeDir,
- statuses: params.names.map((name): McpSyncRemoteStatus => {
- const existing = existingByName.get(normalizeMcpNameKey(name));
- return existing
- ? { name, exists: true, path: existing.path }
- : { name, exists: false };
- }),
- };
- },
- async exportMcpServers(params) {
- const candidates = (await collectEffectiveUserMcpRecords()).map(
- recordToCandidate,
- );
- const candidateById = new Map(
- candidates.map((candidate) => [candidate.id, candidate]),
- );
- return {
- localHomeDir: resolveUserHomeDir(),
- servers: params.serverIds.map((id): McpSyncExportedServer => {
- const candidate = candidateById.get(id);
- if (!candidate) {
- throw new Error(`mcp sync candidate not found: ${id}`);
- }
- return {
- id: candidate.id,
- name: candidate.name,
- config: cloneMcpConfig(candidate.config),
- enabled: candidate.enabled,
- source: candidate.source,
- path: candidate.path,
- };
- }),
- };
- },
- async checkRemoteUserMcpWriteAccess() {
- return checkRemoteSyncDirectoryWriteAccess(dirname(getUserZcodeMcpConfigPath()));
- },
- async importMcpServers(params) {
- if (params.overwrite) {
- throw new Error("mcp sync overwrite is not supported");
- }
- return await importMcpServers(params);
- },
- };
- }
- function resolveUserHomeDir(): string {
- return (
- process.env.HOME?.trim() || process.env.USERPROFILE?.trim() || homedir()
- );
- }
- function buildDirectoryConfigPath(
- descriptor: DirectoryMcpDescriptor,
- scope: Exclude<McpScope, "common">,
- workspacePath?: string,
- ): string {
- const baseDir = scope === "user" ? resolveUserHomeDir() : workspacePath;
- if (!baseDir) {
- throw new Error(
- `Missing workspace path for ${descriptor.directorySource} workspace MCP config`,
- );
- }
- const segments =
- scope === "user"
- ? descriptor.userConfigDirSegments
- : descriptor.workspaceConfigDirSegments;
- return join(baseDir, ...segments, descriptor.fileName);
- }
- function buildUserConfigPath(descriptor: DirectoryMcpDescriptor): string {
- return buildDirectoryConfigPath(descriptor, "user");
- }
- function getUserZcodeMcpConfigPath(): string {
- return buildUserConfigPath(ZCODE_MCP_DESCRIPTOR);
- }
- function buildDirectoryMcpLocation(
- descriptor: DirectoryMcpDescriptor,
- scope: Exclude<McpScope, "common">,
- workspacePath?: string,
- ): SettingsDirectoryLocation {
- const filePath = buildDirectoryConfigPath(descriptor, scope, workspacePath);
- return {
- source: descriptor.directorySource,
- scope: scope === "workspace" ? "project" : "user",
- directoryPath: dirname(filePath),
- ...(workspacePath ? { projectPath: workspacePath } : {}),
- };
- }
- function findDescriptorByLocation(
- location: SettingsDirectoryLocation,
- ): DirectoryMcpDescriptor {
- const descriptor = DIRECTORY_MCP_DESCRIPTORS.find(
- (item) => item.directorySource === location.source,
- );
- if (!descriptor) {
- throw new Error(
- `Unsupported MCP settings directory source: ${location.source}`,
- );
- }
- return descriptor;
- }
- async function collectEffectiveUserMcpRecords(): Promise<UserMcpRecord[]> {
- const zcodeRecords = await readUserMcpRecordsFromFile(ZCODE_MCP_DESCRIPTOR);
- if (zcodeRecords.length > 0) {
- return sortMcpRecords(zcodeRecords);
- }
- return sortMcpRecords(
- await readUserMcpRecordsFromFile(AGENTS_MCP_DESCRIPTOR),
- );
- }
- async function collectEffectiveUserMcpRecordByName(): Promise<
- Map<string, UserMcpRecord>
- > {
- const result = new Map<string, UserMcpRecord>();
- for (const record of await collectEffectiveUserMcpRecords()) {
- const nameKey = normalizeMcpNameKey(record.name);
- if (!result.has(nameKey)) {
- result.set(nameKey, record);
- }
- }
- return result;
- }
- async function loadMcpFromUserDirectory(
- request?: LoadCliMcpFromUserDirectoryRequest,
- ): Promise<LoadCliMcpFromUserDirectoryResult> {
- const servers: NativeMcpServerRecord[] = [];
- if (request?.workspacePath) {
- servers.push(
- ...(await readDirectoryServersFromPreferredSources(
- "workspace",
- request.workspacePath,
- )),
- );
- }
- servers.push(
- ...(await readDirectoryServersFromPreferredSources(
- "user",
- request?.workspacePath,
- )),
- );
- return { servers };
- }
- async function saveMcpToUserDirectory(
- payload: SaveCliMcpToUserDirectoryRequest,
- ): Promise<void> {
- if (payload.action === "set-enabled") {
- if (typeof payload.enabled !== "boolean") {
- throw new Error("Missing enabled value for MCP set-enabled action");
- }
- const scope: Exclude<McpScope, "common"> = payload.projectPath
- ? "workspace"
- : "user";
- const location =
- payload.location ??
- buildDirectoryMcpLocation(
- ZCODE_MCP_DESCRIPTOR,
- scope,
- payload.projectPath,
- );
- await writeServerEnabledToFile(
- findDescriptorByLocation(location),
- location,
- payload.name,
- payload.enabled,
- );
- await cleanupLegacyMcpEnabledOverride(location, payload.name);
- return;
- }
- const scope: Exclude<McpScope, "common"> = payload.projectPath
- ? "workspace"
- : "user";
- const existingServers = await readDirectoryServersFromFile(
- ZCODE_MCP_DESCRIPTOR,
- scope,
- payload.projectPath,
- );
- const nextServers = Object.fromEntries(
- existingServers.map((server) => [
- server.name,
- server.config as Record<string, unknown>,
- ]),
- );
- if (payload.action === "upsert") {
- if (!payload.config) {
- throw new Error("Missing MCP config for upsert action");
- }
- nextServers[payload.name] = payload.config as Record<string, unknown>;
- } else {
- delete nextServers[payload.name];
- }
- await writeZCodeServersToFile(scope, nextServers, payload.projectPath);
- }
- function sortMcpRecords(records: UserMcpRecord[]): UserMcpRecord[] {
- return records.sort((left, right) => left.name.localeCompare(right.name));
- }
- async function readDirectoryServersFromPreferredSources(
- scope: Exclude<McpScope, "common">,
- workspacePath?: string,
- ): Promise<NativeMcpServerRecord[]> {
- const zcodeServers = await readDirectoryServersFromFile(
- ZCODE_MCP_DESCRIPTOR,
- scope,
- workspacePath,
- );
- if (zcodeServers.length > 0) {
- return zcodeServers;
- }
- return readDirectoryServersFromFile(
- AGENTS_MCP_DESCRIPTOR,
- scope,
- workspacePath,
- );
- }
- async function readDirectoryServersFromFile(
- descriptor: DirectoryMcpDescriptor,
- scope: Exclude<McpScope, "common">,
- workspacePath?: string,
- ): Promise<NativeMcpServerRecord[]> {
- const filePath = buildDirectoryConfigPath(descriptor, scope, workspacePath);
- const parsed = await readJsonObject(filePath);
- if (!parsed) {
- return [];
- }
- const serverMap = await readServerMapWithLegacyMigration(
- filePath,
- parsed,
- descriptor.configKeyName,
- );
- const location = buildDirectoryMcpLocation(descriptor, scope, workspacePath);
- return Object.entries(serverMap).map(([name, config]) => ({
- source: "zcodeagentmcp",
- scope,
- name,
- config: config as McpServerConfig,
- enabled: readServerEnabled(config),
- projectPath: scope === "workspace" ? workspacePath : undefined,
- location,
- file: {
- format: "json",
- filePath,
- },
- }));
- }
- async function writeZCodeServersToFile(
- scope: Exclude<McpScope, "common">,
- servers: Record<string, Record<string, unknown>>,
- workspacePath?: string,
- ): Promise<void> {
- const filePath = buildDirectoryConfigPath(
- ZCODE_MCP_DESCRIPTOR,
- scope,
- workspacePath,
- );
- const current = (await readJsonObject(filePath)) ?? {};
- const next = writeServerMapToJson(
- current,
- ZCODE_MCP_DESCRIPTOR.configKeyName,
- servers,
- );
- await writeTextAtomic(filePath, `${JSON.stringify(next, null, 2)}\n`);
- }
- async function readUserCliConfig(): Promise<Record<string, unknown>> {
- return (await readJsonObject(getUserZcodeMcpConfigPath())) ?? {};
- }
- async function writeUserCliConfig(
- config: Record<string, unknown>,
- ): Promise<void> {
- await writeTextAtomic(
- getUserZcodeMcpConfigPath(),
- `${JSON.stringify(config, null, 2)}\n`,
- );
- }
- function removeLegacyMcpEnabledOverride(
- config: Record<string, unknown>,
- location: SettingsDirectoryLocation,
- name: string,
- ): { config: Record<string, unknown>; changed: boolean } {
- if (!isRecord(config.mcp) || !isRecord(config.mcp[location.directoryPath])) {
- return { config, changed: false };
- }
- const mcpConfig = { ...config.mcp };
- const pathConfig = {
- ...(mcpConfig[location.directoryPath] as Record<string, unknown>),
- };
- if (!(name in pathConfig)) {
- return { config, changed: false };
- }
- delete pathConfig[name];
- if (Object.keys(pathConfig).length > 0) {
- mcpConfig[location.directoryPath] = pathConfig;
- } else {
- delete mcpConfig[location.directoryPath];
- }
- return {
- config: {
- ...config,
- mcp: mcpConfig,
- },
- changed: true,
- };
- }
- async function cleanupLegacyMcpEnabledOverride(
- location: SettingsDirectoryLocation,
- name: string,
- ): Promise<void> {
- const userConfig = await readUserCliConfig();
- const result = removeLegacyMcpEnabledOverride(userConfig, location, name);
- if (result.changed) {
- await writeUserCliConfig(result.config);
- }
- }
- async function writeServerEnabledToFile(
- descriptor: DirectoryMcpDescriptor,
- location: SettingsDirectoryLocation,
- name: string,
- enabled: boolean,
- ): Promise<void> {
- const scope: Exclude<McpScope, "common"> =
- location.scope === "project" ? "workspace" : "user";
- const workspacePath =
- scope === "workspace" ? location.projectPath : undefined;
- const filePath = buildDirectoryConfigPath(descriptor, scope, workspacePath);
- const current = (await readJsonObject(filePath)) ?? {};
- const serverMap = readServerMapFromJson(current, descriptor.configKeyName);
- const currentServer = serverMap[name];
- if (!isRecord(currentServer)) {
- return;
- }
- // MCP 自身已有 mcp.servers/mcpServers 结构;禁用状态写在 server 配置对象内,
- // 避免把目录路径写到 mcp 顶层后和真实 MCP 配置混在一起。
- const nextServerMap = {
- ...serverMap,
- [name]: setServerEnabled(
- currentServer as McpServerConfig,
- enabled,
- ) as Record<string, unknown>,
- };
- let next = writeServerMapToJson(
- current,
- descriptor.configKeyName,
- nextServerMap,
- );
- const legacyCleanup = removeLegacyMcpEnabledOverride(next, location, name);
- if (legacyCleanup.changed) {
- next = legacyCleanup.config;
- }
- await writeTextAtomic(filePath, `${JSON.stringify(next, null, 2)}\n`);
- }
- async function readUserMcpRecordsFromFile(
- descriptor: DirectoryMcpDescriptor,
- ): Promise<UserMcpRecord[]> {
- const filePath = buildUserConfigPath(descriptor);
- const parsed = await readJsonObject(filePath);
- if (!parsed) {
- return [];
- }
- const serverMap = await readServerMapWithLegacyMigration(
- filePath,
- parsed,
- descriptor.configKeyName,
- );
- return Object.entries(serverMap).map(([name, config]) => ({
- name,
- config: config as McpServerConfig,
- enabled: readServerEnabled(config),
- source: descriptor.source,
- path: filePath,
- }));
- }
- function recordToCandidate(record: UserMcpRecord): McpSyncCandidate {
- return {
- id: createCandidateId(record),
- name: record.name,
- config: cloneMcpConfig(record.config),
- enabled: record.enabled,
- source: record.source,
- path: record.path,
- };
- }
- function createCandidateId(record: UserMcpRecord): string {
- return createHash("sha256")
- .update(`${record.source}:${record.path}:${record.name}`)
- .digest("hex");
- }
- function normalizeMcpNameKey(name: string): string {
- return name.trim().toLowerCase();
- }
- function readServerEnabled(config: Record<string, unknown>): boolean {
- return config[ENABLED_KEY] !== false;
- }
- function setServerEnabled(
- config: McpServerConfig,
- enabled: boolean,
- ): McpServerConfig {
- // 启用是默认态,不落盘冗余字段;同时清掉可能残留的 legacy enable,
- // 避免再产出 enable:false + enabled:true 这类自相矛盾的配置。
- const {
- [LEGACY_ENABLE_KEY]: _legacyEnable,
- [ENABLED_KEY]: _enabled,
- ...rest
- } = config;
- if (enabled) {
- return rest;
- }
- return { ...rest, [ENABLED_KEY]: false };
- }
- function migrateLegacyEnableFlag(
- serverMap: Record<string, Record<string, unknown>>,
- ): { servers: Record<string, Record<string, unknown>>; changed: boolean } {
- let changed = false;
- const migrated: Record<string, Record<string, unknown>> = {};
- for (const [name, config] of Object.entries(serverMap)) {
- if (!isRecord(config) || !(LEGACY_ENABLE_KEY in config)) {
- migrated[name] = config;
- continue;
- }
- changed = true;
- // 两个字段冲突时以「停用」为准:桌面端写 enable:false 时不会清理外部导入残留的
- // enabled:true,若按 enabled 取值会把用户关掉的 server 重新拉起。
- const disabled =
- config[LEGACY_ENABLE_KEY] === false || config[ENABLED_KEY] === false;
- migrated[name] = setServerEnabled(
- config as McpServerConfig,
- !disabled,
- ) as Record<string, unknown>;
- }
- return { servers: migrated, changed };
- }
- /**
- * 读取 server map,顺带把存量 enable 就地折叠成 enabled 并落盘。
- * 没有残留字段时不写文件,保证幂等;写盘失败不阻断读取,内存结果已是正确口径,下次加载会重试。
- */
- async function readServerMapWithLegacyMigration(
- filePath: string,
- parsed: Record<string, unknown>,
- configKeyName: McpConfigKeyName,
- ): Promise<Record<string, Record<string, unknown>>> {
- const migration = migrateLegacyEnableFlag(
- readServerMapFromJson(parsed, configKeyName),
- );
- if (!migration.changed) {
- return migration.servers;
- }
- try {
- const next = writeServerMapToJson(parsed, configKeyName, migration.servers);
- await writeTextAtomic(filePath, `${JSON.stringify(next, null, 2)}\n`);
- } catch (error) {
- console.warn(
- "[mcp-sync] legacy enable migration failed:",
- filePath,
- formatErrorMessage(error),
- );
- }
- return migration.servers;
- }
- function readServerMapFromJson(
- parsed: Record<string, unknown>,
- configKeyName: McpConfigKeyName,
- ): Record<string, Record<string, unknown>> {
- if (configKeyName === "mcp.servers") {
- const mcp = parsed.mcp;
- if (!isRecord(mcp)) {
- return {};
- }
- const servers = mcp.servers;
- return isRecord(servers)
- ? (servers as Record<string, Record<string, unknown>>)
- : {};
- }
- const rawServerMap = parsed[configKeyName];
- return isRecord(rawServerMap)
- ? (rawServerMap as Record<string, Record<string, unknown>>)
- : {};
- }
- function writeServerMapToJson(
- current: Record<string, unknown>,
- configKeyName: McpConfigKeyName,
- servers: Record<string, Record<string, unknown>>,
- ): Record<string, unknown> {
- if (configKeyName !== "mcp.servers") {
- return {
- ...current,
- [configKeyName]: servers,
- };
- }
- const currentMcp = isRecord(current.mcp) ? current.mcp : {};
- return {
- ...current,
- mcp: {
- ...currentMcp,
- servers,
- },
- };
- }
- async function importMcpServers(params: {
- servers: McpSyncExportedServer[];
- localHomeDir: string;
- localWorkspacePath?: string;
- remoteWorkspacePath?: string;
- }): Promise<McpSyncImportResult> {
- const targetPath = getUserZcodeMcpConfigPath();
- const current = (await readJsonObject(targetPath)) ?? {};
- const targetServers = readServerMapFromJson(
- current,
- ZCODE_MCP_DESCRIPTOR.configKeyName,
- );
- const existingByName = await collectEffectiveUserMcpRecordByName();
- const results: McpSyncImportResult["results"] = [];
- let changed = false;
- for (const server of params.servers) {
- const nameKey = normalizeMcpNameKey(server.name);
- const existingTarget = targetServers[server.name];
- if (existingTarget) {
- results.push({ name: server.name, status: "skipped", path: targetPath });
- continue;
- }
- const existing = existingByName.get(nameKey);
- if (existing) {
- results.push({
- name: server.name,
- status: "skipped",
- path: existing.path,
- });
- continue;
- }
- try {
- const rewrittenConfig = rewriteFilesystemMcpConfig(
- server.name,
- setServerEnabled(cloneMcpConfig(server.config), server.enabled),
- {
- localHomeDir: params.localHomeDir,
- localWorkspacePath: params.localWorkspacePath,
- remoteHomeDir: resolveUserHomeDir(),
- remoteWorkspacePath: params.remoteWorkspacePath,
- },
- );
- targetServers[server.name] = rewrittenConfig as Record<string, unknown>;
- existingByName.set(nameKey, {
- name: server.name,
- config: rewrittenConfig,
- enabled: server.enabled,
- source: "zcode",
- path: targetPath,
- });
- results.push({ name: server.name, status: "synced", path: targetPath });
- changed = true;
- } catch (error) {
- results.push({
- name: server.name,
- status: "failed",
- path: targetPath,
- error: error instanceof Error ? error.message : String(error),
- });
- }
- }
- if (changed) {
- await writeTextAtomic(
- targetPath,
- `${JSON.stringify(writeServerMapToJson(current, ZCODE_MCP_DESCRIPTOR.configKeyName, targetServers), null, 2)}\n`,
- );
- }
- return { results };
- }
- function rewriteFilesystemMcpConfig(
- name: string,
- config: McpServerConfig,
- paths: {
- localHomeDir: string;
- localWorkspacePath?: string;
- remoteHomeDir: string;
- remoteWorkspacePath?: string;
- },
- ): McpServerConfig {
- if (!isStdioMcpConfig(config) || !isFilesystemMcpServer(name, config)) {
- return config;
- }
- if (!Array.isArray(config.args)) {
- return config;
- }
- return {
- ...config,
- args: config.args.map((arg) => rewritePathArgForRemote(arg, paths)),
- };
- }
- function isStdioMcpConfig(config: McpServerConfig): boolean {
- const type =
- typeof config.type === "string" ? config.type.trim().toLowerCase() : "";
- if (!type && typeof config.command === "string" && config.command.trim()) {
- return true;
- }
- return type === "stdio";
- }
- function isFilesystemMcpServer(name: string, config: McpServerConfig): boolean {
- const normalizedName = name.trim().toLowerCase();
- if (
- normalizedName === "filesystem" ||
- normalizedName === "file-system" ||
- normalizedName === "fs"
- ) {
- return true;
- }
- const haystack = [
- config.command,
- ...(Array.isArray(config.args) ? config.args : []),
- ]
- .filter((value): value is string => typeof value === "string")
- .join(" ")
- .toLowerCase();
- return (
- haystack.includes("@modelcontextprotocol/server-filesystem") ||
- haystack.includes("mcp-server-filesystem")
- );
- }
- function rewritePathArgForRemote(
- arg: string,
- paths: {
- localHomeDir: string;
- localWorkspacePath?: string;
- remoteHomeDir: string;
- remoteWorkspacePath?: string;
- },
- ): string {
- const workspaceRelative = getRelativePathIfWithin(
- paths.localWorkspacePath,
- arg,
- );
- if (workspaceRelative !== null && paths.remoteWorkspacePath?.trim()) {
- return joinRemotePath(paths.remoteWorkspacePath, workspaceRelative);
- }
- const homeRelative = getRelativePathIfWithin(paths.localHomeDir, arg);
- if (homeRelative !== null) {
- return joinRemotePath(paths.remoteHomeDir, homeRelative);
- }
- return arg;
- }
- function getRelativePathIfWithin(
- basePath: string | undefined,
- candidatePath: string,
- ): string | null {
- if (!basePath?.trim()) {
- return null;
- }
- const base = normalizeComparablePath(basePath);
- const candidate = normalizeComparablePath(candidatePath);
- if (!base || !candidate) {
- return null;
- }
- const baseValue = base.caseInsensitive
- ? base.value.toLowerCase()
- : base.value;
- const candidateValue = base.caseInsensitive
- ? candidate.value.toLowerCase()
- : candidate.value;
- if (candidateValue === baseValue) {
- return "";
- }
- const prefix = baseValue.endsWith("/") ? baseValue : `${baseValue}/`;
- if (!candidateValue.startsWith(prefix)) {
- return null;
- }
- return candidate.value.slice(prefix.length);
- }
- function normalizeComparablePath(
- rawPath: string,
- ): { value: string; caseInsensitive: boolean } | null {
- const trimmed = rawPath.trim();
- if (!trimmed) {
- return null;
- }
- const isWindowsPath =
- /^[a-zA-Z]:[\\/]/u.test(trimmed) || trimmed.startsWith("\\\\");
- const isPosixPath = trimmed.startsWith("/");
- if (!isWindowsPath && !isPosixPath) {
- return null;
- }
- let value = trimmed.replaceAll("\\", "/");
- value = value.replace(/\/+$/u, "");
- if (value === "") {
- value = "/";
- }
- return {
- value,
- caseInsensitive: isWindowsPath,
- };
- }
- function joinRemotePath(remoteBasePath: string, relativePath: string): string {
- if (!relativePath) {
- return remoteBasePath;
- }
- const segments = relativePath.split(/[\\/]+/u).filter(Boolean);
- // MCP 同步运行在本机进程里,但 remoteWorkspacePath 描述的是远端主机路径。
- // 不能用 process.platform 决定拼接风格,否则 Windows 客户端同步到 Linux/WSL 时会把 /srv/... 写成 \srv\...。
- const normalizedBasePath = remoteBasePath.trim();
- if (normalizedBasePath.startsWith("/")) {
- return posix.join(normalizedBasePath.replaceAll("\\", "/"), ...segments);
- }
- if (
- /^[a-zA-Z]:[\\/]/u.test(normalizedBasePath) ||
- normalizedBasePath.startsWith("\\\\")
- ) {
- return win32.join(normalizedBasePath, ...segments);
- }
- return posix.join(normalizedBasePath.replaceAll("\\", "/"), ...segments);
- }
- function cloneMcpConfig(config: McpServerConfig): McpServerConfig {
- return JSON.parse(JSON.stringify(config)) as McpServerConfig;
- }
- function isRecord(value: unknown): value is Record<string, unknown> {
- return typeof value === "object" && value !== null && !Array.isArray(value);
- }
- async function readJsonObject(
- filePath: string,
- ): Promise<Record<string, unknown> | null> {
- let raw: string;
- try {
- raw = await readFile(filePath, "utf-8");
- } catch (error) {
- if (isErrnoException(error) && error.code === "ENOENT") {
- return null;
- }
- throw new Error(
- `无法读取 MCP 配置文件 ${filePath}: ${formatErrorMessage(error)}`,
- );
- }
- let parsed: unknown;
- try {
- parsed = JSON.parse(raw) as unknown;
- } catch (error) {
- // 远端 MCP 导入必须先确认现有 config.json 可读可合并;
- // JSON 损坏时如果当作空对象继续写,会静默覆盖 provider、MCP 和 secret 配置。
- throw new Error(
- `无法解析 MCP 配置文件 ${filePath}: ${formatErrorMessage(error)}`,
- );
- }
- if (!isRecord(parsed)) {
- throw new Error(`MCP 配置文件 ${filePath} 必须是 JSON 对象`);
- }
- return parsed;
- }
- function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
- return error instanceof Error && "code" in error;
- }
- function formatErrorMessage(error: unknown): string {
- return error instanceof Error ? error.message : String(error);
- }
- async function writeTextAtomic(
- filePath: string,
- content: string,
- ): Promise<void> {
- await mkdir(dirname(filePath), { recursive: true });
- const tempPath = join(
- dirname(filePath),
- `${basename(filePath)}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`,
- );
- try {
- // 远端 MCP 配置会持久化 env/header/token 等 secret;
- // 临时文件权限不能依赖远端 umask,否则 rename 后 config.json 可能被同组或其他用户读取。
- await writeFile(tempPath, content, {
- encoding: "utf-8",
- mode: SECRET_CONFIG_FILE_MODE,
- });
- await rename(tempPath, filePath);
- } catch (error) {
- await rm(tempPath, { force: true });
- throw error;
- }
- }
|