【Claude Desktop】在 nvm 管理下使用 npx 运行 Claude Desktop 的 MCP 服务器

Mcp配置如下:

json 复制代码
{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-filesystem",
        "/Users/username/Desktop",
        "/Users/username/Downloads"
      ]
    }
  }
}

报错如下:

bash 复制代码
[info] [filesystem] Initializing server...
[error] [filesystem] spawn npx ENOENT
[error] [filesystem] spawn npx ENOENT
[info] [filesystem] Server transport closed
[info] [filesystem] Client transport closed
[info] [filesystem] Server transport closed unexpectedly, this is likely due to the process exiting early. If you are developing this MCP server you can add output to stderr (i.e. `console.error('...')` in JavaScript, `print('...', file=sys.stderr)` in python) and it will appear in this log.
[error] [filesystem] Server disconnected. For troubleshooting guidance, please visit our [debugging documentation](https://modelcontextprotocol.io/docs/tools/debugging)

原因:

使用 NVM(Node 版本管理器)时,MCP 服务器的标准安装和使用说明不起作用。应用程序会尝试使用错误的 Node,从而导致失败。

解决方法一:

  1. 避免npx,全局安装包。
  2. 对 Node 可执行文件和服务器脚本使用绝对路径。

举例:

json 复制代码
{
  "mcpServers": {
    "filesystem": {
      "command": "/Users/username/.nvm/versions/node/v22.17.1/bin/node",
      "args": [
        "/Users/username/.nvm/versions/node/v22.17.1/bin/mcp-server-filesystem",
        "/Users/username/Desktop"
      ]
    }
  }
}

解决方法二:

方法一是最简单最直接的方法,但是不够灵活,一旦nvm更新了node版本,可能就会出现报错的情况; 我们可以写一个脚本,来自动选择最新的node版本:

步骤1: 制作 /usr/local/bin/latest-npx-for-claude

bash 复制代码
#!/usr/bin/env bash
NODE_VERSIONS_DIR="$HOME/.nvm/versions/node"
LATEST_NODE_VERSION=$(ls -v "$NODE_VERSIONS_DIR" | grep "^v" | sort -V | tail -n 1)
export PATH="$NODE_VERSIONS_DIR/$LATEST_NODE_VERSION/bin:$PATH"
exec npx "$@"

这个脚本的主要作用是:

  • 找到通过 nvm 安装的最新版本的 Node.js。
  • 将该版本的 bin 目录添加到 PATH 的最前面,确保使用最新版本的 Node.js。
  • 执行 npx 并传递所有原始参数。 使用场景
  • 如果你经常使用 npx 但希望始终使用最新安装的 Node.js 版本,这个脚本可以确保这一点。
  • 可以把这个脚本保存为一个可执行文件(例如 npx-latest),然后通过 ./npx-latest 来运行,这样就会用最新的 Node.js 版本执行 npx 。 注意事项
  • 脚本假设 nvm 的 Node.js 版本安装在 ~/.nvm/versions/node 目录下。如果 nvm 的配置不同,可能需要调整路径。
  • 如果没有安装任何 Node.js 版本,LATEST_NODE_VERSION 可能为空,导致 PATH 设置出错。可以添加错误处理来避免这种情况。

步骤2: latest-npx-for-claude授予执行权限

bash 复制代码
chmod +x /usr/local/bin/npx-for-claude

步骤3: command替换MCP 服务器配置

json 复制代码
{
    "mcpServers": {
        "filesystem": {
            "command": "npx-for-claude",
            "args": [
                "-y",
                "@modelcontextprotocol/server-filesystem",
                "/path/to/dir"
            ]
        }
    }
}
相关推荐
t_hj1 分钟前
Selector的用法
人工智能·python·tensorflow
mortimer14 分钟前
为 Index-TTS 打造一个开箱即用的 Windows 整合包:从环境隔离到依赖难题的解决
人工智能·python·github
算家计算18 分钟前
今天,OpenAI彻底颠覆AI助手!ChatGPT智能体上线,融合三大AI
人工智能·chatgpt·agent
我有一计33321 分钟前
我的"实习生"又添新成员:Kiro产品体验
人工智能
该用户已不存在22 分钟前
我不管,我的 Claude Code 必须用上 Gemini 2.5 Pro
前端·人工智能·后端
我有一计33324 分钟前
FreeTex(v1.0.0):支持多模态公式识别
人工智能
算家计算25 分钟前
Wan2.1-14B-T2V-FusionX-VACE基础使用指南:基于ComfyUI
人工智能·开源
新加坡内哥谈技术1 小时前
大型语言模型的白日梦循环
人工智能
阿里云大数据AI技术2 小时前
云上AI推理平台全掌握 (4):大模型分发加速
大数据·人工智能·llm
lwbg2 小时前
8卡H20服务器 + vLLM企业级部署满血版DeepSeek实录
人工智能·deepseek