Skip to content

fix: local_preview MCP tool now works when server CWD is a repo subdirectory#390

Merged
tomasmik merged 1 commit intospacelift-io:mainfrom
thenoid:fix/mcp-local-preview-repo-root
Mar 16, 2026
Merged

fix: local_preview MCP tool now works when server CWD is a repo subdirectory#390
tomasmik merged 1 commit intospacelift-io:mainfrom
thenoid:fix/mcp-local-preview-repo-root

Conversation

@thenoid
Copy link
Copy Markdown
Contributor

@thenoid thenoid commented Mar 9, 2026

Fixes #389

Problem

The local_preview MCP tool failed when the MCP server was started from a subdirectory of the git repository. createLocalPreviewRun archives from "." (the process CWD), but the MCP handler passed FindRepositoryRoot: false, so it never walked up to the git root before archiving. Spacelift then couldn't find the stack's configured project_root path inside the archive.

This is a common scenario: editors like VS Code and Claude Code typically open a project at a specific stack subdirectory, so the MCP server inherits a subdirectory as its CWD.

Fix

Save the original CWD before calling createLocalPreviewRun with FindRepositoryRoot: true, then defer a restore. This makes the MCP tool walk up to the git root (identical to CLI behavior) while ensuring the long-running MCP server process has its CWD restored after each call.

The defer os.Chdir(origDir) runs when createLocalPreviewRun returns — which is after the archive is built and uploaded — so the upload is unaffected.

… workspaces

When the MCP server is started from a subdirectory of a git repository,
createLocalPreviewRun archives from that subdirectory rather than the repo
root, causing Spacelift to fail with "project root ... does not exist".

Save the original CWD before calling createLocalPreviewRun with
FindRepositoryRoot=true (same as the CLI), then defer a restore so the
long-running MCP server process has its working directory restored after
each call.
@thenoid thenoid requested a review from a team as a code owner March 9, 2026 21:45
@tomasmik tomasmik merged commit 4fc7adf into spacelift-io:main Mar 16, 2026
6 checks passed
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.

local_preview MCP tool fails when MCP server CWD is a repo subdirectory

2 participants