🔌

Claude DesktopのMCP Serverをnvm管理下のnpxで動かす

に公開

Claude DesktopでMCP Serverを設定しようとして少しハマったので、解決策をメモします。

経緯

Quickstartに従ってMCPの構成を書いたところ、Claude Desktopを起動したときに次のようなエラーが出るようになりました。

[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)

ENOENT…Error NO ENTry…パスが通っていなくて npx が見えないようです。

GitHubのIssueを探索してみると、パスを通すことと npx を呼ぶことをまとめてやるスクリプトを作っておいて、それをClaude Desktopから呼べば良いと言っている人がいました

そのアイデアをベースに、少しだけ変更を加えたものが以下です。

解決策

  1. /usr/local/bin/npx-for-claude を作る:
#!/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 に追加
  • 渡された引数をそのまま npx コマンドに転送
  1. npx-for-claude に実行権限をつける
chmod +x /usr/local/bin/npx-for-claude
  1. MCPサーバーの構成のうち command を置き換える
{
    "mcpServers": {
        "filesystem": {
            "command": "npx-for-claude",
            "args": [
                "-y",
                "@modelcontextprotocol/server-filesystem",
                "/path/to/dir"
            ]
        }
    }
}

これで Claude Desktop を再起動すると、正常にMCP Serverが起動し、ファイルシステムへのアクセスも無事使えるようになりました。ちょっとした回避策ですが、同じようにnvm環境で困っている方の参考になれば幸いです。

Discussion