|
| 1 | +import asyncio |
1 | 2 | import click |
2 | | -from pathlib import Path |
3 | 3 | import logging |
| 4 | +from pathlib import Path |
4 | 5 | import sys |
| 6 | +import signal |
5 | 7 | from .server import serve |
6 | 8 |
|
| 9 | +def handle_sigint(): |
| 10 | + for task in asyncio.all_tasks(): |
| 11 | + task.cancel() |
| 12 | + |
7 | 13 | @click.command() |
8 | | -@click.option("--repository", "-r", type=Path, help="Git repository path") |
9 | | -@click.option("-v", "--verbose", count=True) |
10 | | -def main(repository: Path | None, verbose: bool) -> None: |
| 14 | +@click.option( |
| 15 | + "--repository", |
| 16 | + type=click.Path(exists=True, file_okay=False, dir_okay=True, path_type=Path), |
| 17 | + help="Path to Git repository to operate on", |
| 18 | +) |
| 19 | +@click.option("-v", "--verbose", count=True, default=0) |
| 20 | +def main(repository: Path | None, verbose: int = 0) -> None: |
11 | 21 | """MCP Git Server - Git functionality for MCP""" |
12 | | - import asyncio |
13 | | - |
14 | 22 | logging_level = logging.WARN |
15 | 23 | if verbose == 1: |
16 | 24 | logging_level = logging.INFO |
17 | | - elif verbose >= 2: |
| 25 | + elif verbose > 1: |
18 | 26 | logging_level = logging.DEBUG |
19 | | - |
20 | | - logging.basicConfig(level=logging_level, stream=sys.stderr) |
21 | | - asyncio.run(serve(repository)) |
| 27 | + logging.basicConfig(level=logging_level) |
| 28 | + |
| 29 | + loop = asyncio.new_event_loop() |
| 30 | + asyncio.set_event_loop(loop) |
| 31 | + |
| 32 | + # Set up signal handlers |
| 33 | + loop.add_signal_handler(signal.SIGINT, handle_sigint) |
| 34 | + loop.add_signal_handler(signal.SIGTERM, handle_sigint) |
| 35 | + |
| 36 | + try: |
| 37 | + loop.run_until_complete(serve(repository)) |
| 38 | + except asyncio.CancelledError: |
| 39 | + logging.info("Server shutdown initiated") |
| 40 | + finally: |
| 41 | + loop.close() |
22 | 42 |
|
23 | 43 | if __name__ == "__main__": |
24 | 44 | main() |
0 commit comments