知识嵌入服务

Brain Server是一个基于MCP协议的知识嵌入和向量搜索服务,提供高质量的文本嵌入生成和语义搜索功能。

  • by ruca-radio
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 patrickdeluca/mcp-brain-server

使用说明

Brain Server-MCP知识嵌入服务

一个强大的MCP(模型上下文协议)服务器,用于管理知识嵌入和向量搜索。

特性

  • 矢量嵌入:为知识内容生成高质量的嵌入
  • 语义搜索:根据意义而不仅仅是关键字查找知识
  • MCP合规性:遵循AI集成的模型上下文协议
  • 大脑管理:将知识组织到特定领域的大脑中
  • 上下文感知检索:包括周围的背景,以便更好地理解
  • 进度跟踪:实时监控长时间运行的操作

嵌入模型

服务器使用嵌入模型将文本转换为矢量表示:

  • 首次运行时,服务器将自动下载嵌入模型
  • 默认情况下,它使用 Xenova/all-MiniLM-L6-v2 来自HuggingFace
  • 首次下载后,模型将在本地缓存
  • 为了测试,a MockEmbeddingProvider 可以生成随机向量

您可以在中配置要使用的模型 .env 文件:

EMBEDDING_MODEL=Xenova/all-MiniLM-L6-v2

支持的型号包括:

  • Xenova/all-MiniLM-L6-v2 (默认,384个维度)
  • Xenova/bge-small-en-v1.5 (384个维度)
  • Xenova/bge-base-en-v1.5 (768尺寸)
  • Xenova/e5-small-v2 (384个维度)

Docker快速入门

运行Brain Server最简单的方法是使用Docker和Docker Compose:

# Clone the repository
git clone https://github.com/patrickdeluca/mcp-brain-server.git
cd mcp-brain-server

# Start the server with Docker Compose
docker-compose up -d

# View logs
docker-compose logs -f

服务器将在以下时间可用http://localhost:3000MongoDB运行在同一个容器中。

在Claude Desktop中使用Docker

要将dockerized Brain Server与Claude Desktop一起使用,请更新您的 claude_desktop_config.json:

{
  "brain-server": {
    "command": "docker",
    "args": ["run", "--rm", "-p", "3000:3000", "patrickdeluca/mcp-brain-server:latest"],
    "env": {}
  }
}

本地安装的先决条件

MongoDB安装

如果你没有使用Docker,Brain Server需要MongoDB(建议使用6.0或更高版本进行矢量搜索):

现代安装(推荐)

Ubuntu/Debian
# Import public key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo tee /etc/apt/trusted.gpg.d/mongodb-6.0.asc
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
# Update package database and install MongoDB
sudo apt-get update
sudo apt-get install -y mongodb-org
# Start MongoDB service
sudo systemctl start mongod
macOS
# Using Homebrew
brew tap mongodb/brew
brew install mongodb-community@6.0
brew services start mongodb-community@6.0
视窗
  1. 从下载MongoDB 6.0安装程序 MongoDB下载中心
  2. 运行安装程序并按照安装向导进行操作
  3. 从Windows服务控制台启动MongoDB

验证MongoDB安装

要验证MongoDB是否正常运行:

mongosh --eval "db.version()"

手动安装

# Clone the repository
git clone https://github.com/patrickdeluca/mcp-brain-server.git
cd mcp-brain-server

# Install dependencies
npm install

# Configure environment 
cp .env.example .env
# Edit .env with your settings

# Build the project
npm run build

配置

使用中的环境变量配置服务器 .env 文件:

# Server Configuration
PORT=3000

# MongoDB Configuration
MONGODB_URI=mongodb://localhost:27017/brain_db

# Model Configuration
EMBEDDING_MODEL=Xenova/all-MiniLM-L6-v2
MAX_CHUNK_SIZE=1024

用法

启动服务器

# Development mode
npm run dev

# Production mode
npm start

与Claude Desktop一起使用

将大脑服务器添加到您的Claude Desktop配置中 claude_desktop_config.json 文件:

{
  "brain-server": {
    "command": "node",
    "args": ["path/to/mcp-brain-server/dist/index.js"],
    "env": {
      "MONGODB_URI": "mongodb://localhost:27017/brain_db"
    }
  }
}

与MCP检查器一起使用

要调试或测试服务器,您可以使用MCP检查器:

npx @modelcontextprotocol/inspector node dist/index.js

构建自己的Docker镜像

如果你想构建并运行自己的Docker镜像:

# Build the Docker image
docker build -t mcp-brain-server .

# Run the container
docker run -p 3000:3000 -d --name brain-server mcp-brain-server

Docker镜像包括Brain Server和MongoDB,用于自包含部署。

MCP资源

服务器公开以下MCP资源:

  • embedding_config:当前嵌入配置
  • embedding_models:可用的嵌入模型及其配置
  • service_status:嵌入服务的当前状态

MCP工具使用

服务器公开了以下MCP工具:

  • addKnowledge:向矢量数据库添加新知识
  • searchSimilar:查找语义相似的内容
  • updateKnowledge:更新现有知识条目
  • deleteKnowledge:删除知识条目
  • batchAddKnowledge:批量添加多个知识条目
  • getEmbedding:为文本内容生成嵌入

发展

项目结构

src/
├── config/          # Configuration settings
├── controllers/     # Route controllers
├── errors/          # Error definitions
├── middleware/      # Express middleware
├── models/          # Data models and types
├── services/        # Business logic
│   ├── embeddings/  # Embedding providers
│   ├── ingestion/   # Knowledge ingestion
│   ├── processing/  # Knowledge processing
│   └── storage/     # Storage services
├── tools/           # MCP tool definitions
├── types/           # TypeScript type definitions
├── utils/           # Utility functions
├── server.ts        # MCP server setup
└── index.ts         # Application entry point

工具模式示例

下面是一个使用 addKnowledge 工具:

{
  "content": "The Model Context Protocol (MCP) is a standardized interface for AI models to interact with external systems.",
  "metadata": {
    "brainId": "tech-knowledge",
    "userId": "user123",
    "source": "documentation",
    "type": "definition"
  }
}

还有 searchSimilar 工具:

{
  "query": "What is MCP?",
  "options": {
    "limit": 5,
    "minConfidence": 0.7,
    "filters": {
      "metadata.brainId": "tech-knowledge"
    }
  }
}

故障排除

Docker问题

  • 检查容器日志: docker logs brain-server
  • 确保端口正确映射: docker ps
  • 验证MongoDB是否在容器中运行: docker exec brain-server ps aux | grep mongod

MongoDB连接问题

  • 验证MongoDB是否正在运行: ps aux | grep mongod
  • 检查MongoDB日志: sudo cat /var/log/mongodb/mongod.log
  • 确保您的防火墙允许连接到MongoDB(默认端口27017)
  • 在中验证您的连接字符串 .env: MONGODB_URI=mongodb://localhost:27017/brain_db

缺少矢量索引功能

如果遇到与矢量索引功能相关的错误:

  • 确保您使用MongoDB 6.0+以获得最佳的矢量搜索支持
  • 对于较旧的MongoDB版本,服务器将回退到近似近邻搜索

可用脚本

  • npm run build:构建TypeScript项目
  • npm start:运行构建的应用程序
  • npm run dev:在开发模式下运行,热重新加载
  • npm test:运行测试
  • npm run lint:运行门楣

许可证

此项目根据ISC许可证获得许可-有关详细信息,请参阅许可证文件。

贡献

欢迎投稿!请随时提交拉取请求。