22 KiB
Memory MCP Strategic Guidelines
This document outlines the STRATEGY, SEMANTICS, and CASING STANDARDS for using the MCP Memory Server.
You do not need to memorize JSON schemas for these tools; they are strictly defined and typed in the tools/list endpoint. Focus purely on WHEN and WHY to use them.
1. Casing & Naming Standards (CRITICAL)
To prevent graph fragmentation and ensure seamless LLM context retrieval:
- Entity Types (
entity_type): MUST ALWAYS bePascalCase(e.g.DatabaseTable,McpTool,ArchitectureComponent,File,DataStructure). - Relation Types (
relation_type): MUST ALWAYS besnake_case(e.g.depends_on,calls,implements,uses,contains). - Field Keys & Properties: MUST ALWAYS be
snake_case(e.g.file_path,git_commit,created_at). - Relation Tool Parameters:
create_relationssupports Serde field aliases (source->from,target->to,relationType/type->relation_type) so LLM tool calls succeed seamlessly regardless of parameter naming.
Note
The server automatically enforces and migrates incoming entity and relation types to these canonical casing rules on every read and write operation. Store operations enforce atomic lock scope for serialization/deserialization and enter Quarantine Mode upon database corruption to prevent data overwrite hazards.
2. Consolidated Smart Tools Architecture
The server consolidates granular single-purpose tools into domain-named smart tools. Always prefer the consolidated tools over legacy aliases:
-
tasks: Complete task lifecycle management.action: "add": Create a new task (requirestitle, optionaldescription,git_branch,repo_name,priority: "low" | "medium" | "high" | "urgent",assigned_agent,verification_command,parent_id,dependencies).action: "update": Update task status (requiresid,status: "pending" | "completed" | "cancelled").action: "delete": Delete task and child tasks (requiresid).action: "list": List active tasks (optionalgit_branch,summary_level: "compact" | "detailed" | "full",max_tokens).action: "set_criteria": Set acceptance criteria (requiresid,criteria: Vec<String>).action: "verify": Verify criteria met (requiresid, optionalproof).
-
milestones: Milestone tracking.action: "add": Create milestone (requirestitle, optionalnamespace,target_date,description,deliverables: Vec<String>,repo_name).action: "update": Update milestone status (requiresid,status: "active" | "completed" | "cancelled").action: "list": List milestones (optionalnamespace).
-
handoff_memos: Session handoff notes for future agents.action: "leave": Leave a memo (requirescontent, optionalvcs_revision,repo_name,git_branch,blockers: Vec<String>,action_items: Vec<String>,expires_at).action: "read": Read active handoff memos.action: "clear": Clear memos.
-
snippets: Reusable code snippet vault.action: "store": Store snippet (requiresqueryas name, optionallanguage,code,description,tags,origin_file,line_range,repo_name).action: "search": Search snippet vault (optionalquery,tags,hybrid: true).action: "delete": Delete snippet (requiresid).action: "tag": Attach classification tags (requiresid,tags: Vec<String>).
-
decisions: Architectural Decision Records (ADRs).action: "log": Log ADR (requirestitle, optionalstatus: "accepted" | "proposed" | "deprecated" | "superseded",context,decision,consequence,author,affected_components: Vec<String>,alternatives_considered: Vec<String>,supersedes,repo_name).action: "query": Query ADRs (optionalquery).action: "delete": Delete ADR (requiresid).
-
tech_debt: Engineering debt backlog.action: "log": Log debt item (requiresdescription, optionalideal_solution,file_path,line_range,workaround,effort_estimate,severity: "low" | "medium" | "high" | "critical",git_commit,git_branch,symbol_references,repo_name).action: "resolve": Resolve debt item (requiresid).action: "list": List debt items (optionalinclude_resolved).
-
environment: Infrastructure and requirements tracking.action: "update_fingerprint": Update tool versions (requirestool_versions: Map<String, String>, optionalrepo_name).action: "read_fingerprint": Read tool versions fingerprint.action: "log_requirement": Log environment variable requirement (requireskey,description,is_secret, optionaldefault_value,validation_regex,repo_name).action: "register": Register target environment (requiresname,url, optionaldescription,requires_vpn,env_type: "dev" | "staging" | "qa" | "prod",healthcheck_endpoint,ssh_host,repo_name).action: "get_details": Read full environment details.
-
clipboard: OS Clipboard management.action: "read": Read OS clipboard.action: "write": Write text/html/files/image to clipboard.
-
hypotheses: Diagnostic hypothesis memory.action: "log": Record diagnostic hypothesis (requireshypothesis, optionalstatus: "open" | "verified" | "disproven",evidence: Vec<String>,test_command,git_branch,repo_name).action: "query": Query hypotheses (optionalstatus,git_branch,repo_name).
-
agent_signals: Inter-agent signal bus.action: "broadcast": Broadcast signal to other agents (requiressignal_type,payload, optionaltarget_agent,ttl_seconds).action: "query": Query active signals (optionalsignal_type,include_expired: bool).
-
process_logs: Process and daemon log management.action: "watch": Register or update log file watcher (requireslabel,file_path, optionaldescription).action: "get": Retrieve recent log lines from watched process log (requireslabel, optionaltail_lines: usize).action: "clear": Clear or truncate watched process log file (requireslabel).
3. VCS & SVN Agnosticism & Multi-Repo Provenance
To support diverse enterprise repositories (Git, Subversion / SVN, Mercurial / Hg, Monorepos):
vcs_type: Designates the VCS engine ("git","svn","hg","perforce", or"none").vcs_revision: Agnostic commit hash or SVN revision identifier (e.g.,"r12458","3e4f7a9").upstream_url: Canonical remote repository URL (e.g.https://svn.corp/repo/trunk,git@bitbucket.org:org/repo.git).repo_name: Logical project or repository identifier allowing multiple repositories to share or partition memory namespaces cleanly without collision.- Audit Ledger (
log_code_change): Enriched withvcs_type,vcs_revision,upstream_url,author,diff_summary, and extensiblemetadata: HashMap<String, String>.
4. Terminal & Process Telemetry
The server ingests and tracks active terminal commands and sessions:
- Active Terminals: Tracks PIDs, shell interpreters (
pwsh,bash,nu,zsh), current working directories (cwd), command exit codes, and timestamps. - Terminal History Endpoint:
/terminal/historyexposes recent shell commands and output streams to dashboard and LLMs to prevent lost shell context. - Quality Gate Enforcement:
GateRecordcaptures pre-flight and pre-push validation passes withgate_type,enforcer,status,validation_log, andrepo_name.
4. High-Signal Tool Responses & Performance Guidelines
To optimize context usage, response times, and LLM reasoning efficiency:
-
High-Signal Feedback:
- MCP tool calls (such as
create_entities,create_relations,add_observations,pin_file) return explicit, structured summaries containing created names, types, edge counts, and relation paths. - LLMs do NOT need to execute follow-up
open_nodescalls merely to confirm successful creation.
- MCP tool calls (such as
-
Batch Operations:
- When creating or updating multiple entities, snippets, or observations, always batch items into a single tool call array (e.g.
create_entitieswith multiple array items) rather than making separate calls. - The server handles batch store mutations serially in a single transaction pass with single-permit event-driven flushes.
- When creating or updating multiple entities, snippets, or observations, always batch items into a single tool call array (e.g.
-
Real-time Tantivy Search Indexing:
- The Tantivy search engine automatically checks pending commits and reloads search readers prior to executing
omni_searchorsearch_nodes. Search queries always return up-to-date document results immediately following mutations. - Single-item deletions use targeted document removal rather than global index wipes.
- The Tantivy search engine automatically checks pending commits and reloads search readers prior to executing
-
Real-Time AST & Workspace Source Code Symbol Scanning:
find_symbol_references,get_callers, andanalyze_impactscan both stored code snippets and physical workspace source code files on disk (.rs,.ts,.py,.go,.java,.c,.cpp), providing accurate AST symbol references and call site tracking.
-
Graph Entity Merge & Self-Loop Protection:
merge_entitiesre-points all relations fromsource_entitytotarget_entityand automatically prunes cyclic self-loops (target -> target).
-
Safe UTF-8 Token Truncation:
- Large responses (e.g.
get_active_worktree_context,read_graph,summarize_subgraph) are safely truncated along UTF-8 character boundaries (floor_char_boundary), ensuring response bounds without runtime panics.
- Large responses (e.g.
4. Automated Error Fix Auto-Matcher
- Tools:
log_error_fix,search_error_fixes(and aliassuggest_error_fix) - When to use: When encountering a build error, test failure, or stack trace. Call
search_error_fixeswith either a textqueryorstack_tracebefore attempting a fix from scratch. - Behavior: Computes cosine similarity between error trace embeddings and past resolution logs when
stack_traceis provided, or keyword filtering whenqueryis provided, returning top matched solutions, modified files, and git commits.
5. Memory State Checkpointing & Rollbacks
- Tool:
checkpoint_state,restore_state(orcreate_snapshot,restore_snapshot) - When to use: Before initiating a large refactor, running experimental subagent tasks, or executing destructive batch operations.
- Behavior: Saves or restores a point-in-time snapshot of graph entities, active tasks, and tech debt backlogs.
6. Self-Healing Graph Health Sweeper
- Tool:
sweep_graph_health - When to use: Periodically or before committing major graph changes to audit entity consistency.
- Behavior: Detects orphaned nodes (0 relations), computes name similarity using pre-computed lowercase keys to identify near-duplicates (e.g.,
APIGatewayvsApiGateway), and provides structuredmerge_entitiesrecommendations or auto-prunes orphans.
7. Causal Lineage & Provenance Tracker
- Tool:
query_lineage - When to use: When asking "Why was this component modified?" or "What task or ADR led to this code change?"
- Behavior: Searches across tasks, ADRs, audit ledger entries, and error fixes to assemble a unified chronological timeline explaining the provenance behind any file, symbol, or commit.
9. Native Rust Invariants & Subprocess Prohibition (CRITICAL)
To maintain maximum security, speed, and cross-platform reliability:
- Zero Subprocess Fallbacks: System and server tools (
clipboard,ast,search,db) MUST strictly use pure native Rust crates (arboard,tree-sitter,tantivy,psycopg). Invocations of external shell commands (powershell.exe,wl-paste,xclip,cmd.exe) are strictly prohibited in native handlers. - Transient Lock Recovery: Transient OS handle collisions (such as Win32 OLE
OpenClipboardlock contention) must be handled using native retry loops with backoffs directly in Rust. - Automated Static Regression Gates: Automated AST/source audit tests (e.g.
test_no_subprocess_clipboard_regression) verify at test time that forbidden subprocess patterns are absent from handler implementations.
10. High-Performance Concurrency & Resilience Guarantees
- Explicit Fail-Fast Persistence Safety: Replaced silent fallback to temporary databases (
/tmp/mcp_store_fallback_*) with an explicit open retry and fail-fast panic unlessMCP_ALLOW_TMP_FALLBACK=1is explicitly set, preventing silent data loss. - Async Mutex Deadlock Elimination: Converted shared state and Neovim connection locks (
shutdown_tx,NVIM_CONN,ACTIVE_SOCKET,HEADLESS_PROC) totokio::sync::Mutexto prevent worker thread pool starvation across.awaitpoints. - Telemetry Session Deduplication & Channel Pruning: Added
LAST_SESSIONin-memory state deduplication for UDP telemetry writes (eliminating disk I/O thrashing) and distinguished WebSocketTrySendError::Fullbackpressure vsTrySendError::Closedclient pruning. - Graph Adjacency Indexing: Leverages
KnowledgeGraph::build_adjacency_mapto buildO(1)lookup adjacency lists for fast BFS shortest path graph queries. - Atomic Store Write Lock Minimization:
Store::modifyandStore::modify_asyncrelease write guards immediately after in-memory state mutations, serializing JSON payloads under read locks to allow non-blocking concurrent readers. - Async Commit Index Reader Auto-Reload:
MemoryIndex::commit()automatically triggersreader.reload()upon completing background commits, guaranteeing immediate visibility of newly indexed document terms. - Zero-Allocation NDJSON Reader:
mcp-stdioreclaims line buffers viastd::mem::takeand in-place trimming to eliminate heap allocations during high-frequency NDJSON message parsing. - Dynamic Character Micro-Batched Fastembed Inference:
generate_embeddings_asyncdynamically batches text arrays using a 16,000 character budget ceiling insidespawn_blocking, preventing heap spikes during vector indexing while keeping ONNX SIMD execution saturated. - Bounded Telemetry Detail Records: Activity and terminal telemetry buffers enforce a 4,000 character truncation ceiling on log details (
ActivityRecord,TerminalHistory) to prevent unbounded RAM growth under high RPC throughput. - Zero-Allocation Stream Formatting: Graph condensation loops (
condense_graph_worker) format node/relation subgraphs into stream buffers viastd::fmt::Write, eliminating intermediate String allocation overhead. - Single-Handle OS Clipboard Retries & Image Downsampling:
WriteClipboardHandlerinitializes OS clipboard handles once per operation and downsamples images exceeding2048 \times 2048resolution before writing raw RGBA bytes. - Zero Transaction Drop Persistence Guarantee:
Store::modifyautomatically spawns an async task to executepush_asyncwith channel backpressure ifpushencounters queue saturation, ensuring zero data loss under spike write loads. - Token-Budgeted Query Projections: Decision queries (
query_decisions) supportlimitcaps and compactinclude_body: falseprojections for token budget optimization. - Serde Parameter & Enum Ergonomics: Action enums (
SnippetSearchMode,Relation) support case-insensitive variants and common synonyms (create/add,remove/delete,list/read,source/from,target/to,relationType/relation_type) ensuring seamless LLM tool execution. - Atomic Search Index Swaps:
MemoryState::rebuild_indexconstructs and populates a newMemoryIndexinstance in isolation before performing an atomic pointer swap (*self.search_index.write().await = new_idx), eliminating transient empty search result windows. - Non-Blocking Tantivy Search Queries:
MemoryIndex::searchqueries current index searcher snapshots without executing synchronous disk commit locks, preventing query stalls during heavy background indexing. - Zero-Allocation HashSet<&str> Snippet Deduplication:
indexer.rsutilizes borrowedHashSet<&str>name lookups during snippet batch modifications, eliminating heap string re-allocations inside the store write lock. - AST Recursion Depth Safeguard & Zero-Copy Borrowing: Tree-sitter AST traversal caps recursion depth at 100 to prevent thread stack overflows and borrows string slices (
&str) during AST node walking. - Strongly-Typed SearchResult & Pre-Allocated Search Vectors:
search.rsuses a strongly-typedSearchResultstruct with named fields and pre-allocates result vector capacity (Vec::with_capacity(top_docs.len())). - BFS Graph Traversal Pre-allocation & Visited Node Upper Bound:
GraphQueryBuilder::find_shortest_pathpre-allocates adjacency map capacity (HashMap::with_capacity(relations.len() * 2)) and enforces a visited node upper bound (10,000 max) to guarantee deterministic BFS runtime. - LLM Tool Schema Ergonomics & Context Guidance:
ReadGraphHandlerschema explicitly instructs LLMs onnamespacefiltering andsearch_nodes/get_subgraphtools for large graph discovery. - Filesystem Event Debouncing & Proactive State Refresh:
spawn_watcherimplements a sliding 250ms debouncing window per file path, ignores.git,target,.gemini, andnode_modules, and broadcasts activity events toMemoryState. - Buffered Line-by-Line AST Workspace Symbol Scanning:
scan_workspace_for_symbolreads workspace files viaBufReaderline streams instead of loading entire files into heap strings, preventing memory spikes when traversing source trees. - AST Node Type Aliasing & Skeleton Preallocation:
replace_ast_nodedocuments friendly node aliases (function,fn,method,struct,class,enum,trait,type), andread_file_skeletonpreallocates string buffer capacity (code.len() / 2). - Batch Vector Indexing & Similarity Score Guidance:
VectorDBprovidesindex_documents_batchfor single-request multi-point vector upserts and explicit score calibration notes (\ge 0.75high confidence match). - Compact JSON MCP Resources & UTF-8 Activity Truncation: MCP resources serialize using compact JSON (
to_string),TerminalHistoryResource/MilestonesResourceenforce output bounds, andformat_tool_activity_descriptionusesfloor_char_boundaryfor guaranteed UTF-8 safety. - SIMD-Friendly Single-Pass Cosine Similarity:
cosine_similaritycalculates dot product and Euclidean norm squares in a single linear pass over float vectors, enabling SIMD compiler auto-vectorization. - Safe Stream Decoding on Log Tails: Log tail operations (
process_logs, action: "get") read raw bytes and decode using lossy UTF-8 conversion (String::from_utf8_lossy) to ensure resilience when seeking across multi-byte UTF-8 boundaries. - Task Summary UTF-8 Truncation Safety:
taskstool (action = "list") truncates serialized task text strictly along UTF-8 character boundaries usingfloor_char_boundarywhen enforcingmax_tokens. - Sequential Snapshot Lock Scope Flattening:
GenerateStandupReportHandlerreadstasks,ledger, andsession_summariessequentially rather than nesting read locks, preventing multi-lock deadlocks during concurrent store modifications. - Directory Tree Depth Safeguard:
ReadDirectoryArchitectureHandlercaps directory recursion at depth 10 to prevent stack overflow on deep or cyclic directory structures. - Deterministic Total-Order Score Ranking:
OmniSearchHandlerusesf64::total_cmpfor Reciprocal Rank Fusion (RRF) score sorting, guaranteeing deterministic NaN-safe search result ordering. - RPC Timeout Memory Hygiene:
nvim-coremaintains request hygiene by removing pending request entries from static RPC maps upon timeout or channel drop, eliminating orphan memory leaks. - Embedding Input Safeguard:
generate_embedding_asyncreturns explicit errors for empty/0-length text inputs instead of returning empty vectors, preventing downstream vector dimension mismatches during cosine similarity calculations. - Path Traversal Security Guards:
validate_safe_pathenforces path canonicalization and rejects relative parent traversal components (..) across file and process log handlers (process_logs/ProcessLogsTool). - Watcher Map Memory Eviction: Proactive daemon file watcher in
watcher.rscapslast_processedmap size at 1,000 entries and purges entries older than 10 minutes to prevent monotonic memory leakage. - Comprehensive Serde Casing Aliases: All 11 consolidated tool action enums (TaskAction, MilestoneAction, SnippetAction, DecisionAction, TechDebtAction, EnvAction, ClipboardAction, HandoffMemoAction, HypothesisAction, AgentSignalAction, ProcessLogAction) include serde alias attributes supporting
snake_case,camelCase,PascalCase, and uppercase variants for maximum LLM casing resilience. - Two-Phase Graph Condensation:
condense_graph_workeruses a 2-phase commit (non-destructiveread_with-> graph insert -> prune by timestamp/content) to prevent data loss if summarization or graph insertion fails. - Store Write Lock Minimization:
Store::modifyandStore::modify_asyncunblock concurrent readers during JSON serialization by releasing the write lock immediately after mutating memory state. - Redb Database Lock Retry Backoff:
init_dbretries transient Redb lock contention with exponential backoff (3 attempts, 150ms delay) before falling back. - Offloaded Background Index Rebuilds:
MemoryState::rebuild_indexoffloads graph snapshot cloning and Tantivy document re-indexing intotokio::task::spawn_blockingto avoid stalling async event loops. - Broadcast Watch-Based Shutdown Channels: Background workers utilize
tokio::sync::watchfor broadcast shutdown notifications without consuming cancellation signals. - Consolidated Neovim Tool Suite (v2): The Neovim server exposes 7 consolidated domain tools (
nvim_buffer,nvim_window,nvim_view,nvim_diagnostics,nvim_visual,nvim_execute_lua,nvim_system) with comprehensive action dispatching. - Fallback Vector Search Parity: In-memory vector search fallback indexes Knowledge Graph entities, observations, and error fixes when external vector databases are unavailable.