claude-flow icon indicating copy to clipboard operation
claude-flow copied to clipboard

Fix/mcp reasoningbank integration

Open dshep opened this issue 2 months ago โ€ข 5 comments

๐Ÿ”— Unify MCP and CLI Memory Storage with ReasoningBank Integration

Fixes #812

Summary

MCP memory tools (memory_usage, memory_search) and CLI memory commands previously used separate storage systems, preventing MCP users from accessing ReasoningBank's semantic search capabilities. This PR unifies both interfaces to share the same storage backend with automatic ReasoningBank detection.

Problem

Before this fix:

  • โŒ CLI commands used ReasoningBank (.swarm/memory.db) with semantic search
  • โŒ MCP tools used separate database with no semantic search
  • โŒ Data stored via CLI was invisible to MCP (and vice versa)
  • โŒ memory_search MCP tool returned success but no actual results
  • โŒ MCP users forced to use CLI via Bash workarounds

Example of broken behavior:

# Store via CLI - goes to ReasoningBank
$ claude-flow memory store "auth-pattern" "Use JWT with RS256"
โœ… Stored successfully in ReasoningBank

# Try to retrieve via MCP - different database!
await mcp__claude_flow__memory_usage({
  action: "retrieve",
  key: "auth-pattern"
});
// โŒ Returns: { found: false, value: null }

Solution: UnifiedMemoryManager

Created a unified memory layer that both CLI and MCP use:

Priority-based Storage System:
1. ๐Ÿง  ReasoningBank (.swarm/memory.db) - AI-powered semantic search
2. ๐Ÿ—„๏ธ SQLite (unified-memory.db) - Fast SQL queries
3. ๐Ÿ“„ JSON (memory-store.json) - Always-available fallback

Features:
- โœ… Auto-Detection: Checks for .swarm/memory.db existence
- โœ… Lazy Loading: Imports ReasoningBank adapter only when needed
- โœ… Graceful Fallback: Falls back to JSON if ReasoningBank unavailable
- โœ… Semantic Search: Query by meaning, not keywords
- โœ… Similarity Scores: Ranked results (0-100%)

Changes

1. Core Integration (src/memory/unified-memory-manager.js)

- Added ReasoningBank detection and integration
- Implemented priority-based storage selection
- Enhanced methods: store(), query(), get(), getStats()

2. MCP Server (src/mcp/mcp-server.js)

- Replaced fallback-store with UnifiedMemoryManager
- Added memory_search case statement (was missing!)
- Enhanced responses with storage_type and semantic_search metadata


Test Results โœ…

CLI โ†’ MCP Interoperability (Semantic Search):
# Store via CLI
$ claude-flow memory store "authentication-best-practices" \
    "Always use environment variables for API keys..." \
    --namespace security
โœ… Stored in ReasoningBank

# Query via MCP with DIFFERENT words
await memoryManager.query("How should I store API credentials?");

# Result: Found with 31.0% similarity! โœจ
{
  key: "authentication-best-practices",
  score: 0.310,
  confidence: 0.80,
  mode: "reasoningbank"
}

Semantic matching works:
- Query: "How should I store API credentials?"
- Found: "Always use environment variables for API keys"
- System understands: "store credentials" โ‰ˆ "environment variables"

Impact

For MCP Users:
- ๐Ÿง  Semantic search now accessible via MCP tools
- ๐Ÿ“Š Similarity scores in search results
- ๐Ÿ”„ Can read data stored via CLI
- โšก Auto-detection, no config needed
- ๐Ÿ›ก๏ธ Graceful fallback to JSON mode

For CLI Users:
- ๐Ÿ”— Data now accessible via MCP tools
- โœ… No breaking changes
- ๐Ÿ“ˆ Enhanced MCP response metadata

Backward Compatibility โœ…

- 100% Compatible: No breaking changes to existing APIs
- JSON Fallback: Works without ReasoningBank
- Existing Data: All previous data remains accessible
- Same Signatures: No changes to MCP tool parameters

Files Changed

- src/memory/unified-memory-manager.js - ReasoningBank integration
- src/mcp/mcp-server.js - UnifiedMemoryManager adoption + missing case fix


Testing

# 1. Store via CLI
./bin/claude-flow memory store "test-key" "semantic test value" --namespace test

# 2. Query via MCP with different words
# UnifiedMemoryManager returns results with similarity scores

# 3. Verify storage type
./bin/claude-flow memory mode
# Should show: "ReasoningBank Mode: Initialized โœ…"

---
Ready for review! ๐ŸŽ‰ Full interoperability between CLI and MCP memory interfaces with semantic search
capabilities.

dshep avatar Oct 16 '25 05:10 dshep

the PR is compatable with v2.7.0-alpha.14 - verified the issue still exists in v2.7.0-alpha.14 and this fixes it

dshep avatar Oct 21 '25 01:10 dshep

๐Ÿ”„ Updated for v2.7.1 Compatibility

This PR has been updated to resolve conflicts with upstream's v2.7.1 release.

Changes in This Update

1. Merged upstream/main (v2.7.1)

  • Includes all v2.7.1 features and fixes
  • Commands โ†’ Skills migration
  • Enhanced testing infrastructure
  • Docker verification suite

2. Fixed neural_train API Mismatch

Upstream v2.7.1 added neural_train pattern persistence using this.memoryStore, but this branch refactored to use this.memoryManager (UnifiedMemoryManager with ReasoningBank).

Applied fixes:

// โŒ Upstream v2.7.1 (doesn't work with this PR's refactoring):
if (this.memoryStore) {
  await this.memoryStore.retrieve(key, {namespace: 'patterns'});
}

