Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(mcp): honor CLAUDE_CONFIG_DIR for Claude registrar (#886)
Andrew Barnese616dcf788e2
diff --git a/headroom/mcp_registry/claude.py b/headroom/mcp_registry/claude.pyindex 0550563ea..16c9e1d04 100644--- a/headroom/mcp_registry/claude.py+++ b/headroom/mcp_registry/claude.py@@ -12,6 +12,7 @@ from __future__ import annotationsimport jsonimport logging+import osimport shutilimport subprocessfrom pathlib import Path@@ -33,17 +34,20 @@ class ClaudeRegistrar(MCPRegistrar):*,claude_cli: str | None | object = ...,home_dir: Path | None = None,+ config_dir: Path | None = None,) -> None:"""Allow overrides for testing.``claude_cli`` defaults to :func:`shutil.which` lookup. Pass``None`` to force the file-based fallback path. Pass an explicit- path to point at a specific binary.+ path to point at a specific binary. ``CLAUDE_CONFIG_DIR`` is honored+ for real user sessions; ``home_dir`` keeps tests isolated from the+ caller's environment unless ``config_dir`` is passed explicitly."""home = home_dir if home_dir is not None else Path.home()- self._claude_dir = home / ".claude"- self._modern_config = home / ".claude" / ".claude.json"- self._legacy_config = home / ".claude" / "mcp.json"+ self._claude_dir = _resolve_claude_config_dir(home, config_dir, honor_env=home_dir is None)+ self._modern_config = self._claude_dir / ".claude.json"+ self._legacy_config = self._claude_dir / "mcp.json"if claude_cli is ...:self._claude_cli = shutil.which("claude")else:@@ -184,6 +188,21 @@ class ClaudeRegistrar(MCPRegistrar):# ----------------------------------------------------------------------+def _resolve_claude_config_dir(+ home: Path,+ config_dir: Path | None,+ *,+ honor_env: bool,+) -> Path:+ if config_dir is not None:+ return config_dir+ if honor_env:+ env_dir = os.environ.get("CLAUDE_CONFIG_DIR", "").strip()+ if env_dir:+ return Path(env_dir).expanduser()+ return home / ".claude"++def _read_json(path: Path) -> dict[str, Any]:"""Read a JSON file, returning empty dict if absent or unparseable."""if not path.exists():diff --git a/tests/test_mcp_registry/test_claude_registrar.py b/tests/test_mcp_registry/test_claude_registrar.pyindex 223cc801b..ad44bcde9 100644--- a/tests/test_mcp_registry/test_claude_registrar.py+++ b/tests/test_mcp_registry/test_claude_registrar.py@@ -100,6 +100,21 @@ def test_get_server_falls_back_to_legacy(tmp_path: Path) -> None:assert got.env == {}+def test_get_server_reads_claude_config_dir(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ cfg = tmp_path / ".claude.json"+ cfg.write_text(+ json.dumps({"mcpServers": {"headroom": {"command": "headroom", "args": ["mcp", "serve"]}}})+ )+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ reg = ClaudeRegistrar(claude_cli=None)+ got = reg.get_server("headroom")+ assert got is not None+ assert got.command == "headroom"+ assert got.args == ("mcp", "serve")++# ----------------------------------------------------------------------# register_server() — happy paths# ----------------------------------------------------------------------@@ -170,6 +185,19 @@ def test_register_writes_to_legacy_when_only_legacy_exists(tmp_path: Path) -> Noassert not (tmp_path / ".claude" / ".claude.json").exists()+def test_register_writes_to_claude_config_dir(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ reg = ClaudeRegistrar(claude_cli=None)+ result = reg.register_server(_spec())+ assert result.status == RegisterStatus.REGISTERED+ cfg = tmp_path / ".claude.json"+ data = json.loads(cfg.read_text())+ assert data["mcpServers"]["headroom"]["command"] == "headroom"+ assert not (tmp_path / ".claude" / ".claude.json").exists()++# ----------------------------------------------------------------------# register_server() — already / mismatch / force# ----------------------------------------------------------------------@@ -295,6 +323,28 @@ def test_unregister_returns_false_when_absent(tmp_path: Path) -> None:assert reg.unregister_server("headroom") is False+def test_unregister_removes_from_claude_config_dir(+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path+) -> None:+ cfg = tmp_path / ".claude.json"+ cfg.write_text(+ json.dumps(+ {+ "mcpServers": {+ "headroom": {"command": "headroom", "args": ["mcp", "serve"]},+ "other": {"command": "other"},+ }+ }+ )+ )+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path))+ reg = ClaudeRegistrar(claude_cli=None)+ assert reg.unregister_server("headroom") is True+ data = json.loads(cfg.read_text())+ assert "headroom" not in data["mcpServers"]+ assert "other" in data["mcpServers"]++# ----------------------------------------------------------------------# Robustness: bad JSON should not crash# ----------------------------------------------------------------------