Strip embeddings from MCP tool responses

Reduces response size by omitting embedding arrays from serialized output.
This commit is contained in:
2026-02-02 23:16:37 +01:00
parent d64a80281c
commit f273ab3dbd

View File

@@ -5,6 +5,10 @@ import { query, listNodes, getNode, findNodeByPrefix, addNode, addEdge } from '.
import { getConnections } from '../core/graph';
import { NodeKind, EdgeType } from '../types';
function serialize(data: any): string {
return JSON.stringify(data, (key, val) => key === 'embedding' ? undefined : val, 2);
}
const server = new McpServer({
name: 'memory',
version: '1.0.0',
@@ -20,7 +24,7 @@ server.tool(
},
async ({ text, kind, limit }) => {
const results = await query(text, { kind: kind as NodeKind, limit });
return { content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize(results) }] };
}
);
@@ -33,10 +37,10 @@ server.tool(
async ({ id }) => {
const node = getNode(id) ?? findNodeByPrefix(id);
if (!node) {
return { content: [{ type: 'text' as const, text: JSON.stringify({ error: 'Node not found' }) }], isError: true };
return { content: [{ type: 'text' as const, text: serialize({ error: 'Node not found' }) }], isError: true };
}
const connections = getConnections(node.id);
return { content: [{ type: 'text' as const, text: JSON.stringify({ node, connections }, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize({ node, connections }) }] };
}
);
@@ -51,7 +55,7 @@ server.tool(
},
async ({ kind, status, tags, limit }) => {
const nodes = listNodes({ kind: kind as NodeKind, status, tags, limit });
return { content: [{ type: 'text' as const, text: JSON.stringify(nodes, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize(nodes) }] };
}
);
@@ -65,14 +69,14 @@ server.tool(
async ({ id, kind }) => {
const node = getNode(id) ?? findNodeByPrefix(id);
if (!node) {
return { content: [{ type: 'text' as const, text: JSON.stringify({ error: 'Node not found' }) }], isError: true };
return { content: [{ type: 'text' as const, text: serialize({ error: 'Node not found' }) }], isError: true };
}
const { outgoing } = getConnections(node.id);
let children = outgoing.filter(e => e.type === 'contains').map(e => (e as any).node);
if (kind) {
children = children.filter((n: any) => n.kind === kind);
}
return { content: [{ type: 'text' as const, text: JSON.stringify(children, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize(children) }] };
}
);
@@ -91,7 +95,7 @@ server.tool(
const metadata: Record<string, any> = {};
if (sections) metadata.sections = sections;
const node = await addNode({ kind: kind as NodeKind, title, content, tags, status, metadata });
return { content: [{ type: 'text' as const, text: JSON.stringify(node, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize(node) }] };
}
);
@@ -105,7 +109,7 @@ server.tool(
},
async ({ fromId, toId, type }) => {
const edge = addEdge(fromId, toId, type as EdgeType);
return { content: [{ type: 'text' as const, text: JSON.stringify(edge, null, 2) }] };
return { content: [{ type: 'text' as const, text: serialize(edge) }] };
}
);