Skip to content

feat: Add comprehensive Mem0 MCP integration#2

Open
krzemienski wants to merge 1 commit into
mainfrom
feature/mem0-integration
Open

feat: Add comprehensive Mem0 MCP integration#2
krzemienski wants to merge 1 commit into
mainfrom
feature/mem0-integration

Conversation

@krzemienski

@krzemienski krzemienski commented Jun 15, 2025

Copy link
Copy Markdown
Owner

Summary

  • Integrated Mem0 MCP server as an intelligent alternative to standard memory
  • Dynamic configuration based on MEM0_API_KEY environment variable
  • Comprehensive documentation updates

Key Features

🧠 Intelligent Memory with Mem0

When MEM0_API_KEY is set, the builder uses Mem0's advanced memory system:

  • Semantic Search: Find memories using natural language instead of exact keys
  • LLM-Powered Extraction: Automatically extracts and understands important information
  • Contradiction Resolution: Updates and resolves conflicting memories automatically
  • Persistent Storage: Memories persist across sessions for better continuity
  • Context Understanding: Mem0 understands relationships between memories

🔧 Technical Implementation

  • Dynamic MCP configuration in setup_mcp_servers()
  • Memory tool name resolution with get_memory_tools() and get_memory_tool()
  • Updated init_project() and start_project() for both memory systems
  • Enhanced parse_claude_stream() to handle Mem0 tool names
  • User preferences storage with store_user_preferences()
  • Conditional memory key tracking (only for standard memory)

📖 Documentation

  • Updated CLAUDE.md with Mem0 configuration and benefits
  • Enhanced README.md with Mem0 integration examples
  • Added prerequisites for MEM0_API_KEY

Test Plan

  • Test with MEM0_API_KEY set - verifies Mem0 MCP is used
  • Test without MEM0_API_KEY - verifies fallback to standard memory
  • Run full build with Mem0 to verify semantic memory operations
  • Verify user preferences are stored and retrievable across sessions
  • Test phase memory storage and retrieval with natural language queries

Backward Compatibility

  • Fully backward compatible - falls back to standard memory when MEM0_API_KEY is not set
  • No changes required for existing users
  • Opt-in feature via environment variable

🤖 Generated with Claude Code

Summary by Sourcery

Add dynamic integration of the Mem0 MCP memory server driven by the MEM0_API_KEY environment variable, providing an LLM-powered, semantic memory fallback while preserving the existing standard memory flow.

New Features:

  • Support Mem0 as an intelligent memory provider with semantic search, LLM-powered extraction, and contradiction resolution
  • Dynamically configure MCP servers based on MEM0_API_KEY for either Mem0 or standard memory
  • Introduce store_user_preferences to persist builder settings via Mem0
  • Add get_memory_tools and get_memory_tool utilities for resolving memory tool commands

Enhancements:

  • Extend setup_mcp_servers, init_project, start_project, parse_claude_stream, and get_phase_prompt to conditionally handle Mem0 vs standard memory workflows
  • Skip legacy memory key tracking when using Mem0 and enhance logging for memory operations

Documentation:

  • Update README.md with Mem0 integration examples, new MCP server count, and semantic search usage
  • Enhance CLAUDE.md to document enabling Mem0, its benefits, and configuration steps

This commit adds intelligent memory capabilities using Mem0 MCP server as an
alternative to the standard memory server. Key improvements:

- Dynamic MCP configuration based on MEM0_API_KEY environment variable
- Automatic fallback to standard memory when Mem0 not configured
- Updated init_project and start_project functions for Mem0 compatibility
- Dynamic memory tool name resolution for both memory systems
- User preferences storage in Mem0 for cross-session continuity
- Enhanced parse_claude_stream to handle Mem0 tool names
- Comprehensive documentation updates in CLAUDE.md and README.md

Mem0 provides semantic search, intelligent extraction, and persistent
storage across sessions, significantly improving the builder's ability
to maintain context and make informed decisions throughout the build process.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@sourcery-ai

sourcery-ai Bot commented Jun 15, 2025

Copy link
Copy Markdown

Reviewer's Guide

This PR integrates the Mem0 MCP server as an optional, intelligent memory layer driven by the MEM0_API_KEY environment variable, refactors memory tool selection and prompts across build phases, adds user preference storage, enhances memory stream parsing, and updates documentation to reflect the new semantic memory capabilities.

Sequence Diagram for Conditional MCP Server Setup

sequenceDiagram
    actor User
    participant Script as builder-claude-code-builder.sh

    User->>Script: Optionally sets MEM0_API_KEY
    User->>Script: Executes script
    Script->>Script: setup_mcp_servers()
    alt MEM0_API_KEY is set
        Script->>Script: Writes .mcp.json with Mem0 MCP config
    else MEM0_API_KEY is not set
        Script->>Script: Writes .mcp.json with Standard Memory MCP config
    end
Loading

Sequence Diagram for Memory Interaction in init_project (Conditional)

