fix: respect desktop MCP port environment

This commit is contained in:
ekko
2026-06-05 17:44:59 +08:00
committed by ekko
parent b10dd6193b
commit 6de28fa63a
3 changed files with 39 additions and 1 deletions
+19 -1
View File
@@ -38,6 +38,7 @@ interface CliShimInstallOptions {
interface McpShimInstallOptions extends CliShimInstallOptions {
nodePath?: string
scriptPath?: string
webUiUrl?: string
}
function platformDelimiter(platform: NodeJS.Platform): string {
@@ -125,6 +126,7 @@ export function createShimContent(
export function createMcpShimContent(
nodePath: string,
scriptPath: string,
webUiUrl = 'http://127.0.0.1:8748',
platform: NodeJS.Platform = process.platform,
): string {
if (platform === 'win32') {
@@ -142,6 +144,13 @@ export function createMcpShimContent(
' echo Hermes Studio MCP script not found at "%SCRIPT%" 1>&2',
' exit /b 127',
')',
'if "%HERMES_WEB_UI_URL%"=="" (',
' if "%HERMES_DESKTOP_PORT%"=="" (',
` set "HERMES_WEB_UI_URL=${webUiUrl}"`,
' ) else (',
' set "HERMES_WEB_UI_URL=http://127.0.0.1:%HERMES_DESKTOP_PORT%"',
' )',
')',
'set "HERMES_MCP_SERVER_NAME=hermes-studio-mcp"',
'"%NODE%" "%SCRIPT%" %*',
'exit /b %ERRORLEVEL%',
@@ -163,6 +172,14 @@ export function createMcpShimContent(
' echo "Hermes Studio MCP script not found at $SCRIPT" >&2',
' exit 127',
'fi',
'if [ -z "${HERMES_WEB_UI_URL:-}" ]; then',
' if [ -n "${HERMES_DESKTOP_PORT:-}" ]; then',
' HERMES_WEB_UI_URL="http://127.0.0.1:${HERMES_DESKTOP_PORT}"',
' else',
` HERMES_WEB_UI_URL=${shellQuote(webUiUrl)}`,
' fi',
'fi',
'export HERMES_WEB_UI_URL',
'export HERMES_MCP_SERVER_NAME=hermes-studio-mcp',
'exec "$NODE" "$SCRIPT" "$@"',
'',
@@ -311,9 +328,10 @@ export async function installHermesStudioMcpShim(options: McpShimInstallOptions
const shimPath = mcpShimPathForPlatform(binDir, platform)
const nodePath = options.nodePath || process.execPath
const scriptPath = options.scriptPath || resolve(process.cwd(), 'bin', 'hermes-web-ui-mcp.mjs')
const webUiUrl = options.webUiUrl || 'http://127.0.0.1:8748'
mkdirSync(binDir, { recursive: true })
const status = writeShim(shimPath, createMcpShimContent(nodePath, scriptPath, platform), platform, MCP_SHIM_MARKER)
const status = writeShim(shimPath, createMcpShimContent(nodePath, scriptPath, webUiUrl, platform), platform, MCP_SHIM_MARKER)
const pathUpdated = await ensureUserBinOnPath(homeDir, binDir, platform, env).catch((err) => {
console.warn(`[cli-shim] failed to update PATH: ${err instanceof Error ? err.message : String(err)}`)
return false
+1
View File
@@ -454,6 +454,7 @@ function runDesktopApp() {
installHermesStudioMcpShim({
nodePath: bundledNode(),
scriptPath: join(webuiDir(), 'bin', 'hermes-web-ui-mcp.mjs'),
webUiUrl: `http://127.0.0.1:${PORT}`,
}).then(result => {
if (result.status === 'skipped') {
console.warn(`[cli-shim] ${result.reason}: ${result.shimPath}`)
+19
View File
@@ -3,6 +3,7 @@ import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it } from 'vitest'
import {
createMcpShimContent,
createShimContent,
installHermesStudioCliShim,
pathContainsDir,
@@ -47,6 +48,24 @@ describe('Hermes Studio CLI shim', () => {
expect(content).not.toContain('"%APP%" -- --hermes-cli')
})
it('sets the desktop MCP URL from HERMES_DESKTOP_PORT when present', () => {
const content = createMcpShimContent('/runtime/node', '/resources/webui/bin/hermes-web-ui-mcp.mjs', 'http://127.0.0.1:8748', 'darwin')
expect(content).toContain('if [ -n "${HERMES_DESKTOP_PORT:-}" ]; then')
expect(content).toContain('HERMES_WEB_UI_URL="http://127.0.0.1:${HERMES_DESKTOP_PORT}"')
expect(content).toContain("HERMES_WEB_UI_URL='http://127.0.0.1:8748'")
expect(content).toContain('export HERMES_MCP_SERVER_NAME=hermes-studio-mcp')
})
it('sets the desktop MCP URL from HERMES_DESKTOP_PORT in Windows shims', () => {
const content = createMcpShimContent('C:\\runtime\\node.exe', 'C:\\resources\\webui\\bin\\hermes-web-ui-mcp.mjs', 'http://127.0.0.1:8748', 'win32')
expect(content).toContain('if "%HERMES_DESKTOP_PORT%"=="" (')
expect(content).toContain('set "HERMES_WEB_UI_URL=http://127.0.0.1:8748"')
expect(content).toContain('set "HERMES_WEB_UI_URL=http://127.0.0.1:%HERMES_DESKTOP_PORT%"')
expect(content).toContain('set "HERMES_MCP_SERVER_NAME=hermes-studio-mcp"')
})
it('detects user bin paths with platform-specific separators', () => {
expect(pathContainsDir('/usr/bin:/Users/example/bin', '/Users/example/bin', 'darwin')).toBe(true)
expect(pathContainsDir('C:\\Windows;C:\\Users\\Example\\bin', 'C:\\Users\\Example\\bin', 'win32')).toBe(true)