Hmbown/CodeWhale · error

Project MCP server cwd must stay within workspace

Error message

Project MCP server cwd must stay within workspace: {}

What it means

Project-scope MCP servers may only run with a working directory inside the workspace that owns the config. After canonicalizing the configured `cwd` (falling back to component normalization when canonicalization fails), the library verifies it starts with the workspace path; otherwise it refuses to launch the server with this error naming the escaped path. This prevents a project MCP config from executing commands elsewhere on disk.

Solutions

  1. Change the server's `cwd` in the project `mcp.json` to a directory inside the workspace (or remove `cwd` to default to the workspace)
  2. Replace symlinked cwd entries with real directories inside the repo
  3. Update the workspace path so the configured cwd is nested within it
  4. Use a relative or absent `cwd` rather than a machine-specific absolute path

Example fix

// before
{"mcpServers": {"docs": {"command": "python", "args": ["-m", "docs"], "cwd": "/tmp/docs-venv"}}}
// after
{"mcpServers": {"docs": {"command": "python", "args": ["-m", "docs"]}}} // cwd defaults to the workspace
Defensive patterns

Strategy: validation

Validate before calling

fn cwd_within_workspace(cwd: &Path, workspace: &Path) -> bool {
    let resolved = cwd.canonicalize().unwrap_or_else(|_| cwd.to_path_buf());
    resolved.starts_with(workspace)
}
assert!(cwd_within_workspace(&server_cwd, &workspace), "MCP cwd must stay inside the workspace");

Prevention

When it happens

Trigger: Starting or connecting to a project MCP server whose `cwd` setting in `.codewhale/mcp.json` canonicalizes outside the workspace — e.g. `cwd` set to `/tmp` or a symlinked directory, while the workspace is elsewhere (crates/tui/src/mcp.rs:5663).

Common situations: A user hand-edits `mcp.json` and sets an absolute cwd outside the repo; the cwd is a symlink to another filesystem location; shared team config checked in with machine-specific absolute paths; workspace moved so the old cwd no longer nests under it.

Understand the failure class

Background: Path traversal blocked: "path escapes the workspace" and "outside site root" errors when a path will not stay inside its allowed directory — this error's family across 26 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/f2a01228220a6742. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/mcp.rs:5663

    } else {
        std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(workspace)
    };
    normalize_path_components(&absolute)
}

fn resolve_project_mcp_cwd(workspace: &Path, cwd: Option<&Path>) -> Result<PathBuf> {
    let cwd = match cwd {
        Some(cwd) if cwd.is_relative() => normalize_path_components(&workspace.join(cwd)),
        Some(cwd) => normalize_path_components(cwd),
        None => workspace.to_path_buf(),
    };
    let resolved = cwd
        .canonicalize()
        .unwrap_or_else(|_| normalize_path_components(&cwd));
    if !resolved.starts_with(workspace) {
        anyhow::bail!(
            "Project MCP server cwd must stay within workspace: {}",
            resolved.display()
        );
    }
    Ok(resolved)
}

fn normalize_path_components(path: &Path) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            Component::Prefix(_) | Component::RootDir => {
                normalized.push(component.as_os_str());
            }
            Component::CurDir => {}
            Component::ParentDir => {
                normalized.pop();
            }

View on GitHub (pinned to 73e0f67d83)