unified-memory-mcp 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unified_memory_mcp-0.1.1/.github/workflows/ci.yml +46 -0
- unified_memory_mcp-0.1.1/.github/workflows/release.yml +42 -0
- unified_memory_mcp-0.1.1/.gitignore +13 -0
- unified_memory_mcp-0.1.1/CHANGELOG.md +81 -0
- unified_memory_mcp-0.1.1/LICENSE +201 -0
- unified_memory_mcp-0.1.1/NOTICE +19 -0
- unified_memory_mcp-0.1.1/PKG-INFO +218 -0
- unified_memory_mcp-0.1.1/README.md +191 -0
- unified_memory_mcp-0.1.1/pyproject.toml +77 -0
- unified_memory_mcp-0.1.1/src/unified_memory/__init__.py +25 -0
- unified_memory_mcp-0.1.1/src/unified_memory/graph.py +224 -0
- unified_memory_mcp-0.1.1/src/unified_memory/meter.py +129 -0
- unified_memory_mcp-0.1.1/src/unified_memory/rss.py +68 -0
- unified_memory_mcp-0.1.1/src/unified_memory/server.py +1313 -0
- unified_memory_mcp-0.1.1/src/unified_memory/store.py +237 -0
- unified_memory_mcp-0.1.1/src/unified_memory/vitals.py +356 -0
- unified_memory_mcp-0.1.1/src/unified_memory/watchdog.py +314 -0
- unified_memory_mcp-0.1.1/tests/smoke_test.py +179 -0
- unified_memory_mcp-0.1.1/tests/test_objective.py +104 -0
- unified_memory_mcp-0.1.1/tests/test_project_dynamic.py +120 -0
- unified_memory_mcp-0.1.1/tests/test_write_dedupe.py +197 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: ci
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
python-version: ["3.10", "3.11", "3.12"]
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: ${{ matrix.python-version }}
|
|
23
|
+
|
|
24
|
+
- name: Install
|
|
25
|
+
run: |
|
|
26
|
+
python -m pip install --upgrade pip
|
|
27
|
+
pip install -e ".[upstream]"
|
|
28
|
+
|
|
29
|
+
- name: Smoke test
|
|
30
|
+
run: python tests/smoke_test.py
|
|
31
|
+
|
|
32
|
+
- name: Write-dedupe test
|
|
33
|
+
run: python tests/test_write_dedupe.py
|
|
34
|
+
|
|
35
|
+
- name: Project portability test
|
|
36
|
+
run: python tests/test_project_dynamic.py
|
|
37
|
+
|
|
38
|
+
- name: Lint
|
|
39
|
+
run: |
|
|
40
|
+
pip install ruff
|
|
41
|
+
ruff check src tests
|
|
42
|
+
|
|
43
|
+
- name: Build
|
|
44
|
+
run: |
|
|
45
|
+
pip install build
|
|
46
|
+
python -m build
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
permissions:
|
|
12
|
+
id-token: write # PyPI trusted publishing (OIDC)
|
|
13
|
+
contents: write # for the GitHub release
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
|
|
17
|
+
- uses: actions/setup-python@v5
|
|
18
|
+
with:
|
|
19
|
+
python-version: "3.12"
|
|
20
|
+
|
|
21
|
+
- name: Build
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip build twine
|
|
24
|
+
python -m build
|
|
25
|
+
twine check dist/*
|
|
26
|
+
|
|
27
|
+
- name: Create GitHub release
|
|
28
|
+
uses: softprops/action-gh-release@v2
|
|
29
|
+
with:
|
|
30
|
+
files: dist/*
|
|
31
|
+
generate_release_notes: true
|
|
32
|
+
|
|
33
|
+
# Publishes with trusted publishing (OIDC) when the repository is
|
|
34
|
+
# registered as a publisher on PyPI. If the optional PYPI_API_TOKEN
|
|
35
|
+
# secret is set instead, the token is used (first-upload path, before
|
|
36
|
+
# the project exists on PyPI).
|
|
37
|
+
- name: Publish to PyPI
|
|
38
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
39
|
+
with:
|
|
40
|
+
packages-dir: dist/
|
|
41
|
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
|
42
|
+
skip-existing: true
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses
|
|
5
|
+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [0.1.1] - 2026-11-23
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
- **Spaced graph refresh** (`watchdog.py`, `CODEBASE_MEMORY_REFRESH_SPACING_SECONDS` default 15 s): scheduled re-indexing now pauses between targets instead of rebuilding every repo back-to-back (~75 s of CPU in one burst across six projects). Set to `0` to restore the old behaviour. Documented in the README env table.
|
|
13
|
+
- **Objective stated in the README**: cut the tokens AI coding agents spend re-discovering a codebase (cold question vs warm question). If a future change doesn't serve that goal, it's scope creep.
|
|
14
|
+
- **Savings meter** (`meter.py`, `memory_savings` tool): every graph-backed tool records response bytes and the naive-load baseline (bytes of the source files whose symbols the call returned). `memory_savings` reports per-tool counters with running totals and estimated tokens saved (~bytes/4). Counters persist across restarts in `$MEM0_DIR/memory_savings.json`. Fact fetches/writes count calls and bytes only.
|
|
15
|
+
- **Coverage-aware recall** (`recall`): recall now returns a `coverage` block — the graph's own `index_status` note (capped) plus a conservative `refresh_may_help` boolean hint derived from it. No thresholds; agents see the health data and judge.
|
|
16
|
+
- **Watchdog crash-loop breaker** (`watchdog.py`, `AMADEUS_WATCHDOG_CRASH_WINDOW` default 600 s, `AMADEUS_WATCHDOG_CRASH_LIMIT` default 5): every restart now logs the exit code of the last child, and supervision of a service that dies more than the limit inside the window stops loudly instead of thrashing forever — each restart costs 8–27 s of cold graph warm-up in practice. README documents the new env vars.
|
|
17
|
+
- **Guard test** (`tests/test_objective.py`): monkeypatched graph + deterministic fixture facts assert recall returns bounded bytes, `coverage` propagates, and the savings ledger increments on recall.
|
|
18
|
+
|
|
19
|
+
### Fixed
|
|
20
|
+
- **Crash visibility** (`watchdog.py`): on a child death the watchdog now tails `unified_memory_server.log` into its own log (40 lines) before restarting — losing the stack trace cost us this debugging. Exit code + tail are always in `unified_memory_watchdog.log`.
|
|
21
|
+
- **Stale-symbol marking in recall** (`server.py`): every *linked* fact (fact ↔ symbol hit in this recall) is re-resolved against the graph with a bounded budget (max 3 resolutions per recall). Facts whose symbols vanished from the graph since storage are marked `stale_symbol: true`, and the dump carries a `stale_linked_facts` count.
|
|
22
|
+
- **REST facade hardened** (`server.py`): the three plain-HTTP routes (`GET/POST /facts`, `/health`) now wrap store access and input validation in explicit 400/500 JSON responses instead of raising raw exceptions (the ASGI 500 cascade logged as `TypeError: 'dict' object is not callable` was a pre-restart deployment artifact, but it cost a debugging session — never again).
|
|
23
|
+
- **Index hygiene** (`server.py`): `index_repository` now defaults to `fast` mode (the binary's own filtered mode) when the caller doesn't ask; pass `mode="full"` explicitly for a full build.
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
- **Caller-aware default project** (portability): the MCP server's own CWD never changes, so the default project for graph/fact calls was frozen at boot and every other codebase needed an explicit `project=`. Now: `remember_fact` and `recall` read the caller's working directory from the MCP `_meta` (`ai.opencode/directory` best-effort) and match it against the indexed projects (exact root path first, then the `C-<path>` name suffix); an explicit `project=` argument still wins, and the env override still beats everything. `graph_projects` is a new discovery tool — lists every indexed project with its root path and names the deployment default, so working on any codebase is: list → pass `project=<name>`.
|
|
27
|
+
- Also fixed a latent bug found while touching this path: `fact_freshness` and `fact_impact` called the async `_default_project()` without `await`, so with no explicit `project` and no fact scope the symbol resolution received a coroutine object instead of a project name.
|
|
28
|
+
|
|
29
|
+
### Added
|
|
30
|
+
- **Write-side dedup** (caveat #6): the three write paths — MCP `add_memory`, MCP `remember_fact` and REST `POST /facts` — now check for a near-identical fact (token Jaccard ≥ 0.82, the same threshold `memory_dedupe`/`memory_audit` use) in the same user scope **before** inserting. On a hit nothing is written: the tools return `{"stored": null/…, "duplicate_of": {id, similarity, …}}` and the REST endpoint answers `200 {ok: true, duplicate_of: …}` idempotently, so the brain's fire-and-forget write-back cannot pile up duplicate rows when the same event fires twice. Expired facts never block a fresh restatement, and every path takes `dedupe: false` to store anyway. `tests/test_write_dedupe.py` covers all three paths against a faked store (no real store, no graph binary).
|
|
31
|
+
|
|
32
|
+
### Added
|
|
33
|
+
- **System vitals** (`system_vitals`): one call that answers "is the whole system OK?" — brain status and uptime, scheduler job count, open circuit breakers, dead-letter queue depth, vault reachability, response-cache hit rate. Reads the brain's HTTP API (`AMADEUS_BRAIN_URL`, default `http://127.0.0.1:8000`) and reports `unreachable` instead of failing when the brain is down.
|
|
34
|
+
- **Memory hygiene** tools, because the fact store accumulates untidy rows:
|
|
35
|
+
- `memory_audit` — scope coverage per project, unscoped facts, expired facts, facts without provenance or symbol links, and near-duplicate pairs (token Jaccard).
|
|
36
|
+
- `memory_scope_facts` — assigns a project scope to unscoped facts (metadata only, text is never rewritten); without an explicit project the scope is derived from the fact's linked symbols (the graph's qualified-name prefix). Dry run by default.
|
|
37
|
+
- `memory_dedupe` — removes near-duplicate rows (token Jaccard ≥ threshold), keeping the OLDEST copy so the original timestamp survives. Dry run by default.
|
|
38
|
+
- `memory_purge_expired` — deletes facts whose TTL has passed. Dry run by default.
|
|
39
|
+
- `store.mem0_rows` / `store.expiry_of` / `store.call` are now the single implementations of the row/TTL/call helpers (previously duplicated in the server module).
|
|
40
|
+
|
|
41
|
+
## [0.1.0] - 2026-11-11
|
|
42
|
+
|
|
43
|
+
### Added
|
|
44
|
+
- Unified MCP server exposing **both** memory surfaces over one process:
|
|
45
|
+
- semantic facts (mem0): `add_memory`, `search_memories`, `get_memories`,
|
|
46
|
+
`get_memory`, `update_memory`, `delete_memory`, `delete_all_memories`
|
|
47
|
+
- a code knowledge graph (codebase-memory, optional external dependency):
|
|
48
|
+
`search_graph`, `trace_path`, `get_code_snippet`, `query_graph`,
|
|
49
|
+
`get_architecture`, `check_index_coverage`, `detect_changes`,
|
|
50
|
+
`list_projects`, `index_status`, `index_repository`, `delete_project`,
|
|
51
|
+
plus a generic `codebase_memory` escape hatch
|
|
52
|
+
- Cross-linked recall (the reason for merging the two stores):
|
|
53
|
+
- `remember_fact(text, symbols=[…])` — resolves each symbol against the
|
|
54
|
+
graph and stores `{qualified_name, file, kind}` in the fact's metadata
|
|
55
|
+
- `recall(query)` — one fused call over facts **and** code symbols, with
|
|
56
|
+
`facts_linked_to_code` naming the overlap
|
|
57
|
+
- `facts_for_symbol(symbol)` — the reverse index
|
|
58
|
+
- `fact_freshness(memory_id)` — re-validates a fact's symbols (existence,
|
|
59
|
+
file moves) and its TTL
|
|
60
|
+
- `fact_impact(memory_id)` — callers/callees of every linked symbol, plus
|
|
61
|
+
optionally the current working-tree impact
|
|
62
|
+
- **Project scoping** — facts carry `metadata.project`; reads filter by it
|
|
63
|
+
client-side (upstream mem0 filters only on `user_id`/`agent_id`).
|
|
64
|
+
- **TTL / expiry** — `stale_after_days` or an absolute `stale_after`; searches
|
|
65
|
+
and recalls annotate each fact with `ttl {expires_at, expired, days_left}`,
|
|
66
|
+
and `expired_facts` lists expired and expiring-soon facts.
|
|
67
|
+
- **Session provenance** — the calling session id is captured automatically
|
|
68
|
+
from the MCP request `_meta` (`ai.opencode/sessionID`).
|
|
69
|
+
- `facts_by_project` — per-project inventory of the fact store.
|
|
70
|
+
- Optional supervisor: `python -m unified_memory.watchdog` — probes the port,
|
|
71
|
+
restarts the server with a boot grace period, and can re-index the graph on
|
|
72
|
+
a schedule (`CODEBASE_MEMORY_REFRESH_MINUTES`).
|
|
73
|
+
|
|
74
|
+
### Notes
|
|
75
|
+
- Transport: Streamable HTTP by default, in **stateless** mode so a server
|
|
76
|
+
restart does not invalidate client session ids; `--stdio` for stdio clients.
|
|
77
|
+
- The fact store uses 512-dim FastEmbed embeddings; `OPENAI_API_KEY` is
|
|
78
|
+
deliberately ignored so the embedder cannot flip and corrupt the collection.
|
|
79
|
+
- The code graph child is kept warm in a dedicated thread with its own event
|
|
80
|
+
loop: per-call CLI spawning reloads the whole index (7.4–8.5 s measured)
|
|
81
|
+
versus tens of milliseconds warm.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
unified-memory-mcp
|
|
2
|
+
Copyright 2026 Tonpa
|
|
3
|
+
|
|
4
|
+
This product includes software developed as part of the following projects:
|
|
5
|
+
|
|
6
|
+
* mem0 (https://github.com/mem0ai/mem0) — Apache License 2.0
|
|
7
|
+
Used as the semantic fact store (vector store + history bookkeeping).
|
|
8
|
+
|
|
9
|
+
* mem0-mcp-server (https://github.com/zfy258/mem0-mcp-server) — Apache License 2.0
|
|
10
|
+
OPTIONAL runtime dependency. When the `mem0-mcp-server` distribution is
|
|
11
|
+
installed, its `mcp_server` module is used verbatim so that fact storage
|
|
12
|
+
semantics (store layout, history.db bookkeeping, embedder configuration)
|
|
13
|
+
match other tools in the same ecosystem exactly. The module is NOT vendored
|
|
14
|
+
into this repository; it is an ordinary dependency.
|
|
15
|
+
|
|
16
|
+
* codebase-memory-mcp (https://github.com/DeusData/codebase-memory-mcp)
|
|
17
|
+
OPTIONAL external runtime dependency, invoked as a separate process. Its
|
|
18
|
+
licence is that of the upstream project; nothing from it is redistributed
|
|
19
|
+
here.
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: unified-memory-mcp
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: One MCP server for semantic fact memory (mem0) and a code knowledge graph (codebase-memory), with cross-linked recall.
|
|
5
|
+
Project-URL: Homepage, https://github.com/tonpai1007/unified-memory-mcp
|
|
6
|
+
Project-URL: Repository, https://github.com/tonpai1007/unified-memory-mcp
|
|
7
|
+
Project-URL: Issues, https://github.com/tonpai1007/unified-memory-mcp/issues
|
|
8
|
+
Author: Tonpa
|
|
9
|
+
License: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
License-File: NOTICE
|
|
12
|
+
Keywords: codebase-memory,knowledge-graph,llm,mcp,mem0,memory
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Requires-Dist: fastembed>=0.9
|
|
22
|
+
Requires-Dist: loguru>=0.7
|
|
23
|
+
Requires-Dist: mcp<2,>=1.9
|
|
24
|
+
Requires-Dist: mem0ai>=2.2
|
|
25
|
+
Requires-Dist: qdrant-client>=1.14
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# unified-memory-mcp
|
|
29
|
+
|
|
30
|
+
**One MCP server for two memory surfaces: semantic facts (mem0) and a code knowledge graph (codebase-memory) — plus the join between them.**
|
|
31
|
+
|
|
32
|
+
## Objective
|
|
33
|
+
|
|
34
|
+
**Cut the tokens AI coding agents spend re-discovering a codebase.**
|
|
35
|
+
|
|
36
|
+
Every LLM agent session without persistent memory of your code starts cold: it greps for symbols, re-reads whole directories, dumps entire files into the context window — and pays that same price again in every next session, on every question, from scratch. That is not a tool problem; it is a memory deficit.
|
|
37
|
+
|
|
38
|
+
This package is the fix: one shared, always-on memory of what an agent learned *about the code* (the graph — symbols, calls, structure) and *about the decisions* (the facts — what you did last week, why, and which code it touched). Two halves, fused into one callable surface, available to every agent on the machine through one URL.
|
|
39
|
+
|
|
40
|
+
Working definitions:
|
|
41
|
+
|
|
42
|
+
- **Cold question** — "what does this function do / who calls it / what did we decide about this module?" without memory: symbol lookup + file dumps + greps, repeated every session.
|
|
43
|
+
- **Warm question** — same question asked of the memory: one tool call, a few hundred tokens, correct answer *and the code it lives in*.
|
|
44
|
+
|
|
45
|
+
Success test, honest, measurable, and deliberately narrow: an agent asking any graph or recall question should never again answer it by re-reading files it already paid for. If memory is warm, it answers from memory; if it isn't, it reads once and writes the answer into memory so nobody pays for it twice.
|
|
46
|
+
|
|
47
|
+
Everything else in this repo — the warm graph child, scoped facts, write-side dedup, fused `recall`, TTL — is the machinery to make that one goal hold under real use. If any future change doesn't serve the objective above, it's scope creep; leave it out.
|
|
48
|
+
|
|
49
|
+
Two MCP servers used to live behind stdio transport. MCP stdio means *the client spawns the server*, so every editor session and every project directory got its own copy. On one machine that was 13+ processes and ~1 GB of RAM for the same two servers — and the fact store's embedded Qdrant collection is single-process locked, so the extra copies were contending for the same lock instead of adding capacity.
|
|
50
|
+
|
|
51
|
+
This package serves both surfaces from **one process** over Streamable HTTP, and adds what merging makes possible: facts can be linked to the symbols they describe, so recall returns the answer *and* the code.
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
┌── opencode / claude / cursor …─┐
|
|
55
|
+
memory_add_memory memory_search_graph
|
|
56
|
+
memory_remember_fact memory_trace_path
|
|
57
|
+
memory_recall memory_fact_freshness
|
|
58
|
+
memory_facts_for_symbol memory_fact_impact
|
|
59
|
+
memory_facts_by_project memory_expired_facts
|
|
60
|
+
memory_index_repository … ← one shared process
|
|
61
|
+
└───────────────┬────────────────────────────┘
|
|
62
|
+
│ 127.0.0.1:8777/mcp (stateless HTTP)
|
|
63
|
+
┌──────────┴───────────┐
|
|
64
|
+
│ mem0 (facts) │ codebase-memory (code graph)
|
|
65
|
+
│ Qdrant + history.db │ one warm child, index in RAM
|
|
66
|
+
└──────────────────────┘
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Why the join matters
|
|
70
|
+
|
|
71
|
+
| Tool | Question it answers |
|
|
72
|
+
|---|---|
|
|
73
|
+
| `system_vitals` | *Is the whole system OK?* — brain health and uptime, scheduler jobs, open circuit breakers, dead-letter queue, vault reachability, cache hit rate, in one call |
|
|
74
|
+
| `memory_audit` | *Is my memory trustworthy?* — scope coverage, unscoped facts, expired facts, missing provenance, near-duplicates |
|
|
75
|
+
| `memory_scope_facts` | *Stop one project's facts polluting another's recalls* — assigns a project scope (metadata only, dry run by default) |
|
|
76
|
+
| `memory_dedupe` | *Remove the same fact stored twice* — deletes near-duplicate rows, keeping the oldest (dry run by default) |
|
|
77
|
+
| `memory_purge_expired` | *Clean up facts that decayed* — deletes TTL-expired facts (dry run by default) |
|
|
78
|
+
| `remember_fact(text, symbols=[...])` | *Store a fact and pin it to the code it describes* - the symbol is resolved to its qualified name + file and kept in the fact's metadata. Write-side dedup: a near-identical fact (token Jaccard >= 0.82) in the same scope returns the existing id (`duplicate_of`) instead of inserting a second row; `dedupe: false` stores anyway |
|
|
79
|
+
| `recall(query)` | *Give me the answer and the code* — one fused call over facts **and** graph symbols, with `facts_linked_to_code` calling out the overlap |
|
|
80
|
+
| `facts_for_symbol(symbol)` | *What do we know about this function?* — the reverse index |
|
|
81
|
+
| `fact_freshness(memory_id)` | *Is this old fact still true?* — re-checks that every linked symbol still exists, that no file moved, and that the fact's TTL has not passed |
|
|
82
|
+
| `fact_impact(memory_id)` | *What breaks if I touch this?* — callers/callees of every linked symbol, optionally plus the impact of the current working-tree changes |
|
|
83
|
+
|
|
84
|
+
Facts can also be **scoped to a project** (`project` on write and read) so a finding from one project never pollutes another project's recall, **tagged with their session** (captured automatically from the MCP request `_meta`), and given a **TTL** (`stale_after_days` / `stale_after`) so facts about code decay instead of being trusted forever.
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
**Any codebase, not just the deployment default.** The code graph indexes every project it was pointed at; `graph_projects` lists them with their root paths. `remember_fact` / `recall` pick up the calling session's working directory from the MCP `_meta` and match it to an indexed project automatically; for every other graph tool pass `project=<name>` explicitly (or set `CODEBASE_MEMORY_PROJECT` to pin one deployment-wide).
|
|
88
|
+
|
|
89
|
+
## Install
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
pip install unified-memory-mcp
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Straight from the tagged GitHub release (no PyPI account needed):
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
pip install "unified-memory-mcp @ git+https://github.com/tonpai1007/unified-memory-mcp@v0.1.0"
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
The fact store works standalone (the package builds the same store with
|
|
102
|
+
mem0ai directly). To instead reuse the **upstream local-store** mem0 MCP
|
|
103
|
+
module verbatim — identical store layout, `history.db` bookkeeping and embedder
|
|
104
|
+
config — install it from git:
|
|
105
|
+
|
|
106
|
+
```bash
|
|
107
|
+
pip install "git+https://github.com/zfy258/mem0-mcp-server.git"
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
> Note: the PyPI distribution `mem0-mcp-server` (0.2.x) is a **hosted-API**
|
|
111
|
+
> client with a different contract; this package detects that and falls back
|
|
112
|
+
> to the direct mem0ai path.
|
|
113
|
+
|
|
114
|
+
The code graph is an **optional external dependency** — it is not bundled:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# see https://github.com/DeusData/codebase-memory-mcp for install options
|
|
118
|
+
codebase-memory-mcp --help
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Without it, every fact tool still works; the graph tools return a clear error.
|
|
122
|
+
|
|
123
|
+
## Run
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
unified-memory-mcp # Streamable HTTP on 127.0.0.1:8777/mcp
|
|
127
|
+
unified-memory-mcp --stdio # stdio transport, for clients that need it
|
|
128
|
+
unified-memory-mcp --port 9000 # custom port
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Keep it alive (it is a shared service, so something should supervise it):
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
python -m unified_memory.watchdog
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The watchdog probes the port every 45 s, restarts the server when it dies (with a grace period, so a booting server is not killed again), and can re-index the graph on a schedule. Each restart logs the exit code of the last child so a dying server stops being invisible; if the same service dies more than `AMADEUS_WATCHDOG_CRASH_LIMIT` times inside `AMADEUS_WATCHDOG_CRASH_WINDOW` seconds, supervision of that service stops loudly instead of thrashing forever (each restart costs 8–27 s of cold graph warm-up):
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
export CODEBASE_MEMORY_REFRESH_MINUTES=30
|
|
141
|
+
export CODEBASE_MEMORY_REFRESH_TARGETS="MyProject=/path/to/repo"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Client configuration
|
|
145
|
+
|
|
146
|
+
OpenCode (`~/.config/opencode/opencode.jsonc`):
|
|
147
|
+
|
|
148
|
+
```jsonc
|
|
149
|
+
{
|
|
150
|
+
"mcp": {
|
|
151
|
+
"servers": {
|
|
152
|
+
"memory": {
|
|
153
|
+
"type": "remote",
|
|
154
|
+
"url": "http://127.0.0.1:8777/mcp"
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
Claude Desktop / Cursor / any stdio client:
|
|
162
|
+
|
|
163
|
+
```json
|
|
164
|
+
{
|
|
165
|
+
"mcpServers": {
|
|
166
|
+
"memory": {
|
|
167
|
+
"command": "unified-memory-mcp",
|
|
168
|
+
"args": ["--stdio"]
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Environment
|
|
175
|
+
|
|
176
|
+
| Variable | Default | Meaning |
|
|
177
|
+
|---|---|---|
|
|
178
|
+
| `MEM0_DIR` | `~/.amadeus-memory` | fact store root (Qdrant + `history.db`) |
|
|
179
|
+
| `MEM0_HOST` / `MEM0_PORT` | `127.0.0.1` / `8777` | HTTP bind address |
|
|
180
|
+
| `CODEBASE_MEMORY_EXE` | auto-detected | path to the codebase-memory binary |
|
|
181
|
+
| `CODEBASE_MEMORY_PROJECT` | auto (CWD's project) | default project for graph queries |
|
|
182
|
+
| `CODEBASE_MEMORY_REFRESH_MINUTES` | `0` (off) | scheduled graph refresh interval |
|
|
183
|
+
| `AMADEUS_WATCHDOG_INTERVAL` | `45` | how often the watchdog probes the port |
|
|
184
|
+
| `AMADEUS_WATCHDOG_GRACE_SECONDS` | `120` | grace period for a just-started service to bind |
|
|
185
|
+
| `AMADEUS_WATCHDOG_CRASH_WINDOW` | `600` | crash-loop window in seconds |
|
|
186
|
+
| `AMADEUS_WATCHDOG_CRASH_LIMIT` | `5` | restarts allowed per window before supervision stops |
|
|
187
|
+
| `CODEBASE_MEMORY_REFRESH_TARGETS` | – | `name=/path` pairs to re-index |
|
|
188
|
+
| `CODEBASE_MEMORY_REFRESH_SPACING_SECONDS` | `15` | pause between re-index targets (set `0` for back-to-back); spreads the CPU cost of refreshing several large repos |
|
|
189
|
+
| `OPENAI_API_KEY` | ignored | deliberately unset: the store is 512-dim FastEmbed and must not flip to OpenAI embeddings |
|
|
190
|
+
|
|
191
|
+
## Design notes
|
|
192
|
+
|
|
193
|
+
* **Stateless HTTP.** Each POST is self-contained, so a server restart does not invalidate the MCP session ids held by connected clients. With session state enabled, clients keep posting a stale id and get HTTP 400 with no auto-reconnect.
|
|
194
|
+
* **One warm graph child.** The graph child lives in a dedicated thread with its own event loop. It cannot be owned by a request handler — anyio's `stdio_client` raises *"Attempted to exit a cancel scope in a different task than it was entered in"* — and stateless HTTP makes every request its own task. Spawning the CLI per call instead reloads the whole index every time (7.4–8.5 s measured, versus tens of milliseconds warm).
|
|
195
|
+
* **Client-side scoping.** Upstream mem0 filters only on `user_id`/`agent_id`, so project scoping and TTL flags are applied here (over-fetch, then filter) rather than in the store.
|
|
196
|
+
|
|
197
|
+
## Releasing
|
|
198
|
+
|
|
199
|
+
1. Bump `version` in `pyproject.toml` and add a `CHANGELOG.md` entry.
|
|
200
|
+
2. Tag and push: `git tag -a vX.Y.Z -m "…" && git push origin vX.Y.Z`
|
|
201
|
+
→ `release.yml` builds, runs `twine check`, creates the GitHub release and
|
|
202
|
+
publishes to PyPI.
|
|
203
|
+
3. Publishing to PyPI needs a publisher registered for this repository:
|
|
204
|
+
- **Trusted publishing (recommended)** — on PyPI: *Account → Publishing → Add
|
|
205
|
+
a new publisher → GitHub*, owner `tonpai1007`, repository
|
|
206
|
+
`unified-memory-mcp`, workflow `release.yml`. The workflow requests an
|
|
207
|
+
OIDC token (`id-token: write`) and PyPI exchanges it.
|
|
208
|
+
- **API token** — create a token on PyPI and add it as the repository
|
|
209
|
+
secret `PYPI_API_TOKEN`; the workflow uses it when present.
|
|
210
|
+
|
|
211
|
+
Both paths work with the same workflow; with neither configured the GitHub
|
|
212
|
+
release is still created and only the PyPI step fails.
|
|
213
|
+
|
|
214
|
+
## Licence
|
|
215
|
+
|
|
216
|
+
Apache-2.0 — see `LICENSE`.
|
|
217
|
+
|
|
218
|
+
This package builds on [mem0](https://github.com/mem0ai/mem0) (Apache-2.0) and can optionally use the upstream [mem0-mcp-server](https://github.com/zfy258/mem0-mcp-server) module (Apache-2.0) verbatim; see `NOTICE`. The code graph comes from the separate [codebase-memory-mcp](https://github.com/DeusData/codebase-memory-mcp) project, which is an optional runtime dependency and is **not** redistributed here.
|