Skip to content

fix(mcp): fall back to cwd basename on ambiguous project (#248)#258

Open
CarlosPlasencia wants to merge 1 commit intoGentleman-Programming:mainfrom
CarlosPlasencia:fix/mcp-ambiguous-project-fallback
Open

fix(mcp): fall back to cwd basename on ambiguous project (#248)#258
CarlosPlasencia wants to merge 1 commit intoGentleman-Programming:mainfrom
CarlosPlasencia:fix/mcp-ambiguous-project-fallback

Conversation

@CarlosPlasencia
Copy link
Copy Markdown

Summary

Closes #248. Restores v1.12.0 behavior for MCP write tools launched from a directory that is the parent of multiple git repositories.

When the MCP server is launched from a workspace folder containing several cloned repos, all write tools (mem_save, mem_session_summary, mem_session_start, etc.) currently fail with:

{ error_code: "ambiguous_project",
  message: "ambiguous project: multiple git repos found in cwd",
  available_projects: [...] }

This is a regression from v1.12.0 → v1.13.0 introduced by the mcp-project-autodetection change, which switched resolveWriteProject from the basename-fallback CLI helper to the strict DetectProjectFull and propagated ErrAmbiguousProject unconditionally.

What changed

internal/mcp/mcp.goresolveWriteProject:

  • On ErrAmbiguousProject, fall back to filepath.Base(cwd) lowercased — same behavior as the CLI wrapper DetectProject (internal/project/detect.go:224).
  • Set Source = SourceDirBasename.
  • Emit a Warning listing the cwd repo children so the caller knows the project name was derived from the workspace folder, not from a specific repo.
  • Non-ambiguous cases are untouched (single repo, git remote, git child promotion, etc.).

Test changes

Five tests were rewritten to assert the new fallback contract instead of the old fail-fast contract:

Old test (asserted error) New test (asserts fallback success)
TestMemSave_AmbiguousEnvelope TestMemSave_AmbiguousFallback
TestResolveWriteProject_AmbiguousError TestResolveWriteProject_AmbiguousFallback
TestMemSessionSummary_AmbiguousReturnsError TestMemSessionSummary_AmbiguousFallback
TestWriteTool_AmbiguousErrorUsesCwdRepos_NotAllProjects TestWriteTool_AmbiguousFallbackWarningListsCwdRepos
TestHandleGetObservation_DegradedPathNoEnvelope TestHandleGetObservation_AmbiguousFallbackEnvelope

Each new test verifies: (1) write succeeds, (2) project_source = "dir_basename", (3) warning is present, (4) for the JW1 test, the warning lists cwd repos and not unrelated store projects.

Full test suite passes locally on macOS arm64 (Go 1.26.2):

ok  github.com/Gentleman-Programming/engram/cmd/engram
ok  github.com/Gentleman-Programming/engram/internal/cloud (and 7 cloud subpackages)
ok  github.com/Gentleman-Programming/engram/internal/mcp
ok  github.com/Gentleman-Programming/engram/internal/obsidian
ok  github.com/Gentleman-Programming/engram/internal/project
ok  github.com/Gentleman-Programming/engram/internal/server
ok  github.com/Gentleman-Programming/engram/internal/setup
ok  github.com/Gentleman-Programming/engram/internal/store
ok  github.com/Gentleman-Programming/engram/internal/sync
ok  github.com/Gentleman-Programming/engram/internal/tui
ok  github.com/Gentleman-Programming/engram/internal/version

Spec note

The archived change 2026-04-24-mcp-project-autodetection (REQ-309) currently specifies fail-fast on ambiguous cwd. After this PR is merged, that requirement is no longer accurate for the runtime contract. A follow-up SDD change should update the spec to document the basename fallback (Source=dir_basename + non-empty Warning) so the spec and code stay aligned. Happy to open it as a separate PR if you prefer.

Test plan

  • go test ./internal/mcp/... — passes
  • go test ./... — passes
  • Verified locally with mem_save from a workspace with multiple git children — write now succeeds with project_source: "dir_basename" and a warning listing the children.

…rogramming#248)

When the MCP server is launched from a directory that is the parent of
multiple git repositories, all write tools (mem_save, mem_session_summary,
etc.) fail with ErrAmbiguousProject. This is a regression from v1.12.0
where filepath.Base(cwd) was used as a fallback.

resolveWriteProject now mirrors the CLI wrapper DetectProject(): on
ErrAmbiguousProject it falls back to the lowercased cwd basename, sets
Source=SourceDirBasename, and emits a Warning that lists the cwd repos
so the user knows the project name was derived from the workspace folder.

This makes engram MCP usable again from workspace directories (the
common setup with multiple cloned repos under one parent) without
removing the structured detection result for non-ambiguous cases.

Tests adapted to assert the new fallback behavior (success + envelope +
warning) instead of the previous fail-fast error envelope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(mcp): resolveWriteProject fails on "ambiguous project" when CWD is parent of multiple git repos

2 participants