feat(MCPService): enhance PATH management with platform-specific directories
This commit is contained in:
parent
8b00ff4b93
commit
be39c5f40c
@ -1,6 +1,7 @@
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
|
||||
import { isLinux, isMac, isWin } from '@main/constant'
|
||||
import { getBinaryName, getBinaryPath } from '@main/utils/process'
|
||||
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
||||
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
||||
@ -96,7 +97,10 @@ class McpService {
|
||||
transport = new StdioClientTransport({
|
||||
command: cmd,
|
||||
args,
|
||||
env: server.env
|
||||
env: {
|
||||
PATH: this.getEnhancedPath(process.env.PATH || ''),
|
||||
...server.env
|
||||
}
|
||||
})
|
||||
} else {
|
||||
throw new Error('Either baseUrl or command must be provided')
|
||||
@ -203,6 +207,63 @@ class McpService {
|
||||
const bunPath = path.join(dir, bunName)
|
||||
return { dir, uvPath, bunPath }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get enhanced PATH including common tool locations
|
||||
*/
|
||||
private getEnhancedPath(originalPath: string): string {
|
||||
// 将原始 PATH 按分隔符分割成数组
|
||||
const pathSeparator = process.platform === 'win32' ? ';' : ':'
|
||||
const existingPaths = new Set(originalPath.split(pathSeparator).filter(Boolean))
|
||||
const homeDir = process.env.HOME || process.env.USERPROFILE || ''
|
||||
|
||||
// 定义要添加的新路径
|
||||
const newPaths: string[] = []
|
||||
|
||||
if (isMac) {
|
||||
newPaths.push(
|
||||
'/bin',
|
||||
'/usr/bin',
|
||||
'/usr/local/bin',
|
||||
'/usr/local/sbin',
|
||||
'/opt/homebrew/bin',
|
||||
'/opt/homebrew/sbin',
|
||||
'/usr/local/opt/node/bin',
|
||||
`${homeDir}/.nvm/current/bin`,
|
||||
`${homeDir}/.npm-global/bin`,
|
||||
`${homeDir}/.yarn/bin`,
|
||||
`${homeDir}/.cargo/bin`,
|
||||
'/opt/local/bin'
|
||||
)
|
||||
}
|
||||
|
||||
if (isLinux) {
|
||||
newPaths.push(
|
||||
'/bin',
|
||||
'/usr/bin',
|
||||
'/usr/local/bin',
|
||||
`${homeDir}/.nvm/current/bin`,
|
||||
`${homeDir}/.npm-global/bin`,
|
||||
`${homeDir}/.yarn/bin`,
|
||||
`${homeDir}/.cargo/bin`,
|
||||
'/snap/bin'
|
||||
)
|
||||
}
|
||||
|
||||
if (isWin) {
|
||||
newPaths.push(`${process.env.APPDATA}\\npm`, `${homeDir}\\AppData\\Local\\Yarn\\bin`, `${homeDir}\\.cargo\\bin`)
|
||||
}
|
||||
|
||||
// 只添加不存在的路径
|
||||
newPaths.forEach((path) => {
|
||||
if (path && !existingPaths.has(path)) {
|
||||
existingPaths.add(path)
|
||||
}
|
||||
})
|
||||
|
||||
// 转换回字符串
|
||||
return Array.from(existingPaths).join(pathSeparator)
|
||||
}
|
||||
}
|
||||
|
||||
export default new McpService()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user