feat(mcp): add MCP JSON renderer and CLI support, update tests accordingly

- Add MCPRenderer to generate MCP-native JSON bundles (index.json, nav.json, modules/*.json)
- Expose MCPRenderer via public API and CLI (`generate-mcp` command)
- Replace Markdown-based MCP output with structured JSON resources
- Update MCP renderer type stubs to match new JSON-based implementation
- Refactor MCP tests to validate JSON content, bundle structure, and navigation
- Fix MCP module coverage test to use explicit project_root for reliable discovery
This commit is contained in:
2026-01-21 16:43:21 +05:30
parent ce2eafac85
commit 5370a7faa2
8 changed files with 180 additions and 74 deletions

View File

@@ -1,3 +1,4 @@
import json
from pathlib import Path
from docforge import MCPRenderer
@@ -13,6 +14,8 @@ def test_mcp_file_content(tmp_path: Path):
renderer.generate_sources(project, out_dir)
content = (out_dir / "testpkg" / "mod.md").read_text()
module_file = out_dir / "modules" / "testpkg.mod.json"
payload = json.loads(module_file.read_text())
assert "# Module `testpkg.mod`" in content
assert payload["module"] == "testpkg.mod"
assert payload["content"]["path"] == "testpkg.mod"

View File

@@ -12,9 +12,9 @@ def test_mcp_idempotent(tmp_path: Path):
renderer = MCPRenderer()
renderer.generate_sources(project, out_dir)
first = (out_dir / "testpkg" / "mod.md").read_text()
first = (out_dir / "modules" / "testpkg.mod.json").read_text()
renderer.generate_sources(project, out_dir)
second = (out_dir / "testpkg" / "mod.md").read_text()
second = (out_dir / "modules" / "testpkg.mod.json").read_text()
assert first == second

View File

@@ -5,11 +5,13 @@ from docforge import MCPRenderer
def test_mcp_emits_all_modules(tmp_path: Path) -> None:
project_root = Path(__file__).resolve().parents[3]
loader = GriffeLoader()
discovered_paths = discover_module_paths(
"docforge",
Path(r"C:\Users\vishe\WorkSpace\code\aetos\doc-forge"),
project_root=project_root,
)
project = loader.load_project(discovered_paths)
renderer = MCPRenderer()
@@ -17,13 +19,16 @@ def test_mcp_emits_all_modules(tmp_path: Path) -> None:
emitted = {
p.relative_to(tmp_path).as_posix()
for p in tmp_path.rglob("*.md")
for p in (tmp_path / "modules").rglob("*.json")
}
expected = {
m.path.replace(".", "/") + ".md"
f"modules/{m.path}.json"
for m in project.get_all_modules()
}
missing = expected - emitted
assert not missing, f"Missing MCP resources for modules: {missing}"
assert not missing, f"Missing MCP module JSON files: {missing}"
# also assert nav.json exists
assert (tmp_path / "nav.json").exists()

View File

@@ -14,5 +14,10 @@ def test_mcp_directory_structure(tmp_path: Path):
renderer.generate_sources(project, out_dir)
assert (out_dir / "testpkg.md").exists()
assert (out_dir / "testpkg" / "sub.md").exists()
# Bundle-level files
assert (out_dir / "index.json").exists()
assert (out_dir / "nav.json").exists()
# Module resources
assert (out_dir / "modules" / "testpkg.json").exists()
assert (out_dir / "modules" / "testpkg.sub.json").exists()