feat(mem0-plugin): support self-hosted Mem0 server via MEM0_BASE_URL

Adds an optional MEM0_BASE_URL override (env var, falling back to
settings.json's base_url, defaulting to the hosted Platform unchanged)
so the plugin's REST-backed hooks can talk to a self-hosted server/
deployment instead of api.mem0.ai.

A new scripts/_api.py centralizes the hosted-vs-self-hosted protocol
differences (unversioned paths, X-API-Key auth, agent_id instead of
app_id) that every hook previously hardcoded, and all 9 urllib call
sites now route through it.

MCP tools have no self-hosted backend (server/ implements no MCP), so
they continue to talk to the hosted Platform regardless of
MEM0_BASE_URL; this is documented rather than silently left unclear.
Coding-category customization also has no self-hosted SDK equivalent
and now skips cleanly with a log message instead of failing.

Closes MEM-5647
This commit is contained in:
kartik-mem0
2026-08-13 18:29:24 +05:30
parent 96d45b78c7
commit 16c2cfe50e
18 changed files with 579 additions and 151 deletions
@@ -14,6 +14,11 @@ User ID resolution:
Settings resolution:
~/.mem0/settings.json (user-editable, falls back to defaults)
Base URL resolution (first non-empty wins):
1. MEM0_BASE_URL env var (explicit override, for self-hosted servers)
2. "base_url" in ~/.mem0/settings.json
3. DEFAULT_BASE_URL (the hosted Mem0 Platform)
"""
from __future__ import annotations
@@ -75,6 +80,9 @@ def resolve_user_id() -> str:
return os.environ.get("USER") or "default"
DEFAULT_BASE_URL = "https://api.mem0.ai"
def resolve_config() -> dict:
"""Resolve settings from ~/.mem0/settings.json (primary) with env var overrides."""
try:
@@ -88,9 +96,21 @@ def resolve_config() -> dict:
"retention_session_days": 90,
"confidence_threshold": 0.3,
"debug": False,
"base_url": "",
}
def resolve_base_url() -> str:
"""Resolve the Mem0 API base URL: MEM0_BASE_URL env var, then settings.json, then the hosted platform."""
explicit = os.environ.get("MEM0_BASE_URL", "").strip()
if explicit:
return explicit.rstrip("/")
configured = str(resolve_config().get("base_url", "")).strip()
if configured:
return configured.rstrip("/")
return DEFAULT_BASE_URL
try:
from _project import resolve_branch, resolve_project_id, save_project_mapping
except ImportError: