From 6de28fa63af1f01e34539c720ff57d28e556e9e8 Mon Sep 17 00:00:00 2001 From: ekko Date: Fri, 5 Jun 2026 17:44:59 +0800 Subject: [PATCH] fix: respect desktop MCP port environment --- packages/desktop/src/main/cli-shim.ts | 20 +++++++++++++++++++- packages/desktop/src/main/index.ts | 1 + tests/desktop/cli-shim.test.ts | 19 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/packages/desktop/src/main/cli-shim.ts b/packages/desktop/src/main/cli-shim.ts index b67350bd..2bffd4a9 100644 --- a/packages/desktop/src/main/cli-shim.ts +++ b/packages/desktop/src/main/cli-shim.ts @@ -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 diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.ts index 888ea750..20147810 100644 --- a/packages/desktop/src/main/index.ts +++ b/packages/desktop/src/main/index.ts @@ -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}`) diff --git a/tests/desktop/cli-shim.test.ts b/tests/desktop/cli-shim.test.ts index 95893502..ae1f4efa 100644 --- a/tests/desktop/cli-shim.test.ts +++ b/tests/desktop/cli-shim.test.ts @@ -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)