-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathmcp_setup.py
More file actions
80 lines (69 loc) · 2.85 KB
/
mcp_setup.py
File metadata and controls
80 lines (69 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import subprocess
from codegen.cli.api.endpoints import MCP_SERVER_ENDPOINT
from codegen.cli.auth.token_manager import get_current_token
from codegen.cli.commands.claude.quiet_console import console
from codegen.cli.commands.claude.utils import resolve_claude_path
def add_codegen_mcp_server(org_id: int | None = None, repo_id: int | None = None):
console.print("🔧 Configuring MCP server 'codegen-tools'...", style="blue")
try:
token = get_current_token()
if not token:
console.print("⚠️ No authentication token found. Please run 'codegen login' first.", style="yellow")
return
claude_path = resolve_claude_path()
if not claude_path:
console.print("⚠️ 'claude' CLI not found to add MCP server", style="yellow")
return
# Build the command with required headers
cmd = [
claude_path,
"mcp",
"add",
"--transport",
"http",
"codegen-tools",
MCP_SERVER_ENDPOINT,
"--header",
f"Authorization: Bearer {token}",
]
# Add organization ID header if available
if org_id is not None:
cmd.extend(["--header", f"x-organization-id: {org_id}"])
console.print(f" Adding organization ID: {org_id}", style="dim")
# Add repository ID header if available
if repo_id is not None:
cmd.extend(["--header", f"x-repo-id: {repo_id}"])
console.print(f" Adding repository ID: {repo_id}", style="dim")
add_result = subprocess.run(
cmd,
capture_output=True,
text=True,
timeout=15,
)
if add_result.returncode == 0:
console.print("✅ MCP server added: codegen-tools -> http", style="green")
else:
stderr = add_result.stderr.strip() if add_result.stderr else add_result.stdout.strip()
console.print(f"⚠️ Failed to add MCP server (code {add_result.returncode}): {stderr}", style="yellow")
except subprocess.TimeoutExpired:
console.print("⚠️ MCP server add timed out", style="yellow")
except FileNotFoundError:
console.print("⚠️ 'claude' CLI not found to add MCP server", style="yellow")
except Exception as e:
console.print(f"⚠️ Error adding MCP server: {e}", style="yellow")
def cleanup_codegen_mcp_server():
try:
claude_path = resolve_claude_path()
if not claude_path:
# Silently skip if claude is not found during cleanup
return
subprocess.run(
[
claude_path,
"mcp",
"remove",
"codegen-tools",
],
)
except Exception as e:
console.print(f"⚠️ Error removing MCP server: {e}", style="yellow")