Files
mem0/integrations/mem0-plugin/scripts/load_settings.py
kartik-mem0 16c2cfe50e 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
2026-08-13 18:29:24 +05:30

73 lines
2.0 KiB
Python

"""Load plugin settings from ~/.mem0/settings.json.
Settings file is user-editable. Missing file or keys fall back to defaults.
"""
from __future__ import annotations
import json
from pathlib import Path
SETTINGS_PATH = Path.home() / ".mem0" / "settings.json"
DEFAULTS = {
"auto_save": True,
"auto_search": True,
"search_limit": 10,
"retention_session_days": 90,
"confidence_threshold": 0.3,
"global_search": False,
"debug": False,
"base_url": "",
}
def load_settings() -> dict:
settings = dict(DEFAULTS)
if SETTINGS_PATH.exists():
try:
with open(SETTINGS_PATH) as f:
user = json.load(f)
except (json.JSONDecodeError, OSError):
return settings
if isinstance(user, dict):
settings.update({k: v for k, v in user.items() if k in DEFAULTS})
return settings
def create_default_settings() -> bool:
"""Write the default settings file if absent. Returns True if it was created."""
SETTINGS_PATH.parent.mkdir(parents=True, exist_ok=True)
if SETTINGS_PATH.exists():
return False
with open(SETTINGS_PATH, "w") as f:
json.dump(DEFAULTS, f, indent=2)
f.write("\n")
return True
def unknown_keys() -> list[str]:
"""Keys present in the user's settings file that no code reads."""
if not SETTINGS_PATH.exists():
return []
try:
with open(SETTINGS_PATH) as f:
user = json.load(f)
except (json.JSONDecodeError, OSError):
return []
if not isinstance(user, dict):
return []
return sorted(k for k in user if k not in DEFAULTS)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1 and sys.argv[1] == "init":
if create_default_settings():
print(f"Created {SETTINGS_PATH}")
ignored = unknown_keys()
if ignored:
print(f"Ignoring unrecognized settings in {SETTINGS_PATH}: {', '.join(ignored)}")
else:
print(json.dumps(load_settings()))