|
| 1 | +"""MCP server command for the Codegen CLI.""" |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import typer |
| 6 | +from rich.console import Console |
| 7 | + |
| 8 | +console = Console() |
| 9 | + |
| 10 | + |
| 11 | +def mcp( |
| 12 | + host: str = typer.Option("localhost", help="Host to bind the MCP server to"), |
| 13 | + port: Optional[int] = typer.Option(None, help="Port to bind the MCP server to (default: stdio transport)"), |
| 14 | + transport: str = typer.Option("stdio", help="Transport protocol to use (stdio or http)"), |
| 15 | +): |
| 16 | + """Start the Codegen MCP server.""" |
| 17 | + console.print("🚀 Starting Codegen MCP server...", style="bold green") |
| 18 | + |
| 19 | + if transport == "stdio": |
| 20 | + console.print("📡 Using stdio transport", style="dim") |
| 21 | + else: |
| 22 | + console.print(f"📡 Using HTTP transport on {host}:{port}", style="dim") |
| 23 | + |
| 24 | + # Validate transport |
| 25 | + if transport not in ["stdio", "http"]: |
| 26 | + console.print(f"❌ Invalid transport: {transport}. Must be 'stdio' or 'http'", style="bold red") |
| 27 | + raise typer.Exit(1) |
| 28 | + |
| 29 | + # Import here to avoid circular imports and ensure dependencies are available |
| 30 | + from codegen.cli.mcp.server import run_server |
| 31 | + |
| 32 | + try: |
| 33 | + run_server(transport=transport, host=host, port=port) |
| 34 | + except KeyboardInterrupt: |
| 35 | + console.print("\n👋 MCP server stopped", style="yellow") |
| 36 | + except Exception as e: |
| 37 | + console.print(f"❌ Error starting MCP server: {e}", style="bold red") |
| 38 | + raise typer.Exit(1) |
0 commit comments