使用说明
MCP内存服务器
一种智能中间件服务器,通过实现MCP(模型上下文协议)模式为Ollama添加内存和学习功能。
概述
MCP内存服务器充当您的应用程序(如ELVIS)和Ollama之间的智能代理,自动用相关上下文丰富提示,并从每次交互中学习。
主要特点
- 自动上下文丰富:搜索大脑记忆并在提示中添加相关上下文
- 从经验中学习:跟踪哪些方法有效并随着时间的推移而改进
- 模型特定优化:了解每个模型的优势和最佳实践
- 类似任务识别:查找并应用过去类似任务的经验教训
- Olama替换品:与现有Ollama API客户端兼容
- MCP工具集成:访问Brain、文件系统和其他MCP工具
建筑
Your App (ELVIS) → MCP Memory Server → Ollama
↓
Brain Memory System
Learning Engine
Context Enricher
安装
# Clone the repository
git clone [repository-url]
cd mcp-memory-server
# Install dependencies
npm install
# Build the TypeScript code
npm run build
# Copy environment template
cp .env.example .env
# Edit .env with your settings
配置
创建一个 .env 文件包含:
# Server Configuration
PORT=8090 # Port for Memory Server
OLLAMA_URL=http://localhost:11434 # Ollama API endpoint
MCP_URL=http://localhost:3000 # MCP tools endpoint (optional)
# Memory Configuration
MAX_CONTEXT_TOKENS=2000 # Max tokens to add as context
SIMILARITY_LIMIT=10 # How many similar memories to search
RELEVANCE_THRESHOLD=0.3 # Min relevance score (0-1)
AUTO_ENRICH=true # Enable automatic enrichment
# Brain Integration
BRAIN_ENABLED=true # Enable Brain memory system
BRAIN_DATA_DIR=~/.brain # Brain data directory
# Cache Configuration
CACHE_TTL=3600 # Cache TTL in seconds
CACHE_MAX_SIZE=1000 # Max cache entries
用法
启动服务器
# Start the server
npm start
# Or with custom settings
PORT=9000 OLLAMA_URL=http://remote:11434 npm start
与ELVIS一起使用
只需将ELVIS指向内存服务器,而不是Ollama:
// Before (direct to Ollama)
const elvis = new ELVIS({
ollamaUrl: 'http://localhost:11434'
});
// After (through Memory Server)
const elvis = new ELVIS({
ollamaUrl: 'http://localhost:8090' // Memory Server port
});
API终点
服务器提供Ollama兼容的端点以及额外的内存端点:
Ollama兼容端点
POST /api/generate-生成文本(具有自动丰富功能)POST /api/generate/stream-流媒体生成GET /api/tags-列出可用型号
内存管理端点
GET /api/memory/stats-获取内存统计信息POST /api/memory/search-搜索记忆GET /api/memory/insights-获取最新见解
学习终点
POST /api/learning/feedback-对回复提供反馈GET /api/learning/model-stats/:model-获取模型性能统计数据
运作原理
1.丰富上下文
当请求进来时,服务器:
- 从提示中提取关键字
- 在大脑中搜索相关记忆
- 查找类似的过去任务
- 添加特定于型号的提示
- 包括最近的见解
- 构建一个包含所有上下文的丰富提示
2.学习过程
每次响应后,服务器:
- 评估响应质量
- 确定所使用的方法
- 提取见解和模式
- 存储成功模式
- 更新模型性能指标
3.存储器类型
服务器跟踪几种类型的内存:
- 任务记忆:完整记录过去的任务和成果
- 模型上下文:每个模型的性能统计数据和最佳实践
- 洞察:学到的模式和成功的方法
- 领域知识:特定主题信息
示例丰富
原始提示:
Analyze the performance bottlenecks in our Brain memory system
丰富提示(自动生成):
=== MODEL GUIDANCE ===
You are deepseek-r1, with these strengths:
- deep analysis
- complex reasoning
- step-by-step thinking
Tips for best results:
- Use "think step by step" in prompts
- Excellent for mathematical proofs
=== RELEVANT CONTEXT ===
Context 1 (relevance: 89%):
Previous Brain performance analysis showed query optimization...
Context 2 (relevance: 76%):
Memory indexing strategies that improved recall speed by 40%...
=== SIMILAR PAST TASKS ===
Task 1: Analyze todo-manager performance issues
Approach: Profiling-driven analysis
Quality: 92%
Duration: 38 minutes
Key learning: Identifying hotspots first saved significant time
=== RECENT INSIGHTS ===
1. Using profiler data improves analysis accuracy
2. Database queries are often the bottleneck
3. Caching strategies significantly impact performance
=== CURRENT TASK ===
Analyze the performance bottlenecks in our Brain memory system
发展
项目结构
src/
├── types.ts # TypeScript interfaces
├── server.ts # Express server setup
├── MemoryManager.ts # Memory storage and retrieval
├── ContextEnricher.ts # Prompt enrichment logic
├── LearningEngine.ts # Learning from interactions
├── clients/
│ ├── BrainClient.ts # Brain memory integration
│ └── OllamaClient.ts # Ollama API client
└── index.ts # Entry point
运行测试
npm test # Run all tests
npm run test:watch # Watch mode
npm run test:coverage # With coverage
建筑
npm run build # Build TypeScript
npm run dev # Watch mode
高级用法
自定义内存源
您可以通过实现自定义内存提供程序来扩展内存系统:
class CustomMemoryProvider {
async search(query: string): Promise<Memory[]> {
// Your custom search logic
}
}
型号特定配置
在代码中添加特定于模型的配置:
modelStats.set('your-model', {
strengths: ['domain expertise'],
avgResponseTime: 20 * 60 * 1000,
tips: ['Works best with examples']
});
监控和指标
服务器记录所有交互以供分析:
- 请求/响应时间
- 内存命中率
- 模型性能趋势
- 质量评估
故障排除
常见问题
-
Ollama连接失败
- 确保Olama正在运行:
curl http://localhost:11434 - 检查.env中的OLLAMA_URL
- 确保Olama正在运行:
-
大脑不可用
- 服务器在没有Brain的情况下工作,但内存有限
- 检查MCP_URL配置
-
内存使用率高
- 调整CACHE_MAX_SIZE
- 执行缓存清理
-
缓慢的充实
- 减少相似性_限制
- 增加相关性_ HRESHOLD
未来的增强功能
- []语义搜索的矢量数据库集成
- []用于内存管理的Web UI
- []具有独立记忆的多用户支持
- []自定义富集器插件系统
- []指标仪表板
- []内存导出/导入
- []浓缩战略A/B测试
贡献
这是MCP中间件模式的概念验证。欢迎投稿!
主要贡献领域:
- 更好的学习算法
- 更复杂的上下文选择
- 其他内存提供程序
- 性能优化
- 测试基础设施
许可证
麻省理工学院
由MikeyBeez&Claude以好奇心打造🧠✨