-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy path__init__.py
More file actions
2087 lines (1879 loc) · 82.8 KB
/
Copy path__init__.py
File metadata and controls
2087 lines (1879 loc) · 82.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
basic-memory — Hermes Memory Provider plugin
Wraps the basic-memory MCP server (`bm mcp`) to provide knowledge-graph-backed
memory for Hermes. Analog of openclaw-basic-memory.
Architecture:
- `_BmMcpActor` owns a long-lived asyncio loop in a daemon thread that holds
the MCP `ClientSession` open across the agent's lifetime. Sync hooks dispatch
through `asyncio.run_coroutine_threadsafe`.
- `BasicMemoryProvider` implements Hermes's `MemoryProvider` ABC: tools,
prefetch, sync_turn (per-turn capture), on_session_end (summary).
The plugin loader text-greps for `register_memory_provider` or `MemoryProvider`
to detect this file as a memory provider — both tokens are present below.
"""
from __future__ import annotations
import asyncio
import atexit
import concurrent.futures
import json
import logging
import os
import re
import signal
import socket
import subprocess
import threading
import time
from datetime import datetime, timezone
from pathlib import Path
from shutil import which
from typing import Any, Callable
# Hermes ABC + helpers — these resolve because Hermes adds its tree to sys.path
# when loading plugins (same pattern as plugins/memory/mem0/__init__.py:21).
from agent.memory_provider import MemoryProvider
from tools.registry import tool_error
__version__ = "0.23.2"
logger = logging.getLogger("hermes.memory.basic-memory")
# ---------------------------------------------------------------------------
# MCP SDK import — soft. If unavailable, is_available() returns False.
# ---------------------------------------------------------------------------
_MCP_AVAILABLE = False
_MCP_IMPORT_ERROR: BaseException | None = None
try:
from mcp import ClientSession, StdioServerParameters # type: ignore
from mcp.client.stdio import stdio_client # type: ignore
_MCP_AVAILABLE = True
except Exception as _e: # pragma: no cover
_MCP_IMPORT_ERROR = _e
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
PROVIDER_NAME = "basic-memory"
# Hermes-side tool names → BM MCP tool names. Curated subset of BM's surface.
_HERMES_TO_BM: dict[str, str] = {
"bm_search": "search_notes",
"bm_read": "read_note",
"bm_write": "write_note",
"bm_edit": "edit_note",
"bm_context": "build_context",
"bm_delete": "delete_note",
"bm_move": "move_note",
"bm_recent": "recent_activity",
"bm_projects": "list_memory_projects",
"bm_workspaces": "list_workspaces",
}
# Discovery tools that operate across all projects/workspaces. They don't
# accept project/project_id args (no per-call routing) and the user-facing
# schemas omit those properties.
_GLOBAL_TOOLS: frozenset = frozenset({"bm_projects", "bm_workspaces"})
TOOL_SCHEMAS: list[dict[str, Any]] = [
{
"name": "bm_search",
"description": (
"Search the Basic Memory knowledge graph for notes, decisions, observations. "
"Use BEFORE answering questions about prior work — context may already exist."
),
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Search terms (semantic + full-text)."},
"limit": {
"type": "integer",
"description": "Max results (default 10).",
"default": 10,
},
},
"required": ["query"],
},
},
{
"name": "bm_read",
"description": "Read a specific note by title, permalink, or memory:// URL.",
"parameters": {
"type": "object",
"properties": {
"identifier": {
"type": "string",
"description": "Note title, permalink, or memory:// URL.",
},
},
"required": ["identifier"],
},
},
{
"name": "bm_write",
"description": (
"Create a new note in the knowledge graph. Call this whenever the user "
"asks to remember, record, save, or note something — acknowledging "
"without this call saves nothing. The result returns the note's "
"permalink: quote it when confirming the save, never a filename "
"recalled from memory. Use clear titles and a folder "
"(e.g. 'projects', 'decisions', 'meetings')."
),
"parameters": {
"type": "object",
"properties": {
"title": {"type": "string"},
"content": {"type": "string", "description": "Markdown body."},
"folder": {"type": "string", "description": "Folder path within the project."},
"tags": {
"type": "array",
"items": {"type": "string"},
"description": "Optional tags.",
},
},
"required": ["title", "content", "folder"],
},
},
{
"name": "bm_edit",
"description": (
"Edit an existing note. Operations: append, prepend, find_replace, replace_section. "
"find_replace requires find_text. replace_section requires section. "
"The result returns the note's permalink — quote it when confirming the edit."
),
"parameters": {
"type": "object",
"properties": {
"identifier": {"type": "string"},
"operation": {
"type": "string",
"enum": ["append", "prepend", "find_replace", "replace_section"],
},
"content": {"type": "string"},
"find_text": {"type": "string", "description": "Required for find_replace."},
"section": {"type": "string", "description": "Required for replace_section."},
},
"required": ["identifier", "operation", "content"],
},
},
{
"name": "bm_context",
"description": (
"Navigate the knowledge graph from a memory:// URL or note identifier. "
"Returns the target note plus related notes via traversed relations."
),
"parameters": {
"type": "object",
"properties": {
"url": {"type": "string", "description": "memory:// URL or note identifier."},
"depth": {
"type": "integer",
"description": "Relation traversal depth (default 1).",
"default": 1,
},
},
"required": ["url"],
},
},
{
"name": "bm_delete",
"description": "Delete a note from the knowledge graph.",
"parameters": {
"type": "object",
"properties": {"identifier": {"type": "string"}},
"required": ["identifier"],
},
},
{
"name": "bm_move",
"description": "Move a note to a different folder.",
"parameters": {
"type": "object",
"properties": {
"identifier": {"type": "string"},
"new_folder": {"type": "string"},
},
"required": ["identifier", "new_folder"],
},
},
{
"name": "bm_recent",
"description": (
"List notes updated recently. Use to surface what's been touched "
"without a specific search query."
),
"parameters": {
"type": "object",
"properties": {
"timeframe": {
"type": "string",
"description": "Lookback window. Accepts '7d', '2 weeks', 'yesterday', etc.",
"default": "7d",
},
"limit": {
"type": "integer",
"description": "Max results (default 10).",
"default": 10,
},
"type": {
"type": "string",
"description": "Optional filter by item type (e.g. 'entity', 'observation').",
},
},
},
},
{
"name": "bm_projects",
"description": (
"List all available Basic Memory projects (local + cloud). Returns "
"JSON with name and `external_id` (UUID) per project. Use the UUID "
"as `project_id` on other bm_* tools for unambiguous routing across "
"cloud workspaces. Call this when the user names a project that "
"isn't the active one, or when you need to disambiguate same-name "
"projects."
),
"parameters": {"type": "object", "properties": {}},
},
{
"name": "bm_workspaces",
"description": (
"List Basic Memory Cloud workspaces the user belongs to. Workspaces "
"are a BM Cloud concept; local mode returns just the personal "
"workspace. Returns JSON with name, type, role, and default flag. "
"Pair with bm_projects to disambiguate when the same project name "
"exists in multiple workspaces."
),
"parameters": {"type": "object", "properties": {}},
},
]
# Per-call project routing. Every bm_* tool accepts these — the agent overrides
# Hermes's configured project to read/write against a different Basic Memory
# project (e.g. a personal "main" project on BM Cloud). project_id is the
# UUID-based unambiguous form: required when the same project name exists in
# multiple cloud workspaces. _translate_args sends only one of the two to BM,
# with project_id winning when both are passed.
_PROJECT_ROUTING_PROPS: dict[str, dict[str, Any]] = {
"project": {
"type": "string",
"description": (
"Optional. Override the active Basic Memory project (e.g. 'main'). "
"If the same project name exists in multiple cloud workspaces, "
"use project_id instead for unambiguous routing."
),
},
"project_id": {
"type": "string",
"description": (
"Optional. Override by project UUID (external_id from bm_projects). "
"Disambiguates when a project name appears in multiple workspaces. "
"Takes precedence over `project` if both are supplied."
),
},
}
for _schema in TOOL_SCHEMAS:
if _schema["name"] in _GLOBAL_TOOLS:
# Discovery tools (bm_projects, bm_workspaces) list everything —
# they don't take per-call routing.
continue
_schema["parameters"]["properties"].update(_PROJECT_ROUTING_PROPS)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
# Variables that point a child process at the *parent's* Python installation.
# Hermes ships its own interpreter (3.11 today) and exports these; `bm` is a
# uv-managed 3.12+ tool with its own environment. A child that inherits them
# resolves imports against Hermes's site-packages, which surfaces as a native
# extension ABI failure rather than a missing package.
#
# `__PYVENV_LAUNCHER__` is included defensively: the macOS framework launcher
# exports it to steer a child at a specific interpreter. We have not observed a
# failure caused by it, unlike the other three.
_PARENT_PYTHON_ENV_VARS = (
"PYTHONPATH",
"PYTHONHOME",
"VIRTUAL_ENV",
"__PYVENV_LAUNCHER__",
)
def _clean_child_env(env: dict[str, str] | None = None) -> dict[str, str]:
"""
Copy of `env` (default: this process's environment) with the parent's
Python-interpreter variables removed.
Every external process this plugin spawns — `bm mcp`, `uv tool install`,
`bm project add` — runs under its own interpreter and must resolve its own
dependencies. Inheriting Hermes's `PYTHONPATH` makes it import Hermes's
site-packages instead, reported as:
ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'
which is a compiled-extension mismatch between two Python versions, not a
missing dependency — so it is invisible to a `pip install` fix.
Everything else is preserved: the child still needs PATH, HOME, HTTP proxy
settings and credentials. An explicitly supplied `env` is sanitized too,
since the guarantee is about what the child receives, not about who
assembled it.
"""
source = os.environ if env is None else env
return {k: v for k, v in source.items() if k not in _PARENT_PYTHON_ENV_VARS}
def _bm_binary_path() -> str | None:
"""Find the bm CLI without making network calls. Used by is_available()."""
candidates = [
os.path.expanduser("~/.local/bin/bm"),
"/opt/homebrew/bin/bm",
"/usr/local/bin/bm",
]
for c in candidates:
if os.path.isfile(c) and os.access(c, os.X_OK):
return c
return which("bm")
def _uv_binary_path() -> str | None:
"""Find the uv CLI. Used to bootstrap-install basic-memory when bm is missing."""
candidates = [
os.path.expanduser("~/.local/bin/uv"),
"/opt/homebrew/bin/uv",
"/usr/local/bin/uv",
]
for c in candidates:
if os.path.isfile(c) and os.access(c, os.X_OK):
return c
return which("uv")
def _install_bm_via_uv(timeout: float = 180.0) -> str | None:
"""
Bootstrap-install basic-memory via `uv tool install`.
Idempotent — re-runs are no-ops when the tool is already installed, so this
converges with later manual `uv tool install basic-memory --prerelease=allow`
calls and avoids
the two-installations-sharing-one-config-dir foot-gun.
Returns the resolved bm path on success, or None if uv is unavailable or
the install failed.
"""
uv = _uv_binary_path()
if not uv:
return None
try:
result = subprocess.run(
# basic-memory pins a FastMCP pre-release; without this flag uv
# silently installs the last release that had none (#1338).
[uv, "tool", "install", "basic-memory", "--prerelease=allow", "--quiet"],
check=False,
capture_output=True,
timeout=timeout,
env=_clean_child_env(),
)
except Exception as e:
logger.warning(
"basic-memory: `uv tool install basic-memory --prerelease=allow` failed: %s", e
)
return None
if result.returncode != 0:
# uv prints to stderr; capture the tail so the operator can debug.
stderr_tail = (result.stderr or b"").decode("utf-8", errors="replace")[-400:]
logger.warning(
"basic-memory: `uv tool install basic-memory --prerelease=allow` exited %s: %s",
result.returncode,
stderr_tail.strip(),
)
return None
return _bm_binary_path()
def _hostname() -> str:
return socket.gethostname().split(".")[0].lower().replace(" ", "-")
def _default_project() -> str:
# Each machine gets its own local project with this name. Cloud setups
# use a different name (e.g. hermes-memory-cloud) so the two don't
# collide in BM's per-workspace project registry.
return "hermes-memory"
def _default_project_path() -> str:
# ~/.basic-memory/ is reserved for BM's own application state; user
# project files live in user space, parallel to ~/basic-memory/.
return os.path.expanduser("~/hermes-memory/")
def _config_path(hermes_home: str) -> Path:
return Path(hermes_home) / "basic-memory.json"
def _bm_config_path() -> Path:
"""Location of bm's own project registry."""
return Path.home() / ".basic-memory" / "config.json"
def _bm_known_projects() -> dict[str, Any] | None:
"""
Read bm's project registry. Returns None if the file is absent or
unparseable — callers should treat that as "can't prove anything"
rather than "project is missing".
"""
path = _bm_config_path()
if not path.exists():
return None
try:
data = json.loads(path.read_text())
except Exception:
return None
if not isinstance(data, dict):
return None
projects = data.get("projects")
return projects if isinstance(projects, dict) else None
def _load_config(hermes_home: str) -> dict[str, Any]:
p = _config_path(hermes_home)
if not p.exists():
return {}
try:
return json.loads(p.read_text())
except Exception as e:
logger.warning("could not parse %s: %s — using defaults", p, e)
return {}
def _truncate(s: Any, n: int) -> str:
if not isinstance(s, str):
s = "" if s is None else str(s)
if len(s) <= n:
return s
return s[: n - 3] + "..."
def _join_message_content(parts: Any) -> str:
if isinstance(parts, str):
return parts
if isinstance(parts, list):
out: list[str] = []
for p in parts:
if isinstance(p, dict):
t = p.get("text") or p.get("content")
if isinstance(t, str):
out.append(t)
elif isinstance(p, str):
out.append(p)
return "\n".join(out)
return str(parts) if parts is not None else ""
def _coerce_bool(v: Any) -> Any:
if isinstance(v, bool):
return v
if isinstance(v, str):
if v.lower() in ("true", "1", "yes", "y"):
return True
if v.lower() in ("false", "0", "no", "n"):
return False
return v
def _extract_mcp_text(result: Any) -> str:
"""
Extract text from an MCP CallToolResult.
Returns a JSON string for the agent. If the result is itself JSON, returns it
as-is. Otherwise wraps the text in `{"text": "..."}` for downstream parsing.
"""
# The `mcp` SDK is an unpinned dependency (see plugin.yaml), and its result
# shapes (CallToolResult, content blocks) have shifted across versions — read
# these fields defensively with getattr rather than direct attribute access.
is_error = bool(getattr(result, "isError", False))
parts: list[str] = []
for c in getattr(result, "content", None) or []:
text = getattr(c, "text", None)
if isinstance(text, str):
parts.append(text)
text = "\n".join(parts).strip()
if is_error:
return tool_error(text or "MCP tool returned error")
if not text:
return json.dumps({"ok": True})
# If it's already JSON, pass through verbatim
try:
json.loads(text)
return text
except Exception:
return json.dumps({"text": text})
_PERMALINK_JSON_RE = re.compile(r'"permalink"\s*:\s*"([^"]+)"')
_PERMALINK_MD_RE = re.compile(r"^\s*permalink\s*:\s*(\S+)\s*$", re.MULTILINE)
def _extract_permalink(text: str, fallback: str) -> str:
"""
Extract a note permalink from any plausible BM response shape:
1. Bare JSON dict with `permalink` key (output_format=json path)
2. `{"text": "..."}` wrapping inner JSON or markdown
3. Raw markdown response text (output_format=text default)
Falls back to the supplied fallback when nothing matches.
"""
if not isinstance(text, str) or not text:
return fallback
# Strategy 1: parse outer as JSON
try:
d = json.loads(text)
if isinstance(d, dict):
if isinstance(d.get("permalink"), str):
return d["permalink"]
inner = d.get("text")
if isinstance(inner, str):
# Strategy 2: inner is JSON
try:
d2 = json.loads(inner)
if isinstance(d2, dict) and isinstance(d2.get("permalink"), str):
return d2["permalink"]
except Exception:
pass
# Strategy 3a: inner is markdown with `permalink: ...` line
m = _PERMALINK_MD_RE.search(inner)
if m:
return m.group(1).rstrip(",.;")
# Strategy 3b: inner contains JSON substring with permalink
m = _PERMALINK_JSON_RE.search(inner)
if m:
return m.group(1)
except Exception:
pass
# Strategy 4: best-effort regex on raw text (covers exotic shapes)
m = _PERMALINK_JSON_RE.search(text)
if m:
return m.group(1)
m = _PERMALINK_MD_RE.search(text)
if m:
return m.group(1).rstrip(",.;")
return fallback
# ---------------------------------------------------------------------------
# MCP actor — single asyncio loop in a daemon thread, owns ClientSession
# ---------------------------------------------------------------------------
class _BmMcpActor:
"""
Owns the lifetime of one MCP ClientSession to the bm MCP server.
Why this exists: Hermes calls memory provider hooks synchronously from
a sync code path (memory_manager.py invokes provider.sync_turn / .prefetch
/ .handle_tool_call directly). MCP `ClientSession` is asyncio-bound and
not thread-safe across event loops. So we run one asyncio loop in a
daemon thread and ferry calls in via run_coroutine_threadsafe.
"""
def __init__(self, server_argv: list[str], env: dict[str, str] | None = None):
self._server_argv = list(server_argv)
# Sanitized so `bm mcp` resolves imports against its own interpreter,
# not Hermes's — see _clean_child_env.
self._env = _clean_child_env(env)
self._loop: asyncio.AbstractEventLoop | None = None
self._thread: threading.Thread | None = None
self._session: "ClientSession" | None = None
self._ready = threading.Event()
self._init_error: BaseException | None = None
self._stop_future: asyncio.Future | None = None
self._main_task: asyncio.Task[None] | None = None
self._shutdown_requested = threading.Event()
self._tools_cache: list[dict[str, Any]] = []
self._running = False
def start(self, timeout: float = 25.0) -> None:
if self._thread and self._thread.is_alive():
return
self._shutdown_requested.clear()
self._running = True
self._thread = threading.Thread(target=self._run, daemon=True, name="bm-mcp-actor")
self._thread.start()
if not self._ready.wait(timeout=timeout):
# Trigger: startup timed out before _main created its stop future.
# Why: joining alone cannot exit the stdio context or reap its child.
# Outcome: cancel the actor task and wait for its async contexts to close.
self.shutdown(timeout=5.0)
raise TimeoutError(f"basic-memory MCP server didn't initialize within {timeout}s")
if self._init_error is not None:
self._running = False
raise RuntimeError(f"basic-memory MCP server failed to start: {self._init_error}")
def _run(self) -> None:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self._loop = loop
try:
self._main_task = loop.create_task(self._main())
if self._shutdown_requested.is_set():
self._main_task.cancel()
loop.run_until_complete(self._main_task)
except asyncio.CancelledError:
# Cancellation is the intentional pre-ready shutdown path. Wake a
# concurrent start() without logging an expected cleanup as a crash.
if not self._ready.is_set() and self._init_error is None:
self._init_error = RuntimeError("basic-memory MCP actor stopped during startup")
self._ready.set()
except BaseException as e:
if self._init_error is None:
self._init_error = e
self._ready.set()
logger.exception("basic-memory MCP actor terminated with error")
finally:
self._running = False
self._main_task = None
try:
loop.close()
except Exception:
pass
async def _main(self) -> None:
params = StdioServerParameters(
command=self._server_argv[0],
args=self._server_argv[1:],
env=self._env,
)
try:
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
await session.initialize()
self._session = session
self._stop_future = asyncio.get_running_loop().create_future()
try:
listing = await session.list_tools()
# Same unpinned-`mcp`-SDK reason as parse_result above:
# ListToolsResult / Tool field shapes vary across SDK versions,
# so read them defensively rather than by direct attribute access.
self._tools_cache = [
{
"name": getattr(t, "name", ""),
"description": getattr(t, "description", "") or "",
}
for t in getattr(listing, "tools", []) or []
]
except Exception as e:
logger.warning("list_tools failed: %s", e)
self._tools_cache = []
self._ready.set()
await self._stop_future # blocks until shutdown
except BaseException as e:
if self._init_error is None:
self._init_error = e
self._ready.set()
raise
def call(self, tool_name: str, arguments: dict[str, Any], timeout: float = 30.0) -> str:
if not self._running:
raise RuntimeError("basic-memory MCP actor not running")
if self._loop is None or self._session is None:
raise RuntimeError("basic-memory MCP actor not started")
future = asyncio.run_coroutine_threadsafe(
self._session.call_tool(tool_name, arguments),
self._loop,
)
try:
result = future.result(timeout=timeout)
except concurrent.futures.TimeoutError:
# Cancel the coroutine on the actor loop so we don't leak
# a stuck call_tool. cancel() on a run_coroutine_threadsafe
# future propagates cancellation into the wrapped coroutine.
future.cancel()
raise
return _extract_mcp_text(result)
def list_tools(self) -> list[dict[str, Any]]:
return list(self._tools_cache)
def is_alive(self) -> bool:
"""True while the actor loop thread is up and accepting calls."""
return self._running and self._thread is not None and self._thread.is_alive()
def shutdown(self, timeout: float = 5.0) -> None:
self._running = False
self._shutdown_requested.set()
if self._loop is not None:
try:
def request_stop() -> None:
if not self._ready.is_set():
if self._main_task is not None and not self._main_task.done():
self._main_task.cancel()
return
if self._stop_future is not None and not self._stop_future.done():
self._stop_future.set_result(None)
elif self._main_task is not None and not self._main_task.done():
self._main_task.cancel()
self._loop.call_soon_threadsafe(request_stop)
except Exception:
# Loop may already be closed; safe to ignore.
pass
if self._thread is not None:
try:
self._thread.join(timeout=timeout)
except Exception:
pass
# ---------------------------------------------------------------------------
# Argument translation: Hermes-side tool args → BM MCP tool args
# ---------------------------------------------------------------------------
_WORKSPACE_HASH_SLUG_RE = re.compile(r"^[a-z0-9][a-z0-9-]*-[0-9a-f]{32}$")
def _strip_memory_url_prefix(value: str) -> str:
if value.startswith("memory://"):
return value[len("memory://") :]
return value
def _looks_workspace_qualified(value: str) -> bool:
"""Return True for BM Cloud workspace-qualified identifiers.
Hermes normally injects its configured default project so calls operate on
the provider's project instead of Basic Memory's process default. But BM
Cloud routes fully-qualified identifiers itself; adding a default local
project makes `personal/main/...` resolve under that local project instead.
"""
path = _strip_memory_url_prefix(value).strip("/")
parts = [part for part in path.split("/") if part]
if len(parts) < 3:
return False
workspace_slug = parts[0]
return workspace_slug == "personal" or bool(_WORKSPACE_HASH_SLUG_RE.match(workspace_slug))
def _should_omit_default_project(hermes_tool: str, args: dict[str, Any]) -> bool:
if hermes_tool in {"bm_read", "bm_edit", "bm_delete", "bm_move"}:
identifier = args.get("identifier")
return isinstance(identifier, str) and _looks_workspace_qualified(identifier)
if hermes_tool == "bm_context":
url = args.get("url")
return isinstance(url, str) and _looks_workspace_qualified(url)
return False
def _translate_args(
hermes_tool: str, args: dict[str, Any], default_project: str
) -> tuple[str, dict[str, Any]]:
bm_tool = _HERMES_TO_BM[hermes_tool]
out: dict[str, Any] = {}
# Project routing: project_id > project > configured default.
# The agent passes one of these to operate on a project other than the
# one Hermes is configured for. project_id (UUID from bm_projects) is
# the unambiguous form across cloud workspaces — preferred when project
# names might collide between workspaces. Only one of the two reaches
# BM so server-side precedence rules don't enter the picture.
#
# Global discovery tools (bm_projects, bm_workspaces) list everything
# and don't take routing args at all — skip the block for them.
if hermes_tool not in _GLOBAL_TOOLS:
project_id_override = args.get("project_id")
project_name_override = args.get("project")
if project_id_override:
out["project_id"] = str(project_id_override)
elif project_name_override:
out["project"] = str(project_name_override)
elif not _should_omit_default_project(hermes_tool, args):
out["project"] = default_project
if hermes_tool == "bm_search":
out["query"] = args["query"]
if "limit" in args and args["limit"] is not None:
out["page_size"] = int(args["limit"])
elif hermes_tool == "bm_read":
out["identifier"] = args["identifier"]
elif hermes_tool == "bm_write":
out["title"] = args["title"]
out["content"] = args["content"]
out["directory"] = args["folder"]
if args.get("tags"):
out["tags"] = list(args["tags"])
elif hermes_tool == "bm_edit":
out["identifier"] = args["identifier"]
out["operation"] = args["operation"]
out["content"] = args["content"]
if args.get("find_text") is not None:
out["find_text"] = args["find_text"]
if args.get("section") is not None:
out["section"] = args["section"]
elif hermes_tool == "bm_context":
out["url"] = args["url"]
if args.get("depth") is not None:
out["depth"] = int(args["depth"])
elif hermes_tool == "bm_delete":
out["identifier"] = args["identifier"]
elif hermes_tool == "bm_move":
out["identifier"] = args["identifier"]
out["destination_folder"] = args["new_folder"]
elif hermes_tool == "bm_recent":
if args.get("timeframe"):
out["timeframe"] = str(args["timeframe"])
if args.get("limit") is not None:
out["page_size"] = int(args["limit"])
if args.get("type"):
out["type"] = args["type"]
elif hermes_tool in _GLOBAL_TOOLS:
# The agent needs to parse identifiers (UUIDs, workspace slugs) out
# of the response, so request JSON regardless of BM's text default.
out["output_format"] = "json"
return bm_tool, out
# ---------------------------------------------------------------------------
# Provider
# ---------------------------------------------------------------------------
class BasicMemoryProvider(MemoryProvider):
"""Hermes Memory Provider backed by the basic-memory MCP server."""
def __init__(self) -> None:
self._actor: _BmMcpActor | None = None
self._project: str = _default_project()
self._mode: str = "local"
self._project_path: str = _default_project_path()
self._capture_per_turn: bool = True
self._capture_session_end: bool = True
self._capture_folder: str = "hermes-sessions"
self._remember_folder: str = "bm-remember"
self._session_id: str = ""
self._hermes_home: str = ""
self._session_note_id: str | None = None
self._session_started_at: datetime | None = None
self._sync_thread: threading.Thread | None = None
self._prefetch_thread: threading.Thread | None = None
self._prefetch_lock = threading.Lock()
self._pending_prefetch: str = ""
self._failure_count: int = 0
self._failure_pause_until: float = 0.0
self._initialized: bool = False
self._first_user_msg: str | None = None
# ---- Identity ----
@property
def name(self) -> str:
return PROVIDER_NAME
def is_available(self) -> bool:
# Discovery hot path. NEVER make network calls or spawn subprocesses here.
# We report available when either bm is present already OR uv is present
# (we bootstrap-install bm via `uv tool install` at initialize() time).
if not _MCP_AVAILABLE:
return False
if _bm_binary_path():
return True
if _uv_binary_path():
return True
return False
# ---- Lifecycle ----
def initialize(self, session_id: str, **kwargs: Any) -> None:
requested_session_id = session_id or ""
self._hermes_home = kwargs.get("hermes_home") or os.path.expanduser("~/.hermes")
cfg = _load_config(self._hermes_home)
self._mode = cfg.get("mode") or "local"
self._project = cfg.get("project") or _default_project()
self._project_path = os.path.expanduser(cfg.get("project_path") or _default_project_path())
self._capture_per_turn = bool(_coerce_bool(cfg.get("capture_per_turn", True)))
self._capture_session_end = bool(_coerce_bool(cfg.get("capture_session_end", True)))
self._capture_folder = cfg.get("capture_folder") or "hermes-sessions"
self._remember_folder = cfg.get("remember_folder") or "bm-remember"
if not _MCP_AVAILABLE:
logger.error(
"basic-memory: MCP SDK unavailable; provider will not initialize: %s",
_MCP_IMPORT_ERROR,
)
return
# Bootstrap-install bm via uv if it's not already on disk. One-time cost
# on a fresh machine; idempotent no-op once basic-memory is installed.
if not _bm_binary_path():
if _uv_binary_path() is None:
logger.error(
"basic-memory: bm CLI not found and uv is not installed. "
"Install uv (https://docs.astral.sh/uv/) or run "
"`pip install basic-memory` manually. Provider will not initialize."
)
return
logger.info(
"basic-memory: bm CLI not found — installing basic-memory via "
"`uv tool install` (one-time bootstrap)"
)
if _install_bm_via_uv() is None:
logger.error(
"basic-memory: auto-install via uv failed. Run "
"`uv tool install basic-memory --prerelease=allow` manually to debug. "
"Provider will not initialize."
)
return
if self._mode == "local":
self._ensure_local_project()
if not self._verify_project_registered():
self._log_missing_project()
return
# Trigger: initialize() called while a previous actor is still running
# (Hermes re-initializes providers per session; _ensure_slash_ready's
# lazy init can also precede a later session initialize).
# Why: every _BmMcpActor owns one `bm mcp` child process. Allocating a
# fresh actor without stopping the old one orphans that child — issue
# #1017 saw 18 idle `bm mcp` processes (~2.3 GB) accumulate this way.
# Outcome: a healthy actor is reused — its argv is always `bm mcp` and
# project routing happens per call, so the config re-read above never
# invalidates the connection. A dead actor is shut down first (joining
# its thread reaps the child) and then replaced below.
if self._actor is not None and self._actor.is_alive():
self._mark_session_ready(requested_session_id)
logger.info(
"basic-memory provider ready (reusing running MCP actor): mode=%s project=%s",
self._mode,
self._project,
)
return
if self._actor is not None:
try:
self._actor.shutdown(timeout=5.0)
except Exception as e:
logger.debug("stale actor shutdown during initialize: %s", e)
self._actor = None
try:
argv = self._server_argv()
except Exception as e:
logger.error("basic-memory: cannot determine server argv: %s", e)
return
# Expose the actor to cleanup BEFORE start() blocks (up to 25s):
# _atexit_cleanup and the SIGTERM handler only reach actors through
# provider.shutdown() → self._actor, so a local-only actor would leak
# its freshly spawned `bm mcp` child if a signal lands mid-start.
# Callers can't observe the half-started actor: every tool/capture
# path gates on _initialized, which stays False until start() returns.
actor = _BmMcpActor(argv)
self._actor = actor
try:
actor.start(timeout=25.0)
except Exception as e:
try:
actor.shutdown(timeout=5.0)
except Exception as shutdown_error:
logger.debug("actor shutdown after failed start: %s", shutdown_error)
if self._actor is actor:
self._actor = None
logger.error("basic-memory: MCP server failed to start: %s", e)
return
if self._actor is not actor:
# Signal/atexit cleanup detached the actor while start() was