From 896aa6111e13accd02139bd76c6d8bb4e691923c Mon Sep 17 00:00:00 2001 From: Devid HW Date: Mon, 20 Apr 2026 00:40:41 +0700 Subject: [PATCH] feat: auto-detect and register MCP on multiple platforms - Add multi-platform MCP detection (Claude, Windsurf, Cursor, VS Code) - Auto-unregister previous installations before reinstall - Create platform-specific docs (CURSOR.md, VSCODE.md, ANTIGRAVITY.md) - Update Windsurf config to use correct JSON path (~/.codeium/windsurf/mcp_config.json) - Fix VS Code config format (uses 'servers' not 'mcpServers') - Remove MT5_MCP_HOME requirement from MCP configs (binary auto-detects) - Slim down README installation section with platform table - Update QUICKSTART.md with all platform instructions --- README.md | 58 ++---- docs/ANTIGRAVITY.md | 145 +++++++++++++ docs/CURSOR.md | 129 ++++++++++++ docs/QUICKSTART.md | 67 +++++- docs/VSCODE.md | 147 +++++++++++++ docs/WINDSURF.md | 37 +++- scripts/setup.sh | 489 +++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 979 insertions(+), 93 deletions(-) create mode 100644 docs/ANTIGRAVITY.md create mode 100644 docs/CURSOR.md create mode 100644 docs/VSCODE.md diff --git a/README.md b/README.md index c304d9e..792d26d 100644 --- a/README.md +++ b/README.md @@ -33,50 +33,15 @@ bash scripts/setup.sh ### 2. Register MCP Server -#### Claude Code +| Platform | Command / Config | Docs | +|----------|------------------|------| +| **Claude Code** | `claude mcp add mt5-quant -- $(pwd)/mt5-quant` | [Setup →](docs/QUICKSTART.md) | +| **Windsurf** | Edit `~/.codeium/windsurf/mcp_config.json` | [WINDSURF.md →](docs/WINDSURF.md) | +| **Cursor** | Edit `~/.cursor/mcp.json` or use Settings → MCP | [CURSOR.md →](docs/CURSOR.md) | +| **VS Code** | Edit `.vscode/mcp.json` or run `MCP: Add Server` | [VSCODE.md →](docs/VSCODE.md) | +| **Antigravity** | Agent Panel → ... → MCP Servers → Edit configuration | [ANTIGRAVITY.md →](docs/ANTIGRAVITY.md) | -```bash -# Navigate to your project directory first -cd /path/to/your/mt5-quant - -# Register MCP server (requires absolute path) -claude mcp add MT5-Quant -- $(pwd)/mt5-quant - -# Verify installation -claude mcp list -``` - -#### Windsurf - -Add to `~/.windsurf/config.yaml`: - -```yaml -mcpServers: - mt5-quant: - command: /absolute/path/to/mt5-quant - env: - MT5_MCP_HOME: /absolute/path/to/mt5-quant -``` - -Or use the config command: - -```bash -# Get absolute path -which mt5-quant - -# Add to Windsurf config -cat >> ~/.windsurf/config.yaml << EOF -mcpServers: - mt5-quant: - command: $(which mt5-quant) - env: - MT5_MCP_HOME: $(dirname $(which mt5-quant)) -EOF -``` - -**[Full Setup →](docs/QUICKSTART.md)** - -> **Note:** MCP servers require **absolute paths**. Use `$(pwd)` or full path like `/Users/name/mt5-quant/mt5-quant`, not relative paths like `./mt5-quant`. +> **Note:** Use absolute paths like `/Users/name/mt5-quant/mt5-quant` or `$(pwd)/mt5-quant`, not relative paths like `./mt5-quant`. ## Quick Start @@ -91,8 +56,11 @@ The AI runs the full pipeline: compile → clean cache → backtest → extract | Doc | Purpose | |-----|---------| | [QUICKSTART.md](docs/QUICKSTART.md) | Complete setup for macOS/Linux | +| [WINDSURF.md](docs/WINDSURF.md) | Windsurf IDE setup | +| [CURSOR.md](docs/CURSOR.md) | Cursor IDE setup | +| [VSCODE.md](docs/VSCODE.md) | VS Code setup | +| [ANTIGRAVITY.md](docs/ANTIGRAVITY.md) | Antigravity IDE setup | | [CONFIG.md](docs/CONFIG.md) | Configuration reference | -| [WINDSURF.md](docs/WINDSURF.md) | Windsurf IDE integration | | [TOOLS.md](docs/MCP_TOOLS.md) | All 57 tools documented | | [ARCHITECTURE.md](docs/ARCHITECTURE.md) | Design and internals | | [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md) | Common issues | @@ -233,5 +201,3 @@ Run `verify_setup` from Claude first — it checks all paths and returns actiona MIT --- - -*Built from battle-tested production infrastructure. Every edge case in the pipeline was hit in production.* diff --git a/docs/ANTIGRAVITY.md b/docs/ANTIGRAVITY.md new file mode 100644 index 0000000..5c76bdc --- /dev/null +++ b/docs/ANTIGRAVITY.md @@ -0,0 +1,145 @@ +# Antigravity MCP Integration Setup + +## Quick Setup + +### Option 1: Download Prebuilt Binary (Recommended) + +```bash +# macOS (Apple Silicon) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-macos-arm64.tar.gz +tar -xzf mt5-quant.tar.gz + +# Linux (x64) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-linux-x64.tar.gz +tar -xzf mt5-quant.tar.gz +``` + +### Option 2: Build from Source + +```bash +cargo build --release +``` + +## Configure Antigravity + +### Step 1: Open MCP Manager + +1. Launch Antigravity +2. Look at the **right-side Agent Panel** +3. Click the **"..."** (More Options) menu at the top +4. Select **MCP Servers** + +### Step 2: Access Configuration + +1. Click **Manage MCP Servers** +2. Click **View raw config** or **Edit configuration** +3. This opens `mcp_config.json` + +### Step 3: Add mt5-quant Configuration + +Add to `mcp_config.json`: + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/absolute/path/to/mt5-quant" + } + } +} +``` + +### Step 4: Reload and Verify + +1. Save the file +2. **Restart Antigravity** (or reload the window) +3. Open the Agent chat and type: `What tools do you have access to?` +4. The agent should list MT5 tools like `verify_setup`, `run_backtest`, etc. + +## Full Example Configuration + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/Users/name/mt5-quant/target/release/mt5-quant" + }, + "github": { + "command": "npx", + "args": ["-y", "@modelcontextprotocol/server-github"], + "env": { + "GITHUB_PERSONAL_ACCESS_TOKEN": "${env:GITHUB_TOKEN}" + } + } + } +} +``` + +## Environment Variables + +Antigravity supports `${VAR_NAME}` syntax for environment variable substitution: + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/Users/name/mt5-quant/target/release/mt5-quant", + "env": { + "CUSTOM_VAR": "${env:MY_VAR}" + } + } + } +} +``` + +This keeps secrets out of the config file. + +## Verify Setup + +In Antigravity chat, type: + +``` +Run verify_setup +``` + +Expected output: +``` +Wine: /Applications/MetaTrader 5.app/.../wine64 +MT5 dir: ~/Library/Application Support/.../MetaTrader 5 +Display: gui +Arch: arch -x86_64 +``` + +## Troubleshooting + +### "Connection Refused" or "Tool not found" + +1. Double-check the server path in `mcp_config.json` +2. Ensure the binary has execute permissions: `chmod +x /path/to/mt5-quant` +3. Try completely restarting Antigravity +4. Check the server is listed in MCP manager + +### "Stdio Error" or JSON Parsing Error + +1. Verify the JSON syntax in `mcp_config.json` +2. Use a JSON validator if needed +3. Ensure no trailing commas + +### Agent Hallucinating Tool Parameters + +If the agent makes up incorrect parameters: +1. Check the tool is actually available: "List your available tools" +2. Restart the agent session +3. Be explicit in your requests + +## Configuration Location + +| Platform | Path | +|----------|------| +| All | Via UI: Agent Panel → ... → MCP Servers → Manage → Edit configuration | + +## Resources + +- [Antigravity Documentation](https://docs.antigravity.dev/) +- [MCP Server Reference](https://github.com/modelcontextprotocol/servers) +- [MT5-Quant Tools Reference](./MCP_TOOLS.md) diff --git a/docs/CURSOR.md b/docs/CURSOR.md new file mode 100644 index 0000000..90fdc12 --- /dev/null +++ b/docs/CURSOR.md @@ -0,0 +1,129 @@ +# Cursor MCP Integration Setup + +## Quick Setup + +### Option 1: Download Prebuilt Binary (Recommended) + +```bash +# macOS (Apple Silicon) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-macos-arm64.tar.gz +tar -xzf mt5-quant.tar.gz + +# Linux (x64) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-linux-x64.tar.gz +tar -xzf mt5-quant.tar.gz +``` + +### Option 2: Build from Source + +```bash +cargo build --release +``` + +## Configure Cursor + +### Method 1: Settings UI (Recommended) + +1. Open Cursor Settings (`Cmd/Ctrl + ,`) +2. Navigate to **Features** → **MCP** +3. Click **Add Custom MCP** +4. Enter: + - **Name**: `mt5-quant` + - **Command**: Full path to binary (e.g., `/Users/name/mt5-quant/target/release/mt5-quant`) + - **Type**: `stdio` + +### Method 2: Edit mcp.json Directly + +Add to `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (project): + +```json +{ + "mcpServers": { + "mt5-quant": { + "type": "stdio", + "command": "/absolute/path/to/mt5-quant" + } + } +} +``` + +Create the file if it doesn't exist: + +```bash +mkdir -p ~/.cursor +cat > ~/.cursor/mcp.json << 'EOF' +{ + "mcpServers": { + "mt5-quant": { + "type": "stdio", + "command": "/path/to/mt5-quant" + } + } +} +EOF +``` + +## Verify Setup + +In Cursor chat, type: + +``` +Run verify_setup +``` + +Expected output: +``` +Wine: /Applications/MetaTrader 5.app/.../wine64 +MT5 dir: ~/Library/Application Support/.../MetaTrader 5 +Display: gui +Arch: arch -x86_64 +``` + +## Configuration Locations + +| Scope | Path | Use Case | +|-------|------|----------| +| Global | `~/.cursor/mcp.json` | Available in all projects | +| Project | `.cursor/mcp.json` | Project-specific tools | + +## Troubleshooting + +### MCP server not appearing + +1. Check MCP panel in Cursor Settings +2. Verify the path is absolute (not relative) +3. Test binary: `/path/to/mt5-quant --help` +4. View MCP logs: Output panel → select "MCP" from dropdown + +### Config interpolation + +Cursor supports variable substitution in `mcp.json`: + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "${userHome}/bin/mt5-quant", + "args": ["--config", "${workspaceFolder}/config.yaml"] + } + } +} +``` + +Available variables: +- `${userHome}` - Home directory +- `${workspaceFolder}` - Project root +- `${workspaceFolderBasename}` - Project folder name +- `${env:VAR_NAME}` - Environment variable + +### Tool not found errors + +If the agent says "Tool not found": +1. Check the server is enabled in MCP settings +2. Try disabling and re-enabling the server +3. Restart Cursor + +## Resources + +- [Cursor MCP Documentation](https://cursor.com/docs/context/mcp) +- [MT5-Quant Tools Reference](./MCP_TOOLS.md) diff --git a/docs/QUICKSTART.md b/docs/QUICKSTART.md index c079694..bf04b40 100644 --- a/docs/QUICKSTART.md +++ b/docs/QUICKSTART.md @@ -91,15 +91,68 @@ claude mcp list ### Windsurf -Add to `~/.windsurf/config.yaml`: -```yaml -mcpServers: - mt5-quant: - command: /path/to/mt5-quant - env: - MT5_MCP_HOME: /path/to/mt5-mcp +Add to `~/.codeium/windsurf/mcp_config.json`: + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } +} ``` +### Cursor + +Add to `~/.cursor/mcp.json` (global) or `.cursor/mcp.json` (project): + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } +} +``` + +Or use Settings → MCP → Add Custom MCP. + +### VS Code + +Add to `.vscode/mcp.json` in your workspace: + +```json +{ + "servers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } +} +``` + +Or run `MCP: Add Server` from Command Palette. + +### Antigravity + +1. Open Agent panel → Click "..." menu → MCP Servers +2. Click "Manage MCP Servers" +3. Click "View raw config" or "Edit configuration" +4. Add to `mcp_config.json`: + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } +} +``` +5. Reload Antigravity to apply changes + ## 5. Verify Setup ```bash diff --git a/docs/VSCODE.md b/docs/VSCODE.md new file mode 100644 index 0000000..801bc84 --- /dev/null +++ b/docs/VSCODE.md @@ -0,0 +1,147 @@ +# VS Code MCP Integration Setup + +## Quick Setup + +### Option 1: Download Prebuilt Binary (Recommended) + +```bash +# macOS (Apple Silicon) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-macos-arm64.tar.gz +tar -xzf mt5-quant.tar.gz + +# Linux (x64) +curl -L -o mt5-quant.tar.gz https://github.com/masdevid/mt5-mcp/releases/latest/download/mt5-quant-linux-x64.tar.gz +tar -xzf mt5-quant.tar.gz +``` + +### Option 2: Build from Source + +```bash +cargo build --release +``` + +## Configure VS Code + +### Method 1: Command Palette (Recommended) + +1. Press `Cmd/Ctrl + Shift + P` +2. Run `MCP: Add Server` +3. Choose **Workspace** or **User** scope +4. Enter server name: `mt5-quant` +5. Enter command: Full path to binary (e.g., `/Users/name/mt5-quant/target/release/mt5-quant`) + +### Method 2: Edit mcp.json Directly + +Add to `.vscode/mcp.json` in your workspace: + +```json +{ + "servers": { + "mt5-quant": { + "command": "/absolute/path/to/mt5-quant" + } + } +} +``` + +Create the file: + +```bash +mkdir -p .vscode +cat > .vscode/mcp.json << 'EOF' +{ + "servers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } +} +EOF +``` + +### Method 3: VS Code CLI + +```bash +code --add-mcp '{"name":"mt5-quant","command":"/path/to/mt5-quant"}' +``` + +## Verify Setup + +In Copilot chat, type: + +``` +Run verify_setup +``` + +Expected output: +``` +Wine: /Applications/MetaTrader 5.app/.../wine64 +MT5 dir: ~/Library/Application Support/.../MetaTrader 5 +Display: gui +Arch: arch -x86_64 +``` + +## Configuration Locations + +| Scope | Path | Use Case | +|-------|------|----------| +| Workspace | `.vscode/mcp.json` | Share with team via source control | +| User | `~/.vscode/mcp.json` | Personal tools across all projects | +| Dev Container | `devcontainer.json` → `customizations.vscode.mcp` | Containerized environments | + +## Troubleshooting + +### MCP server not appearing + +1. Open **Output** panel (`Cmd/Ctrl + Shift + U`) +2. Select **MCP** from dropdown +3. Check for connection errors +4. Verify the path is absolute + +### Config not found + +The binary auto-detects its config, but you can also: +1. Run `setup.sh` to create `config/mt5-quant.yaml` +2. Or let the binary auto-discover on first run + +### Dev Container Setup + +Add to `.devcontainer/devcontainer.json`: + +```json +{ + "customizations": { + "vscode": { + "mcp": { + "servers": { + "mt5-quant": { + "command": "/path/to/mt5-quant" + } + } + } + } + } +} +``` + +## Key Differences from Other IDEs + +VS Code uses `servers` (not `mcpServers`) in the JSON structure: + +```json +{ + "servers": { // ← VS Code uses "servers" + "mt5-quant": { + "command": "..." + } + } +} +``` + +Other platforms use `mcpServers`. + +## Resources + +- [VS Code MCP Documentation](https://code.visualstudio.com/docs/copilot/customization/mcp-servers) +- [MCP Configuration Reference](https://code.visualstudio.com/docs/copilot/reference/mcp-configuration) +- [MT5-Quant Tools Reference](./MCP_TOOLS.md) diff --git a/docs/WINDSURF.md b/docs/WINDSURF.md index 0978a5b..78c88bd 100644 --- a/docs/WINDSURF.md +++ b/docs/WINDSURF.md @@ -22,14 +22,22 @@ bash scripts/build-rust.sh ### 2. Configure Windsurf -Edit `~/.windsurf/config.yaml`: +Edit `~/.codeium/windsurf/mcp_config.json`: -```yaml -mcpServers: - mt5-quant: - command: /Users/masdevid/jobs/mt5-quant/target/release/mt5-quant - env: - MT5_MCP_HOME: /Users/masdevid/jobs/mt5-quant +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/Users/masdevid/jobs/mt5-quant/target/release/mt5-quant" + } + } +} +``` + +Or use the automated setup: + +```bash +bash scripts/setup.sh ``` ### 3. Restart Windsurf @@ -66,12 +74,19 @@ scp -r config/mt5-quant.yaml user@server:~/.config/mt5-quant/config/ - **NO Python required!** ### Windsurf Config on Target Machine -```yaml -mcpServers: - mt5-quant: - command: /usr/local/bin/mt5-quant + +```json +{ + "mcpServers": { + "mt5-quant": { + "command": "/usr/local/bin/mt5-quant" + } + } +} ``` +The binary auto-detects its config location. No environment variables needed. + ## Troubleshooting ### MCP server not appearing diff --git a/scripts/setup.sh b/scripts/setup.sh index 7ad0057..de97d03 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -7,6 +7,16 @@ # --keep-last N Keep only last N backtest reports (default: 20) # --claude-code Generate CLAUDE.md template and .claude/hooks/user-prompt-submit.sh # (skips main config wizard — run standalone or alongside normal setup) +# +# MCP Auto-Registration (per official 2025 docs): +# Automatically detects and registers with available MCP platforms: +# - Claude Code : via 'claude mcp add' (stored in ~/.claude.json) +# - Windsurf : ~/.codeium/windsurf/mcp_config.json (JSON, mcpServers) +# - Cursor : ~/.cursor/mcp.json (JSON, mcpServers) +# - VS Code : .vscode/mcp.json (JSON, servers - not mcpServers) +# - Antigravity : mcp_config.json via UI (JSON, mcpServers) +# +# Previous installations are auto-detected and uninstalled before re-registering. set -uo pipefail @@ -648,6 +658,30 @@ main() { _bold "MT5-Quant setup — auto-detecting Wine and MT5 paths" echo "────────────────────────────────────────────────────" + # ── Check for previous installation ──────────────────────────────────────── + if _check_any_existing_registration; then + _yellow "Previous mt5-quant MCP installation detected on one or more platforms" + echo " New path: ${REPO_DIR}/server/main.py" + + local reinstall_ans="yes" + if ! $AUTO_YES; then + reinstall_ans=$(_ask "Unregister all previous installations and reinstall?" "yes") + fi + + if [[ "$reinstall_ans" =~ ^[Yy] ]]; then + _unregister_all_platforms || { + if ! $AUTO_YES; then + local force_ans + force_ans=$(_ask "Some platforms failed to unregister. Continue anyway?" "no") + [[ ! "$force_ans" =~ ^[Yy] ]] && exit 1 + fi + } + else + echo "Aborted — keeping existing installations." + exit 0 + fi + fi + # ── Check if config already exists ─────────────────────────────────────── if [[ -f "$CONFIG_OUT" ]] && ! $AUTO_YES; then _yellow "Config already exists: $CONFIG_OUT" @@ -784,49 +818,446 @@ main() { _ok "Written: $CONFIG_OUT" echo " Tip: see config/example.set for optimization .set file format" - # ── Register with Claude Code ────────────────────────────────────────────── - _offer_mcp_register + # ── Register with all detected MCP platforms ───────────────────────────── + _register_all_mcp_platforms echo "" _green "Setup complete!" echo "" } -# ── MCP registration ────────────────────────────────────────────────────────── -_offer_mcp_register() { - echo "" - _bold "Registering with Claude Code..." +# ── MCP Platform Detection & Registration ─────────────────────────────────── - if ! command -v claude &>/dev/null; then - _warn "claude CLI not found — register manually:" +# Detect available MCP platforms and return them as a list +detect_mcp_platforms() { + local platforms=() + + # Claude Code + if command -v claude &>/dev/null; then + platforms+=("claude") + fi + + # Windsurf (uses ~/.codeium/windsurf/mcp_config.json) + if [[ -d "$HOME/.codeium/windsurf" ]] || [[ -d "$HOME/.windsurf" ]] || command -v windsurf &>/dev/null; then + platforms+=("windsurf") + fi + + # Cursor (uses ~/.cursor/mcp.json) + if [[ -d "$HOME/.cursor" ]] || [[ -d "$HOME/Library/Application Support/Cursor" ]] || command -v cursor &>/dev/null; then + platforms+=("cursor") + fi + + # VS Code (uses .vscode/mcp.json or user profile) + if [[ -d "$HOME/.vscode" ]] || [[ -d "$HOME/Library/Application Support/Code" ]] || command -v code &>/dev/null; then + platforms+=("vscode") + fi + + printf '%s\n' "${platforms[@]:-}" +} + +# Check if mt5-quant is registered on a specific platform +_is_registered_on_platform() { + local platform="$1" + case "$platform" in + claude) + if command -v claude &>/dev/null; then + local mcp_list + mcp_list=$(claude mcp list 2>/dev/null || true) + echo "$mcp_list" | grep -q "mt5-quant" + return $? + fi + return 1 + ;; + windsurf) + local config_file="$HOME/.codeium/windsurf/mcp_config.json" + [[ -f "$config_file" ]] && grep -q '"mt5-quant"' "$config_file" 2>/dev/null + return $? + ;; + cursor) + local config_file + config_file="$HOME/.cursor/mcp.json" + [[ -f "$config_file" ]] && grep -q "mt5-quant" "$config_file" 2>/dev/null + return $? + ;; + vscode) + # VS Code uses .vscode/mcp.json in workspace or user profile + local workspace_config=".vscode/mcp.json" + local user_config + user_config="$HOME/.vscode/mcp.json" + [[ -f "$workspace_config" ]] && grep -q '"mt5-quant"' "$workspace_config" 2>/dev/null && return 0 + [[ -f "$user_config" ]] && grep -q '"mt5-quant"' "$user_config" 2>/dev/null && return 0 + return 1 + ;; + esac + return 1 +} + +# Get registered path for a platform +_get_platform_mcp_path() { + local platform="$1" + case "$platform" in + claude) + if command -v claude &>/dev/null; then + local mcp_list + mcp_list=$(claude mcp list 2>/dev/null || true) + echo "$mcp_list" | grep "mt5-quant" | head -1 | sed -E 's/.*--[[:space:]]*//' | tr -d ' ' + fi + ;; + windsurf) + local config_file="$HOME/.codeium/windsurf/mcp_config.json" + if [[ -f "$config_file" ]]; then + grep -A3 '"mt5-quant"' "$config_file" 2>/dev/null | grep '"command"' | sed 's/.*"command":[[:space:]]*"\([^"]*\)".*/\1/' + fi + ;; + cursor) + local config_file="$HOME/.cursor/mcp.json" + if [[ -f "$config_file" ]]; then + grep -A3 '"mt5-quant"' "$config_file" 2>/dev/null | grep '"command"' | sed 's/.*"command":[[:space:]]*"\([^"]*\)".*/\1/' + fi + ;; + esac +} + +# Unregister from a specific platform +_unregister_from_platform() { + local platform="$1" + echo "" + _bold "Unregistering from $platform..." + + case "$platform" in + claude) + if ! command -v claude &>/dev/null; then + _warn "claude CLI not found" + return 1 + fi + local out + out=$(claude mcp remove mt5-quant 2>&1) || true + if echo "$out" | grep -qi "removed\|success\|deleted"; then + _ok "Unregistered from Claude Code" + return 0 + elif echo "$out" | grep -qi "not found\|does not exist"; then + _ok "No existing registration on Claude Code" + return 0 + else + _warn "Unregister result: $out" + return 1 + fi + ;; + windsurf) + local config_file="$HOME/.codeium/windsurf/mcp_config.json" + if [[ -f "$config_file" ]]; then + # Remove mt5-quant from JSON using Python + if command -v python3 &>/dev/null; then + python3 -c " +import json, sys +with open('$config_file') as f: + data = json.load(f) +if 'mcpServers' in data and 'mt5-quant' in data['mcpServers']: + del data['mcpServers']['mt5-quant'] + with open('$config_file', 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Unregistered from Windsurf"; return 0; } + fi + _warn "Could not auto-unregister from Windsurf — edit $config_file manually" + return 1 + fi + _ok "No existing registration on Windsurf" + return 0 + ;; + cursor) + local config_file="$HOME/.cursor/mcp.json" + if [[ -f "$config_file" ]]; then + # Remove mt5-quant from JSON using Python if available, or sed as fallback + if command -v python3 &>/dev/null; then + python3 -c " +import json, sys +with open('$config_file') as f: + data = json.load(f) +if 'mcpServers' in data and 'mt5-quant' in data['mcpServers']: + del data['mcpServers']['mt5-quant'] + with open('$config_file', 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Unregistered from Cursor"; return 0; } + fi + _warn "Could not auto-unregister from Cursor — edit $config_file manually" + return 1 + fi + _ok "No existing registration on Cursor" + return 0 + ;; + vscode) + # VS Code uses 'servers' (not 'mcpServers') in mcp.json + local workspace_config=".vscode/mcp.json" + local user_config="$HOME/.vscode/mcp.json" + local config_file="" + + # Determine which config file to use + if [[ -f "$workspace_config" ]] && grep -q '"mt5-quant"' "$workspace_config" 2>/dev/null; then + config_file="$workspace_config" + elif [[ -f "$user_config" ]] && grep -q '"mt5-quant"' "$user_config" 2>/dev/null; then + config_file="$user_config" + fi + + if [[ -n "$config_file" ]]; then + if command -v python3 &>/dev/null; then + python3 -c " +import json, sys +with open('$config_file') as f: + data = json.load(f) +if 'servers' in data and 'mt5-quant' in data['servers']: + del data['servers']['mt5-quant'] + with open('$config_file', 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Unregistered from VS Code ($config_file)"; return 0; } + fi + _warn "Could not auto-unregister from VS Code — edit $config_file manually" + return 1 + fi + _ok "No existing registration on VS Code" + return 0 + ;; + esac +} + +# Register with a specific platform +_register_with_platform() { + local platform="$1" + local use_binary="${2:-false}" + + # Determine command path (binary for Windsurf/Cursor, Python for Claude) + local cmd_path + if $use_binary && [[ -f "${REPO_DIR}/target/release/mt5-quant" ]]; then + cmd_path="${REPO_DIR}/target/release/mt5-quant" + elif [[ -f "${REPO_DIR}/mt5-quant" ]]; then + cmd_path="${REPO_DIR}/mt5-quant" + else + cmd_path="python3 ${REPO_DIR}/server/main.py" + fi + + case "$platform" in + claude) + local out + out=$(claude mcp add mt5-quant -- $cmd_path 2>&1) || true + if echo "$out" | grep -qi "already\|exists"; then + _ok "Already registered on Claude Code" + elif echo "$out" | grep -qi "error\|failed"; then + _warn "Claude Code registration failed: $out" + return 1 + else + _ok "Registered on Claude Code" + fi + ;; + windsurf) + local config_file="$HOME/.codeium/windsurf/mcp_config.json" + mkdir -p "$(dirname "$config_file")" + + # Create or update JSON config + if command -v python3 &>/dev/null; then + python3 -c " +import json, os +config_path = '$config_file' +data = {'mcpServers': {}} +if os.path.exists(config_path): + try: + with open(config_path) as f: + data = json.load(f) + except: + pass +if 'mcpServers' not in data: + data['mcpServers'] = {} + +data['mcpServers']['mt5-quant'] = { + 'command': '$cmd_path' +} + +with open(config_path, 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Registered on Windsurf ($config_file)"; return 0; } + else + _warn "Python3 required for Windsurf registration" + return 1 + fi + ;; + cursor) + local config_file="$HOME/.cursor/mcp.json" + mkdir -p "$(dirname "$config_file")" + + # Create or update JSON config + if command -v python3 &>/dev/null; then + python3 -c " +import json, os +config_path = '$config_file' +data = {'mcpServers': {}} +if os.path.exists(config_path): + try: + with open(config_path) as f: + data = json.load(f) + except: + pass +if 'mcpServers' not in data: + data['mcpServers'] = {} + +data['mcpServers']['mt5-quant'] = { + 'command': '$cmd_path' +} + +with open(config_path, 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Registered on Cursor ($config_file)"; return 0; } + else + _warn "Python3 required for Cursor registration" + return 1 + fi + ;; + vscode) + # VS Code uses .vscode/mcp.json (workspace) or user profile + # Format: { "servers": { "name": { "command": "...", "args": [...], "env": {...} } } + local workspace_config=".vscode/mcp.json" + mkdir -p ".vscode" + + if command -v python3 &>/dev/null; then + python3 -c " +import json, os +config_path = '$workspace_config' +data = {'servers': {}} +if os.path.exists(config_path): + try: + with open(config_path) as f: + data = json.load(f) + except: + pass +if 'servers' not in data: + data['servers'] = {} + +# For stdio servers, VS Code uses 'command' and optional 'args' +data['servers']['mt5-quant'] = { + 'command': '$cmd_path' +} + +with open(config_path, 'w') as f: + json.dump(data, f, indent=2) +" && { _ok "Registered on VS Code ($workspace_config)"; return 0; } + else + _warn "Python3 required for VS Code registration" + return 1 + fi + ;; + esac +} + +# Check for any existing registrations across all platforms +_check_any_existing_registration() { + local platforms=() + while IFS= read -r platform; do + [[ -n "$platform" ]] && platforms+=("$platform") + done < <(detect_mcp_platforms) + + local found=false + for platform in "${platforms[@]}"; do + if _is_registered_on_platform "$platform"; then + found=true + break + fi + done + + $found +} + +# Unregister from all platforms +_unregister_all_platforms() { + local platforms=() + while IFS= read -r platform; do + [[ -n "$platform" ]] && platforms+=("$platform") + done < <(detect_mcp_platforms) + + for platform in "${platforms[@]}"; do + _is_registered_on_platform "$platform" && _unregister_from_platform "$platform" + done +} + +# Main registration function - detects platforms and registers with all +_register_all_mcp_platforms() { + echo "" + _bold "Detecting MCP platforms..." + + local platforms=() + while IFS= read -r platform; do + [[ -n "$platform" ]] && platforms+=("$platform") + done < <(detect_mcp_platforms) + + if [[ ${#platforms[@]} -eq 0 ]]; then + _warn "No MCP platforms detected (Claude, Windsurf, Cursor, VS Code)" echo "" - echo " claude mcp add mt5-quant -- python3 \"${REPO_DIR}/server/main.py\"" + echo " Manual registration required:" + echo " - Claude Code: claude mcp add mt5-quant -- python3 \"${REPO_DIR}/server/main.py\"" + echo " - Windsurf: Edit ~/.codeium/windsurf/mcp_config.json" + echo " - Cursor: Edit ~/.cursor/mcp.json" + echo " - VS Code: Edit .vscode/mcp.json (workspace) or use MCP: Add Server command" + echo " - Antigravity:Use Agent panel → MCP Servers → Manage → Edit configuration" echo "" return fi - local register=true - if ! $AUTO_YES; then - local ans - ans=$(_ask "Register mt5-quant with Claude Code now?" "yes") - [[ ! "$ans" =~ ^[Yy] ]] && register=false + _ok "Found platforms: ${platforms[*]}" + + # Check for existing registrations + local has_existing=false + for platform in "${platforms[@]}"; do + if _is_registered_on_platform "$platform"; then + has_existing=true + local old_path + old_path=$(_get_platform_mcp_path "$platform") + _yellow "Existing registration detected on $platform" + [[ -n "$old_path" ]] && echo " Current path: $old_path" + fi + done + + # Prompt for reinstall if any existing registrations found + if $has_existing && ! $AUTO_YES; then + echo "" + local reinstall_ans + reinstall_ans=$(_ask "Unregister existing installations and reinstall on all platforms?" "yes") + if [[ ! "$reinstall_ans" =~ ^[Yy] ]]; then + echo " Keeping existing registrations." + return + fi fi - if $register; then - local out - out=$(claude mcp add mt5-quant -- python3 "${REPO_DIR}/server/main.py" 2>&1) || true - if echo "$out" | grep -qi "already\|exists"; then - _ok "Already registered (no change needed)" - elif echo "$out" | grep -qi "error\|failed"; then - _warn "Registration failed: $out" - echo " Run manually: claude mcp add mt5-quant -- python3 \"${REPO_DIR}/server/main.py\"" - else - _ok "Registered: claude mcp add mt5-quant" - fi - else - echo " Skipped. Run manually:" - echo " claude mcp add mt5-quant -- python3 \"${REPO_DIR}/server/main.py\"" - fi + # Unregister from all platforms first + for platform in "${platforms[@]}"; do + _is_registered_on_platform "$platform" && _unregister_from_platform "$platform" + done + + # Register with all detected platforms + echo "" + _bold "Registering mt5-quant MCP..." + + for platform in "${platforms[@]}"; do + # Use binary for Windsurf/Cursor, Python for Claude + case "$platform" in + windsurf|cursor) + _register_with_platform "$platform" true + ;; + *) + _register_with_platform "$platform" false + ;; + esac + done + + echo "" + _green "MCP registration complete!" + echo "" + echo " Registered on: ${platforms[*]}" + echo "" + echo " Config locations:" + [[ " ${platforms[*]} " =~ " claude " ]] && echo " Claude Code: ~/.claude.json (managed via CLI)" + [[ " ${platforms[*]} " =~ " windsurf " ]] && echo " Windsurf: ~/.codeium/windsurf/mcp_config.json" + [[ " ${platforms[*]} " =~ " cursor " ]] && echo " Cursor: ~/.cursor/mcp.json" + [[ " ${platforms[*]} " =~ " vscode " ]] && echo " VS Code: .vscode/mcp.json" + echo "" + echo " Binary: ${REPO_DIR}/target/release/mt5-quant" + echo " Python: ${REPO_DIR}/server/main.py" + echo "" } main