collective-memory-mcp 0.3.2 → 0.5.0

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.
Files changed (3) hide show
  1. package/README.md +20 -2
  2. package/package.json +1 -1
  3. package/src/server.js +333 -20
package/README.md CHANGED
@@ -34,6 +34,24 @@ Add to your Claude Desktop MCP configuration (`~/.config/Claude/claude_desktop_c
34
34
 
35
35
  The `-y` flag suppresses the npx prompt and auto-installs the latest version.
36
36
 
37
+ ## System Prompt (Recommended)
38
+
39
+ Add this to your Claude system prompt to ensure agents know about the Collective Memory:
40
+
41
+ ```
42
+ You have access to a Collective Memory MCP Server that stores knowledge from previous tasks.
43
+
44
+ BEFORE starting work, search for similar past tasks using:
45
+ - search_collective_memory
46
+ - find_similar_procedures
47
+
48
+ AFTER completing any task, document it using:
49
+ - record_task_completion
50
+
51
+ When writing observations, be SPECIFIC and include facts like file paths, versions,
52
+ metrics, and error messages. Avoid vague statements like "works well" or "fixed bugs".
53
+ ```
54
+
37
55
  ## Entity Types
38
56
 
39
57
  | Type | Description |
@@ -118,10 +136,10 @@ const result = await session.callTool("find_similar_procedures", {
118
136
 
119
137
  ## Database
120
138
 
121
- The server uses SQLite for persistence (via `better-sqlite3`). The database is stored at:
139
+ The server uses JSON file storage for persistence. Data is stored at:
122
140
 
123
141
  ```
124
- ~/.collective-memory/memory.db
142
+ ~/.collective-memory/memory.json
125
143
  ```
126
144
 
127
145
  ## Requirements
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "collective-memory-mcp",
3
- "version": "0.3.2",
3
+ "version": "0.5.0",
4
4
  "description": "A persistent, graph-based memory system for AI agents (MCP Server)",
5
5
  "type": "module",
6
6
  "main": "src/server.js",
package/src/server.js CHANGED
@@ -9,6 +9,8 @@ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"
9
9
  import {
10
10
  CallToolRequestSchema,
11
11
  ListToolsRequestSchema,
12
+ ListPromptsRequestSchema,
13
+ GetPromptRequestSchema,
12
14
  } from "@modelcontextprotocol/sdk/types.js";
13
15
  import { getStorage } from "./storage.js";
14
16
  import { Entity, Relation, ENTITY_TYPES, RELATION_TYPES } from "./models.js";
@@ -27,6 +29,7 @@ function createServer() {
27
29
  {
28
30
  capabilities: {
29
31
  tools: {},
32
+ prompts: {},
30
33
  },
31
34
  }
32
35
  );
@@ -227,15 +230,16 @@ function createServer() {
227
230
  {
228
231
  name: "search_collective_memory",
229
232
  description:
230
- "Search for relevant past work based on a natural language query. " +
231
- "Search scope includes entity names, entity types, all observation content, and relation types. " +
232
- "Returns entities with their immediate relations, ordered by relevance.",
233
+ "**Search all past work** - Use before starting a task to learn from previous solutions. " +
234
+ "Searches entity names, types, and all observation content. " +
235
+ "Returns matching entities with their relations. " +
236
+ "Use find_similar_procedures for more detailed results with artifacts.",
233
237
  inputSchema: {
234
238
  type: "object",
235
239
  properties: {
236
240
  query: {
237
241
  type: "string",
238
- description: "Natural language search query",
242
+ description: "What are you looking for? (e.g., 'authentication', 'CORS fix', 'database')",
239
243
  },
240
244
  },
241
245
  required: ["query"],
@@ -262,33 +266,48 @@ function createServer() {
262
266
  {
263
267
  name: "record_task_completion",
264
268
  description:
265
- "[Primary tool for agents] Document a completed task with full context. " +
266
- "Automatically creates the task entity, agent entity if needed, all artifact entities, " +
267
- "and establishes all relevant relations (executed_by, created, modified, part_of). " +
268
- "Returns the complete task node with all relations.",
269
+ "**PRIMARY TOOL - Use this after completing any task** Document completed work with full context. " +
270
+ "Automatically creates task entity, agent entity, artifacts, and relations. " +
271
+ "" +
272
+ "**GUIDELINES FOR observations:** " +
273
+ "- Be SPECIFIC: 'Added JWT with 1-hour expiry' not 'Added auth' " +
274
+ "- Include FACTS: file paths, versions, metrics, error messages " +
275
+ "- Be ATOMIC: One fact per observation " +
276
+ "- BAD: 'Works well', 'Fixed bugs', 'Good code' " +
277
+ "- GOOD: 'API response time reduced from 500ms to 120ms', 'Fixed CORS by adding Origin header' " +
278
+ "" +
279
+ "**Parameters:** " +
280
+ "- agent_name: Your identifier (e.g., 'Agent_Backend_Developer') " +
281
+ "- task_name: Unique descriptive name (e.g., 'Task_Add_JWT_Auth_20241224') " +
282
+ "- task_type: Type (implementation/debugging/refactoring/testing) " +
283
+ "- description: High-level summary " +
284
+ "- observations: Array of specific facts (see guidelines above) " +
285
+ "- created_artifacts: Files/code created with their observations " +
286
+ "- modified_structures: Architectural changes " +
287
+ "- session_id: Optional - groups related tasks together",
269
288
  inputSchema: {
270
289
  type: "object",
271
290
  properties: {
272
291
  agent_name: {
273
292
  type: "string",
274
- description: "Name of the agent completing the task",
293
+ description: "Your identifier (e.g., 'Agent_Backend_Developer')",
275
294
  },
276
295
  task_name: {
277
296
  type: "string",
278
- description: "Unique name for the task",
297
+ description: "Unique descriptive name (e.g., 'Task_Add_JWT_Auth_20241224')",
279
298
  },
280
299
  task_type: {
281
300
  type: "string",
282
- description: "Type of task (e.g., implementation, debugging, refactoring)",
301
+ description: "Type: implementation, debugging, refactoring, testing, etc.",
283
302
  },
284
303
  description: {
285
304
  type: "string",
286
- description: "High-level description of what was accomplished",
305
+ description: "High-level summary of what was accomplished",
287
306
  },
288
307
  observations: {
289
308
  type: "array",
290
309
  items: { type: "string" },
291
- description: "Detailed observations about the task execution",
310
+ description: "Specific facts about the work. Be atomic and detailed. Include paths, versions, metrics.",
292
311
  },
293
312
  created_artifacts: {
294
313
  type: "array",
@@ -303,7 +322,7 @@ function createServer() {
303
322
  },
304
323
  required: ["name"],
305
324
  },
306
- description: "Artifacts created during the task",
325
+ description: "Files, code, or configs you created",
307
326
  },
308
327
  modified_structures: {
309
328
  type: "array",
@@ -318,11 +337,11 @@ function createServer() {
318
337
  },
319
338
  required: ["name"],
320
339
  },
321
- description: "Structures modified during the task",
340
+ description: "Architectural patterns or structures that changed",
322
341
  },
323
342
  session_id: {
324
343
  type: "string",
325
- description: "Optional session identifier",
344
+ description: "Optional session identifier to group related tasks",
326
345
  },
327
346
  },
328
347
  required: ["agent_name", "task_name"],
@@ -331,15 +350,16 @@ function createServer() {
331
350
  {
332
351
  name: "find_similar_procedures",
333
352
  description:
334
- "Search for tasks that match a procedural query and return their full implementation details. " +
335
- "Searches task entities and observations. Returns complete subgraphs (tasks + related artifacts + structures). " +
336
- "Sorted by relevance and recency.",
353
+ "**Use BEFORE starting work** - Find how similar tasks were solved previously. " +
354
+ "Returns complete implementation details including artifacts and structures. " +
355
+ "Learn from past solutions before implementing new features. " +
356
+ "Query examples: 'authentication', 'database migration', 'API design', 'error handling'.",
337
357
  inputSchema: {
338
358
  type: "object",
339
359
  properties: {
340
360
  query: {
341
361
  type: "string",
342
- description: "Procedural search query (e.g., 'authentication implementation')",
362
+ description: "What are you trying to do? (e.g., 'authentication implementation', 'database migration')",
343
363
  },
344
364
  },
345
365
  required: ["query"],
@@ -349,6 +369,299 @@ function createServer() {
349
369
  };
350
370
  });
351
371
 
372
+ // ========== Prompts ==========
373
+
374
+ /**
375
+ * Available prompts that guide agents on using the collective memory system
376
+ */
377
+ const PROMPTS = [
378
+ {
379
+ name: "task-documentation-guide",
380
+ description: "Guide for documenting completed tasks in the collective memory",
381
+ arguments: [],
382
+ },
383
+ {
384
+ name: "search-workflow-guide",
385
+ description: "Guide for searching and learning from past work",
386
+ arguments: [],
387
+ },
388
+ {
389
+ name: "getting-started",
390
+ description: "Introduction to the Collective Memory System for agents",
391
+ arguments: [],
392
+ },
393
+ {
394
+ name: "observation-best-practices",
395
+ description: "Best practices for writing effective observations",
396
+ arguments: [],
397
+ },
398
+ ];
399
+
400
+ /**
401
+ * List available prompts
402
+ */
403
+ server.setRequestHandler(ListPromptsRequestSchema, async () => {
404
+ return { prompts: PROMPTS };
405
+ });
406
+
407
+ /**
408
+ * Get a specific prompt
409
+ */
410
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
411
+ const { name, arguments: args } = request.params;
412
+
413
+ const prompts = {
414
+ "task-documentation-guide": {
415
+ messages: [
416
+ {
417
+ role: "user",
418
+ content: {
419
+ type: "text",
420
+ text: `# Collective Memory: Task Documentation Guide
421
+
422
+ When you complete a task, document it using the \`record_task_completion\` tool.
423
+
424
+ ## Required Parameters:
425
+ - \`agent_name\`: Your identifier (e.g., "Agent_Backend_Developer")
426
+ - \`task_name\`: Unique, descriptive name (e.g., "Task_Add_JWT_Auth_20241224")
427
+
428
+ ## Optional but Recommended:
429
+ - \`task_type\`: Type of work (implementation, debugging, refactoring, testing, etc.)
430
+ - \`description\`: High-level summary of what was accomplished
431
+ - \`observations\`: Array of specific, atomic facts about the work
432
+ - \`created_artifacts\`: Files, code, configs created
433
+ - \`modified_structures\`: Architectural patterns or structures changed
434
+ - \`session_id\`: Session identifier to group related tasks
435
+
436
+ ## Writing Good Observations:
437
+
438
+ ✅ **Good observations** (specific, actionable):
439
+ - "API uses JWT tokens with 1-hour expiry"
440
+ - "Database schema includes composite index on (user_id, created_at)"
441
+ - "Deployment succeeded after adding 2GB RAM to container"
442
+ - "Error handling uses custom exception hierarchy from exceptions.py"
443
+
444
+ ❌ **Poor observations** (vague, subjective):
445
+ - "API works well"
446
+ - "Database is fast"
447
+ - "Fixed some bugs"
448
+ - "Good code structure"
449
+
450
+ ## Example:
451
+
452
+ \`\`\`json
453
+ {
454
+ "agent_name": "Agent_Backend_Developer",
455
+ "task_name": "Task_Implement_Pagination_20241224",
456
+ "task_type": "implementation",
457
+ "description": "Added cursor-based pagination to all list endpoints",
458
+ "observations": [
459
+ "Used keyset pagination pattern for better performance",
460
+ "Cursor encodes last_seen_id and last_seen_timestamp",
461
+ "Default page size: 50 items, max: 200",
462
+ "Backward compatibility maintained with offset-based fallback"
463
+ ],
464
+ "created_artifacts": [
465
+ {
466
+ "name": "Artifact_Pagination_Middleware",
467
+ "observations": ["Located at /src/middleware/pagination.js", "Handles cursor encoding/decoding"]
468
+ }
469
+ ]
470
+ }
471
+ \`\`\``,
472
+ },
473
+ },
474
+ ],
475
+ },
476
+
477
+ "search-workflow-guide": {
478
+ messages: [
479
+ {
480
+ role: "user",
481
+ content: {
482
+ type: "text",
483
+ text: `# Collective Memory: Search Workflow Guide
484
+
485
+ Before starting a task, search the collective memory to learn from past work.
486
+
487
+ ## When to Search:
488
+ 1. **Before implementing** a feature - check if similar work was done
489
+ 2. **When debugging** - find how similar issues were resolved
490
+ 3. **When refactoring** - understand existing architectural patterns
491
+ 4. **When choosing libraries** - see what's been used successfully
492
+
493
+ ## Search Tools:
494
+
495
+ ### \`search_collective_memory\`
496
+ Natural language search across all entities and observations.
497
+ \`\`\`json
498
+ { "query": "authentication implementation" }
499
+ \`\`\`
500
+
501
+ ### \`find_similar_procedures\`
502
+ Returns complete implementation details including artifacts and structures.
503
+ \`\`\`json
504
+ { "query": "database migration" }
505
+ \`\`\`
506
+
507
+ ### \`open_nodes\`
508
+ Retrieve specific entities by exact name.
509
+ \`\`\`json
510
+ { "names": ["Task_JWT_Auth", "Artifact_Auth_Middleware"] }
511
+ \`\`\`
512
+
513
+ ### \`read_graph\`
514
+ Export the entire knowledge graph for analysis.
515
+
516
+ ## Search Strategy:
517
+ 1. Start with broad natural language queries
518
+ 2. Use \`find_similar_procedures\` to get full context
519
+ 3. Examine related entities (artifacts, structures)
520
+ 4. Look for patterns in observations (errors, solutions, decisions)
521
+
522
+ ## Example Workflow:
523
+ \`\`\`
524
+ 1. search_collective_memory: "user authentication"
525
+ 2. find_similar_procedures: "JWT implementation"
526
+ 3. Review returned artifacts and structures
527
+ 4. Adapt patterns to current task
528
+ 5. Document new task with record_task_completion
529
+ \`\`\``,
530
+ },
531
+ },
532
+ ],
533
+ },
534
+
535
+ "getting-started": {
536
+ messages: [
537
+ {
538
+ role: "user",
539
+ content: {
540
+ type: "text",
541
+ text: `# Collective Memory System - Quick Start
542
+
543
+ You are connected to the Collective Memory System, a persistent knowledge graph that helps AI agents learn from each other's work.
544
+
545
+ ## Key Concepts:
546
+
547
+ **Entities** are nodes in the knowledge graph:
548
+ - **agent**: An AI agent (you or other agents)
549
+ - **task**: A unit of completed work
550
+ - **artifact**: Concrete output (file, code, config)
551
+ - **structure**: Architectural pattern or decision
552
+ - **session**: Groups related tasks
553
+
554
+ **Relations** connect entities:
555
+ - executed_by, created, modified, documented, depends_on, part_of, similar_to, uses, implements
556
+
557
+ ## Your Workflow:
558
+
559
+ 1. **Before starting** → Search for similar past work
560
+ - Use \`search_collective_memory\` or \`find_similar_procedures\`
561
+
562
+ 2. **While working** → Learn from patterns
563
+ - Review how similar tasks were solved
564
+ - Examine artifacts and structures
565
+
566
+ 3. **After completing** → Document your work
567
+ - Use \`record_task_completion\` with specific observations
568
+
569
+ ## Available Tools:
570
+ - \`record_task_completion\` - Document completed work (PRIMARY)
571
+ - \`search_collective_memory\` - Natural language search
572
+ - \`find_similar_procedures\` - Find similar tasks with full context
573
+ - \`read_graph\` - Export entire knowledge graph
574
+ - \`create_entities\` - Create new entities
575
+ - \`create_relations\` - Connect entities
576
+ - \`open_nodes\` - Retrieve specific entities
577
+
578
+ ## Best Practices:
579
+ - Write specific, atomic observations
580
+ - Include file paths, versions, metrics
581
+ - Document both successes and failures
582
+ - Link related artifacts and structures
583
+
584
+ The system persists all data in ~/.collective-memory/memory.json`,
585
+ },
586
+ },
587
+ ],
588
+ },
589
+
590
+ "observation-best-practices": {
591
+ messages: [
592
+ {
593
+ role: "user",
594
+ content: {
595
+ type: "text",
596
+ text: `# Collective Memory: Observation Best Practices
597
+
598
+ Observations are the heart of the collective memory. Write them well to help future agents.
599
+
600
+ ## Characteristics of Good Observations:
601
+
602
+ 1. **Atomic** - One fact per observation
603
+ 2. **Specific** - Include concrete details (versions, paths, metrics)
604
+ 3. **Actionable** - Provide enough context to replicate or understand
605
+ 4. **Time-bound** - Include timestamps or relative time when relevant
606
+
607
+ ## Examples by Category:
608
+
609
+ ### Code Changes:
610
+ ✅ "Added JWT validation middleware at /src/auth/jwt.js"
611
+ ✅ "Refactored user service to use dependency injection pattern"
612
+ ❌ "Improved code quality"
613
+
614
+ ### Configuration:
615
+ ✅ "Set JWT expiry to 3600 seconds (1 hour)"
616
+ ✅ "Enabled CORS for https://example.com in production config"
617
+ ❌ "Configured settings"
618
+
619
+ ### Errors & Fixes:
620
+ ✅ "Fixed CORS error by adding Origin header to allowed list"
621
+ ✅ "Resolved memory leak by properly closing database connections"
622
+ ❌ "Fixed bugs"
623
+
624
+ ### Performance:
625
+ ✅ "Reduced API response time from 500ms to 120ms by adding Redis cache"
626
+ ✅ "Optimized database query by adding composite index on (user_id, created_at)"
627
+ ❌ "Made it faster"
628
+
629
+ ### Decisions:
630
+ ✅ "Chose PostgreSQL over MySQL for JSONB support"
631
+ ✅ "Used WebSockets instead of polling for real-time updates"
632
+ ❌ "Good technical choices"
633
+
634
+ ### Dependencies:
635
+ ✅ "Upgraded lodash from 4.17.15 to 4.17.21 for security fix"
636
+ ✅ "Added bcrypt@5.1.1 for password hashing (cost factor: 12)"
637
+ ❌ "Updated packages"
638
+
639
+ ## Template for Complex Tasks:
640
+
641
+ For each significant task, include observations about:
642
+ 1. **What was changed** - Specific files, functions, configs
643
+ 2. **Why it was done** - Reasoning behind decisions
644
+ 3. **How it works** - Key implementation details
645
+ 4. **Edge cases handled** - Special conditions addressed
646
+ 5. **Testing done** - How it was verified
647
+ 6. **Known limitations** - Any remaining issues
648
+
649
+ ## Remember:
650
+ Future agents will read your observations to learn. Write for them, not for yourself.`,
651
+ },
652
+ },
653
+ ],
654
+ },
655
+ };
656
+
657
+ const prompt = prompts[name];
658
+ if (!prompt) {
659
+ throw new Error(`Unknown prompt: ${name}`);
660
+ }
661
+
662
+ return prompt;
663
+ });
664
+
352
665
  /**
353
666
  * Handle tool calls
354
667
  */