85 lines
3.7 KiB
Python
85 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Local installation and launch. No global npm install or automatic client edits."""
|
|
from __future__ import annotations
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import venv
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "mcp_server"))
|
|
from runtime import cli_command, child_environment, state_directory
|
|
|
|
|
|
def python() -> Path:
|
|
return ROOT / ".venv" / "bin" / "python"
|
|
|
|
|
|
def run(command, **kwargs):
|
|
subprocess.run([str(x) for x in command], check=True, **kwargs)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("action", choices=["setup", "login", "run", "config", "check", "test", "cli"])
|
|
parser.add_argument("--account", choices=["main", "reserve"], default="main")
|
|
parser.add_argument("--transport", choices=["tcp", "wss"], default="tcp")
|
|
args, extra = parser.parse_known_args()
|
|
if args.action != "cli" and extra:
|
|
parser.error("unexpected arguments")
|
|
if os.name != "posix":
|
|
parser.error("Use macOS, Linux or WSL; native Windows is not supported")
|
|
if args.action == "setup":
|
|
if sys.version_info < (3, 11):
|
|
parser.error("Python 3.11+ is required")
|
|
for name in ("node", "npm"):
|
|
if not shutil.which(name):
|
|
parser.error(f"Install {name} first (Node 20+)")
|
|
version = subprocess.check_output(["node", "--version"], text=True)
|
|
if int(version.lstrip("v").split('.')[0]) < 20:
|
|
parser.error("Node 20+ is required")
|
|
if not python().exists():
|
|
venv.EnvBuilder(with_pip=True).create(ROOT / ".venv")
|
|
run([python(), "-m", "pip", "install", "--require-hashes", "-r", ROOT / "requirements.lock"])
|
|
run(["npm", "ci", "--ignore-scripts", "--no-audit", "--no-fund"], cwd=ROOT / "cli")
|
|
run(["npm", "run", "build"], cwd=ROOT / "cli")
|
|
print("Installed locally. Next: python3 scripts/manage.py login")
|
|
elif args.action == "config":
|
|
env = {}
|
|
if "TG_MCP_STATE_DIR" in os.environ:
|
|
env["TG_MCP_STATE_DIR"] = str(state_directory())
|
|
server = {"command": str(python()), "args": [str(ROOT / "mcp_server" / "server.py")]}
|
|
if env:
|
|
server["env"] = env
|
|
print(json.dumps({"mcpServers": {"telegram": server}}, indent=2))
|
|
elif args.action == "run":
|
|
os.execv(str(python()), [str(python()), str(ROOT / "mcp_server" / "server.py")])
|
|
elif args.action == "login":
|
|
state = state_directory()
|
|
state.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
os.chmod(state, 0o700)
|
|
# Interactive terminal only: credentials and codes are never MCP arguments.
|
|
if not sys.stdin.isatty():
|
|
parser.error("Run login yourself in an interactive terminal")
|
|
os.umask(0o077)
|
|
run(cli_command(args.account) + ["--transport", args.transport, "auth", "login", "--client", "desktop"],
|
|
env=child_environment(args.account))
|
|
elif args.action == "cli":
|
|
run(cli_command(args.account) + (extra[1:] if extra[:1] == ["--"] else extra),
|
|
env=child_environment(args.account))
|
|
elif args.action == "check":
|
|
run([python(), ROOT / "scripts" / "smoke.py"])
|
|
elif args.action == "test":
|
|
run(["npm", "run", "typecheck"], cwd=ROOT / "cli")
|
|
run(["npm", "test", "--", "--maxWorkers=2", "--minWorkers=1"], cwd=ROOT / "cli")
|
|
env = dict(os.environ, PYTHONPATH=str(ROOT / "mcp_server"))
|
|
run([python(), "-m", "unittest", "discover", "-s", ROOT / "tests", "-v"], env=env)
|
|
run([python(), ROOT / "scripts" / "smoke.py"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|