Skip to content

feat(mcp): expose personal memory tools over MCP with policy gating, … - #1048

Merged
AmitoVrito merged 6 commits into
SynapseKit:mainfrom
Premvkmishra:feat/memory-mcp-server-986
Sep 28, 2026
Merged

AmitoVrito merged 6 commits into
SynapseKit:mainfrom
Premvkmishra:feat/memory-mcp-server-986

Conversation

@Premvkmishra

Copy link
Copy Markdown
Contributor

Closes #986

Summary

Exposes personal memory capabilities to Model Context Protocol (MCP) clients (such as Claude Desktop or Cursor) via KnowledgeMesh and MCPServer. Adds three new MCP tools to build_mesh_tools():

  • memory_search: Searches indexed memory and returns PII-redacted hits audited as MEMORY_READ.
  • memory_recall: Returns synthesized answers and ranked hits, redacted and audited as MEMORY_READ.
  • memory_store: Gates content via policy, serializes UMP markdown documents into memory_root, triggers incremental reindexing, and audits writes as MEMORY_WRITE.

Key Design Details

  • Zero Server-Side Changes: MCPServer dynamically consumes target.as_mcp_tools(), making all 7 tools available over standard MCP transports without modifying server code.
  • Unified Policy & Audit Backbone: Policy gating and audit tracing flow through a shared GuardrailPolicy instance.
  • Actor-Attributed Receipts: Policy-blocked writes leave zero files on disk and emit an explicit, actor-attributed MEMORY_WRITE event (blocked=True, actor=actor) on the audit tracer alongside the generic GUARDRAIL_VIOLATION event.
  • Privacy-Preserving Redaction: Hit text and synthesized answers undergo PII redaction (email, SSN, credit cards) via PIIRedactor, while structural citation metadata (path, line_start, line_end, score) remains intact. Audit payloads record query and hit counts without raw body text.
  • Memory Root Validation: Enforces that memory_root must be one of mesh.config.expanded_roots(), raising ValueError at tool initialization if unconfigured.

Known Limitations

  • Local file paths are preserved without redaction during read operations to maintain citation utility. Documented in docs/memory/mcp-server.md.

Verification Status

  • Automated Tests: 325 test cases passing across tests/mcp/, tests/guardrails/, tests/audit/, and tests/ump/.
  • MCP Integration: Verified via unit tests driving MCPServer's list_tools() and call_tool() handlers directly in-process. Not yet verified against a live external MCP client process over stdio transport.

class MemoryStoreTool(BaseTool):
"""Store a new personal memory item, gated by policy and recorded in the audit trail."""

name = "memory_store"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Privacy gap: only text is redacted here. hit_to_dict also returns headings (user content) and metadata (raw embedding dict, may carry content previews) unredacted, so PII in a heading/metadata leaks to the client despite the stated redaction guarantee. Redact the full serialized hit, not just text.



def _new_id() -> str:
return uuid.uuid4().hex[:8]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memory_type from the client is unvalidated; the UMPType Literal isn't enforced at runtime, so an arbitrary value is persisted as invalid frontmatter. Validate against the allowed set and error on mismatch instead of # type: ignore.

{"answer": redacted_answer, "hits": redacted_hits},
indent=2,
)
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty/whitespace query makes mesh.query raise an uncaught ValueError (it rejects blank queries), crashing the tool call instead of returning a ToolResult error like the policy-block path. Guard for empty query. (also applies to memory_recall)

from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Project standard is no MagicMock/patch in tests (it silently accepts anything and hid a past async bug). Prefer a hand-written fake MCP server so the test exercises real registration.

mesh_roots = mesh.config.expanded_roots()
default_root = Path.home() / ".synapsekit" / "memory"
if default_root not in mesh_roots and mesh_roots:
memory_root = mesh_roots[0]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default fallback writes memories into mesh_roots[0], which defaults to Path.cwd(). MCPServer(mesh) over a code repo would write UMP .md files into that repo and reindex them, polluting the corpus. Prefer/create the dedicated memory dir instead of the first indexed root.

AmitoVrito and others added 4 commits September 28, 2026 18:33
memory_search and memory_recall only redacted hit.text, leaving
document headings and the raw metadata dict (which can carry content
previews) exposed to the MCP client despite the stated PII-redaction
guarantee. Route hits through a _redact_hit helper that redacts text,
headings, and metadata string values while preserving structural
citation fields (path, line numbers, score).

Co-authored-by: Premvkmishra <[email protected]>
The UMPType Literal is not enforced at runtime, so a client could pass
an arbitrary memory_type and have it persisted as invalid UMP
frontmatter. Reject values outside the allowed set with a ToolResult
error before writing anything, and drop the type: ignore in favour of a
checked cast.

Co-authored-by: Premvkmishra <[email protected]>
memory_search and memory_recall passed a blank query straight to
KnowledgeMesh.query, which raises ValueError for empty queries and
surfaced as an uncaught exception. Guard for an empty/whitespace query
and return a ToolResult error, matching the policy-block path.

Co-authored-by: Premvkmishra <[email protected]>
Project standard forbids MagicMock/patch in tests (they silently accept
anything and have masked real bugs). Swap the MagicMock-based server
double for small hand-written fakes of Server/Tool/TextContent/Resource
and manual sys.modules management, so the test exercises real handler
registration and dispatch.

Co-authored-by: Premvkmishra <[email protected]>
@AmitoVrito
AmitoVrito merged commit 5286111 into SynapseKit:main Sep 28, 2026
18 of 19 checks passed
@AmitoVrito

Copy link
Copy Markdown
Collaborator

Merged, thank you @Premvkmishra! 🎉 This is a great addition — personal memory over MCP with policy gating and signed audit receipts.

I reviewed the branch and pushed four small fixes before merging (all with regression tests):

  • Privacy: read redaction only covered hit.text, so section headings and the raw metadata dict (which can carry content previews) were leaking to the client. Now every free-text hit field is redacted while structural citation fields (path/line/score) are preserved.
  • Validation: memory_store accepted an arbitrary memory_type and persisted invalid UMP frontmatter. It's now rejected against the allowed set before any write.
  • Robustness: a blank query raised an uncaught ValueError from KnowledgeMesh.query; it now returns a clean tool error matching the policy-block path.
  • Tests: replaced the MagicMock-based server test with a hand-written fake (house standard is no MagicMock).

CHANGELOG updated in the merge commit. Nice work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory-as-MCP-server — expose personal memory to any MCP client

2 participants