diff --git a/README.md b/README.md index 8f13cf1..9efaa0e 100644 --- a/README.md +++ b/README.md @@ -256,7 +256,7 @@ OPENSHELL_CONTROL_MCP_BROKER_URL=http://localhost:3000/api/mcp/broker `OPENSHELL_CONTROL_MCP_BROKER_URL` is optional. Set it only when you need to override discovery. By default the dashboard discovers the active OpenShell Docker gateway and the selected sandbox's proxy environment before writing `/sandbox/openshell_control_mcp.md`. -Stdio MCP servers run on the control host. The installer verifies `npx` and installs `uvx` into `~/.local/bin` when it is missing. Custom MCP server launch commands, such as `node` or `python`, must also be available to the dashboard process. +Stdio MCP servers run on the control host. The installer verifies `npx`, creates or reuses a Python virtual environment, installs `uvx` there, and persists that venv path in `.env.local` so the MCP broker can launch `uvx` servers later. Custom MCP server launch commands, such as `node` or `python`, must also be available to the dashboard process. ## File Transfer diff --git a/app/lib/hostCommands.ts b/app/lib/hostCommands.ts index 73b3455..7a17111 100644 --- a/app/lib/hostCommands.ts +++ b/app/lib/hostCommands.ts @@ -54,6 +54,8 @@ function discoverHomeFiles(relativePath: string) { function pathEntries() { return [ process.env.TERMINAL_EXTRA_PATH, + process.env.OPENSHELL_CONTROL_VENV ? path.join(process.env.OPENSHELL_CONTROL_VENV, "bin") : undefined, + path.join(process.cwd(), ".venv/bin"), HOME ? path.join(HOME, ".local/bin") : undefined, HOME ? path.join(HOME, ".nvm/versions/node/v22.22.2/bin") : undefined, HOME ? path.join(HOME, ".nvm/versions/node/v22.22.1/bin") : undefined, diff --git a/app/lib/mcpBrokerClient.ts b/app/lib/mcpBrokerClient.ts index b439be4..5419a02 100644 --- a/app/lib/mcpBrokerClient.ts +++ b/app/lib/mcpBrokerClient.ts @@ -1,6 +1,7 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js" import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js" import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" +import { HOST_PATH } from "./hostCommands" import type { McpServerInstall } from "./mcpServerStore" const MCP_BROKER_REQUEST_TIMEOUT_MS = Number.parseInt(process.env.MCP_BROKER_REQUEST_TIMEOUT_MS || "45000", 10) @@ -31,6 +32,7 @@ function transportForServer(server: McpServerInstall) { args: server.args, env: { ...inheritedEnv(), + PATH: HOST_PATH, ...cleanEnv(server.env), }, stderr: "pipe", diff --git a/install.sh b/install.sh index e202865..2f70a6e 100755 --- a/install.sh +++ b/install.sh @@ -14,6 +14,7 @@ APP_NAME="OpenShell Control" ENV_FILE=".env.local" OPEN_SHELL_CONTAINER_DEFAULT="openshell-cluster-nemoclaw" MIN_NODE_MAJOR=20 +PROJECT_VENV="${OPENSHELL_CONTROL_VENV:-$PWD/.venv}" DO_BUILD=1 DO_START=0 @@ -106,6 +107,31 @@ prepend_user_bin() { fi } +prepend_virtualenv_bin() { + if [[ -n "${VIRTUAL_ENV:-}" && -d "$VIRTUAL_ENV/bin" && ":$PATH:" != *":$VIRTUAL_ENV/bin:"* ]]; then + export PATH="$VIRTUAL_ENV/bin:$PATH" + fi +} + +ensure_project_venv() { + if [[ -n "${VIRTUAL_ENV:-}" ]]; then + log "Using active Python virtual environment: $VIRTUAL_ENV" + prepend_virtualenv_bin + return + fi + + require_command python3 + if [[ ! -x "$PROJECT_VENV/bin/python" ]]; then + log "Creating Python virtual environment: $PROJECT_VENV" + python3 -m venv "$PROJECT_VENV" + else + log "Using Python virtual environment: $PROJECT_VENV" + fi + + export VIRTUAL_ENV="$PROJECT_VENV" + prepend_virtualenv_bin +} + ensure_npx() { if command -v npx >/dev/null 2>&1; then log "npx MCP package runner: $(command -v npx)" @@ -119,34 +145,21 @@ ensure_npx() { } ensure_uvx() { - prepend_user_bin - if command -v uvx >/dev/null 2>&1; then - log "uvx MCP package runner: $(command -v uvx)" + ensure_project_venv + if [[ -x "$VIRTUAL_ENV/bin/uvx" ]]; then + log "uvx MCP package runner: $VIRTUAL_ENV/bin/uvx" return fi - log "Installing uvx MCP package runner" - local installed=0 - if command -v python3 >/dev/null 2>&1 && python3 -m pip --version >/dev/null 2>&1; then - if python3 -m pip install --user --upgrade uv; then - installed=1 - else - warn "Python pip could not install uv for this user; trying the standalone uv installer." - fi - fi + local venv_python="$VIRTUAL_ENV/bin/python" + [[ -x "$venv_python" ]] || fail "Python is required in the virtual environment to install uvx." + "$venv_python" -m pip --version >/dev/null 2>&1 || fail "pip is required in the virtual environment to install uvx." - if [[ "$installed" -ne 1 ]] && command -v curl >/dev/null 2>&1; then - curl -LsSf https://astral.sh/uv/install.sh | sh - installed=1 - fi - - if [[ "$installed" -ne 1 ]]; then - fail "uvx is required for uvx-based MCP servers. Install Python pip or curl, then rerun the installer." - fi - - prepend_user_bin - command -v uvx >/dev/null 2>&1 || fail "uvx installation completed, but uvx is still not on PATH." - log "uvx MCP package runner: $(command -v uvx)" + log "Installing uvx MCP package runner into virtual environment: $VIRTUAL_ENV" + "$venv_python" -m pip install --upgrade uv + prepend_virtualenv_bin + [[ -x "$VIRTUAL_ENV/bin/uvx" ]] || fail "uvx installation completed, but uvx was not installed into the virtual environment." + log "uvx MCP package runner: $VIRTUAL_ENV/bin/uvx" } port_owner() { @@ -396,6 +409,7 @@ upsert_env "NEXT_PUBLIC_API_BASE" "/api" upsert_env "NEXT_PUBLIC_ENABLE_SANDBOX_OPERATIONS" "true" upsert_env "OPEN_SHELL_CONTAINER" "$OPEN_SHELL_CONTAINER_DEFAULT" upsert_env "OPENSHELL_GATEWAY" "nemoclaw" +set_env "OPENSHELL_CONTROL_VENV" "${VIRTUAL_ENV:-$PROJECT_VENV}" set_env "OPENSHELL_BIN" "${OPENSHELL_BIN:-}" set_env "NEMOCLAW_BIN" "${NEMOCLAW_BIN:-}" set_env "NEMOCLAW_SETUP" "${NEMOCLAW_SETUP:-}" diff --git a/tests/portable-install-check.mjs b/tests/portable-install-check.mjs index f746cd3..5c45aaa 100644 --- a/tests/portable-install-check.mjs +++ b/tests/portable-install-check.mjs @@ -16,6 +16,7 @@ const [ inferenceSource, supportBundleSource, mcpBrokerUrlSource, + mcpBrokerClientSource, ] = await Promise.all([ readFile(path.join(root, 'install.sh'), 'utf8'), readFile(path.join(root, 'app/lib/hostCommands.ts'), 'utf8'), @@ -28,6 +29,7 @@ const [ readFile(path.join(root, 'app/api/inference/route.ts'), 'utf8'), readFile(path.join(root, 'app/api/support-bundle/route.ts'), 'utf8'), readFile(path.join(root, 'app/lib/mcpBrokerUrl.ts'), 'utf8'), + readFile(path.join(root, 'app/lib/mcpBrokerClient.ts'), 'utf8'), ]) assert.match(installSource, /find_nemoclaw_bin\(\)/, 'installer must discover NemoClaw CLI') @@ -41,11 +43,15 @@ assert.match(installSource, /set_env "OPENSHELL_BIN"/, 'installer must persist d assert.match(installSource, /upsert_env "TERMINAL_SERVER_AUTOSTART" "true"/, 'installer must enable terminal bridge autostart by default') assert.match(installSource, /ensure_npx\(\)/, 'installer must install or verify npx for stdio MCP servers') assert.match(installSource, /ensure_uvx\(\)/, 'installer must install or verify uvx for stdio MCP servers') -assert.match(installSource, /python3 -m pip install --user --upgrade uv/, 'installer must support user-scoped uvx installation') -assert.match(installSource, /standalone uv installer/, 'installer must fall back when Python blocks user-scoped uv installs') -assert.match(installSource, /prepend_user_bin\(\)/, 'installer must put ~/.local/bin on PATH for user-scoped MCP runners') +assert.match(installSource, /ensure_project_venv\(\)/, 'installer must create or reuse a Python virtual environment for uvx') +assert.match(installSource, /prepend_virtualenv_bin\(\)/, 'installer must prefer an active virtual environment for uvx') +assert.match(installSource, /python3 -m venv "\$PROJECT_VENV"/, 'installer must create a project virtual environment when none is active') +assert.match(installSource, /\$venv_python" -m pip install --upgrade uv/, 'installer must install uvx into the virtual environment') +assert.match(installSource, /set_env "OPENSHELL_CONTROL_VENV"/, 'installer must persist the virtual environment path for runtime MCP launches') assert.match(hostCommandsSource, /export const HOST_PATH/, 'host command resolution must centralize PATH construction') +assert.match(hostCommandsSource, /OPENSHELL_CONTROL_VENV/, 'host command resolution must include the installer-managed virtual environment') +assert.match(hostCommandsSource, /\.venv\/bin/, 'host command resolution must include the default project virtual environment') assert.match(hostCommandsSource, /\.nemoclaw\/source\/bin\/nemoclaw\.js/, 'host command resolution must support standard ~/.nemoclaw installs') assert.match(hostCommandsSource, /\.nemoclaw\/source\/scripts\/setup\.sh/, 'host command resolution must support legacy ~/.nemoclaw setup workflows') assert.match(hostCommandsSource, /discoverHomeFiles\("scripts\/setup\.sh"\)/, 'runtime command resolution must scan user home directories for setup.sh') @@ -57,6 +63,7 @@ assert.match(mcpBrokerUrlSource, /OPEN_SHELL_CONTAINER/, 'MCP broker URL generat assert.match(mcpBrokerUrlSource, /host\.docker\.internal/, 'MCP broker URL generation must keep a fallback for hosts without Docker inspect') assert.match(mcpBrokerUrlSource, /discoverSandboxProxyOrigin/, 'MCP broker URL generation must discover each sandbox proxy endpoint') assert.match(mcpBrokerUrlSource, /HTTP_PROXY/, 'MCP broker URL generation must use the sandbox proxy environment when available') +assert.match(mcpBrokerClientSource, /PATH: HOST_PATH/, 'MCP stdio broker launches must inherit the shared host PATH') assert.match(createRouteSource, /buildNemoClawCreateCommand\(\)/, 'NemoClaw blueprint create must resolve a current CLI command') assert.match(createRouteSource, /"onboard", "--non-interactive"/, 'NemoClaw blueprint create must use the supported onboard CLI flow')