Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save debedb/1f98ee63a7c64b8e44a1bc2ff0df4bbe to your computer and use it in GitHub Desktop.
Save debedb/1f98ee63a7c64b8e44a1bc2ff0df4bbe to your computer and use it in GitHub Desktop.
Sync Claude Desktop MCP settings with Claude Code
#!/bin/bash
CONFIG_FILE="$HOME/Library/Application Support/Claude/claude_desktop_config.json"
# Check if the config file exists
if [ ! -f "$CONFIG_FILE" ]; then
echo "Config file not found: $CONFIG_FILE"
exit 1
fi
# Read MCP servers from config file
CONFIG_SERVERS=$(cat "$CONFIG_FILE" | jq -r '.mcpServers // []')
# Convert config servers to a structured format
echo "Reading MCP servers from config file..."
SERVERS_TO_ADD=()
while read -r line; do
if [ ! -z "$line" ]; then
NAME=$(echo "$line" | jq -r '.name // "null"')
COMMAND=$(echo "$line" | jq -r '.command')
ARGS=$(echo "$line" | jq -r '.args | join(" ")')
SERVERS_TO_ADD+=("$NAME:$COMMAND:$ARGS")
echo "Found server: $NAME - $COMMAND $ARGS"
fi
done < <(echo "$CONFIG_SERVERS" | jq -c '.[]')
# Clear all existing MCP servers
echo "Removing all existing MCP servers..."
MCP_LIST_OUTPUT=$(claude mcp list)
if [[ "$MCP_LIST_OUTPUT" != *"No MCP servers configured"* ]]; then
while IFS=: read -r NAME COMMAND_WITH_ARGS; do
if [ ! -z "$NAME" ] && [[ "$NAME" != "No MCP servers configured"* ]]; then
# Remove leading/trailing whitespace
NAME=$(echo "$NAME" | xargs)
echo "Removing server: $NAME"
claude mcp remove "$NAME"
# Wait a moment for the operation to complete
sleep 1
fi
done < <(echo "$MCP_LIST_OUTPUT")
fi
# Add each server from config
echo "Adding servers from config..."
for config in "${SERVERS_TO_ADD[@]}"; do
CONFIG_NAME=$(echo "$config" | cut -d: -f1)
CONFIG_COMMAND=$(echo "$config" | cut -d: -f2)
CONFIG_ARGS=$(echo "$config" | cut -d: -f3-)
echo "Adding server: $CONFIG_NAME - $CONFIG_COMMAND $CONFIG_ARGS"
# Handle problematic flags
ADD_ARGS=$(echo "$CONFIG_ARGS" | sed 's/-y//g' | sed 's/--with fastmcp//')
# If this is the fastmcp command, handle it specially
if [[ "$CONFIG_COMMAND" == *"uv"* && "$CONFIG_ARGS" == *"fastmcp"* ]]; then
echo "Skipping fastmcp command as it contains unsupported flags"
else
# Add the server with a unique name to avoid conflicts
# Use md5 on macOS or md5sum on Linux
if command -v md5 > /dev/null 2>&1; then
SERVER_ID=$(date +%s | md5 | head -c 6)
elif command -v md5sum > /dev/null 2>&1; then
SERVER_ID=$(date +%s | md5sum | head -c 6)
else
SERVER_ID=$(date +%s | cksum | head -c 6)
fi
# Use the original name if available, otherwise use a unique name
if [ "$CONFIG_NAME" = "null" ]; then
UNIQUE_NAME="server-${SERVER_ID}"
else
UNIQUE_NAME="$CONFIG_NAME"
fi
claude mcp add "$UNIQUE_NAME" "$CONFIG_COMMAND" $ADD_ARGS
# Wait a moment for the operation to complete
sleep 1
fi
done
echo "MCP server synchronization complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment