How to install node MCPs using nvm

Ashutosh ShuklaAshutosh Shukla
2 min read

Background

This solution is inspired by https://github.com/modelcontextprotocol/servers/issues/64 but with a little more detail. With a few changes, it also works for macOS, Linux, and Windows.

Problem

When using NVM or standard Node.js installation, the default configuration using npx commands fails to connect MCP servers in Cursor, VS-Code, Windsurf, or Claude desktop.

Solution

The solution steps in brief:

  1. Install MCP Server package globally using nvm

  2. Build the MCP Server and add any missing global dependencies

  3. Modifying the configuration json to use these absolute paths

Guide

For this sample guide, we will try installing mongodb-mcp-server as currently, it does not have a docker container for mcp.

1. Locate your absolute NVM path

nvm use 20
# Now using node v20.18.0 (npm v10.8.2)
which node
# /Users/deepalpha/.nvm/versions/node/v20.18.0/bin/node

2. Install the package globally for nvm

# Installing globally
npm i -g mongodb-mcp-server

# List the path
npm list -g mongodb-mcp-server    
#/Users/deepalpha/.nvm/versions/node/v20.18.0/lib
#โ””โ”€โ”€ mongodb-mcp-server@0.1.0

3. Navigate to the global node_modules and build the package to produce the dist folder

# Navigate to global nvm node_modules folder
cd /Users/deepalpha/.nvm/versions/node/v20.18.0/lib/node_modules

# List what and all packages are already available
ls
# corepack   npm        mongodb-mcp-server

4. Install the package and build the repo

cd mongodb-mcp-server  

npm i

# refer package.json incase they have start or a different command
npm run build

# creates a dist folder with index.js

5. In case the build fails

Usually, a build fails due to missing global dependencies, in my MongoDB-mcp-server case typescript was missing.

tsc --project tsconfig.build.json
sh: tsc: command not found

# Just install the missing dependency
npm i -g typescript

6. Final Config change

The original config, given by MongoDB-mcp-server maintainers:


{
  "servers": {
    "MongoDB": {
      "command": "npx",
      "args": [
        "-y",
        "mongodb-mcp-server",
        "--connectionString",
        "mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
      ]
    }
  }
}

Modified config JSON that you can add to vs code, cursor, windsurf, or Claude desktop

  • We remove npx and use the nvm node absolute path.

  • Remove -y as that is used with npx and not valid with node.

  • We then pass the path to the index.js/entry-point of the dist package.


{
  "servers": {
    "MongoDB": {
        "command": "/Users/deepalpha/.nvm/versions/node/v20.18.0/bin/node",
        "args": [
          "/Users/deepalpha/.nvm/versions/node/v20.18.0/lib/node_modules/mongodb-mcp-server/dist/index.js",
          "--connectionString",
           "mongodb+srv://username:password@cluster.mongodb.net/myDatabase"
        ]
      }
  }
}

Conclusion

Save the modified config file and restart the server it should work now! ๐Ÿš€

0
Subscribe to my newsletter

Read articles from Ashutosh Shukla directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashutosh Shukla
Ashutosh Shukla