Python API reference¶
Generated from the source, so it never drifts from the code.
For task-oriented examples, see the Python library guide.
Memory store¶
localmem_mcp.store.MemoryStore
¶
Local memory store backed by SQLite and on-device embeddings.
Source code in src/localmem_mcp/core/store.py
add
¶
Embed and persist a memory. Returns the stored record.
created_at overrides the timestamp used for both created_at and
updated_at, which is what an import needs: a memory restored from a
JSONL file should keep the day it was first recorded, not the day it was
restored. Leave it unset for normal writes.
Source code in src/localmem_mcp/core/store.py
update
¶
Correct an existing memory in place. Returns None if there's no such memory.
Only the fields you pass are changed. Changing content re-embeds the
memory so search finds the correction; a tag- or source-only update
leaves the vector alone. created_at is preserved and updated_at
refreshed. The FTS5 index stays in sync via the AFTER UPDATE trigger.
Source code in src/localmem_mcp/core/store.py
delete
¶
Delete a memory. Returns True if it existed, False if it didn't.
Source code in src/localmem_mcp/core/store.py
matching
¶
Memories a bulk delete would remove, newest first.
Same filters as :meth:delete_many, without deleting — the CLI uses
this to show what forget --tag stale would remove before asking.
Source code in src/localmem_mcp/core/store.py
delete_many
¶
Bulk-delete memories matching all given tags and/or age.
Requires at least one of tags or older_than_days; an unfiltered
call raises :class:ValueError so the store can't be wiped by accident.
Deletion is hard — rows are gone, not hidden — and the FTS5 index stays
in sync via the AFTER DELETE trigger. Returns the number removed.
Source code in src/localmem_mcp/core/store.py
get
¶
Fetch one memory by id, or None if there's no such memory.
Source code in src/localmem_mcp/core/store.py
list
¶
List stored memories with tag filtering, ordering, and pagination.
Returns (memories, total_count) where total_count is the total
number of stored memories matching all specified tags, regardless of
limit and offset.
Source code in src/localmem_mcp/core/store.py
recent
¶
Most recently stored memories, newest first.
Source code in src/localmem_mcp/core/store.py
search
¶
Semantic search, nudged by exact keyword matches.
Every stored memory is scored by cosine similarity against the query embedding; memories that also match the FTS5 index get a bounded keyword bonus so literal terms are not lost to paraphrase.
offset skips that many ranked results before limit is applied,
so limit=5, offset=5 is the second page. Ranking is deterministic
(score descending, ties broken by id descending), so pages neither
overlap nor skip. Negative offsets are clamped to 0, and an offset past
the end returns an empty list rather than raising.
Source code in src/localmem_mcp/core/store.py
contains
¶
Whether a memory with exactly this content is already stored.
Content equality is how the import path recognises a re-import, which is deliberately cruder than semantic similarity: a duplicate here means the same text, not merely a memory that means the same thing.
Source code in src/localmem_mcp/core/store.py
export_records
¶
Yield every memory as a plain dict, oldest first.
The shape matches :meth:Memory.to_dict, so an exported record is the
same structure --json already prints. tags filters
conjunctively, like :meth:search.
Embeddings are left out unless with_embeddings is set: a vector is
only meaningful to a machine running the same model, and the point of
an export is to be portable. When included, each record also carries
embedding_model and dim so a reader can tell what produced it.
Rows are fetched under the store's lock so a concurrent write can't produce a torn read, then yielded one at a time.
Source code in src/localmem_mcp/core/store.py
count
¶
stats
¶
Database location, memory count, and the embedding model in use.
Data types¶
localmem_mcp.store.Memory
dataclass
¶
localmem_mcp.store.ImportReport
dataclass
¶
What an import did, or — with dry_run — what it would have done.
Import¶
localmem_mcp.store.import_records
¶
Store one memory per JSONL line, skipping (and reporting) bad ones.
lines is any iterable of strings — an open file, so a large import
streams rather than loading the whole thing. Blank lines are ignored.
Each memory is re-embedded by store; embeddings present in the file are
ignored. created_at is preserved when the record has one. By default a
record whose content already exists in the store is skipped, so importing
the same file twice doesn't double the store — pass allow_duplicates to
store it anyway. With dry_run nothing is written and nothing is
embedded; the report says what would have happened.
Source code in src/localmem_mcp/core/portability.py
Embedders¶
localmem_mcp.store.Embedder
¶
Bases: Protocol
Anything that can turn text into a fixed-length vector.
localmem_mcp.store.FastEmbedEmbedder
¶
Local ONNX embeddings via fastembed.
The model is loaded lazily so importing this module (and starting the MCP
server) stays fast — the first store_memory/search_memory call pays the
load cost, not process startup.
Source code in src/localmem_mcp/core/embedders.py
embed
¶
Embed texts locally, loading the model on the first call.
Helpers¶
localmem_mcp.store.default_db_path
¶
Where memories live unless told otherwise.
Honours LOCALMEM_DB_PATH, then LOCALMEM_HOME, then ~/.localmem.
Source code in src/localmem_mcp/core/utils.py
Server¶
The MCP tool functions are documented in the MCP tools guide. These are the module-level helpers for embedding the server in your own process.
localmem_mcp.server.get_store
¶
Return the process-wide store, opening it on first use.
Source code in src/localmem_mcp/mcp/app.py
localmem_mcp.server.configure
¶
Point the server at a specific database/model before serving.