Mnemosyne MCP Server: Retrieve Memories Detail
Level 3 (Detail) — Exact step-by-step flow of the semantic search pipeline.
Concept
retrieve_memories accepts a natural language query, embeds it via Gemini, performs cosine similarity search in pgvector, and returns matching memories ranked by relevance.
Handler Flow (MCP Layer)
File: internal/mcp/mcp.go — handleRetrieve() (lines 101-126)
- Parse
query(string, required) andlimit(int, optional, default 5) from tool arguments - Call
controller.SearchMemories(query, limit, 0, "", "") - Format results as
--- [<id>] [<date>] ---\n<content>\n - Return formatted text
Parameters NOT exposed:
daysBack— always passed as0startStr— always passed as""endStr— always passed as""
Search Flow (Logic + DB Layer)
File: internal/logic/logic.go — SearchMemories() (lines 145-177)
- Embed the query text via
embed.GetEmbedding(query)— same Gemini API call as ingestion - Parse temporal filters (all disabled from MCP):
daysBack > 0→ setstarttonow - daysBackstartStr != ""→ parse asYYYY-MM-DDand setstartendStr != ""→ parse asYYYY-MM-DDand setend
- Call
db.Search(vector, limit, start, end)
File: internal/db/db.go — Search() (lines 113-150)
- Build SQL dynamically:
SELECT id, timestamp, content FROM memories WHERE 1=1 [AND timestamp >= $2] -- if start is set [AND timestamp <= $3] -- if end is set ORDER BY embedding <=> $1 LIMIT $N <=>is pgvector’s cosine distance operator (0 = identical, 2 = opposite)- Return
[]Memorywith ID, timestamp, content
Result Formatting
Each result is formatted as:
--- [<uuid>] [<YYYY-MM-DD>] ---
<full content text>
The list_memories tool uses the same content but applies extractTitle() (lines 224-236):
- Looks for a
TITLE:prefix on any line - Falls back to first 50 characters of content
Temporal Filter Gap
The DB layer fully supports time-range queries. The controller parses daysBack, startStr, endStr correctly. The gap is purely in the MCP tool schema:
retrieve_memoriesonly declaresqueryandlimit- The handler always passes
0, "", ""toSearchMemories() - Any client wanting time-filtered searches must call the controller directly (not possible over MCP)
Code Paths
| File | Function | Line | Role |
|---|---|---|---|
internal/mcp/mcp.go | handleRetrieve() | 101 | MCP handler, parses query/limit |
internal/logic/logic.go | SearchMemories() | 145 | Query embedding + temporal filter parsing |
internal/db/db.go | Search() | 113 | Dynamic SQL + cosine similarity |
internal/mcp/mcp.go | extractTitle() | 224 | Title extraction for list display |
See Also
- Parent topic: MCP Tools
- Sibling details: Ingest Memory Detail, Deployment Detail, Python Tools Detail
- Debt: Known Issues — Temporal Filter Gap