arcade-mcp/libs/arcade-cli/arcade_cli/configure.py
Eric Gustin b780e5b807
Fix stdio bugs (#608)
1. Updates `arcade configure claude --from-local` to create a valid json
config for claude desktop. NOTE: The `arcade configure` command needs
some re-work. It's fragile.
2. Fixes bug where stdio servers were sending logs to the wrong sink.
3. Disabled colorized logs for stdio.
4. Added missing dependency `httpx` for servers created with `arcade
new`

## Claude Desktop json configuration for stdio
Personally I like option 1 because the configuration looks the simplest
### Option 1:
Equivalent to `python server.py stdio`
```
{
  "globalShortcut": "Alt+Ctrl+Space",
  "mcpServers": {
    "my_server": {
      "command": "/path/to/my/mcp/server/directory/.venv/bin/python",
      "args": [
        "/path/to/my/mcp/server/directory/server.py",
        "stdio"
      ]
    }
  }
}
```
### Option 2:
Equivalent to `uv run server.py stdio`
```
{
  "mcpServers": {
    "my_server": {
      "command": "uv",
      "args": [
        "run",
        "--directory",
        "/path/to/my/mcp/server/directory",
        "python",
        "server.py",
        "stdio"
      ]
    }
  }
}
```
### Option 3:
Equivalent to `python -m arcade_mcp_server stdio --cwd ./`
```
{
  "mcpServers": {
    "my_server": {
      "command": "/path/to/my/mcp/server/directory/.venv/bin/python",
      "args": [
        "-m",
        "arcade_mcp_server",
        "stdio",
        "--cwd",
        "/path/to/my/mcp/server/directory"
      ]
    }
  }
}
```
2025-10-07 18:53:53 -07:00

252 lines
9.2 KiB
Python

"""Connect command for configuring MCP clients."""
import json
import os
import platform
from pathlib import Path
import typer
from rich.console import Console
console = Console()
def get_claude_config_path() -> Path:
"""Get the Claude Desktop configuration file path."""
system = platform.system()
if system == "Darwin": # macOS
return (
Path.home()
/ "Library"
/ "Application Support"
/ "Claude"
/ "claude_desktop_config.json"
)
elif system == "Windows":
return Path(os.environ["APPDATA"]) / "Claude" / "claude_desktop_config.json"
else: # Linux
return Path.home() / ".config" / "Claude" / "claude_desktop_config.json"
def get_cursor_config_path() -> Path:
"""Get the Cursor configuration file path."""
system = platform.system()
if system == "Darwin": # macOS
return Path.home() / ".cursor" / "mcp.json"
elif system == "Windows":
return Path(os.environ["APPDATA"]) / "Cursor" / "mcp.json"
else: # Linux
return Path.home() / ".config" / "Cursor" / "mcp.json"
def get_vscode_config_path() -> Path:
"""Get the VS Code configuration file path."""
# Paths to global 'Default User' MCP configuration file
system = platform.system()
if system == "Darwin": # macOS
return Path.home() / "Library" / "Application Support" / "Code" / "User" / "mcp.json"
elif system == "Windows":
return Path(os.environ["APPDATA"]) / "Code" / "User" / "mcp.json"
else: # Linux
return Path.home() / ".config" / "Code" / "User" / "mcp.json"
def configure_claude_local(server_name: str, port: int = 8000, path: Path | None = None) -> None:
"""Configure Claude Desktop to add a local MCP server to the configuration."""
config_path = path or get_claude_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
# Assume server.py is the entry point for the server
server_file = Path.cwd() / "server.py"
# Find the Python interpreter in the virtual environment
venv_python = None
# Check for .venv first (uv default)
if (Path.cwd() / ".venv").exists():
system = platform.system()
if system == "Windows":
venv_python = Path.cwd() / ".venv" / "Scripts" / "python.exe"
else:
venv_python = Path.cwd() / ".venv" / "bin" / "python"
# Fall back to system python if no venv found
if not venv_python or not venv_python.exists():
console.print("[yellow]Warning: No .venv found, using system python[/yellow]")
import sys
venv_python = Path(sys.executable)
# Load existing config or create new one
config = {}
if config_path.exists():
with open(config_path) as f:
config = json.load(f)
# Add or update MCP servers configuration
if "mcpServers" not in config:
config["mcpServers"] = {}
# Claude Desktop uses stdio transport
config["mcpServers"][server_name] = {
"command": str(venv_python),
"args": [str(server_file), "stdio"],
}
# Write updated config
with open(config_path, "w") as f:
json.dump(config, f, indent=2)
console.print(
f"✅ Configured Claude Desktop by adding local MCP server '{server_name}' to the configuration",
style="green",
)
config_file_path = config_path.as_posix().replace(" ", "\\ ")
console.print(f" MCP client config file: {config_file_path}", style="dim")
console.print(f" Server file: {server_file}", style="dim")
console.print(f" Python interpreter: {venv_python}", style="dim")
console.print(" Restart Claude Desktop for changes to take effect.", style="yellow")
def configure_claude_arcade(server_name: str, path: Path | None = None) -> None:
"""Configure Claude Desktop to add an Arcade Cloud MCP server to the configuration."""
# This would connect to the Arcade Cloud to get the server URL
# For now, this is a placeholder
console.print("[red]Connecting to Arcade Cloud servers not yet implemented[/red]")
def configure_cursor_local(server_name: str, port: int = 8000, path: Path | None = None) -> None:
"""Configure Cursor to add a local MCP server to the configuration."""
config_path = path or get_cursor_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
# Load existing config or create new one
config = {}
if config_path.exists():
with open(config_path) as f:
config = json.load(f)
# Add or update MCP servers configuration
if "mcpServers" not in config:
config["mcpServers"] = {}
config["mcpServers"][server_name] = {
"name": server_name,
"type": "stream", # Cursor prefers stream
"url": f"http://localhost:{port}/mcp",
}
# Write updated config
with open(config_path, "w") as f:
json.dump(config, f, indent=2)
console.print(
f"✅ Configured Cursor by adding local MCP server '{server_name}' to the configuration",
style="green",
)
config_file_path = config_path.as_posix().replace(" ", "\\ ")
console.print(f" MCP client config file: {config_file_path}", style="dim")
console.print(f" MCP Server URL: http://localhost:{port}/mcp", style="dim")
console.print(" Restart Cursor for changes to take effect.", style="yellow")
def configure_cursor_arcade(server_name: str, path: Path | None = None) -> None:
"""Configure Cursor to add an Arcade Cloud MCP server to the configuration."""
console.print("[red]Connecting to Arcade Cloud servers not yet implemented[/red]")
def configure_vscode_local(server_name: str, port: int = 8000, path: Path | None = None) -> None:
"""Configure VS Code to add a local MCP server to the configuration."""
config_path = path or get_vscode_config_path()
config_path.parent.mkdir(parents=True, exist_ok=True)
# Load existing config or create new one
config = {}
if config_path.exists():
with open(config_path) as f:
try:
config = json.load(f)
except json.JSONDecodeError as e:
raise ValueError(
f"\n\tFailed to load MCP configuration file at {config_path.as_posix()} "
f"\n\tThe file contains invalid JSON: {e}. "
"\n\tPlease check the file format or delete it to create a new configuration."
)
# Add or update MCP servers configuration
if "servers" not in config:
config["servers"] = {}
config["servers"][server_name] = {
"type": "http",
"url": f"http://localhost:{port}/mcp",
}
# Write updated config
with open(config_path, "w") as f:
json.dump(config, f, indent=2)
console.print(
f"✅ Configured VS Code by adding local MCP server '{server_name}' to the configuration",
style="green",
)
config_file_path = config_path.as_posix().replace(" ", "\\ ")
console.print(f" MCP client config file: {config_file_path}", style="dim")
console.print(f" MCP Server URL: http://localhost:{port}/mcp", style="dim")
console.print(" Restart VS Code for changes to take effect.", style="yellow")
def configure_vscode_arcade(server_name: str, path: Path | None = None) -> None:
"""Configure VS Code to add an Arcade Cloud MCP server to the configuration."""
console.print("[red]Connecting to Arcade Cloud servers not yet implemented[/red]")
def configure_client(
client: str,
server_name: str | None = None,
from_local: bool = False,
from_arcade: bool = False,
port: int = 8000,
path: Path | None = None,
) -> None:
"""
Configure an MCP client to connect to a server.
Args:
client: The MCP client to configure (claude, cursor, vscode)
server_name: Name of the server to add to the configuration
from_local: Add a local server to the configuration
from_arcade: Add an Arcade Cloud server to the configuration
port: Port for local servers (default: 8000)
path: Custom path to the MCP client configuration file
"""
if not from_local and not from_arcade:
raise typer.BadParameter("Must specify either --from-local or --from-arcade")
if from_local and from_arcade:
raise typer.BadParameter("Cannot specify both --from-local and --from-arcade")
# Default server name if not provided
if not server_name:
# Try to detect from current directory
server_name = Path.cwd().name if Path("server.py").exists() else "arcade-mcp-server"
client_lower = client.lower()
if client_lower == "claude":
if from_local:
configure_claude_local(server_name, port, path)
else:
configure_claude_arcade(server_name, path)
elif client_lower == "cursor":
if from_local:
configure_cursor_local(server_name, port, path)
else:
configure_cursor_arcade(server_name, path)
elif client_lower == "vscode":
if from_local:
configure_vscode_local(server_name, port, path)
else:
configure_vscode_arcade(server_name, path)
else:
raise typer.BadParameter(
f"Unknown client: {client}. Supported clients: claude, cursor, vscode."
)