Repository navigation
feat(mcp): expose personal memory tools over MCP with policy gating, … - #1048
Conversation
…redaction, and signed receipts (#986)
…p_tools.py with ruff
| class MemoryStoreTool(BaseTool): | ||
| """Store a new personal memory item, gated by policy and recorded in the audit trail.""" | ||
|
|
||
| name = "memory_store" |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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, | ||
| ) | ||
| ) |
There was a problem hiding this comment.
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 | ||
|
|
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
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]>
|
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):
CHANGELOG updated in the merge commit. Nice work! |
Closes #986
Summary
Exposes personal memory capabilities to Model Context Protocol (MCP) clients (such as Claude Desktop or Cursor) via
KnowledgeMeshandMCPServer. Adds three new MCP tools tobuild_mesh_tools():memory_search: Searches indexed memory and returns PII-redacted hits audited asMEMORY_READ.memory_recall: Returns synthesized answers and ranked hits, redacted and audited asMEMORY_READ.memory_store: Gates content via policy, serializes UMP markdown documents intomemory_root, triggers incremental reindexing, and audits writes asMEMORY_WRITE.Key Design Details
MCPServerdynamically consumestarget.as_mcp_tools(), making all 7 tools available over standard MCP transports without modifying server code.GuardrailPolicyinstance.MEMORY_WRITEevent (blocked=True,actor=actor) on the audit tracer alongside the genericGUARDRAIL_VIOLATIONevent.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_rootmust be one ofmesh.config.expanded_roots(), raisingValueErrorat tool initialization if unconfigured.Known Limitations
docs/memory/mcp-server.md.Verification Status
tests/mcp/,tests/guardrails/,tests/audit/, andtests/ump/.MCPServer'slist_tools()andcall_tool()handlers directly in-process. Not yet verified against a live external MCP client process over stdio transport.