Skip to content

Auto-detect Project Root from MCP Client #234

Description

@JeremyDev87

Auto-detect Project Root from MCP Client

Problem Statement

The context.md file (used for preserving context across conversation compaction) works correctly in the codingbuddy project but fails to work in other projects.

Root Cause Analysis

Why it works in this project

In /Users/jeremy/workspace/codebuddy/.mcp.json:

"codingbuddy": {
  "command": "npx",
  "args": ["codingbuddy@2.4.1", "mcp"],
  "env": {
    "CODINGBUDDY_PROJECT_ROOT": "/Users/jeremy/workspace/codebuddy"
  }
}

The CODINGBUDDY_PROJECT_ROOT environment variable is hardcoded for this specific project.

Why it fails in other projects

Other projects have:

  • No CODINGBUDDY_PROJECT_ROOT environment variable configured
  • No codingbuddy.config.js file

When neither is present, the MCP server falls back to process.cwd(), which may not point to the actual project directory when the MCP server is launched by Claude Code.

Impact

Scenario This Project Other Projects
CODINGBUDDY_PROJECT_ROOT Hardcoded Not set
codingbuddy.config.js Exists Missing
context.md creation Works Fails

Why This Work is Needed

  1. Poor Developer Experience: Users must manually configure CODINGBUDDY_PROJECT_ROOT for every project
  2. Hidden Failure: No error messages when context.md is created in the wrong location
  3. Context Loss: PLAN → ACT → EVAL workflow breaks when context is not persisted correctly
  4. Inconsistent Behavior: Works in some projects but not others, causing confusion

Proposed Solution

Utilize the MCP protocol's roots/list capability:

  • MCP clients (like Claude Code) can expose their workspace directories via roots/list request
  • The MCP server can request this information after connection
  • Automatically detect the correct project root without manual configuration

Acceptance Criteria

  • MCP server requests roots/list from client after connection
  • If roots are available, use the first root as project root
  • Maintain backward compatibility (env var takes precedence)
  • Graceful fallback if client doesn't support roots capability
  • All existing tests pass

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions