refactor: update MCPService and process utilities for improved binary management
- Refactored MCPService to streamline command handling for 'npx' and 'uvx', removing unnecessary installation checks and directly retrieving binary paths. - Updated getBinaryPath and isBinaryExists functions to be asynchronous, enhancing their reliability in checking binary existence and paths. - Cleaned up imports and removed unused dependencies for better code clarity.
This commit is contained in:
parent
0e44f9cd2a
commit
0863cfb2af
@ -1,13 +1,11 @@
|
|||||||
import { isLinux, isMac, isWin } from '@main/constant'
|
import { isLinux, isMac, isWin } from '@main/constant'
|
||||||
import { getBinaryPath, isBinaryExists, runInstallScript } from '@main/utils/process'
|
import { getBinaryPath } from '@main/utils/process'
|
||||||
import type { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
import type { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
||||||
import type { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
import type { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
||||||
import type { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
|
import type { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
|
||||||
import { MCPServer, MCPTool } from '@types'
|
import { MCPServer, MCPTool } from '@types'
|
||||||
import log from 'electron-log'
|
import log from 'electron-log'
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import os from 'os'
|
|
||||||
import path from 'path'
|
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
|
||||||
import { windowService } from './WindowService'
|
import { windowService } from './WindowService'
|
||||||
@ -327,16 +325,13 @@ export default class MCPService extends EventEmitter {
|
|||||||
transport = new this.sseTransport!(new URL(baseUrl))
|
transport = new this.sseTransport!(new URL(baseUrl))
|
||||||
} else if (command) {
|
} else if (command) {
|
||||||
let cmd: string = command
|
let cmd: string = command
|
||||||
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin')
|
|
||||||
log.info(`[MCP] Using binaries directory: ${binariesDir}`)
|
|
||||||
if (command === 'npx') {
|
if (command === 'npx') {
|
||||||
// check if cmd exists, if not exist, install it using `node scripts/install-bun.js`
|
cmd = await getBinaryPath('bun')
|
||||||
const isBunExist = await isBinaryExists('bun')
|
|
||||||
if (!isBunExist) {
|
if (cmd === 'bun') {
|
||||||
log.info(`[MCP] Installing bun...`)
|
cmd = 'npx'
|
||||||
await runInstallScript('install-bun.js')
|
|
||||||
}
|
}
|
||||||
cmd = getBinaryPath('bun')
|
|
||||||
log.info(`[MCP] Using command: ${cmd}`)
|
log.info(`[MCP] Using command: ${cmd}`)
|
||||||
|
|
||||||
// add -x to args if args exist
|
// add -x to args if args exist
|
||||||
@ -344,21 +339,16 @@ export default class MCPService extends EventEmitter {
|
|||||||
if (!args.includes('-y')) {
|
if (!args.includes('-y')) {
|
||||||
args.unshift('-y')
|
args.unshift('-y')
|
||||||
}
|
}
|
||||||
if (!args.includes('x')) {
|
if (cmd.includes('bun') && !args.includes('x')) {
|
||||||
args.unshift('x')
|
args.unshift('x')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (command === 'uvx') {
|
} else if (command === 'uvx') {
|
||||||
// check if cmd exists, if not exist, install it using `node scripts/install-uv.js`
|
cmd = await getBinaryPath('uvx')
|
||||||
const isUvxExist = await isBinaryExists('uvx')
|
|
||||||
if (!isUvxExist) {
|
|
||||||
log.info(`[MCP] Installing uvx...`)
|
|
||||||
await runInstallScript('install-uv.js')
|
|
||||||
}
|
|
||||||
cmd = getBinaryPath('uvx')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info(`[MCP] Starting server with command: ${cmd} ${args ? args.join(' ') : ''}`)
|
log.info(`[MCP] Starting server with command: ${cmd} ${args ? args.join(' ') : ''}`)
|
||||||
|
|
||||||
transport = new this.stdioTransport!({
|
transport = new this.stdioTransport!({
|
||||||
command: cmd,
|
command: cmd,
|
||||||
args,
|
args,
|
||||||
|
|||||||
@ -35,16 +35,15 @@ export function runInstallScript(scriptPath: string): Promise<void> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getBinaryPath(name: string): string {
|
export async function getBinaryPath(name: string): Promise<string> {
|
||||||
|
let cmd = process.platform === 'win32' ? `${name}.exe` : name
|
||||||
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin')
|
const binariesDir = path.join(os.homedir(), '.cherrystudio', 'bin')
|
||||||
let cmd = path.join(binariesDir, name)
|
const binariesDirExists = await fs.existsSync(binariesDir)
|
||||||
cmd = process.platform === 'win32' ? `${cmd}.exe` : cmd
|
cmd = binariesDirExists ? path.join(binariesDir, name) : name
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isBinaryExists(name: string): Promise<boolean> {
|
export async function isBinaryExists(name: string): Promise<boolean> {
|
||||||
return new Promise((resolve) => {
|
const cmd = await getBinaryPath(name)
|
||||||
const cmd = getBinaryPath(name)
|
return await fs.existsSync(cmd)
|
||||||
resolve(fs.existsSync(cmd))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user