🔥
Claude MCPをWSL2で使いたい!
はじめに
Windows環境は汚したくない。
だけど、WSL経由でClaude Desktop AppからMCPがうまくいかない。
nodeはバージョン管理のためにnvmを使用している。
そんな方を救いたい。
結論
以下の設定をしましょう。
WSL
ターミナルで次のコマンドを実行
which node
/home/{wsl_user}/.nvm/versions/node/v22.11.0/bin/node
sudo ln -s /home/{wsl_user}/.nvm/versions/node/v22.11.0/bin/node /usr/bin/node
Claude Settings > Developer > Edit Config
C:\Users\{user}\AppData\Roaming\Claude\claude_desktop_config.json
{
"mcpServers": {
"filesystem": {
"command": "C:\\Windows\\System32\\wsl.exe",
"args": [
"bash",
"-c",
"/home/{wsl_user}/.nvm/versions/node/v22.11.0/bin/npx -y @modelcontextprotocol/server-filesystem /mnt/c/Users/{user}/Documents"
]
}
}
}
Windowsのドキュメントフォルダにあるディレクトリを確認してもらう。
Can you list the contents of my Documents directory?
動いた!!!
ちょっとした解説
まずはエラーログを読む。
C:\Users\{user}\AppData\Roaming\Claude\logs\mcp.log
2025-02-27T12:01:47.530Z [info] [filesystem] Initializing server...
2025-02-27T12:01:47.539Z [info] [filesystem] Server started and connected successfully
2025-02-27T12:01:47.562Z [info] [filesystem] Message from client: {"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"claude-ai","version":"0.1.0"}},"jsonrpc":"2.0","id":0}
2025-02-27T12:01:47.690Z [info] [filesystem] Server transport closed
2025-02-27T12:01:47.691Z [info] [filesystem] Client transport closed
2025-02-27T12:01:47.691Z [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.
2025-02-27T12:01:47.691Z [error] [filesystem] Server disconnected. For troubleshooting guidance, please visit our [debugging documentation](https://modelcontextprotocol.io/docs/tools/debugging)
2025-02-27T12:01:47.691Z [info] [filesystem] Client transport closed
目新しいものはない。
もう一つのエラーログを読む。
C:\Users\{user}\AppData\Roaming\Claude\logs\mcp-server-filesystem.log
2025-02-27T12:01:47.530Z [filesystem] [info] Initializing server...
2025-02-27T12:01:47.539Z [filesystem] [info] Server started and connected successfully
2025-02-27T12:01:47.562Z [filesystem] [info] Message from client: {"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"claude-ai","version":"0.1.0"}},"jsonrpc":"2.0","id":0}
/usr/bin/env: ‘node’: No such file or directory
2025-02-27T12:01:47.690Z [filesystem] [info] Server transport closed
2025-02-27T12:01:47.691Z [filesystem] [info] Client transport closed
2025-02-27T12:01:47.691Z [filesystem] [info] 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.
2025-02-27T12:01:47.691Z [filesystem] [error] Server disconnected. For troubleshooting guidance, please visit our [debugging documentation](https://modelcontextprotocol.io/docs/tools/debugging) {"context":"connection"}
2025-02-27T12:01:47.691Z [filesystem] [info] Client transport closed
/usr/bin/env: ‘node’: No such file or directory
こいつや。
WSLでnodeのバージョン管理のためにnvmを入れている方も多いと思う。nodeの実態がどこにあるかを確認するのが大切。
Claudeはnodeを直接導入している想定なので、/usr/bin/node
にnodeの実態がある前提と推測される。nvmで管理していると、残念ながら/usr/bin/node
なんてものは存在しないので、このエラーでこけるという話。
シンボリックリンクで、/usr/bin/node
を用意してあげればOK。
ただし、この方法だと、nvmでnodeのバージョン変えた時に事故ると想定されるので、そこはよしなに...
Discussion