Skip to content

About

Local-first memory for AI agents over REST and MCP, with semantic retrieval and SQLite persistence.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

8 Commits

Folders and files

Repository files navigation

MemoryMesh

MemoryMesh banner showing episodic, semantic, procedural, and preference memory

CI Python 3.11+ MIT License

MemoryMesh is a local-first memory server for AI agents. An agent can save a user preference over REST, restart the service, and recall the same record over MCP. Both interfaces use the same SQLite store and rank memories by meaning, retention, and importance.

The demo below runs the REST app and a separate MCP process against the same database. It checks that the recalled record has the original ID, even with an unrelated memory in the store.

Terminal capture of the offline MemoryMesh demo saving a preference over REST, restarting the API, and recalling the same memory over MCP

Quickstart

Requires Python 3.11 or later. Run these commands from the repository root in your virtual environment.

python -m venv .venv
source .venv/bin/activate
python -m pip install -e .
python -m Demo.persistence

In PowerShell, activate the environment with .\.venv\Scripts\Activate.ps1.

The offline demo uses deterministic embeddings and a temporary SQLite database. Add --real-model to run the same flow with the configured embedding model. Both modes run without LLM provider keys.

To store and search your own memories, start the API and open Swagger UI.

memorymesh-api

The first real startup downloads the configured Sentence Transformers model to the local cache. The default is BAAI/bge-large-en-v1.5. CPU inference works, and the first download and load can take time. Set EMBEDDING_MODEL before populating a database. Use a fresh DB_PATH when changing models, or re-embed the existing memories before searching.

Try this from a second terminal.

curl -X POST http://127.0.0.1:8000/v1/memories/ \
  -H 'Content-Type: application/json' \
  -d '{"agent_id":"demo-agent","memory_type":"preference","content":"The user prefers concise bullet points."}'

curl -X POST http://127.0.0.1:8000/v1/memories/search \
  -H 'Content-Type: application/json' \
  -d '{"agent_id":"demo-agent","query":"How should I format my response?","k":1}'

The calls above use Bash quoting. PowerShell users can run the demo unchanged or use Swagger UI for the two requests. More requests and the REST walkthrough script cover batch writes, filters, graphs, and updates.

MCP setup

Add the following server to your MCP client. Set command to the Python executable in your project environment and DB_PATH to the same absolute database path the REST server uses. Install the package in that environment first.

{
  "mcpServers": {
    "memorymesh": {
      "command": "/absolute/path/to/.venv/bin/python",
      "args": ["-m", "memorymesh.mcp.server"],
      "env": {
        "DB_PATH": "/absolute/path/to/memorymesh.db"
      }
    }
  }
}

On Windows, use the virtual environment’s Scripts\python.exe path and escape backslashes in JSON. You can start the server directly with memorymesh-mcp.

Tool Purpose
store_memory Store a typed memory with an importance score
retrieve_memories Find ranked, compact context
get_memory_source Read the original content and provenance by ID
update_memory Edit content, importance, or metadata
delete_memory Delete a memory
list_memories Browse previews and see which sources were superseded
get_memory_stats Inspect memory counts and average retention
compress_agent_memories Synthesize a batch of episodes with Tier 2

How it works

flowchart LR
    Client["Agent or dashboard"] --> REST["FastAPI · REST"]
    MCPClient["MCP client"] --> MCP["MCP · stdio"]
    REST --> Store["Store · search · compression"]
    MCP --> Store
    Store --> SQLite["SQLite in WAL mode"]
    Store --> Embeddings["Local sentence embeddings"]
    Store --> Ranking["Cosine similarity + retention + importance"]
    Store -. optional .-> Providers["Groq and Mistral"]
Loading