sequenceDiagram
    participant Script as builder-claude-code-builder.sh
    participant Mem0_MCP as Mem0 MCP Server
    participant StdMem_MCP as Standard Memory MCP Server

    Script->>Script: init_project()
    Script->>Script: tool_name = get_memory_tool("add")
    alt MEM0_API_KEY is set (checked by get_memory_tool)
        Script-->>Script: tool_name is "mem0-mcp__add-memory"
        Script->>Mem0_MCP: Use tool_name to store project analysis
    else MEM0_API_KEY is not set
        Script-->>Script: tool_name is "memory__create_memory"
        Script->>StdMem_MCP: Use tool_name to store project analysis
    end
Loading

Sequence Diagram for Storing User Preferences with Mem0

sequenceDiagram
    participant Script as builder-claude-code-builder.sh
    participant Mem0_MCP as Mem0 MCP Server

    Script->>Script: store_user_preferences()
    opt If MEM0_API_KEY is set
        Script->>Script: tool_name = get_memory_tool("add")
        Script->>Script: run_claude_auto("STORE USER PREFERENCES...", "User Preferences")
        Script->>Mem0_MCP: Add User Preferences (using tool_name, e.g. mem0-mcp__add-memory)
    end
Loading

File-Level Changes

Change Details Files
Dynamic MCP server configuration with Mem0 fallback
  • Check MEM0_API_KEY to choose between standard memory or Mem0 MCP server
  • Assemble MEMORY_SERVER_CONFIG string accordingly
  • Generate .mcp.json file and adjust success logging based on selection
builder-claude-code-builder.sh
Introduce Mem0 memory tool resolution
  • Add get_memory_tools() to list available MCP tools
  • Add get_memory_tool() to select add/search/list/delete tool names
  • Replace hardcoded memory tool names with get_memory_tool() calls
builder-claude-code-builder.sh
Implement user preference storage via Mem0
  • Add store_user_preferences() to capture key builder settings in Mem0
  • Invoke store_user_preferences before project initialization
builder-claude-code-builder.sh
Enhance initialization and research prompts for Mem0
  • Branch init_project() and start_project() prompts to include Mem0-specific instructions and tools
  • Embed user_id in Mem0 operations and list semantic search/use patterns
  • Conditionally mark memory keys only for standard memory
builder-claude-code-builder.sh
Extend parse_claude_stream() for Mem0 tool events
  • Handle mem0-mcp__add-memory, __search-memories, __list-memories, __delete-memories actions
  • Log Mem0 tool usage and conditionally skip add_memory_key
builder-claude-code-builder.sh
Refactor phase prompt memory context for semantic memory
  • Conditionally build memory_context blocks with Mem0 instructions
  • Adjust available MCP tool listings to include Mem0 commands
  • Preserve backward compatibility for standard memory
builder-claude-code-builder.sh
Update documentation for Mem0 integration
  • Revise README.md JSON samples to include Mem0 server
  • Add Mem0 integration examples and instructions
  • Enhance CLAUDE.md with Mem0 sections and configuration guidance
README.md
CLAUDE.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @krzemienski - I've reviewed your changes - here's some feedback:

  • Validate the generated .mcp.json to ensure correct comma placement when injecting MEMORY_SERVER_CONFIG to avoid invalid JSON formatting.
  • Refactor the duplicated MEM0 vs standard-memory prompt construction into shared helper functions or templates to reduce code repetition and improve maintainability.
  • The get_memory_tool function relies on positional array indices, which is brittle—consider using an explicit mapping (e.g. associative array) for tool name resolution instead.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Validate the generated .mcp.json to ensure correct comma placement when injecting MEMORY_SERVER_CONFIG to avoid invalid JSON formatting.
- Refactor the duplicated MEM0 vs standard-memory prompt construction into shared helper functions or templates to reduce code repetition and improve maintainability.
- The get_memory_tool function relies on positional array indices, which is brittle—consider using an explicit mapping (e.g. associative array) for tool name resolution instead.

## Individual Comments

### Comment 1
<location> `builder-claude-code-builder.sh:478` </location>
<code_context>
+These preferences will help maintain consistency across sessions."
+        
+        # Run Claude to store preferences
+        run_claude_auto "$preferences_prompt" "User Preferences" >/dev/null 2>&1
+    fi
+}
</code_context>

<issue_to_address>
Avoid silencing errors when storing user preferences

Capturing and logging errors instead of redirecting output will help identify issues during preference storage.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        # Run Claude to store preferences
        run_claude_auto "$preferences_prompt" "User Preferences" >/dev/null 2>&1
=======
        # Run Claude to store preferences
        preferences_output=$(run_claude_auto "$preferences_prompt" "User Preferences" 2>&1)
        preferences_status=$?
        if [ $preferences_status -ne 0 ]; then
            log_error "Failed to store user preferences: $preferences_output"
        fi
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +477 to +478
# Run Claude to store preferences
run_claude_auto "$preferences_prompt" "User Preferences" >/dev/null 2>&1

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Avoid silencing errors when storing user preferences

Capturing and logging errors instead of redirecting output will help identify issues during preference storage.

Suggested change
# Run Claude to store preferences
run_claude_auto "$preferences_prompt" "User Preferences" >/dev/null 2>&1
# Run Claude to store preferences
preferences_output=$(run_claude_auto "$preferences_prompt" "User Preferences" 2>&1)
preferences_status=$?
if [ $preferences_status -ne 0 ]; then
log_error "Failed to store user preferences: $preferences_output"
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant