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 <zhixuan.li@banosuperapp.com>
This commit is contained in:
parent
6d568688ed
commit
4bf15aed25
@ -2,6 +2,7 @@ import os from 'node:os'
|
|||||||
import path from 'node:path'
|
import path from 'node:path'
|
||||||
|
|
||||||
import { isLinux, isMac, isWin } from '@main/constant'
|
import { isLinux, isMac, isWin } from '@main/constant'
|
||||||
|
import { makeSureDirExists } from '@main/utils'
|
||||||
import { getBinaryName, getBinaryPath } from '@main/utils/process'
|
import { getBinaryName, getBinaryPath } from '@main/utils/process'
|
||||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
||||||
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
||||||
@ -82,12 +83,18 @@ class McpService {
|
|||||||
args.unshift('x')
|
args.unshift('x')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (server.registryUrl) {
|
if (server.registryUrl) {
|
||||||
server.env = {
|
server.env = {
|
||||||
...server.env,
|
...server.env,
|
||||||
NPM_CONFIG_REGISTRY: server.registryUrl
|
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') {
|
} else if (server.command === 'uvx' || server.command === 'uv') {
|
||||||
cmd = await getBinaryPath(server.command)
|
cmd = await getBinaryPath(server.command)
|
||||||
|
|||||||
@ -46,3 +46,9 @@ export function dumpPersistState() {
|
|||||||
export const runAsyncFunction = async (fn: () => void) => {
|
export const runAsyncFunction = async (fn: () => void) => {
|
||||||
await fn()
|
await fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function makeSureDirExists(dir: string) {
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir, { recursive: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -42,7 +42,11 @@ export async function getBinaryName(name: string): Promise<string> {
|
|||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBinaryPath(name: string): Promise<string> {
|
export async function getBinaryPath(name?: string): Promise<string> {
|
||||||
|
if (!name) {
|
||||||
|
return path.join(os.homedir(), '.cherrystudio', 'bin')
|
||||||
|
}
|
||||||
|
|
||||||
const binaryName = await getBinaryName(name)
|
const binaryName = await getBinaryName(name)
|
||||||
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin')
|
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin')
|
||||||
const binariesDirExists = await fs.existsSync(binariesDir)
|
const binariesDirExists = await fs.existsSync(binariesDir)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user