-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat(mcp): add listMemories tool for enumerating stored memories #1183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abhay-codes07
wants to merge
1
commit into
supermemoryai:main
Choose a base branch
from
abhay-codes07:feat/mcp-list-memories-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { randomUUID } from "node:crypto" | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest" | ||
| import { | ||
| API_KEY, | ||
| callTool, | ||
| connect, | ||
| type Session, | ||
| sleep, | ||
| textOf, | ||
| } from "./helpers" | ||
|
|
||
| // listMemories reads extracted memory entries, which appear only after the | ||
| // async ingestion pipeline finishes — poll like recallUntil does. | ||
| async function listUntil( | ||
| s: Session, | ||
| needle: string, | ||
| { tries = 18, delayMs = 5000 } = {}, | ||
| ): Promise<string | null> { | ||
| for (let i = 0; i < tries; i++) { | ||
| // The marker document is the newest, so page 1 is enough. | ||
| const res = await callTool(s.client, "listMemories", { limit: 20 }) | ||
| const txt = textOf(res) | ||
| if (txt.includes(needle)) return txt | ||
| await sleep(delayMs) | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| describe.skipIf(!API_KEY)("MCP — listMemories", () => { | ||
| let s: Session | ||
| const created: string[] = [] | ||
|
|
||
| beforeAll(async () => { | ||
| s = await connect() | ||
| }) | ||
| afterAll(async () => { | ||
| for (const content of created) { | ||
| await callTool(s.client, "memory", { | ||
| content, | ||
| action: "forget", | ||
| }).catch(() => {}) | ||
| } | ||
| await s?.close() | ||
| }) | ||
|
|
||
| it("appears in tool discovery", async () => { | ||
| const tools = await s.client.listTools() | ||
| const names = tools.tools.map((t) => t.name) | ||
| expect(names).toContain("listMemories") | ||
| }) | ||
|
|
||
| it("lists a saved memory without dumping document content", async () => { | ||
| const marker = `lm-${randomUUID()}` | ||
| const content = `e2e listMemories. token=${marker}. The list test fruit is rambutan.` | ||
| created.push(content) | ||
|
|
||
| const save = await callTool(s.client, "memory", { content, action: "save" }) | ||
| expect(save.isError).toBeFalsy() | ||
|
|
||
| const listing = await listUntil(s, marker) | ||
| expect( | ||
| listing, | ||
| `listMemories never returned marker ${marker}`, | ||
| ).not.toBeNull() | ||
| // Header shape: "N memories across M documents (page X of Y, ...)" | ||
| expect(listing).toMatch(/memor(y|ies) across \d+ document/) | ||
| }, 120_000) | ||
|
|
||
| it("paginates with a bounded page size", async () => { | ||
| const res = await callTool(s.client, "listMemories", { page: 1, limit: 1 }) | ||
| expect(res.isError).toBeFalsy() | ||
| const txt = textOf(res) | ||
| // With the memory saved above there is at least one document. | ||
| expect(txt).toMatch(/page 1 of \d+/) | ||
| }, 30_000) | ||
|
|
||
| it("rejects an out-of-range limit", async () => { | ||
| const res = await callTool(s.client, "listMemories", { limit: 500 }) | ||
| // Zod schema caps limit at 50 — the SDK surfaces this as a tool error. | ||
| expect(res.isError).toBeTruthy() | ||
| }, 30_000) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| import { describe, expect, it } from "vitest" | ||
| import type { DocumentsApiResponse } from "./client" | ||
| import { formatMemoriesList } from "./format" | ||
|
|
||
| function makeResponse( | ||
| overrides: Partial<DocumentsApiResponse> = {}, | ||
| ): DocumentsApiResponse { | ||
| return { | ||
| documents: [], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 0, totalPages: 1 }, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| function makeEntry(memory: string, extra: Record<string, unknown> = {}) { | ||
| return { | ||
| id: `mem_${memory.slice(0, 8)}`, | ||
| memory, | ||
| spaceId: "space_1", | ||
| createdAt: "2026-06-10T12:00:00Z", | ||
| updatedAt: "2026-06-10T12:00:00Z", | ||
| ...extra, | ||
| } | ||
| } | ||
|
|
||
| describe("formatMemoriesList", () => { | ||
| it("reports an empty store", () => { | ||
| expect(formatMemoriesList(makeResponse())).toBe("No memories stored yet.") | ||
| }) | ||
|
|
||
| it("reports an out-of-range page distinctly from an empty store", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| pagination: { | ||
| currentPage: 3, | ||
| limit: 10, | ||
| totalItems: 12, | ||
| totalPages: 2, | ||
| }, | ||
| }), | ||
| ) | ||
| expect(result).toBe("No documents on page 3 (2 pages total).") | ||
| }) | ||
|
|
||
| it("groups memories under their source document with title, type, and date", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: "Preferences", | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [ | ||
| makeEntry("User prefers dark mode"), | ||
| makeEntry("User works in TypeScript"), | ||
| ], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 1, totalPages: 1 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain( | ||
| "2 memories across 1 document (page 1 of 1, 1 documents total), newest first.", | ||
| ) | ||
| expect(result).toContain('"Preferences" (text, 2026-06-12)') | ||
| expect(result).toContain("- User prefers dark mode") | ||
| expect(result).toContain("- User works in TypeScript") | ||
| expect(result).not.toContain("More available") | ||
| }) | ||
|
|
||
| it("excludes forgotten and superseded memory entries", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: "Facts", | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [ | ||
| makeEntry("Current fact"), | ||
| makeEntry("Forgotten fact", { isForgotten: true }), | ||
| makeEntry("Old version of a fact", { isLatest: false }), | ||
| ], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 1, totalPages: 1 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain("- Current fact") | ||
| expect(result).not.toContain("Forgotten fact") | ||
| expect(result).not.toContain("Old version of a fact") | ||
| expect(result).toContain("1 memory across 1 document") | ||
| }) | ||
|
|
||
| it("marks documents whose extraction has not produced memories yet", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: "Still processing", | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 1, totalPages: 1 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain( | ||
| '"Still processing" (text, 2026-06-12) — no extracted memories yet', | ||
| ) | ||
| }) | ||
|
|
||
| it("falls back to (untitled) for documents without a title", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: null, | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [makeEntry("Some fact")], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 1, totalPages: 1 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain('"(untitled)" (text, 2026-06-12)') | ||
| }) | ||
|
|
||
| it("flattens multi-line memories and truncates oversized ones", () => { | ||
| const longMemory = `start ${"x".repeat(600)}` | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: "Big", | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [ | ||
| makeEntry("line one\nline two\ttabbed"), | ||
| makeEntry(longMemory), | ||
| ], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 10, totalItems: 1, totalPages: 1 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain("- line one line two tabbed") | ||
| expect(result).toContain("… [truncated]") | ||
| const truncatedLine = result | ||
| .split("\n") | ||
| .find((line) => line.includes("[truncated]")) | ||
| expect(truncatedLine).toBeDefined() | ||
| expect((truncatedLine as string).length).toBeLessThan(600) | ||
| }) | ||
|
|
||
| it("points at the next page when more documents exist", () => { | ||
| const result = formatMemoriesList( | ||
| makeResponse({ | ||
| documents: [ | ||
| { | ||
| id: "doc_1", | ||
| title: "Page one doc", | ||
| type: "text", | ||
| createdAt: "2026-06-12T08:00:00Z", | ||
| updatedAt: "2026-06-12T08:00:00Z", | ||
| memoryEntries: [makeEntry("A fact")], | ||
| }, | ||
| ], | ||
| pagination: { currentPage: 1, limit: 1, totalItems: 3, totalPages: 3 }, | ||
| }), | ||
| ) | ||
|
|
||
| expect(result).toContain("page 1 of 3, 3 documents total") | ||
| expect(result).toContain("More available — call listMemories with page: 2.") | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatMemoriesListtruncates each individualentry.memory, but this loop still renders every activememoryEntriesitem for every document on the page. Since thelimitonly caps documents, a single document with hundreds or thousands of memories, or a full page of documents with many memories each, can produce unbounded MCP output and large Worker string allocations. Please add an overall response character cap and/or a per-document/page memory cap with a clear truncation message, such as asking the caller to request a narrower scope or later page.