|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script installs the Git MCP server and registers it for use with the cline VSCode extension: https://github.com/cline/cline |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./register-cline.sh <repository-path> [write-access] [mode] |
| 7 | +# |
| 8 | +# Parameters: |
| 9 | +# repository-path: Required. Path to a Git repository to use. |
| 10 | +# write-access: Optional. Set to "true" to enable write operations. Default is "false". |
| 11 | +# mode: Optional. Git operation mode: 'shell' or 'go-git'. Default is "shell". |
| 12 | +# |
| 13 | +# Examples: |
| 14 | +# ./register-cline.sh ~/my-repo # Install with read-only mode (default) |
| 15 | +# ./register-cline.sh ~/my-repo true # Install with write operations enabled |
| 16 | +# ./register-cline.sh ~/my-repo false go-git # Specify mode |
| 17 | + |
| 18 | +# Get parameters with defaults |
| 19 | +REPO_PATH=$1 |
| 20 | +WRITE_ACCESS=${2:-false} |
| 21 | +MODE=${3:-shell} |
| 22 | + |
| 23 | +# Check for required tools |
| 24 | +if ! command -v jq &> /dev/null; then |
| 25 | + echo "Error: jq is required but not installed. Please install jq first." |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +MCP_SERVERS_DIR="$HOME/mcp-servers" |
| 30 | +mkdir -p "$MCP_SERVERS_DIR" |
| 31 | + |
| 32 | +# Check if the Git MCP server binary is on the path already |
| 33 | +GIT_MCP_BINARY="$(which git-mcp-go)" |
| 34 | +if [ -z "$GIT_MCP_BINARY" ]; then |
| 35 | + echo "Did not find git-mcp-go on the path, installing from latest GitHub release..." |
| 36 | + |
| 37 | + # This fetches information about the latest release to determine the download URL |
| 38 | + LATEST_RELEASE=$(curl -s https://api.github.com/repos/geropl/git-mcp-go/releases/latest) |
| 39 | + |
| 40 | + # Determine platform for download |
| 41 | + PLATFORM="linux" |
| 42 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 43 | + PLATFORM="darwin" |
| 44 | + elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then |
| 45 | + PLATFORM="windows" |
| 46 | + fi |
| 47 | + |
| 48 | + # Extract the download URL for the appropriate binary |
| 49 | + DOWNLOAD_URL=$(echo "$LATEST_RELEASE" | jq -r ".assets[] | select(.name | contains(\"$PLATFORM\")) | .browser_download_url") |
| 50 | + |
| 51 | + if [ -z "$DOWNLOAD_URL" ]; then |
| 52 | + echo "Error: Could not find appropriate binary in the latest release" |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + |
| 56 | + # Download the Git MCP server binary |
| 57 | + echo "Downloading Git MCP server from $DOWNLOAD_URL..." |
| 58 | + curl -L -o "$MCP_SERVERS_DIR/git-mcp-go" "$DOWNLOAD_URL" |
| 59 | + |
| 60 | + # Make the binary executable |
| 61 | + chmod +x "$MCP_SERVERS_DIR/git-mcp-go" |
| 62 | + |
| 63 | + echo "Git MCP server installed successfully at $MCP_SERVERS_DIR/git-mcp-go" |
| 64 | + GIT_MCP_BINARY="$MCP_SERVERS_DIR/git-mcp-go" |
| 65 | +fi |
| 66 | + |
| 67 | +# Configure cline to use the MCP server |
| 68 | +# This is where Cline looks for MCP server configurations |
| 69 | +CLINE_CONFIG_DIR="$HOME/.vscode-server/data/User/globalStorage/saoudrizwan.claude-dev/settings" |
| 70 | +mkdir -p "$CLINE_CONFIG_DIR" |
| 71 | + |
| 72 | +CLINE_MCP_SETTINGS="$CLINE_CONFIG_DIR/cline_mcp_settings.json" |
| 73 | + |
| 74 | +# Build the args array based on parameters |
| 75 | +SERVER_ARGS="[" |
| 76 | + |
| 77 | +# Add repository path if provided |
| 78 | +if [ -n "$REPO_PATH" ]; then |
| 79 | + # Expand the path to absolute path |
| 80 | + REPO_PATH=$(realpath "$REPO_PATH") |
| 81 | + SERVER_ARGS="$SERVER_ARGS\"--repository=$REPO_PATH\"" |
| 82 | +fi |
| 83 | + |
| 84 | +# Add write-access flag if enabled |
| 85 | +if [ "$WRITE_ACCESS" = "true" ]; then |
| 86 | + if [ -n "$SERVER_ARGS" ] && [ "$SERVER_ARGS" != "[" ]; then |
| 87 | + SERVER_ARGS="$SERVER_ARGS, " |
| 88 | + fi |
| 89 | + SERVER_ARGS="$SERVER_ARGS\"--write-access=true\"" |
| 90 | +fi |
| 91 | + |
| 92 | +# Add mode if not the default |
| 93 | +if [ -n "$MODE" ]; then |
| 94 | + if [ -n "$SERVER_ARGS" ] && [ "$SERVER_ARGS" != "[" ]; then |
| 95 | + SERVER_ARGS="$SERVER_ARGS, " |
| 96 | + fi |
| 97 | + SERVER_ARGS="$SERVER_ARGS\"--mode=$MODE\"" |
| 98 | +fi |
| 99 | + |
| 100 | +SERVER_ARGS="$SERVER_ARGS]" |
| 101 | + |
| 102 | +# Merge the existing settings with the new MCP server configuration |
| 103 | +cat <<EOF > "$CLINE_MCP_SETTINGS.new" |
| 104 | +{ |
| 105 | + "mcpServers": { |
| 106 | + "git": { |
| 107 | + "command": "$GIT_MCP_BINARY", |
| 108 | + "args": $SERVER_ARGS, |
| 109 | + "disabled": false, |
| 110 | + "autoApprove": [] |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +EOF |
| 115 | + |
| 116 | +if [ -f "$CLINE_MCP_SETTINGS" ]; then |
| 117 | + echo "Found existing Cline MCP settings at $CLINE_MCP_SETTINGS" |
| 118 | + echo "Merging with new MCP server configuration..." |
| 119 | + jq -s '.[0] * .[1]' "$CLINE_MCP_SETTINGS" "$CLINE_MCP_SETTINGS.new" > "$CLINE_MCP_SETTINGS.tmp" |
| 120 | + mv "$CLINE_MCP_SETTINGS.tmp" "$CLINE_MCP_SETTINGS" |
| 121 | +else |
| 122 | + echo "Creating new Cline MCP settings at $CLINE_MCP_SETTINGS" |
| 123 | + mv "$CLINE_MCP_SETTINGS.new" "$CLINE_MCP_SETTINGS" |
| 124 | +fi |
| 125 | +rm -f "$CLINE_MCP_SETTINGS.new" |
| 126 | + |
| 127 | +echo "Cline MCP settings updated at $CLINE_MCP_SETTINGS" |
| 128 | +echo "Git MCP server has been registered with the following configuration:" |
| 129 | +echo " - Write Access: $WRITE_ACCESS" |
| 130 | +if [ -n "$REPO_PATH" ]; then |
| 131 | + echo " - Repository Path: $REPO_PATH" |
| 132 | +fi |
| 133 | +echo " - Mode: $MODE" |
0 commit comments