Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .mcp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"mcpServers": {
"devops-cli": {
"command": "uv",
"args": [
"run",
"--with",
"fastmcp",
"devops",
"mcp",
"serve",
"--transport",
"stdio"
],
"env": {
"PATH": "${HOME}/.local/bin:/usr/local/bin:/usr/bin:/bin"
}
}
}
}
24 changes: 24 additions & 0 deletions src/devops_cli/commands/devcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from devops_cli.ai.instruction_generator import scaffold_agent_instructions
from devops_cli.config.constants import (
CONST_AGENTS_MD_FILENAME,
CONST_CLAUDE_MCP_JSON_NAME,
CONST_DEVCONTAINER_DIR_NAME,
CONST_DEVCONTAINER_IMAGE_PREFIX,
CONST_DEVCONTAINER_JSON_NAME,
Expand Down Expand Up @@ -159,6 +160,19 @@ def init(
)
print_success(MESSAGES.devcontainer.created_file.format(path=mcp_file))

# The Claude Code extension reads its own project-scoped file, and it is not a copy of
# the VS Code one: `${workspaceFolder}` is a VS Code substitution Claude Code does not
# expand, so sharing the file would put a literal `${workspaceFolder}` on the server's
# PATH. Claude Code already runs a stdio server with the project root as its working
# directory, so the Claude template needs neither that variable nor `cwd`.
claude_mcp_file = repo_path / CONST_CLAUDE_MCP_JSON_NAME
if not claude_mcp_file.exists() or force:
write_text_file(
claude_mcp_file,
render_json_template("claude_mcp.json.j2", project_name=name),
)
print_success(MESSAGES.devcontainer.created_file.format(path=claude_mcp_file))

# Scaffold AI agent instruction files (AGENTS.md, CLAUDE.md, .github/copilot-instructions.md)
agent_files = scaffold_agent_instructions(repo_path, force=force, template=True)
for af in agent_files:
Expand Down Expand Up @@ -639,6 +653,16 @@ def _sync_mcp_configuration(workspace_dir: Path, *, dry_run: bool = False) -> li
)
actions.append(f"Scaffolded MCP configuration at {vscode_mcp}")

claude_mcp = workspace_dir / CONST_CLAUDE_MCP_JSON_NAME
if not claude_mcp.exists() and (workspace_dir / CONST_PYPROJECT_FILENAME).exists():
if not dry_run:
claude_name = re.sub(r"[^a-zA-Z0-9._-]+", "_", workspace_dir.name)
write_text_file(
claude_mcp,
render_json_template("claude_mcp.json.j2", project_name=claude_name),
)
actions.append(f"Scaffolded Claude MCP configuration at {claude_mcp}")

if vscode_mcp.exists():
for mcp_dest in (
Path.home() / ".gemini" / "config" / "mcp_config.json",
Expand Down
5 changes: 5 additions & 0 deletions src/devops_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
CONST_TESTS_DIR_PATH = Path(CONST_TESTS_DIR_NAME)
CONST_VSCODE_DIR_NAME = ".vscode"
CONST_MCP_JSON_NAME = "mcp.json"
# Claude Code reads project-scoped MCP servers from `.mcp.json` at the repository root.
# VS Code's own MCP support reads `.vscode/mcp.json`, and the two are not interchangeable:
# `${workspaceFolder}` is a VS Code substitution that Claude Code does not expand, so a
# copy of the VS Code file would hand the server a literal `${workspaceFolder}` on PATH.
CONST_CLAUDE_MCP_JSON_NAME = ".mcp.json"
CONST_MCP_RESOURCE_SCHEME = "resource://"
CONST_MCP_DOMAINS: Final[frozenset[str]] = frozenset(
{
Expand Down
20 changes: 20 additions & 0 deletions src/devops_cli/templates/claude_mcp.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"mcpServers": {
{{ project_name | tojson }}: {
"command": "uv",
"args": [
"run",
"--with",
"fastmcp",
"devops",
"mcp",
"serve",
"--transport",
"stdio"
],
"env": {
"PATH": "${HOME}/.local/bin:/usr/local/bin:/usr/bin:/bin"
}
}
}
}
66 changes: 66 additions & 0 deletions tests/test_devcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,3 +1142,69 @@ def test_start_minikube_cluster_delegates_to_shared_runtime(
assert ok is False
assert "ghp_secrettoken" not in msg
assert "<masked-github-token>" in msg


# =============================================================================
# Claude Code MCP configuration
# =============================================================================


def test_the_claude_config_avoids_vscode_substitutions() -> None:
"""`${workspaceFolder}` is a VS Code variable that Claude Code does not expand.

Copying `.vscode/mcp.json` would hand the server a literal `${workspaceFolder}` on its
PATH, so the two files are rendered from different templates rather than shared.
"""
from devops_cli.core.templating import render_json_template

rendered = render_json_template("claude_mcp.json.j2", project_name="devops-cli")
assert "${workspaceFolder}" not in rendered and "${env:HOME}" not in rendered


def test_both_configs_launch_the_same_server() -> None:
"""Two files for two clients must not drift into describing different servers."""
import json

from devops_cli.core.templating import render_json_template

claude = json.loads(render_json_template("claude_mcp.json.j2", project_name="devops-cli"))
vscode = json.loads(render_json_template("mcp.json.j2", project_name="devops-cli"))
claude_server = claude["mcpServers"]["devops-cli"]
vscode_server = vscode["mcpServers"]["devops-cli"]
assert (claude_server["command"], claude_server["args"]) == (
vscode_server["command"],
vscode_server["args"],
)


def test_the_claude_config_requests_a_stdio_transport() -> None:
"""The extension speaks stdio; an sse server would never be reached."""
import json

from devops_cli.core.templating import render_json_template

server = json.loads(render_json_template("claude_mcp.json.j2", project_name="p"))["mcpServers"][
"p"
]
assert server["args"][-2:] == ["--transport", "stdio"]


def test_scaffolding_writes_the_claude_config(tmp_path: Path) -> None:
"""The extension reads `.mcp.json` at the repository root, not `.vscode/`."""
from devops_cli.config.constants import CONST_CLAUDE_MCP_JSON_NAME
from devops_cli.core.templating import render_json_template
from devops_cli.output import write_text_file

write_text_file(
tmp_path / CONST_CLAUDE_MCP_JSON_NAME,
render_json_template("claude_mcp.json.j2", project_name=tmp_path.name),
)
assert (tmp_path / ".mcp.json").is_file()


def test_this_repository_ships_a_claude_mcp_config() -> None:
"""The devcontainer installs the Claude extension, which needs this file to find the server."""
import json

config = json.loads(Path(".mcp.json").read_text(encoding="utf-8"))
assert "devops-cli" in config["mcpServers"]
Loading