[BUG] [MCP] Working directory reset when server name mismatch workspace
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
When configuring MCP servers in .mcp.json with bash cd commands to set different working directories, the working directory is forcibly reset to the current VSCode workspace path for some servers but not others. The only difference between working and non-working servers is the server name.
Servers whose names do NOT start with the workspace directory name have their cwd reset to the workspace path, while servers whose names do start with the workspace directory name work correctly.
What Should Happen?
All MCP servers should respect the working directory set by the bash cd command in the configuration, regardless of the server name.
Error Messages/Logs
When running `cd /path && claude --version` in terminal, the following message appears:
Shell cwd was reset to /home/user/workspace/main-project
No error is thrown - the cwd is silently reset.
Steps to Reproduce
- Open VSCode with workspace at
/home/user/workspace/main-project - Create
.mcp.jsonin the workspace root with two MCP servers:
{
"mcpServers": {
"other-project": {
"command": "/bin/bash",
"args": ["-c", "cd /home/user/workspace/other-project && claude mcp serve"]
},
"main-project-sub": {
"command": "/bin/bash",
"args": ["-c", "cd /home/user/workspace/sub-project && claude mcp serve"]
}
}
}
- Restart VSCode
- Use each MCP server's Bash tool to run
pwd - Observe:
other-projectreports/home/user/workspace/main-project(wrong - reset to workspace)main-project-subreports/home/user/workspace/sub-project(correct)
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.7 (Claude Code)
Platform
Anthropic API
Operating System
macOS
Terminal/Shell
VS Code integrated terminal
Additional Information
Observed Pattern
| Server Name | Starts with Workspace Name? | cwd Result |
|-------------|----------------------------|------------|
| main-project-sub | ✅ Yes | ✅ Correct |
| main-project-api | ✅ Yes | ✅ Correct |
| other-project | ❌ No | ❌ Reset to workspace |
| different-name | ❌ No | ❌ Reset to workspace |
Workaround
Rename MCP servers to start with the workspace directory name:
other-project→main-project-fe(now works)
Attempted Fixes (All Failed)
- Using
env.PWDin server config - Using
cwdfield in server config - Using
--cwdflag (not supported byclaude mcp serve) - Changing server order in
.mcp.json - Disabling hooks in target project's
.claude/settings.json
Related Issues
- #5037 - MCP servers in .claude/.mcp.json not loading properly
- #4938 - Multiple mcpServers sections silently override each other
Suggested Investigation
The MCP server initialization logic may be checking if the server name matches or starts with the workspace name, and if not, forcing the cwd to the workspace path. This appears to be either:
- An undocumented security feature
- A bug in path resolution
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