Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
feat(mcp): improve workspace HTTP discovery (#446)
joptimus838c8d5de31c
diff --git a/packages/cli/src/repowise/cli/commands/mcp_cmd.py b/packages/cli/src/repowise/cli/commands/mcp_cmd.pyindex 4a4ef8495..c2af690ae 100644--- a/packages/cli/src/repowise/cli/commands/mcp_cmd.py+++ b/packages/cli/src/repowise/cli/commands/mcp_cmd.py@@ -8,6 +8,63 @@ import clickfrom repowise.cli.helpers import console, find_repowise_repo_root, resolve_repo_pathfrom repowise.cli.ui import load_dotenv+from repowise.core.workspace.config import WorkspaceConfig, find_workspace_root+++def _workspace_summary(path: Path) -> dict[str, object] | None:+ workspace_root = find_workspace_root(path)+ if workspace_root is None:+ return None++ try:+ ws_config = WorkspaceConfig.load(workspace_root)+ except Exception:+ return None++ aliases = ws_config.repo_aliases()+ default = ws_config.get_primary()+ default_alias = default.alias if default else (aliases[0] if aliases else None)+ resolved_path = path.resolve()++ for entry in ws_config.repos:+ entry_path = (workspace_root / entry.path).resolve()+ try:+ resolved_path.relative_to(entry_path)+ except ValueError:+ continue+ default_alias = entry.alias+ break++ return {+ "workspace_root": workspace_root,+ "default_repo": default_alias,+ "aliases": aliases,+ }+++def _print_network_startup(+ transport: str,+ repo_path: Path,+ port: int,+ workspace: dict[str, object] | None,+) -> None:+ label = "streamable HTTP" if transport == "streamable-http" else "SSE"+ endpoint = "mcp" if transport == "streamable-http" else "sse"+ console.print(+ f"[bold green]Starting repowise MCP server ({label})[/bold green]\n"+ f"URL: http://127.0.0.1:{port}/{endpoint}"+ )++ if workspace is not None:+ aliases = workspace["aliases"]+ repo_list = ", ".join(aliases) if isinstance(aliases, list) else ""+ console.print(+ f"Workspace: {workspace['workspace_root']}\n"+ f"Default repo: {workspace['default_repo'] or 'none'}\n"+ f"Repos: {repo_list or 'none'}"+ )+ else:+ console.print(f"Repo: {repo_path}")@click.command("mcp")@@ -30,7 +87,7 @@ from repowise.cli.ui import load_dotenvdef mcp_command(path: str | None, transport: str, port: int) -> None:"""Start the MCP server for editor integration.- Exposes 16 tools for querying the repowise wiki via the MCP protocol.+ Exposes 17 tools for querying the repowise wiki via the MCP protocol.Supports stdio (for Claude Code, Codex, Cursor, Cline), streamable HTTP,and legacy SSE transports.@@ -51,22 +108,16 @@ def mcp_command(path: str | None, transport: str, port: int) -> None:repo_path = resolve_repo_path(path)load_dotenv(repo_path)+ workspace = _workspace_summary(repo_path)repowise_dir = repo_path / ".repowise"- if not repowise_dir.exists():+ if workspace is None and not repowise_dir.exists():console.print(f"[yellow]Warning: No .repowise directory found at {repo_path}.[/yellow]\n""Run 'repowise init' first to generate documentation.")- if transport == "sse":- console.print(- f"[bold green]Starting repowise MCP server (SSE) on port {port}...[/bold green]"- )- elif transport == "streamable-http":- console.print(- "[bold green]Starting repowise MCP server (streamable HTTP) "- f"at http://127.0.0.1:{port}/mcp...[/bold green]"- )+ if transport in {"sse", "streamable-http"}:+ _print_network_startup(transport, repo_path, port, workspace)else:# stdio mode — no console output (it would corrupt the protocol)passdiff --git a/packages/server/src/repowise/server/mcp_server/__init__.py b/packages/server/src/repowise/server/mcp_server/__init__.pyindex 71fe20886..304aef841 100644--- a/packages/server/src/repowise/server/mcp_server/__init__.py+++ b/packages/server/src/repowise/server/mcp_server/__init__.py@@ -1,4 +1,4 @@-"""repowise MCP Server — 9 tools for AI coding assistants.+"""repowise MCP Server — 17 tools for AI coding assistants.Exposes the full repowise wiki as queryable tools via the MCP protocol.Supports stdio transport (Claude Code, Cursor, Cline), streamable HTTP, and@@ -42,6 +42,7 @@ from repowise.server.mcp_server.tool_context import get_contextfrom repowise.server.mcp_server.tool_dead_code import get_dead_codefrom repowise.server.mcp_server.tool_health import get_healthfrom repowise.server.mcp_server.tool_overview import get_overview+from repowise.server.mcp_server.tool_repos import list_reposfrom repowise.server.mcp_server.tool_risk import get_riskfrom repowise.server.mcp_server.tool_search import search_codebasefrom repowise.server.mcp_server.tool_symbol import get_symbol@@ -114,6 +115,7 @@ __all__ = ["get_risk","get_symbol","get_why",+ "list_repos","mcp","run_mcp","search_codebase",diff --git a/packages/server/src/repowise/server/mcp_server/tool_repos.py b/packages/server/src/repowise/server/mcp_server/tool_repos.pynew file mode 100644index 000000000..b2c8ed128--- /dev/null+++ b/packages/server/src/repowise/server/mcp_server/tool_repos.py@@ -0,0 +1,78 @@+"""MCP tool: list_repos - discover repos served by this MCP server."""++from __future__ import annotations++from typing import Any++from repowise.core.registry import mcp_tool_registry as mcp+from repowise.server.mcp_server import _state+from repowise.server.mcp_server._meta import build_meta as _build_meta+++def _workspace_repos(registry: Any) -> list[dict[str, Any]]:+ ws_config = getattr(registry, "ws_config", None)+ default_alias = registry.get_default_alias()++ if ws_config is None:+ return [+ {+ "alias": alias,+ "path": None,+ "absolute_path": None,+ "is_default": alias == default_alias,+ }+ for alias in registry.get_all_aliases()+ ]++ repos: list[dict[str, Any]] = []+ for entry in ws_config.repos:+ absolute_path = (registry.workspace_root / entry.path).resolve()+ repos.append(+ {+ "alias": entry.alias,+ "path": entry.path,+ "absolute_path": absolute_path.as_posix(),+ "is_default": entry.alias == default_alias,+ "indexed_at": entry.indexed_at,+ "last_commit_at_index": entry.last_commit_at_index,+ }+ )+ return repos+++async def list_repos() -> dict[str, Any]:+ """List repos available through this MCP server.++ In workspace mode this returns every configured workspace repo alias. Use+ those aliases as the ``repo`` parameter on workspace-aware tools.+ """+ registry = _state._registry+ if registry is not None:+ return {+ "workspace": True,+ "workspace_root": registry.workspace_root.as_posix(),+ "default_repo": registry.get_default_alias(),+ "repos": _workspace_repos(registry),+ "hint": "Use repo='<alias>' on tools that accept a repo parameter.",+ "_meta": _build_meta(),+ }++ return {+ "workspace": False,+ "workspace_root": None,+ "default_repo": "default",+ "repos": [+ {+ "alias": "default",+ "path": str(_state._repo_path) if _state._repo_path else None,+ "absolute_path": str(_state._repo_path) if _state._repo_path else None,+ "is_default": True,+ }+ ],+ "hint": "This MCP server is serving a single repo; omit repo unless a tool asks for it.",+ "_meta": _build_meta(),+ }+++__all__ = ["list_repos"]diff --git a/tests/unit/cli/test_mcp_transport.py b/tests/unit/cli/test_mcp_transport.pyindex 8972784c5..726c6c6d2 100644--- a/tests/unit/cli/test_mcp_transport.py+++ b/tests/unit/cli/test_mcp_transport.py@@ -7,6 +7,7 @@ from pathlib import Pathfrom click.testing import CliRunnerfrom repowise.cli.main import cli+from repowise.core.workspace.config import RepoEntry, WorkspaceConfigdef test_mcp_help_lists_streamable_http_transport() -> None:@@ -50,6 +51,56 @@ def test_mcp_cli_accepts_streamable_http_transport(}+def test_mcp_cli_streamable_http_prints_workspace_summary(+ monkeypatch,+ tmp_path: Path,+) -> None:+ workspace = tmp_path / "workspace"+ api = workspace / "services" / "api"+ web = workspace / "apps" / "web"+ api.mkdir(parents=True)+ web.mkdir(parents=True)+ WorkspaceConfig(+ repos=[+ RepoEntry(path="services/api", alias="api", is_primary=True),+ RepoEntry(path="apps/web", alias="web"),+ ],+ default_repo="api",+ ).save(workspace)++ captured: dict[str, object] = {}++ def fake_run_mcp(**kwargs):+ captured.update(kwargs)++ monkeypatch.setattr("repowise.server.mcp_server.run_mcp", fake_run_mcp)++ result = CliRunner().invoke(+ cli,+ [+ "mcp",+ str(workspace),+ "--transport",+ "streamable-http",+ "--port",+ "7341",+ ],+ )++ assert result.exit_code == 0+ output = result.output.replace("\n", "")+ assert "URL: http://127.0.0.1:7341/mcp" in result.output+ assert f"Workspace: {workspace.resolve()}" in output+ assert "Default repo: api" in result.output+ assert "Repos: api, web" in result.output+ assert "Warning: No .repowise directory" not in result.output+ assert captured == {+ "transport": "streamable-http",+ "repo_path": str(workspace.resolve()),+ "port": 7341,+ }++def test_run_mcp_dispatches_streamable_http(monkeypatch) -> None:from repowise.server.mcp_server import _serverdiff --git a/tests/unit/server/test_mcp_workspace.py b/tests/unit/server/test_mcp_workspace.pyindex c3038e089..e8400d9e7 100644--- a/tests/unit/server/test_mcp_workspace.py+++ b/tests/unit/server/test_mcp_workspace.py@@ -25,7 +25,7 @@ from repowise.core.persistence.models import (from repowise.core.persistence.search import FullTextSearchfrom repowise.core.persistence.vector_store import InMemoryVectorStorefrom repowise.core.providers.embedding.base import MockEmbedder-from repowise.core.workspace.registry import RepoContext, RepoRegistry+from repowise.core.workspace.registry import RepoContext# ---------------------------------------------------------------------------# Fixtures@@ -601,3 +601,45 @@ async def test_overview_all_includes_cross_repo_topology(workspace_mcp_with_enriresult = await get_overview(repo="all")assert "cross_repo_topology" in resultassert result["cross_repo_topology"]["co_change_count"] == 1+++async def test_list_repos_discovers_workspace_aliases(workspace_mcp):+ from repowise.server.mcp_server import list_repos++ result = await list_repos()++ assert result["workspace"] is True+ assert result["workspace_root"] == "/tmp/workspace"+ assert result["default_repo"] == "backend"+ assert [repo["alias"] for repo in result["repos"]] == ["backend", "frontend"]+ assert result["repos"][0]["is_default"] is True+ assert "repo='<alias>'" in result["hint"]+++async def test_list_repos_single_repo_mode():+ import repowise.server.mcp_server as mcp_mod+ from repowise.server.mcp_server import list_repos++ original_registry = mcp_mod._registry+ original_repo_path = mcp_mod._repo_path+ mcp_mod._registry = None+ mcp_mod._repo_path = "/tmp/solo"++ try:+ result = await list_repos()+ finally:+ mcp_mod._registry = original_registry+ mcp_mod._repo_path = original_repo_path++ assert result["workspace"] is False+ assert result["default_repo"] == "default"+ assert result["repos"] == [+ {+ "alias": "default",+ "path": "/tmp/solo",+ "absolute_path": "/tmp/solo",+ "is_default": True,+ }+ ]