|
| 1 | +"""MCP server module entry point for running with python -m mcp_server.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import click |
| 5 | + |
| 6 | +# Import logging configuration first to intercept all logging |
| 7 | +from gitingest.utils.logging_config import get_logger |
| 8 | +from mcp_server.main import start_mcp_server_tcp |
| 9 | + |
| 10 | +logger = get_logger(__name__) |
| 11 | + |
| 12 | +@click.command() |
| 13 | +@click.option( |
| 14 | + "--transport", |
| 15 | + type=click.Choice(["stdio", "tcp"]), |
| 16 | + default="stdio", |
| 17 | + show_default=True, |
| 18 | + help="Transport protocol for MCP communication" |
| 19 | +) |
| 20 | +@click.option( |
| 21 | + "--host", |
| 22 | + default="0.0.0.0", |
| 23 | + show_default=True, |
| 24 | + help="Host to bind TCP server (only used with --transport tcp)" |
| 25 | +) |
| 26 | +@click.option( |
| 27 | + "--port", |
| 28 | + type=int, |
| 29 | + default=8001, |
| 30 | + show_default=True, |
| 31 | + help="Port for TCP server (only used with --transport tcp)" |
| 32 | +) |
| 33 | +def main(transport: str, host: str, port: int) -> None: |
| 34 | + """Start the Gitingest MCP (Model Context Protocol) server. |
| 35 | + |
| 36 | + The MCP server provides repository analysis capabilities to LLMs through |
| 37 | + the Model Context Protocol standard. |
| 38 | + |
| 39 | + Examples: |
| 40 | + |
| 41 | + # Start with stdio transport (default, for MCP clients) |
| 42 | + python -m mcp_server |
| 43 | + |
| 44 | + # Start with TCP transport for remote access |
| 45 | + python -m mcp_server --transport tcp --host 0.0.0.0 --port 8001 |
| 46 | + """ |
| 47 | + if transport == "tcp": |
| 48 | + # TCP mode needs asyncio |
| 49 | + asyncio.run(_async_main_tcp(host, port)) |
| 50 | + else: |
| 51 | + # FastMCP stdio mode gère son propre event loop |
| 52 | + _main_stdio() |
| 53 | + |
| 54 | +def _main_stdio() -> None: |
| 55 | + """Main function for stdio transport.""" |
| 56 | + try: |
| 57 | + logger.info("Starting Gitingest MCP server with stdio transport") |
| 58 | + # FastMCP gère son propre event loop pour stdio |
| 59 | + from mcp_server.main import mcp |
| 60 | + mcp.run(transport="stdio") |
| 61 | + except KeyboardInterrupt: |
| 62 | + logger.info("MCP server stopped by user") |
| 63 | + except Exception as exc: |
| 64 | + logger.error(f"Error starting MCP server: {exc}", exc_info=True) |
| 65 | + raise click.Abort from exc |
| 66 | + |
| 67 | +async def _async_main_tcp(host: str, port: int) -> None: |
| 68 | + """Async main function for TCP transport.""" |
| 69 | + try: |
| 70 | + logger.info(f"Starting Gitingest MCP server with TCP transport on {host}:{port}") |
| 71 | + await start_mcp_server_tcp(host, port) |
| 72 | + except KeyboardInterrupt: |
| 73 | + logger.info("MCP server stopped by user") |
| 74 | + except Exception as exc: |
| 75 | + logger.error(f"Error starting MCP server: {exc}", exc_info=True) |
| 76 | + raise click.Abort from exc |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments