Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MCP Server Issues with Node Version Managers #1246

Open
Michaelzag opened this issue Feb 27, 2025 · 1 comment
Open

MCP Server Issues with Node Version Managers #1246

Michaelzag opened this issue Feb 27, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@Michaelzag
Copy link

Michaelzag commented Feb 27, 2025

Which version of the app are you using?

v3.7.6

Which API Provider are you using?

Anthropic

Which Model are you using?

Claude 3.7 Sonnet

What happened?

Issue Description

Users with Node version managers installed (particularly Fast Node Manager - fnm) are experiencing issues when setting up Model Context Protocol (MCP) servers. When attempting to use simple MCP server configurations like:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/Users/username/Desktop",
        "/path/to/other/allowed/dir"
      ]
    }
  }
}

They encounter the following error:

spawn npx ENOENT spawn npx ENOENT

This error indicates that the system cannot find the npx executable, despite it being available when using the terminal directly.

Root Cause Analysis

The issue appears to be caused by two factors in the MCP server implementation:

  1. Insufficient Environment Variable Passing: The code only passes the PATH environment variable to the child process, but node version managers like fnm require additional environment variables to function correctly.

  2. Lack of Shell Context: Commands aren't being executed in a shell context, which is necessary for proper path resolution with node version managers.

When using node version managers, the shell setup scripts modify not just the PATH but often set other environment variables that are necessary for proper operation. Additionally, they may rely on shell features for executable resolution.

Suggested Fix

The issue could be resolved with two key changes to the src/services/mcp/McpHub.ts file, specifically in the connectToServer method where the StdioClientTransport is configured:

  1. Pass All Environment Variables: Change the environment configuration to inherit all environment variables from the parent process (VSCode):
// Current implementation
const transport = new StdioClientTransport({
    command: config.command,
    args: config.args,
    env: {
        ...config.env,
        ...(process.env.PATH ? { PATH: process.env.PATH } : {}),
        // ...(process.env.NODE_PATH ? { NODE_PATH: process.env.NODE_PATH } : {}),
    },
    stderr: "pipe", // necessary for stderr to be available
})

// Proposed implementation
// Create the environment object, merging PATH properly if present
const mergedEnv = { ...process.env }; // Start with all parent env vars

// Merge in config env vars, with special handling for PATH
if (config.env) {
    Object.entries(config.env).forEach(([key, value]) => {
        if (key.toUpperCase() === 'PATH' && process.env.PATH) {
            // For PATH, append the user config to the existing path
            mergedEnv[key] = `${process.env.PATH}${path.delimiter}${value}`;
        } else {
            // For other env vars, use the config value
            mergedEnv[key] = value;
        }
    });
}

const transport = new StdioClientTransport({
    command: config.command,
    args: config.args,
    env: mergedEnv,
    stderr: "pipe", // necessary for stderr to be available
    shell: true,    // Use shell to execute the command, which helps with PATH resolution
})
  1. Enable Shell Execution: Add shell: true to ensure commands run in a shell context (already included in the proposed implementation above)

Steps to reproduce

  1. Have a node version manager installed
  2. Configure a MCP server according to anthropic's example.
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/Users/username/Desktop",
        "/path/to/other/allowed/dir"
      ]
    }
  }
}
  1. Receive spawn npx ENOENT spawn npx ENOENT

Relevant API REQUEST output

Additional context

Roo think's it can fix the issue, as written in the block above.
It might be better to have a check box in the mcp config area to allow the user to execute using the vs code shell context or without it. Might have unexpected issues for some users if the code changes proposed by my Roo instance are just plugged in.

@Michaelzag Michaelzag added the bug Something isn't working label Feb 27, 2025
@ApocDev
Copy link

ApocDev commented Feb 27, 2025

Related #1062

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants