#!/bin/bash # TokenSaver Cloud Bootstrap — run once per cloud session. # Registers the token-saver MCP so all token-burning operations route # to free local compute (neon CPU / qfox-1 build server). # # Environment variables (set these in cloud env config): # BUILD_SERVER_URL=http://100.88.57.96:8765 # NEON_OLLAMA_URL=http://100.92.88.64:11434 # LAKE_WORKDIR=/workspace/0-Core-Formalism/lean/Semantics set -e echo "[token-saver] Installing Lean toolchain..." curl -sSf https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh | sh -s -- -y export PATH="$HOME/.elan/bin:$PATH" echo "[token-saver] Registering MCP server..." mkdir -p ~/.config/opencode # Merge token-saver into existing opencode config cat > /tmp/token_saver_mcp.json << 'EOF' { "token-saver": { "type": "local", "command": ["python3", "5-Applications/tools-scripts/mcp/token_saver_mcp.py"], "environment": { "BUILD_SERVER_URL": "{env:BUILD_SERVER_URL}", "NEON_OLLAMA_URL": "{env:NEON_OLLAMA_URL}", "LAKE_WORKDIR": "{env:LAKE_WORKDIR}" }, "enabled": true } } EOF if [ -f ~/.config/opencode/opencode.jsonc ]; then python3 -c " import json with open('/root/.config/opencode/opencode.jsonc') as f: cfg = json.load(f) cfg.setdefault('mcp', {}).update(json.load(open('/tmp/token_saver_mcp.json'))) with open('/root/.config/opencode/opencode.jsonc', 'w') as f: json.dump(cfg, f, indent=2) " else cp /tmp/token_saver_mcp.json ~/.config/opencode/opencode.jsonc fi echo "[token-saver] Verifying reachable resources..." python3 -c " import urllib.request try: r = urllib.request.urlopen('${BUILD_SERVER_URL:-http://100.88.57.96:8765}/health', timeout=5) print(' build_server: UP') except: print(' build_server: DOWN (tailnet required)') try: r = urllib.request.urlopen('${NEON_OLLAMA_URL:-http://100.92.88.64:11434}/api/tags', timeout=5) print(' neon_ollama: UP') except: print(' neon_ollama: DOWN (tailnet required)') " echo "[token-saver] Ready. MCP tools available:" echo " verify_build, compile_check, linter_check, classify_proof," echo " generate_proof (neon → paid fallback), format_code, doc_lookup"