Three design choices keep the core easy to run and verify.

  • One store across two interfaces. FastAPI and MCP reuse the same persistence and retrieval code. SQLite in WAL mode lets the two processes share a local database. The transport test checks that compact search results and original records agree across interfaces.
  • Ranking with a testable outcome. Semantic similarity, retention, and importance contribute explicit scores. The retrieval tests check which memory wins in NumPy and FAISS and verify that memory type changes retention at the same age.
  • Compact context with provenance. Search sends summaries by default. Source records remain available by ID. When two processes attempt the same merge, the database commits only one result. The compression tests cover overlapping merges, rollback, and legacy data.
Memory type Example Retention multiplier
Episodic The user discussed an async migration yesterday 1.0
Semantic The migration targets PostgreSQL 2.0
Procedural How to run a migration in staging 3.0
Preference The user prefers answers in bullet points 2.5

Search blends cosine similarity (0.5), retention (0.3), and importance with an access-count boost (0.2). The retention term follows an Ebbinghaus-style curve based on days since access. Memory type, importance, and past accesses determine how quickly it falls. The API takes per-query weights under semantic_weight, recency_weight, and importance_weight. Here recency_weight controls the retention term. REST refreshes stored retention snapshots for the dashboard every six hours by default. Search computes retention at request time.

SQLite stores the text and embedding vectors. Search uses NumPy by default. Install the vector extra to use FAISS IndexFlatIP for exact cosine scoring.

Optional compression

python -m pip install -e ".[compression]"

Set GROQ_API_KEY for Tier 1 and MISTRAL_API_KEY for Tier 2. The relevant memory text is sent to that provider when a compression operation runs.

  • Tier 1 saves a shorter summary when the provider produces one. Search returns it as content, and the original remains searchable by its embedding. Fetch the source with GET /v1/memories/{id} or the MCP get_memory_source tool. Set include_source to true to return the original and summary together for a Tier 1 search hit.
  • Tier 2 synthesizes at least three episodic memories into one semantic memory, with source IDs in its metadata. Active search hides the superseded episodes, which remain available by ID. Editing or deleting a source removes its synthesis and makes the other sources eligible for retrieval and future merges.
  • Run on demand. Trigger an age-based pass with POST /v1/compress/trigger, use a single-memory or agent endpoint, or run Tier 2 from MCP. The age-based pass considers both creation time and last access. Schedule the trigger externally if you want nightly runs. The log records source text and provider model for completed compressions.

Optional dashboard

python -m pip install -e ".[dashboard]"
streamlit run dashboard/app.py

Run the API on its default port, then open the dashboard for search, memory graphs, compression history, and retention curves.

REST endpoints

Purpose Endpoints
Write and read memories POST /v1/memories/, POST /v1/memories/batch, GET /v1/memories/{id}
Edit or delete a memory PATCH /v1/memories/{id}, DELETE /v1/memories/{id}
Search and inspect POST /v1/memories/search, GET /v1/memories/agent/{id}, POST /v1/memories/agent/{id}/graph
Compress and audit POST /v1/compress/trigger, POST /v1/compress/memory/{id}/tier1, POST /v1/compress/agent/{id}/tier2, GET /v1/compress/log
Health and stats GET /health, GET /v1/stats

Configuration and deployment

The REST CLI binds to 127.0.0.1:8000 by default. REST and MCP are intended for a trusted local environment. Agent IDs filter records and do not authenticate callers. Keep the service on loopback for local use. A shared deployment needs authentication and authorization that scope access to each caller’s data.

Set options with environment variables or copy .env.example to .env. DB_PATH defaults to memorymesh.db relative to the working directory. For REST and MCP, use the same absolute DB_PATH and embedding model. A database and its WAL can contain stored content, query text, and compression history. Use an isolated database with synthetic content for public demos.

Development

python -m pip install -e ".[dev]"
python -m pytest
python -m Demo.persistence

The test suite uses synthetic data and mocked providers. CI runs the persistence demo and tests on Linux and Windows. To also run the native FAISS regression locally, install .[dev,vector].

MIT licensed. Built by Devdutt S.

About

Local-first memory for AI agents over REST and MCP, with semantic retrieval and SQLite persistence.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages