persyst-mcp 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools.js CHANGED
@@ -39,7 +39,8 @@ import {
39
39
  getAnyMemoryById,
40
40
  searchVector,
41
41
  getMemoryById,
42
- getActiveMemoryCount
42
+ getActiveMemoryCount,
43
+ getNamespaceStats
43
44
  } from './database.js';
44
45
  import { searchHybrid, getOptimizedContext, consolidateMemories } from './search.js';
45
46
  import { getRecentCommits } from './git.js';
@@ -117,14 +118,15 @@ export function registerTools(server) {
117
118
  // 1. ADD MEMORY
118
119
  server.tool(
119
120
  'add_memory',
120
- 'Store a new memory. It will be searchable by both keywords and meaning.',
121
+ 'Store a new memory. It will be searchable by both keywords and meaning. Use shared=true to make it visible to all agents.',
121
122
  {
122
123
  content: z.string().describe('The memory content to store'),
123
124
  importance: z.number().min(0).max(1).default(1.0).describe('Importance score from 0 (low) to 1 (high)'),
124
- agent_id: z.string().optional().describe('Agent ID for provenance tracking'),
125
- session_id: z.string().optional().describe('Session ID')
125
+ agent_id: z.string().optional().describe('Agent ID for provenance tracking and namespace isolation'),
126
+ session_id: z.string().optional().describe('Session ID'),
127
+ shared: z.boolean().default(true).describe('If true, memory is visible to all agents. If false, only visible to this agent.')
126
128
  },
127
- async ({ content, importance, agent_id, session_id }) => {
129
+ async ({ content, importance, agent_id, session_id, shared }) => {
128
130
  try {
129
131
  // Bug 7 + Feature 4: Validate content size
130
132
  const validation = validateMemoryContent(content);
@@ -132,13 +134,17 @@ export function registerTools(server) {
132
134
  return text({ error: validation.error });
133
135
  }
134
136
 
135
- // Deduplication check
136
- const existing = getMemoryByContent(content);
137
+ // Derive namespace from agent_id and shared flag
138
+ const namespace = (shared || !agent_id) ? 'shared' : agent_id;
139
+
140
+ // Deduplication check (namespace-aware)
141
+ const existing = getMemoryByContent(content, namespace);
137
142
  if (existing) {
138
143
  boostMemory(existing.id);
139
144
  return text({
140
145
  success: true,
141
146
  id: existing.id,
147
+ namespace,
142
148
  message: `Memory #${existing.id} already exists. Boosted importance.`
143
149
  });
144
150
  }
@@ -147,7 +153,7 @@ export function registerTools(server) {
147
153
  source_type: agent_id ? 'agent' : 'manual',
148
154
  source_id: agent_id || null,
149
155
  confidence: 1.0
150
- });
156
+ }, namespace);
151
157
 
152
158
  const embedding = await generateEmbedding(content);
153
159
  insertVector(id, embedding);
@@ -165,7 +171,7 @@ export function registerTools(server) {
165
171
 
166
172
  const sim = Math.max(0, 1 - (hit.distance * hit.distance) / 2);
167
173
  if (sim > 0.75) {
168
- const existingMemory = getMemoryById(hitId);
174
+ const existingMemory = getMemoryById(hitId, namespace);
169
175
  if (!existingMemory) continue;
170
176
 
171
177
  // Check if content is substantially different (Jaccard distance > 0.5)
@@ -187,7 +193,7 @@ export function registerTools(server) {
187
193
  console.error(`[persyst] Contradiction detection error: ${e.message}`);
188
194
  }
189
195
 
190
- const result = { success: true, id, message: `Memory #${id} stored` };
196
+ const result = { success: true, id, namespace, message: `Memory #${id} stored` };
191
197
  if (contradictions.length > 0) {
192
198
  result.contradictions_detected = contradictions;
193
199
  result.message += `. Detected ${contradictions.length} contradiction(s) — older memories archived.`;
@@ -203,19 +209,22 @@ export function registerTools(server) {
203
209
  // 2. SEARCH MEMORIES
204
210
  server.tool(
205
211
  'search_memories',
206
- 'Search memories using hybrid keyword + semantic search with cryptographic attestation.',
212
+ 'Search memories using hybrid keyword + semantic search with cryptographic attestation. Results are filtered by agent namespace.',
207
213
  {
208
214
  query: z.string().describe('What to search for'),
209
215
  limit: z.number().default(5).describe('Max results (default: 5)'),
210
- agent_id: z.string().optional().describe('Agent ID calling this search'),
216
+ agent_id: z.string().optional().describe('Agent ID filters results to this agent\'s namespace + shared'),
211
217
  session_id: z.string().optional().describe('Session ID')
212
218
  },
213
219
  async ({ query, limit, agent_id, session_id }) => {
214
220
  try {
215
- const results = await searchHybrid(query, limit, agent_id, session_id);
221
+ // Derive namespace from agent_id (null = search all)
222
+ const namespace = agent_id || null;
223
+ const results = await searchHybrid(query, limit, agent_id, session_id, namespace);
216
224
  return text({
217
225
  results,
218
226
  count: results.length,
227
+ namespace: namespace || 'all',
219
228
  attestation: results.attestation
220
229
  });
221
230
  } catch (err) {
@@ -314,14 +323,16 @@ export function registerTools(server) {
314
323
  // 6. GET RECENT MEMORIES
315
324
  server.tool(
316
325
  'get_recent_memories',
317
- 'Get the most recently created memories, newest first.',
326
+ 'Get the most recently created memories, newest first. Filtered by agent namespace if agent_id is provided.',
318
327
  {
319
- limit: z.number().default(10).describe('How many to return (default: 10)')
328
+ limit: z.number().default(10).describe('How many to return (default: 10)'),
329
+ agent_id: z.string().optional().describe('Agent ID — filters to this agent\'s namespace + shared')
320
330
  },
321
- async ({ limit }) => {
331
+ async ({ limit, agent_id }) => {
322
332
  try {
323
- const memories = getRecentMemories(limit);
324
- return text({ memories, count: memories.length });
333
+ const namespace = agent_id || null;
334
+ const memories = getRecentMemories(limit, namespace);
335
+ return text({ memories, count: memories.length, namespace: namespace || 'all' });
325
336
  } catch (err) {
326
337
  return text({ error: err.message });
327
338
  }
@@ -331,14 +342,16 @@ export function registerTools(server) {
331
342
  // 7. GET IMPORTANT MEMORIES
332
343
  server.tool(
333
344
  'get_important_memories',
334
- 'Get memories ranked by importance score, highest first.',
345
+ 'Get memories ranked by importance score, highest first. Filtered by agent namespace if agent_id is provided.',
335
346
  {
336
- limit: z.number().default(10).describe('How many to return (default: 10)')
347
+ limit: z.number().default(10).describe('How many to return (default: 10)'),
348
+ agent_id: z.string().optional().describe('Agent ID — filters to this agent\'s namespace + shared')
337
349
  },
338
- async ({ limit }) => {
350
+ async ({ limit, agent_id }) => {
339
351
  try {
340
- const memories = getImportantMemories(limit);
341
- return text({ memories, count: memories.length });
352
+ const namespace = agent_id || null;
353
+ const memories = getImportantMemories(limit, namespace);
354
+ return text({ memories, count: memories.length, namespace: namespace || 'all' });
342
355
  } catch (err) {
343
356
  return text({ error: err.message });
344
357
  }
@@ -634,16 +647,17 @@ export function registerTools(server) {
634
647
  // 18. GET OPTIMIZED CONTEXT
635
648
  server.tool(
636
649
  'get_optimized_context',
637
- 'Compile a condensed context prompt within a token budget by hopping the knowledge graph and ranking by temporal decay + agent reputation.',
650
+ 'Compile a condensed context prompt within a token budget by hopping the knowledge graph and ranking by temporal decay + agent reputation. Results filtered by agent namespace.',
638
651
  {
639
652
  query: z.string().describe('The search query context'),
640
653
  max_tokens: z.number().default(4000).describe('Token budget for LLM context compression (default: 4000)'),
641
- agent_id: z.string().optional().describe('Agent ID requesting context'),
654
+ agent_id: z.string().optional().describe('Agent ID requesting context — filters to this agent\'s namespace + shared'),
642
655
  session_id: z.string().optional().describe('Session ID')
643
656
  },
644
657
  async ({ query, max_tokens, agent_id, session_id }) => {
645
658
  try {
646
- const contextData = await getOptimizedContext(query, max_tokens, agent_id, session_id);
659
+ const namespace = agent_id || null;
660
+ const contextData = await getOptimizedContext(query, max_tokens, agent_id, session_id, namespace);
647
661
  return text(contextData);
648
662
  } catch (err) {
649
663
  return text({ error: err.message });