Skip to content

Commit 4aee7bc

Browse files
Implement MCP Server functionality for Codegen CLI (#1153)
Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Jay Hack <jayhack@users.noreply.github.com>
1 parent 21576a8 commit 4aee7bc

File tree

15 files changed

+599
-35
lines changed

15 files changed

+599
-35
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ dependencies = [
2323
"codeowners>=0.6.0",
2424
"unidiff>=0.7.5",
2525
"datamodel-code-generator>=0.26.5",
26-
"mcp[cli]",
26+
"mcp[cli]==1.9.4",
27+
"fastmcp>=2.9.0",
2728
# Utility dependencies
2829
"colorlog>=6.9.0",
2930
"psutil>=5.8.0",

src/codegen/cli/api/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from pydantic import BaseModel
66
from rich import print as rprint
77

8-
from codegen.cli.api.endpoints import IDENTIFY_ENDPOINT
9-
from codegen.cli.api.schemas import IdentifyResponse
108
from codegen.cli.env.global_env import global_env
119
from codegen.cli.errors import InvalidTokenError, ServerError
1210

@@ -16,11 +14,13 @@
1614

1715
class AuthContext(BaseModel):
1816
"""Authentication context model."""
17+
1918
status: str
2019

2120

2221
class Identity(BaseModel):
2322
"""User identity model."""
23+
2424
auth_context: AuthContext
2525

2626

src/codegen/cli/auth/session.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"\n",
1212
"\n",
1313
"# Create a session with the current directory as repo_path\n",
14-
"session = CodegenSession(repo_path=Path('.'))\n",
14+
"session = CodegenSession(repo_path=Path(\".\"))\n",
1515
"print(f\"Session: {session}\")\n",
1616
"print(f\"Repo path: {session.repo_path}\")\n",
1717
"print(f\"Config: {session.config}\")\n",

src/codegen/cli/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from codegen.cli.commands.init.main import init
1111
from codegen.cli.commands.login.main import login
1212
from codegen.cli.commands.logout.main import logout
13+
from codegen.cli.commands.mcp.main import mcp
1314
from codegen.cli.commands.profile.main import profile
1415
from codegen.cli.commands.style_debug.main import style_debug
1516
from codegen.cli.commands.update.main import update
@@ -31,6 +32,7 @@ def version_callback(value: bool):
3132
main.command("init", help="Initialize or update the Codegen folder.")(init)
3233
main.command("login", help="Store authentication token.")(login)
3334
main.command("logout", help="Clear stored authentication token.")(logout)
35+
main.command("mcp", help="Start the Codegen MCP server.")(mcp)
3436
main.command("profile", help="Display information about the currently authenticated user.")(profile)
3537
main.command("style-debug", help="Debug command to visualize CLI styling (spinners, etc).")(style_debug)
3638
main.command("update", help="Update Codegen to the latest or specified version")(update)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

src/codegen/cli/commands/mcp/main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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)

src/codegen/cli/commands/update/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ def update(
4343
rich.print("[red]Error:[/red] Cannot specify both --list and --version")
4444
raise typer.Exit(1)
4545

46-
package_info = distribution(codegen.__package__)
46+
package_name = codegen.__package__ or "codegen"
47+
package_info = distribution(package_name)
4748
current_version = Version(package_info.version)
4849

4950
if list_:

src/codegen/cli/mcp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""MCP (Model Context Protocol) server for Codegen."""

0 commit comments

Comments
 (0)