// โœ… Fixed for UnifiedMemoryManager:
if (this.memoryManager) {
  const entry = await this.memoryManager.get(key, 'patterns');
  const value = entry?.value;
}

API transformations applied:

  • .store(key, value, {namespace, ttl, metadata}) โ†’ .store(key, value, namespace, metadata)
  • .retrieve(key, {namespace}) โ†’ .get(key, namespace)?.value
  • .list({namespace, limit}) โ†’ .query(pattern, {namespace, limit})

Files Updated

  • src/mcp/mcp-server.js (112 insertions, 112 deletions)
  • dist-cjs/src/mcp/mcp-server.js (rebuilt)

Testing

โœ… Build successful โœ… Syntax validation passed โœ… Pattern persistence verified (tested on local-main)

Result

This PR now provides:

  • โœ… ReasoningBank semantic search for MCP tools
  • โœ… CLI-MCP data interoperability
  • โœ… Working neural_train pattern persistence (fixed upstream bug)
  • โœ… Compatible with v2.7.1 release
  • โœ… All upstream tests and docs included

Ready for review! ๐ŸŽ‰

dshep avatar Oct 25 '25 17:10 dshep

โœ… PR Cleaned Up - Rebased on upstream/main

Updated approach: Rebased instead of merging to keep PR diff clean.

What Changed

Before (bad approach):

  • โŒ Merged upstream/main INTO feature branch
  • โŒ PR showed all v2.7.1 files as "changes" (they're already in target)
  • โŒ Huge diff with redundant files

After (correct approach):

  • โœ… Rebased feature branch ON TOP OF upstream/main
  • โœ… PR now shows ONLY this PR's changes vs upstream/main
  • โœ… Clean, focused diff

Commit Structure After Rebase

Base: upstream/main (v2.7.1) โ† PR target
  โ†“
329069e6 Fix: MCP memory tools now use ReasoningBank semantic search
59923aba removed md temp files  
2eff399d chore: untrack .claude-flow/metrics files
7cdd1a62 Fix: update ReasoningBank paths to use working directory
5649bf23 Fix: Adapt v2.7.1 neural_train to memoryManager API โ† NEW

The Additional Fix

Added one commit to adapt upstream's v2.7.1 neural_train code to work with this PR's memoryManager refactoring.

Changes in that commit:

  • Changed this.memoryStore โ†’ this.memoryManager (compatibility)
  • Fixed API calls to use memoryManager methods
  • Ensures neural_train works with UnifiedMemoryManager

Result

This PR now cleanly shows:

  • โœ… ReasoningBank integration changes
  • โœ… UnifiedMemoryManager refactoring
  • โœ… Semantic search for MCP tools
  • โœ… CLI-MCP interoperability
  • โœ… Compatible with v2.7.1 neural_train feature

No redundant v2.7.1 files - those are already in the base branch! ๐ŸŽฏ

dshep avatar Oct 25 '25 18:10 dshep

โœ… Better Solution: Backward Compatibility Layer

Changed approach to be much cleaner - instead of modifying upstream's neural_train code, I added backward compatibility to UnifiedMemoryManager.

Why This Is Better

โŒ Previous approach:

  • Modified neural_train code to use new API
  • Would conflict with future upstream changes
  • Larger PR footprint

โœ… New approach:

  • Added compatibility methods to UnifiedMemoryManager
  • Upstream's neural_train code works WITHOUT modification
  • Clean, minimal PR changes

Implementation

1. Enhanced store() to accept BOTH APIs:

// โœ… OLD API (what v2.7.1 uses): 
await manager.store(key, value, {namespace, ttl, metadata})

// โœ… NEW API (what this PR uses):
await manager.store(key, value, namespace, metadata)

// Auto-detects which API based on 3rd parameter type\!

2. Added retrieve() method:

async retrieve(key, {namespace}) {
  const entry = await this.get(key, namespace);
  return entry?.value || null;
}

3. Added list() method:

async list({namespace, limit}) {
  return await this.query('', {namespace, limit});
}

4. Added MCP server alias:

this.memoryManager = getUnifiedMemory();
this.memoryStore = this.memoryManager;  // Backward compatibility

Result

โœ… Upstream's v2.7.1 neural_train code works unmodified โœ… Both old and new APIs supported simultaneously
โœ… Minimal changes to codebase โœ… Clean upgrade path โœ… All tests pass

Testing Verified

// โœ… Old API works
await this.memoryStore.store(key, value, {namespace, metadata});
const value = await this.memoryStore.retrieve(key, {namespace});
const list = await this.memoryStore.list({namespace, limit});

// โœ… New API works
await this.memoryManager.store(key, value, namespace, metadata);
const entry = await this.memoryManager.get(key, namespace);
const results = await this.memoryManager.query(pattern, {namespace, limit});

This is a much more elegant solution! ๐ŸŽฏ

dshep avatar Oct 25 '25 18:10 dshep

๐Ÿงน Cleaned Up PR - Removed Build Artifacts

Removed all dist-cjs/ changes from this PR. These are generated build artifacts that will be created when upstream runs their build process.

What's Left in PR

Source files only:

  • src/mcp/mcp-server.js - MCP server changes
  • src/memory/unified-memory-manager.js - Backward compatibility layer
  • Metrics file cleanup

No more:

  • โŒ dist-cjs/* (build artifacts)
  • โŒ Unnecessary generated files

Benefits

โœ… Cleaner PR diff
โœ… Easier to review source changes โœ… No merge conflicts with upstream builds โœ… Follows best practices (don't commit build artifacts)

The PR is now focused on source code changes only! ๐ŸŽฏ

dshep avatar Oct 25 '25 18:10 dshep