Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/memory-create-relations-skipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@modelcontextprotocol/server-memory": patch
---

`create_relations` now reports the relations it skipped because they already exist (or repeat an earlier relation in the same call): the structured result lists them in a new optional `skipped` array, a second text item names them, and the tool description says so. Skipped relations are still not added again ([#5654](https://github.com/modelcontextprotocol/servers/issues/5654)).
3 changes: 2 additions & 1 deletion src/memory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ Example:
- `from` (string): Source entity name
- `to` (string): Target entity name
- `relationType` (string): Relationship type in active voice
- Skips duplicate relations
- Skips relations that already exist (and repeats of a relation earlier in the same call)
- Returns the created relations. When any were skipped, the structured result also lists them in `skipped`, and a second text item says which were skipped
- Fails if either the source or target entity doesn't exist

- **add_observations**
Expand Down
41 changes: 38 additions & 3 deletions src/memory/__tests__/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ const expectedTools = [
name: "create_relations",
title: "Create Relations",
description:
"Create multiple new relations between entities in the knowledge graph. Relations should be in active voice",
"Create multiple new relations between entities in the knowledge graph. Relations should be in active voice. A relation already in the graph, or that repeats an earlier relation in the same call, is skipped; the result lists the skipped relations in `skipped`.",
inputSchema: {
type: "object",
properties: {
Expand All @@ -178,7 +178,10 @@ const expectedTools = [
},
outputSchema: {
type: "object",
properties: { relations: { type: "array", items: relationItem(true) } },
properties: {
relations: { type: "array", items: relationItem(true) },
skipped: { type: "array", items: relationItem(true) },
},
required: ["relations"],
$schema: JSON_SCHEMA_2020_12,
additionalProperties: false,
Expand Down Expand Up @@ -653,20 +656,52 @@ describe("memory tools over the protocol", () => {
);
});

it("skips relations that already exist or repeat within the batch", async () => {
it("skips relations that already exist or repeat within the batch, and reports them (#5654)", async () => {
await call(client, "create_relations", { relations: [aliceKnowsBob] });
const result = await call(client, "create_relations", {
relations: [aliceKnowsBob, aliceWorksAtAcme, aliceWorksAtAcme],
});
expect(result.isError).toBeUndefined();
expect(result.structuredContent).toEqual({
relations: [aliceWorksAtAcme],
skipped: [aliceKnowsBob, aliceWorksAtAcme],
});
expect(result.content).toEqual([
{ type: "text", text: JSON.stringify([aliceWorksAtAcme], null, 2) },
{
type: "text",
text: `Skipped 2 relations that already exist: Alice -[${aliceKnowsBob.relationType}]-> Bob, Alice -[${aliceWorksAtAcme.relationType}]-> ${aliceWorksAtAcme.to}. They are already in the graph, so nothing was added for them.`,
},
]);
expect(await readGraph()).toEqual({
entities: [alice, bob, acme],
relations: [aliceKnowsBob, aliceWorksAtAcme],
});
});

it("reports a repeated call's relation as skipped rather than returning a bare empty list (#5654)", async () => {
await call(client, "create_relations", { relations: [aliceKnowsBob] });
const result = await call(client, "create_relations", {
relations: [aliceKnowsBob],
});
expect(result.isError).toBeUndefined();
expect(result.structuredContent).toEqual({
relations: [],
skipped: [aliceKnowsBob],
});
expect(result.content).toEqual([
{ type: "text", text: "[]" },
{
type: "text",
text: `Skipped 1 relation that already exists: Alice -[${aliceKnowsBob.relationType}]-> Bob. It is already in the graph, so nothing was added for it.`,
},
]);
expect(await readGraph()).toEqual({
entities: [alice, bob, acme],
relations: [aliceKnowsBob],
});
});

it("treats a different relation type between the same pair as new", async () => {
const aliceManagesBob = { ...aliceKnowsBob, relationType: "manages" };
await call(client, "create_relations", { relations: [aliceKnowsBob] });
Expand Down
5 changes: 4 additions & 1 deletion src/memory/__tests__/write-limits.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ describe("write limits", () => {
relations: [{ from: "A", to: "B", relationType: "knows" }],
});
expect(repeat.isError).toBeUndefined();
expect(repeat.structuredContent).toEqual({ relations: [] });
expect(repeat.structuredContent).toEqual({
relations: [],
skipped: [{ from: "A", to: "B", relationType: "knows" }],
});
});
});

Expand Down
48 changes: 43 additions & 5 deletions src/memory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ export function skippedEntitiesNotice(skipped: string[]): string {
);
}

// Requested relations that createRelations did not create: the relation
// already existed, or repeated an earlier one in the same batch (#5654).
// createRelations returns the requested relation objects themselves, so
// identity tells them apart, as it does for skippedEntityNames.
export function skippedRelations(
requested: Relation[],
created: Relation[],
): Relation[] {
return requested.filter((r) => !created.includes(r));
}

// Text telling the agent which relations create_relations skipped.
export function skippedRelationsNotice(skipped: Relation[]): string {
const one = skipped.length === 1;
const listed = skipped
.map((r) => `${r.from} -[${r.relationType}]-> ${r.to}`)
.join(", ");
return (
`Skipped ${skipped.length} ${one ? "relation that already exists" : "relations that already exist"}: ` +
`${listed}. ${one ? "It is" : "They are"} already in the graph, so nothing was added for ${one ? "it" : "them"}.`
);
}

// Opt-in ceilings on how large the write tools may grow the graph (#5525).
// Every mutation reads the whole graph file and writes it back under the
// cross-process lock, so its cost, and how long it holds the lock, grow
Expand Down Expand Up @@ -1646,7 +1669,7 @@ export function createServer(
{
title: "Create Relations",
description:
"Create multiple new relations between entities in the knowledge graph. Relations should be in active voice",
"Create multiple new relations between entities in the knowledge graph. Relations should be in active voice. A relation already in the graph, or that repeats an earlier relation in the same call, is skipped; the result lists the skipped relations in `skipped`.",
inputSchema: z.object({
relations: z
.array(RelationSchema)
Expand All @@ -1656,6 +1679,8 @@ export function createServer(
}),
outputSchema: z.object({
relations: z.array(RelationSchema),
// Present only when at least one requested relation was skipped.
skipped: z.array(RelationSchema).optional(),
}),
annotations: {
readOnlyHint: false,
Expand All @@ -1667,11 +1692,24 @@ export function createServer(
async ({ relations }) => {
const result = await knowledgeGraphManager.createRelations(relations);
notifyGraphUpdated();
// Existing relations are ignored. Say so, as create_entities does:
// otherwise an empty result reads as nothing having happened (#5654).
const skipped = skippedRelations(relations, result);
const content = [
{ type: "text" as const, text: JSON.stringify(result, null, 2) },
];
if (skipped.length > 0) {
content.push({
type: "text" as const,
text: skippedRelationsNotice(skipped),
});
}
return {
content: [
{ type: "text" as const, text: JSON.stringify(result, null, 2) },
],
structuredContent: { relations: result },
content,
structuredContent:
skipped.length > 0
? { relations: result, skipped }
: { relations: result },
};
},
);
Expand Down