from __future__ import annotations import argparse from collections.abc import Sequence from pathlib import Path from mcp_service_config import ( DEFAULT_CONFIG_PATH, DEFAULT_HOST, DEFAULT_NB_ENV, DEFAULT_PATH, DEFAULT_PORT, DEFAULT_TOKEN_BYTES, SERVICE_NAME, ) from mcp_service_runtime import new_token, service_status, show_config, start_service, stop_service, uninstall_service def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser(description="Manage the HTTP NocoBase CRM MCP user service.") subparsers = parser.add_subparsers(dest="command", required=True) new_token_parser = subparsers.add_parser( "new-token", help="Generate a new token and print local MCP client configuration." ) new_token_parser.add_argument("--project-root", default=Path(__file__).resolve().parent.parent, type=Path) new_token_parser.add_argument("--host", default=DEFAULT_HOST) new_token_parser.add_argument("--port", default=DEFAULT_PORT, type=int) new_token_parser.add_argument("--path", default=DEFAULT_PATH) new_token_parser.add_argument("--nb-env", default=DEFAULT_NB_ENV) new_token_parser.add_argument("--config-path", default=DEFAULT_CONFIG_PATH) new_token_parser.add_argument("--token-bytes", default=DEFAULT_TOKEN_BYTES, type=int) start_parser = subparsers.add_parser( "start", help="Install or update the user service using the saved token, then start it." ) start_parser.add_argument("--project-root", default=Path(__file__).resolve().parent.parent, type=Path) subparsers.add_parser("stop", help="Stop the user service without deleting service files.") uninstall_parser = subparsers.add_parser( "uninstall", help="Stop and remove the managed user service and token file." ) uninstall_parser.add_argument("--project-root", default=Path(__file__).resolve().parent.parent, type=Path) show_config_parser = subparsers.add_parser( "show-config", help="Print the saved token and local MCP client configuration." ) show_config_parser.add_argument("--project-root", default=Path(__file__).resolve().parent.parent, type=Path) status_parser = subparsers.add_parser("status", help="Show systemd status for the managed service.") status_parser.add_argument("--project-root", default=Path(__file__).resolve().parent.parent, type=Path) args = parser.parse_args(argv) if args.command == "new-token": print( new_token( project_root=args.project_root, host=args.host, port=args.port, path=args.path, nb_env=args.nb_env, config_path=args.config_path, token_bytes=args.token_bytes, ), end="", ) return 0 if args.command == "start": print(start_service(project_root=args.project_root), end="") return 0 if args.command == "stop": stop_service() print(f"Stopped {SERVICE_NAME}.") return 0 if args.command == "uninstall": uninstall_service(args.project_root) print(f"Uninstalled {SERVICE_NAME}.") return 0 if args.command == "show-config": print(show_config(args.project_root), end="") return 0 if args.command == "status": print(service_status(project_root=args.project_root), end="") return 0 raise AssertionError(f"unhandled command: {args.command}") __all__ = ["main"]