""" Health check — run with: frappe-mcp --test-connection Verifies connectivity, auth, and Frappe version before starting the MCP server. """ import asyncio import sys from frappe_mcp.client.frappe_api import FrappeClient, FrappeAPIError from frappe_mcp.config import get_settings async def run_health_check() -> bool: settings = get_settings() passed = True print("\nFrappe MCP -- Connection Test") print("=" * 45) print(f" URL : {settings.frappe_url}") print(f" API Key : {settings.frappe_api_key[:6]}..." if settings.frappe_api_key else " API Key : (not set)") print(f" Site Name : {settings.frappe_site_name or '(default)'}") print(f" Read-only : {settings.read_only_mode}") print("=" * 45) client = FrappeClient() # 1. Ping / version check print("\n[1/4] Checking Frappe version...", end=" ", flush=True) try: versions = await client.call_method("frappe.utils.change_log.get_versions") frappe_ver = versions.get("frappe", {}).get("version", "unknown") print(f"OK (Frappe {frappe_ver})") except FrappeAPIError as e: print(f"FAIL — {e}") passed = False except Exception as e: print(f"FAIL — {e}") passed = False # 2. Auth check — fetch current user print("[2/4] Verifying API credentials...", end=" ", flush=True) try: user = await client.call_method("frappe.auth.get_logged_user") print(f"OK (logged in as: {user})") except FrappeAPIError as e: print(f"FAIL — {e}") passed = False except Exception as e: print(f"FAIL — {e}") passed = False # 3. Read DocType list print("[3/4] Testing DocType list access...", end=" ", flush=True) try: result = await client.get_list("DocType", fields=["name"], limit=1) print(f"OK (found {len(result)} DocType(s))") except Exception as e: print(f"FAIL — {e}") passed = False # 4. Write check (skipped in read-only mode) if settings.read_only_mode: print("[4/4] Write check... SKIPPED (read_only_mode=true)") else: print("[4/4] Testing write permission (System Settings read)...", end=" ", flush=True) try: await client.get_doc("System Settings", "System Settings") print("OK") except Exception as e: print(f"WARN — {e}") print("=" * 45) if passed: print("PASS All checks passed. MCP server is ready.\n") else: print("FAIL Some checks failed. Fix the issues above before starting.\n") return passed def main_health_check(): ok = asyncio.run(run_health_check()) sys.exit(0 if ok else 1)