brain-mcp

brain-mcp

Provides intelligent conversation memory storage using DuckDB, with support for multiple storage backends and automatic memory consolidation.

Category
访问服务器

README

Brain MCP - Memory Store MCP Server

A Model Context Protocol (MCP) server that provides intelligent conversation memory storage using DuckDB. This server stores and retrieves the context of your last conversations with support for multiple storage backends including local files, S3, and Google Cloud Storage.

Features

  • 🧠 Smart Memory Storage: Automatically maintains context from the last 2 conversations
  • 💾 DuckDB Backend: Efficient data lake storage using DuckDB
  • ☁️ Multi-Backend Support: Store data locally or in the cloud (S3, GCS)
  • 🔄 Conversation Management: Track and retrieve multiple conversations
  • 🔍 Powerful Search: Search through memories by text, topics, or date range
  • 🌙 Automatic Memory Consolidation: Nightly worker using LLMs to consolidate short-term memories into long-term insights
  • 🤖 OpenAI-Compatible: Works with OpenAI API and compatible services (LocalAI, Ollama, etc.)
  • 🚀 Easy Integration: Standard MCP protocol for seamless integration

Installation

npm install
npm run build

Configuration

The server supports different storage backends through the STORAGE_URL environment variable:

File-based Storage (Default)

export STORAGE_URL="file://brain-memory.duckdb"

S3 Storage

export STORAGE_URL="s3://my-bucket/brain-memory"
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_REGION="us-east-1"

Google Cloud Storage

export STORAGE_URL="gcs://my-bucket/brain-memory"
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials.json"

Memory Consolidation (Optional but Recommended)

The server includes an automatic nightly worker that consolidates short-term memories into long-term insights using an LLM:

# Required for consolidation
export OPENAI_API_KEY="your-api-key"

# Optional: Use OpenAI-compatible services
export OPENAI_BASE_URL="https://api.openai.com/v1"  # Or your compatible API
export OPENAI_MODEL="gpt-5-mini"  # Default model

# Optional: Customize consolidation schedule (cron format)
export CONSOLIDATION_SCHEDULE="0 0 * * *"  # Default: midnight daily

# Optional: Disable consolidation
export ENABLE_CONSOLIDATION="false"  # Default: true

You can also copy .env.example to .env and configure your settings there.

Usage

Running the Server

npm start

Or for development with auto-rebuild:

npm run dev

Configuring with Claude Desktop

Add this to your Claude Desktop configuration file:

MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json Windows: %APPDATA%/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "brain-mcp": {
      "command": "node",
      "args": ["/path/to/brain-mcp/dist/index.js"],
      "env": {
        "STORAGE_URL": "file://brain-memory.duckdb",
        "OPENAI_API_KEY": "your-api-key-here",
        "OPENAI_MODEL": "gpt-5-mini"
      }
    }
  }
}

Available Tools

1. store_memory

Store a message in the conversation memory.

Parameters:

  • role (required): The role of the message sender - "user", "assistant", or "system"
  • content (required): The content of the message to store
  • new_conversation (optional): Whether to start a new conversation (default: false)

Example:

{
  "role": "user",
  "content": "What is the weather like today?",
  "new_conversation": false
}

2. get_memory

Retrieve stored conversation memory from the last N conversations.

Parameters:

  • conversation_count (optional): Number of recent conversations to retrieve (default: 2, max: 10)

Example:

{
  "conversation_count": 2
}

Response:

{
  "success": true,
  "conversationCount": 2,
  "currentConversationId": "conv-1234567890",
  "conversations": [
    {
      "conversationId": "conv-1234567890",
      "messageCount": 4,
      "messages": [
        {
          "role": "user",
          "content": "Hello!",
          "timestamp": "2024-01-01T12:00:00.000Z"
        }
      ]
    }
  ]
}

3. new_conversation

Start a new conversation with a fresh conversation ID.

Parameters: None

Example Response:

{
  "success": true,
  "message": "New conversation started",
  "conversationId": "conv-1234567890"
}

4. get_long_term_memory

Retrieve consolidated long-term memories created by the consolidation worker.

Parameters:

  • limit (optional): Number of memories to retrieve (default: 10, max: 50)

Example Response:

{
  "success": true,
  "count": 5,
  "memories": [
    {
      "summary": "Discussion about implementing memory systems with DuckDB...",
      "topics": ["DuckDB", "Memory Systems", "MCP Servers"],
      "keyInsights": [
        "DuckDB provides efficient storage for conversation data",
        "Memory consolidation helps maintain context over time"
      ],
      "consolidatedFrom": "Conversations from 2024-01-15",
      "timestamp": "2024-01-16T00:00:00.000Z"
    }
  ]
}

5. consolidate_memory

Manually trigger memory consolidation (normally runs automatically at night).

Parameters: None

Example Response:

{
  "success": true,
  "message": "Memory consolidation completed successfully"
}

6. search_memory

Search through both short-term and long-term memories using flexible filters.

Parameters:

  • query (optional): Text to search for in conversation content, summaries, and insights
  • topics (optional): Array of topics to filter long-term memories
  • start_date (optional): Start date in ISO format (e.g., "2024-01-01T00:00:00.000Z")
  • end_date (optional): End date in ISO format (e.g., "2024-12-31T23:59:59.999Z")
  • memory_type (optional): Type of memory to search - "short-term", "long-term", or "both" (default: "both")
  • limit (optional): Maximum number of results per memory type (default: 20, max: 100)

