72 lines
2.2 KiB
Python
72 lines
2.2 KiB
Python
"""Stdio launcher so Claude Code itself can load the d-wiki MCP server.
|
||
|
||
Auth is disabled (no live JWKS locally) and the d-wiki engine is the test
|
||
fake bundled in tests/fakes — but the MCP server, the stdio transport and
|
||
the tool/resource implementation are all the real production code paths.
|
||
Claude Code launches this process and speaks JSON-RPC over its stdio.
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import importlib.util
|
||
import sys
|
||
import tempfile
|
||
from pathlib import Path
|
||
|
||
# Make the fake d-wiki engine importable (mirrors tests/conftest.py).
|
||
if importlib.util.find_spec("d_wiki") is None:
|
||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "tests" / "fakes"))
|
||
|
||
from service.mcp_app import McpServiceSettings, create_mcp_server # noqa: E402
|
||
|
||
|
||
KB_DIR = Path(tempfile.gettempdir()) / "d-wiki-e2e-kb"
|
||
|
||
|
||
def _seed_kb() -> Path:
|
||
"""Write a small markdown knowledge base if not already present."""
|
||
root = KB_DIR / "x5"
|
||
root.mkdir(parents=True, exist_ok=True)
|
||
(root / "calibration.md").write_text(
|
||
"# X5 传感器标定流程\n\n"
|
||
"## 范围\n\n"
|
||
"本文件描述 X5 传感器的出厂标定步骤,包括点亮、坐标对齐与残差校验。\n\n"
|
||
"## 配置\n\n"
|
||
"标定需读取 mount.yaml,确认 IMU 与相机外参,标定残差应小于 0.5 度。\n",
|
||
encoding="utf-8",
|
||
)
|
||
(root / "boot.md").write_text(
|
||
"# X5 启动流程\n\n"
|
||
"## 上电顺序\n\n"
|
||
"先 12V 主供电,再 USB 控制总线,最后等待 IMU 就绪信号。\n",
|
||
encoding="utf-8",
|
||
)
|
||
cfg = KB_DIR / "d-wiki.yml"
|
||
cfg.write_text(
|
||
"knowledge_bases:\n"
|
||
" x5:\n"
|
||
f" root: {root}\n"
|
||
" source_type: markdown\n",
|
||
encoding="utf-8",
|
||
)
|
||
return cfg
|
||
|
||
|
||
def main() -> None:
|
||
import asyncio
|
||
|
||
from d_wiki.config import load_config
|
||
from d_wiki.http_server import handle_search
|
||
|
||
config = load_config(_seed_kb())
|
||
server = create_mcp_server(
|
||
knowledge_bases=config.knowledge_bases,
|
||
search_handler=handle_search,
|
||
token_verifier=None,
|
||
settings=McpServiceSettings(auth_enabled=False),
|
||
)
|
||
asyncio.run(server.run_stdio_async())
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|