guohanghui/graph-theory
0
1#!/usr/bin/env bash2set -euo pipefail3 4# Switch to the directory where this script is located5cd "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"6 7mcp_entry_name="${MCP_ENTRY_NAME:-graph-theory}"8mcp_entry_url="${MCP_ENTRY_URL:-http://localhost:7860/mcp}"9mcp_dir="${HOME}/.cursor"10mcp_path="${mcp_dir}/mcp.json"11mkdir -p "${mcp_dir}"12 13if command -v python3 >/dev/null 2>&1; then14python3 - "${mcp_path}" "${mcp_entry_name}" "${mcp_entry_url}" <<'PY'15import json, os, sys16path, name, url = sys.argv[1:4]17cfg = {"mcpServers": {}}18if os.path.exists(path):19 try:20 with open(path, "r", encoding="utf-8") as f:21 cfg = json.load(f)22 except Exception:23 cfg = {"mcpServers": {}}24if not isinstance(cfg, dict):25 cfg = {"mcpServers": {}}26servers = cfg.get("mcpServers")27if not isinstance(servers, dict):28 servers = {}29ordered = {}30for k, v in servers.items():31 if k != name:32 ordered[k] = v33ordered[name] = {"url": url}34cfg = {"mcpServers": ordered}35with open(path, "w", encoding="utf-8") as f:36 json.dump(cfg, f, indent=2, ensure_ascii=False)37PY38elif command -v python >/dev/null 2>&1; then39python - "${mcp_path}" "${mcp_entry_name}" "${mcp_entry_url}" <<'PY'40import json, os, sys41path, name, url = sys.argv[1:4]42cfg = {"mcpServers": {}}43if os.path.exists(path):44 try:45 with open(path, "r", encoding="utf-8") as f:46 cfg = json.load(f)47 except Exception:48 cfg = {"mcpServers": {}}49if not isinstance(cfg, dict):50 cfg = {"mcpServers": {}}51servers = cfg.get("mcpServers")52if not isinstance(servers, dict):53 servers = {}54ordered = {}55for k, v in servers.items():56 if k != name:57 ordered[k] = v58ordered[name] = {"url": url}59cfg = {"mcpServers": ordered}60with open(path, "w", encoding="utf-8") as f:61 json.dump(cfg, f, indent=2, ensure_ascii=False)62PY63elif command -v jq >/dev/null 2>&1; then64 name="${mcp_entry_name}"; url="${mcp_entry_url}"65 if [ -f "${mcp_path}" ]; then66 tmp="$(mktemp)"67 jq --arg name "$name" --arg url "$url" '68 .mcpServers = (.mcpServers // {})69 | .mcpServers as $s70 | ($s | with_entries(select(.key != $name))) as $base71 | .mcpServers = ($base + {($name): {"url": $url}})72 ' "${mcp_path}" > "${tmp}" && mv "${tmp}" "${mcp_path}"73 else74 printf '{ "mcpServers": { "%s": { "url": "%s" } } }75' "$name" "$url" > "${mcp_path}"76 fi77else78 echo "Warning: neither python nor jq found; skipped updating ~/.cursor/mcp.json" >&279fi80 81docker build -t graph-theory-mcp .82docker run --rm -p 7860:7860 graph-theory-mcp83 