From 4bf15aed25f407ac2aace6fce8ca3a927db5bf4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=A2=E5=A5=8B=E7=8C=AB?= Date: Tue, 1 Apr 2025 20:57:56 +0800 Subject: [PATCH] refactor(MCPService, process): Updated MCPService to conditionally set the NPM_CONFIG_REGISTRY * refactor(MCPService, process): enhance registry URL handling and improve getBinaryPath function - Updated MCPService to conditionally set the NPM_CONFIG_REGISTRY based on server name, improving flexibility for auto-install scenarios. - Modified getBinaryPath function to handle optional name parameter, returning a default path when no name is provided, enhancing usability. * refactor(MCPService, utils): add directory existence check for registry file - Introduced makeSureDirExists utility function to ensure the specified directory exists, enhancing robustness. - Updated MCPService to utilize this function when setting the registry URL for the mcp-auto-install server, improving error handling. * feat:change MCP_REGISTRY_PATH * refactor(MCPService): streamline environment variable setup for mcp-auto-install - Updated MCPService to conditionally set NPM_CONFIG_REGISTRY and MCP_REGISTRY_PATH in a more concise manner. - Enhanced readability by removing redundant code while maintaining functionality. --------- Co-authored-by: lizhixuan --- src/main/services/MCPService.ts | 9 ++++++++- src/main/utils/index.ts | 6 ++++++ src/main/utils/process.ts | 6 +++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/services/MCPService.ts b/src/main/services/MCPService.ts index 21a518aa..e8a0807b 100644 --- a/src/main/services/MCPService.ts +++ b/src/main/services/MCPService.ts @@ -2,6 +2,7 @@ import os from 'node:os' import path from 'node:path' import { isLinux, isMac, isWin } from '@main/constant' +import { makeSureDirExists } from '@main/utils' import { getBinaryName, getBinaryPath } from '@main/utils/process' import { Client } from '@modelcontextprotocol/sdk/client/index.js' import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js' @@ -82,12 +83,18 @@ class McpService { args.unshift('x') } } - if (server.registryUrl) { server.env = { ...server.env, NPM_CONFIG_REGISTRY: server.registryUrl } + + // if the server name is mcp-auto-install, use the mcp-registry.json file in the bin directory + if (server.name === 'mcp-auto-install') { + const binPath = await getBinaryPath() + makeSureDirExists(binPath) + server.env.MCP_REGISTRY_PATH = path.join(binPath, 'mcp-registry.json') + } } } else if (server.command === 'uvx' || server.command === 'uv') { cmd = await getBinaryPath(server.command) diff --git a/src/main/utils/index.ts b/src/main/utils/index.ts index 29595621..4a6fde67 100644 --- a/src/main/utils/index.ts +++ b/src/main/utils/index.ts @@ -46,3 +46,9 @@ export function dumpPersistState() { export const runAsyncFunction = async (fn: () => void) => { await fn() } + +export function makeSureDirExists(dir: string) { + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }) + } +} diff --git a/src/main/utils/process.ts b/src/main/utils/process.ts index e10cdc4b..36a0d731 100644 --- a/src/main/utils/process.ts +++ b/src/main/utils/process.ts @@ -42,7 +42,11 @@ export async function getBinaryName(name: string): Promise { return name } -export async function getBinaryPath(name: string): Promise { +export async function getBinaryPath(name?: string): Promise { + if (!name) { + return path.join(os.homedir(), '.cherrystudio', 'bin') + } + const binaryName = await getBinaryName(name) const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin') const binariesDirExists = await fs.existsSync(binariesDir)