I recently ran into an issue where VS Code couldn't start several local MCP servers because npx was not available. Whenever it tried to start the server, it errors with the message that the command npx cannot be found even though the command worked fine in my shell. After some digging I was able to narrow it down to my shell setup with nvm and how VS Code starts MCP servers.

nvm is made available through the shell by registering it in a configuration file like .zshrc (or .bashrc). Since VS Code starts MCP server commands without sourcing that configuration, neither nvm nor npx will be available.

The Solution

The fix to this is simple. A small script which loads nvm and executes npx with the given arguments.

#!/usr/bin/env bash
# ensure-npx — ensure npx is available, then exec into it.

set -euo pipefail

if ! command -v npx &>/dev/null; then
export NVM_DIR="${NVM_DIR:-$HOME/.nvm}"
source "$NVM_DIR/nvm.sh"
fi

exec npx "$@"

This way, the script doesn't rely on the nvm setup in the shell configuration but loads nvm explicitly before executing npx. I've saved this script in ~/bin/ensure-npx, made it executable and referenced it in my MCP server settings.

{
"servers": {
"io.github.ChromeDevTools/chrome-devtools-mcp": {
"command": "~/bin/ensure-npx",
"args": [
"-y",
"chrome-devtools-mcp"
],
"env": {},
"type": "stdio"
},
},
"inputs": []
}

That's it. Just a simple wrapper script which ensures that npx is available. This isn't limited to MCP servers as the same approach can be handy anywhere the shell configuration isn't sourced.