"""MCP server entry point for paper-doc. Usage (from anywhere — the script anchors ``sys.path`` itself):: backend/.venv/bin/python backend/scripts/mcp_server.py # stdio backend/.venv/bin/python backend/scripts/mcp_server.py --check # 自检 backend/.venv/bin/python backend/scripts/mcp_server.py \\ --transport http --host 0.0.0.0 --port 8931 --token secret ``stdio`` is what an MCP client spawns: Claude Code, Codex and the DeepSeek Harness all run a command and speak JSON-RPC over its stdin/stdout. Two rules follow from that, and both are easy to break by accident: * **Nothing may print to stdout** except the protocol. Diagnostics go to stderr; a ``print`` added here for debugging would corrupt the stream and present as "the server disconnected". * **The command must work from any working directory**, because a client's ``cwd`` is its own, not this project's. That is why the path anchor below exists rather than a relative ``import app``. The CLI itself lives in :func:`app.mcp.server.main` so this file stays a launcher: the same arguments are reachable as ``python -m app.mcp`` from ``backend/``. """ from __future__ import annotations import sys from pathlib import Path # Running this as a plain script puts scripts/ on sys.path, not backend/, so # `import app` would fail. Anchor to the backend directory instead. BACKEND_DIR = Path(__file__).resolve().parents[1] if str(BACKEND_DIR) not in sys.path: sys.path.insert(0, str(BACKEND_DIR)) from app.mcp.server import main # noqa: E402 if __name__ == "__main__": sys.exit(main())