Example Request:

{
  "query": "DuckDB",
  "topics": ["databases", "storage"],
  "memory_type": "both",
  "limit": 10
}

Example Response:

{
  "success": true,
  "filters": {
    "query": "DuckDB",
    "topics": ["databases", "storage"],
    "dateRange": "all time",
    "memoryType": "both"
  },
  "shortTermResults": {
    "count": 5,
    "conversations": [
      {
        "conversationId": "conv-1234567890",
        "role": "user",
        "content": "Tell me about DuckDB performance...",
        "timestamp": "2024-01-15T14:30:00.000Z"
      }
    ]
  },
  "longTermResults": {
    "count": 3,
    "memories": [
      {
        "summary": "Discussion about DuckDB as a storage solution...",
        "topics": ["DuckDB", "databases", "storage"],
        "keyInsights": [
          "DuckDB provides excellent analytical query performance",
          "Embedded database with no server required"
        ],
        "consolidatedFrom": "Conversations from 2024-01-14",
        "timestamp": "2024-01-15T00:00:00.000Z"
      }
    ]
  }
}

Architecture

How Memory Consolidation Works

The Brain MCP implements a two-tier memory system inspired by human memory:

  1. Short-Term Memory (Conversations Table)

    • Stores recent conversation messages
    • Fast access for context in ongoing conversations
    • Automatically cleared after consolidation
  2. Long-Term Memory (Long-Term Memory Table)

    • Stores consolidated insights and summaries
    • Created by analyzing short-term memories with an LLM
    • Persists important information indefinitely
  3. Nightly Consolidation Process

    • Scheduled worker runs at midnight (configurable)
    • Retrieves all conversations from the current day
    • Analyzes them using the configured LLM model
    • Extracts key insights, topics, and creates summaries
    • Stores the consolidation in long-term memory
    • Clears short-term memory to free up space

This approach ensures:

  • Recent context is always available
  • Important information is preserved long-term
  • Storage remains efficient
  • Knowledge is organized and searchable

Storage Backends

The server uses DuckDB as its storage engine with support for:

  1. Local File Storage: Data is stored in a local DuckDB file
  2. S3 Storage: Data is persisted to Amazon S3 using DuckDB's httpfs extension
  3. GCS Storage: Data is persisted to Google Cloud Storage using DuckDB's httpfs extension

Data Schema

Short-Term Memory (Conversations):

CREATE TABLE conversations (
  id VARCHAR PRIMARY KEY,
  timestamp BIGINT NOT NULL,
  role VARCHAR NOT NULL,
  content TEXT NOT NULL,
  conversation_id VARCHAR NOT NULL
);

Long-Term Memory:

CREATE TABLE long_term_memory (
  id VARCHAR PRIMARY KEY,
  timestamp BIGINT NOT NULL,
  summary TEXT NOT NULL,
  topics TEXT NOT NULL,          -- JSON array
  key_insights TEXT NOT NULL,    -- JSON array
  consolidated_from TEXT NOT NULL
);

Memory Management

  • The server automatically tracks conversations using unique conversation IDs
  • By default, it maintains and can retrieve the last 2 conversations
  • You can retrieve up to 10 recent conversations using the get_memory tool
  • Messages are ordered by timestamp for accurate conversation reconstruction

Development

Project Structure

brain-mcp/
├── src/
│   ├── index.ts          # Main MCP server implementation
│   ├── storage.ts        # Storage manager with DuckDB integration
│   └── consolidation.ts  # Memory consolidation worker
├── dist/                 # Compiled JavaScript (generated)
├── package.json
├── tsconfig.json
├── .env.example
└── README.md

Building

npm run build

Type Checking

The project uses TypeScript with strict type checking enabled.

Environment Variables

Variable Description Default
Storage Configuration
STORAGE_URL Storage backend URL file://brain-memory.duckdb
AWS_ACCESS_KEY_ID AWS access key (for S3) -
AWS_SECRET_ACCESS_KEY AWS secret key (for S3) -
AWS_REGION AWS region (for S3) us-east-1
GOOGLE_APPLICATION_CREDENTIALS GCS credentials path -
Consolidation Configuration
OPENAI_API_KEY OpenAI API key (required for consolidation) -
OPENAI_BASE_URL OpenAI-compatible API endpoint https://api.openai.com/v1
OPENAI_MODEL Model to use for consolidation gpt-5-mini
CONSOLIDATION_SCHEDULE Cron schedule for consolidation 0 0 * * * (midnight)
ENABLE_CONSOLIDATION Enable/disable auto-consolidation true

Use Cases

Basic Usage

Store and retrieve recent conversations for immediate context.

Long-Term Knowledge Base

Automatically build a searchable knowledge base of important insights and information from all your conversations.

Multi-User Systems

Use different storage backends for different users or contexts.

Offline-First with Cloud Sync

Use local storage for fast access, with periodic sync to cloud storage for backup and sharing.

Custom Consolidation Schedules

Run consolidation multiple times per day, weekly, or on-demand based on your needs.

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选