layered-memory-mcp 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- layered_memory_mcp-0.1.0/.gitignore +32 -0
- layered_memory_mcp-0.1.0/LICENSE +21 -0
- layered_memory_mcp-0.1.0/PKG-INFO +296 -0
- layered_memory_mcp-0.1.0/README.md +273 -0
- layered_memory_mcp-0.1.0/examples/README.md +29 -0
- layered_memory_mcp-0.1.0/examples/development.md +17 -0
- layered_memory_mcp-0.1.0/examples/infrastructure.md +14 -0
- layered_memory_mcp-0.1.0/pyproject.toml +40 -0
- layered_memory_mcp-0.1.0/src/layered_memory_mcp/__init__.py +20 -0
- layered_memory_mcp-0.1.0/src/layered_memory_mcp/config.py +66 -0
- layered_memory_mcp-0.1.0/src/layered_memory_mcp/recall.py +129 -0
- layered_memory_mcp-0.1.0/src/layered_memory_mcp/server.py +411 -0
- layered_memory_mcp-0.1.0/src/layered_memory_mcp/session_scanner.py +129 -0
- layered_memory_mcp-0.1.0/tests/test_core.py +129 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Byte-compiled / optimized
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# Distribution
|
|
7
|
+
dist/
|
|
8
|
+
build/
|
|
9
|
+
*.egg-info/
|
|
10
|
+
*.egg
|
|
11
|
+
|
|
12
|
+
# Virtual environments
|
|
13
|
+
venv/
|
|
14
|
+
.venv/
|
|
15
|
+
|
|
16
|
+
# IDE
|
|
17
|
+
.vscode/
|
|
18
|
+
.idea/
|
|
19
|
+
*.swp
|
|
20
|
+
|
|
21
|
+
# OS
|
|
22
|
+
.DS_Store
|
|
23
|
+
Thumbs.db
|
|
24
|
+
|
|
25
|
+
# Testing
|
|
26
|
+
.pytest_cache/
|
|
27
|
+
.coverage
|
|
28
|
+
htmlcov/
|
|
29
|
+
|
|
30
|
+
# Personal data (never commit)
|
|
31
|
+
.knowledge/
|
|
32
|
+
sessions/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LAIguapi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: layered-memory-mcp
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Layered Memory MCP Server — Extend AI agent memory beyond token limits with a 4-tier knowledge architecture
|
|
5
|
+
Author: LAIguapi
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: ai-agent,knowledge-management,layered-memory,mcp,memory
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
17
|
+
Requires-Python: >=3.10
|
|
18
|
+
Requires-Dist: fastmcp>=2.0.0
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
21
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# Layered Memory MCP Server
|
|
25
|
+
|
|
26
|
+
> Extend AI agent memory beyond token limits with a 4-tier knowledge architecture.
|
|
27
|
+
|
|
28
|
+
[](https://modelcontextprotocol.io)
|
|
29
|
+
[](https://python.org)
|
|
30
|
+
[](LICENSE)
|
|
31
|
+
|
|
32
|
+
## The Problem
|
|
33
|
+
|
|
34
|
+
AI agents have **limited memory** — typically 2-4KB of persistent context injected every turn. Once it's full, the agent forgets everything else. You can't store project configurations, user preferences, API conventions, or domain knowledge without constantly fighting the space limit.
|
|
35
|
+
|
|
36
|
+
## The Solution
|
|
37
|
+
|
|
38
|
+
**Layered Memory** organizes knowledge into 4 tiers, trading immediacy for capacity:
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
┌─────────────────────────────────────────────────────┐
|
|
42
|
+
│ L0 — Index Layer (2-4KB, injected every turn) │
|
|
43
|
+
│ Pure pointers: "what knowledge exists and where" │
|
|
44
|
+
├─────────────────────────────────────────────────────┤
|
|
45
|
+
│ L1 — Knowledge Files (unlimited, loaded on-demand) │
|
|
46
|
+
│ Structured markdown: configs, conventions, facts │
|
|
47
|
+
├─────────────────────────────────────────────────────┤
|
|
48
|
+
│ L2 — Skills Layer (loaded when needed) │
|
|
49
|
+
│ Procedures, workflows, tool-specific knowledge │
|
|
50
|
+
├─────────────────────────────────────────────────────┤
|
|
51
|
+
│ L3 — Raw Sessions (searched rarely) │
|
|
52
|
+
│ Full conversation history, searchable by keyword │
|
|
53
|
+
└─────────────────────────────────────────────────────┘
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**L0 is your table of contents. L1 is your bookshelf. L2 is your cookbook. L3 is your diary.**
|
|
57
|
+
|
|
58
|
+
## Features
|
|
59
|
+
|
|
60
|
+
- **Keyword Search** — Find relevant knowledge across all L1 files with relevance scoring
|
|
61
|
+
- **Session Scanning** — Extract knowledge candidates from recent agent sessions
|
|
62
|
+
- **Space Analytics** — Monitor memory usage and get optimization suggestions
|
|
63
|
+
- **Agent Agnostic** — Works with any MCP-compatible agent (Hermes, Claude, Cursor, etc.)
|
|
64
|
+
- **Zero Dependencies** — Core engine uses only Python stdlib; only `fastmcp` for MCP transport
|
|
65
|
+
- **Privacy First** — All data stays local, no external API calls
|
|
66
|
+
|
|
67
|
+
## Quick Start
|
|
68
|
+
|
|
69
|
+
### Install
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install layered-memory-mcp
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Hermes Agent
|
|
76
|
+
|
|
77
|
+
Add to `~/.hermes/config.yaml`:
|
|
78
|
+
|
|
79
|
+
```yaml
|
|
80
|
+
mcp_servers:
|
|
81
|
+
layered-memory:
|
|
82
|
+
command: layered-memory-mcp
|
|
83
|
+
timeout: 30
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### OpenClaw
|
|
87
|
+
|
|
88
|
+
Install the MCP server, then register it:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pip install layered-memory-mcp
|
|
92
|
+
|
|
93
|
+
# Register as an MCP server
|
|
94
|
+
openclaw mcp set layered-memory --command layered-memory-mcp
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Layered Memory complements OpenClaw's built-in vector-based memory:
|
|
98
|
+
- **OpenClaw memory**: semantic search over session transcripts (heavy, needs embeddings)
|
|
99
|
+
- **Layered Memory**: structured keyword search over curated knowledge files (light, instant)
|
|
100
|
+
- Use both: OpenClaw for "what did I say about X?" and Layered Memory for "what's the database connection string?"
|
|
101
|
+
|
|
102
|
+
### Claude Desktop
|
|
103
|
+
|
|
104
|
+
Add to your Claude Desktop MCP config:
|
|
105
|
+
|
|
106
|
+
```json
|
|
107
|
+
{
|
|
108
|
+
"mcpServers": {
|
|
109
|
+
"layered-memory": {
|
|
110
|
+
"command": "layered-memory-mcp"
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Cursor / Other MCP Clients
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
# stdio mode (default)
|
|
120
|
+
layered-memory-mcp
|
|
121
|
+
|
|
122
|
+
# HTTP mode
|
|
123
|
+
layered-memory-mcp --transport http --port 8080
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Environment Variables
|
|
127
|
+
|
|
128
|
+
| Variable | Description | Default |
|
|
129
|
+
|----------|-------------|---------|
|
|
130
|
+
| `LAYERED_MEMORY_HOME` | Root directory for memory data | `~/.layered-memory/` |
|
|
131
|
+
| `LAYERED_MEMORY_SESSIONS_DIR` | Agent sessions directory (auto-detected) | `~/.hermes/sessions/` |
|
|
132
|
+
|
|
133
|
+
## Usage
|
|
134
|
+
|
|
135
|
+
### 1. Initialize Knowledge Base
|
|
136
|
+
|
|
137
|
+
Create markdown files in `~/.layered-memory/knowledge/`:
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
mkdir -p ~/.layered-memory/knowledge
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Create your first knowledge file:
|
|
144
|
+
|
|
145
|
+
```markdown
|
|
146
|
+
<!-- ~/.layered-memory/knowledge/infrastructure.md -->
|
|
147
|
+
## Server Configuration
|
|
148
|
+
- Production server: prod.example.com (port 22)
|
|
149
|
+
- Staging server: stage.example.com
|
|
150
|
+
- Deploy via: `./deploy.sh --env production`
|
|
151
|
+
|
|
152
|
+
## Database
|
|
153
|
+
- PostgreSQL 15 on prod-db:5432
|
|
154
|
+
- Connection pool: 20 max connections
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### 2. Build L0 Index
|
|
158
|
+
|
|
159
|
+
In your agent's persistent memory (the 2-4KB injected every turn), store only pointers:
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
[L0] infrastructure: server config, DB, deploy → knowledge/infrastructure.md
|
|
163
|
+
[L0] api-conventions: REST patterns, auth, errors → knowledge/api-conventions.md
|
|
164
|
+
[L0] user-prefs: coding style, tool preferences → knowledge/user-prefs.md
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### 3. Search Knowledge (MCP Tool)
|
|
168
|
+
|
|
169
|
+
The agent calls `recall_knowledge` when it needs details:
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
Agent: "What's the database connection string?"
|
|
173
|
+
→ recall_knowledge(keyword="database")
|
|
174
|
+
← Returns relevant sections from infrastructure.md
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 4. Session Compression (Cron Job)
|
|
178
|
+
|
|
179
|
+
Set up a daily cron to extract new knowledge from conversations:
|
|
180
|
+
|
|
181
|
+
```
|
|
182
|
+
1. scan_recent_sessions → get session summaries
|
|
183
|
+
2. AI analyzes summaries → identifies stable facts
|
|
184
|
+
3. New facts → written to L1 knowledge files
|
|
185
|
+
4. L0 index → updated with new pointers
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## MCP Tools
|
|
189
|
+
|
|
190
|
+
| Tool | Description |
|
|
191
|
+
|------|-------------|
|
|
192
|
+
| `recall_knowledge` | Search L1 knowledge files by keyword |
|
|
193
|
+
| `scan_recent_sessions` | Scan recent sessions for knowledge candidates |
|
|
194
|
+
| `get_knowledge_file` | Read a specific knowledge file |
|
|
195
|
+
| `list_memory_stats` | Get space statistics and optimization suggestions |
|
|
196
|
+
| `search_sessions_by_keyword` | Search session history for a keyword |
|
|
197
|
+
|
|
198
|
+
## MCP Resources
|
|
199
|
+
|
|
200
|
+
| Resource | Description |
|
|
201
|
+
|----------|-------------|
|
|
202
|
+
| `memory://status` | Overall system status and configuration |
|
|
203
|
+
| `knowledge://files` | List all knowledge files with metadata |
|
|
204
|
+
|
|
205
|
+
## MCP Prompts
|
|
206
|
+
|
|
207
|
+
| Prompt | Description |
|
|
208
|
+
|--------|-------------|
|
|
209
|
+
| `knowledge_compression_prompt` | Template for AI-driven knowledge extraction from sessions |
|
|
210
|
+
|
|
211
|
+
## Architecture Deep Dive
|
|
212
|
+
|
|
213
|
+
### Why 4 Tiers?
|
|
214
|
+
|
|
215
|
+
| Tier | Cost | Capacity | Use Case |
|
|
216
|
+
|------|------|----------|----------|
|
|
217
|
+
| L0 (Index) | Tokens per turn | ~2KB | Quick lookup table |
|
|
218
|
+
| L1 (Knowledge) | 1 file read | Unlimited | Structured facts |
|
|
219
|
+
| L2 (Skills) | 1 skill load | Unlimited | Procedures |
|
|
220
|
+
| L3 (Sessions) | Full search | Unlimited | Historical recall |
|
|
221
|
+
|
|
222
|
+
### Relevance Scoring
|
|
223
|
+
|
|
224
|
+
When you call `recall_knowledge`, files are scored by:
|
|
225
|
+
|
|
226
|
+
1. **Filename match** (+10 points) — keyword appears in filename
|
|
227
|
+
2. **Heading match** (+3 points) — keyword appears in a `## heading`
|
|
228
|
+
3. **Content frequency** (+0.5 per occurrence, capped at 5) — how often keyword appears
|
|
229
|
+
|
|
230
|
+
Results are sorted by score, and only matching `## sections` are returned (not entire files).
|
|
231
|
+
|
|
232
|
+
### Session Compression
|
|
233
|
+
|
|
234
|
+
The `scan_recent_sessions` tool is designed for cron-job automation:
|
|
235
|
+
|
|
236
|
+
1. It scans session files from the past N days
|
|
237
|
+
2. Extracts user messages, assistant topics, and tool calls
|
|
238
|
+
3. Returns a structured JSON for an AI to analyze
|
|
239
|
+
4. The AI identifies stable knowledge and writes it to L1 files
|
|
240
|
+
|
|
241
|
+
This creates a **self-improving memory system** — the agent gets smarter over time as more knowledge is distilled from conversations.
|
|
242
|
+
|
|
243
|
+
## Agent Compatibility
|
|
244
|
+
|
|
245
|
+
Layered Memory is an MCP server — it works with any MCP-compatible agent.
|
|
246
|
+
|
|
247
|
+
| Agent | Config Method | Notes |
|
|
248
|
+
|-------|--------------|-------|
|
|
249
|
+
| **Hermes Agent** | `config.yaml` → `mcp_servers` | Native MCP client, zero config |
|
|
250
|
+
| **OpenClaw** | `openclaw mcp set` | Complements built-in vector memory |
|
|
251
|
+
| **Claude Desktop** | `claude_desktop_config.json` | Full MCP support |
|
|
252
|
+
| **Cursor** | Settings → MCP | Full MCP support |
|
|
253
|
+
| **Codex CLI** | Codex MCP config | Full MCP support |
|
|
254
|
+
| **Any MCP client** | stdio or HTTP transport | Standard MCP protocol |
|
|
255
|
+
|
|
256
|
+
### When to use Layered Memory vs. built-in memory
|
|
257
|
+
|
|
258
|
+
Most agents have **limited persistent memory** (2-4KB per turn). Layered Memory solves this by:
|
|
259
|
+
|
|
260
|
+
1. **Separating index from content** — L0 stays small (fits in agent memory), L1 holds unlimited knowledge
|
|
261
|
+
2. **On-demand loading** — the agent only reads what it needs, when it needs it
|
|
262
|
+
3. **Self-improving** — session compression automatically extracts new knowledge over time
|
|
263
|
+
|
|
264
|
+
### Integration patterns
|
|
265
|
+
|
|
266
|
+
```
|
|
267
|
+
Agent (2KB memory limit)
|
|
268
|
+
└── L0 index (injected every turn, ~500 bytes)
|
|
269
|
+
├── [L0] infrastructure: servers, DB → knowledge/infrastructure.md
|
|
270
|
+
├── [L0] api: REST conventions → knowledge/api-conventions.md
|
|
271
|
+
└── [L0] dev: code style, testing → knowledge/development.md
|
|
272
|
+
│
|
|
273
|
+
↓ (on demand via recall_knowledge)
|
|
274
|
+
L1 knowledge files (unlimited, loaded by keyword)
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Development
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
# Clone
|
|
281
|
+
git clone https://github.com/LAIguapi/layered-memory-mcp.git
|
|
282
|
+
cd layered-memory-mcp
|
|
283
|
+
|
|
284
|
+
# Install in dev mode
|
|
285
|
+
pip install -e ".[dev]"
|
|
286
|
+
|
|
287
|
+
# Run tests
|
|
288
|
+
pytest
|
|
289
|
+
|
|
290
|
+
# Run locally
|
|
291
|
+
python -m layered_memory_mcp.server
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
## License
|
|
295
|
+
|
|
296
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
# Layered Memory MCP Server
|
|
2
|
+
|
|
3
|
+
> Extend AI agent memory beyond token limits with a 4-tier knowledge architecture.
|
|
4
|
+
|
|
5
|
+
[](https://modelcontextprotocol.io)
|
|
6
|
+
[](https://python.org)
|
|
7
|
+
[](LICENSE)
|
|
8
|
+
|
|
9
|
+
## The Problem
|
|
10
|
+
|
|
11
|
+
AI agents have **limited memory** — typically 2-4KB of persistent context injected every turn. Once it's full, the agent forgets everything else. You can't store project configurations, user preferences, API conventions, or domain knowledge without constantly fighting the space limit.
|
|
12
|
+
|
|
13
|
+
## The Solution
|
|
14
|
+
|
|
15
|
+
**Layered Memory** organizes knowledge into 4 tiers, trading immediacy for capacity:
|
|
16
|
+
|
|
17
|
+
```
|
|
18
|
+
┌─────────────────────────────────────────────────────┐
|
|
19
|
+
│ L0 — Index Layer (2-4KB, injected every turn) │
|
|
20
|
+
│ Pure pointers: "what knowledge exists and where" │
|
|
21
|
+
├─────────────────────────────────────────────────────┤
|
|
22
|
+
│ L1 — Knowledge Files (unlimited, loaded on-demand) │
|
|
23
|
+
│ Structured markdown: configs, conventions, facts │
|
|
24
|
+
├─────────────────────────────────────────────────────┤
|
|
25
|
+
│ L2 — Skills Layer (loaded when needed) │
|
|
26
|
+
│ Procedures, workflows, tool-specific knowledge │
|
|
27
|
+
├─────────────────────────────────────────────────────┤
|
|
28
|
+
│ L3 — Raw Sessions (searched rarely) │
|
|
29
|
+
│ Full conversation history, searchable by keyword │
|
|
30
|
+
└─────────────────────────────────────────────────────┘
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**L0 is your table of contents. L1 is your bookshelf. L2 is your cookbook. L3 is your diary.**
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Keyword Search** — Find relevant knowledge across all L1 files with relevance scoring
|
|
38
|
+
- **Session Scanning** — Extract knowledge candidates from recent agent sessions
|
|
39
|
+
- **Space Analytics** — Monitor memory usage and get optimization suggestions
|
|
40
|
+
- **Agent Agnostic** — Works with any MCP-compatible agent (Hermes, Claude, Cursor, etc.)
|
|
41
|
+
- **Zero Dependencies** — Core engine uses only Python stdlib; only `fastmcp` for MCP transport
|
|
42
|
+
- **Privacy First** — All data stays local, no external API calls
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
### Install
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install layered-memory-mcp
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Hermes Agent
|
|
53
|
+
|
|
54
|
+
Add to `~/.hermes/config.yaml`:
|
|
55
|
+
|
|
56
|
+
```yaml
|
|
57
|
+
mcp_servers:
|
|
58
|
+
layered-memory:
|
|
59
|
+
command: layered-memory-mcp
|
|
60
|
+
timeout: 30
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### OpenClaw
|
|
64
|
+
|
|
65
|
+
Install the MCP server, then register it:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install layered-memory-mcp
|
|
69
|
+
|
|
70
|
+
# Register as an MCP server
|
|
71
|
+
openclaw mcp set layered-memory --command layered-memory-mcp
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Layered Memory complements OpenClaw's built-in vector-based memory:
|
|
75
|
+
- **OpenClaw memory**: semantic search over session transcripts (heavy, needs embeddings)
|
|
76
|
+
- **Layered Memory**: structured keyword search over curated knowledge files (light, instant)
|
|
77
|
+
- Use both: OpenClaw for "what did I say about X?" and Layered Memory for "what's the database connection string?"
|
|
78
|
+
|
|
79
|
+
### Claude Desktop
|
|
80
|
+
|
|
81
|
+
Add to your Claude Desktop MCP config:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"mcpServers": {
|
|
86
|
+
"layered-memory": {
|
|
87
|
+
"command": "layered-memory-mcp"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Cursor / Other MCP Clients
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# stdio mode (default)
|
|
97
|
+
layered-memory-mcp
|
|
98
|
+
|
|
99
|
+
# HTTP mode
|
|
100
|
+
layered-memory-mcp --transport http --port 8080
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Environment Variables
|
|
104
|
+
|
|
105
|
+
| Variable | Description | Default |
|
|
106
|
+
|----------|-------------|---------|
|
|
107
|
+
| `LAYERED_MEMORY_HOME` | Root directory for memory data | `~/.layered-memory/` |
|
|
108
|
+
| `LAYERED_MEMORY_SESSIONS_DIR` | Agent sessions directory (auto-detected) | `~/.hermes/sessions/` |
|
|
109
|
+
|
|
110
|
+
## Usage
|
|
111
|
+
|
|
112
|
+
### 1. Initialize Knowledge Base
|
|
113
|
+
|
|
114
|
+
Create markdown files in `~/.layered-memory/knowledge/`:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
mkdir -p ~/.layered-memory/knowledge
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Create your first knowledge file:
|
|
121
|
+
|
|
122
|
+
```markdown
|
|
123
|
+
<!-- ~/.layered-memory/knowledge/infrastructure.md -->
|
|
124
|
+
## Server Configuration
|
|
125
|
+
- Production server: prod.example.com (port 22)
|
|
126
|
+
- Staging server: stage.example.com
|
|
127
|
+
- Deploy via: `./deploy.sh --env production`
|
|
128
|
+
|
|
129
|
+
## Database
|
|
130
|
+
- PostgreSQL 15 on prod-db:5432
|
|
131
|
+
- Connection pool: 20 max connections
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 2. Build L0 Index
|
|
135
|
+
|
|
136
|
+
In your agent's persistent memory (the 2-4KB injected every turn), store only pointers:
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
[L0] infrastructure: server config, DB, deploy → knowledge/infrastructure.md
|
|
140
|
+
[L0] api-conventions: REST patterns, auth, errors → knowledge/api-conventions.md
|
|
141
|
+
[L0] user-prefs: coding style, tool preferences → knowledge/user-prefs.md
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### 3. Search Knowledge (MCP Tool)
|
|
145
|
+
|
|
146
|
+
The agent calls `recall_knowledge` when it needs details:
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
Agent: "What's the database connection string?"
|
|
150
|
+
→ recall_knowledge(keyword="database")
|
|
151
|
+
← Returns relevant sections from infrastructure.md
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### 4. Session Compression (Cron Job)
|
|
155
|
+
|
|
156
|
+
Set up a daily cron to extract new knowledge from conversations:
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
1. scan_recent_sessions → get session summaries
|
|
160
|
+
2. AI analyzes summaries → identifies stable facts
|
|
161
|
+
3. New facts → written to L1 knowledge files
|
|
162
|
+
4. L0 index → updated with new pointers
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## MCP Tools
|
|
166
|
+
|
|
167
|
+
| Tool | Description |
|
|
168
|
+
|------|-------------|
|
|
169
|
+
| `recall_knowledge` | Search L1 knowledge files by keyword |
|
|
170
|
+
| `scan_recent_sessions` | Scan recent sessions for knowledge candidates |
|
|
171
|
+
| `get_knowledge_file` | Read a specific knowledge file |
|
|
172
|
+
| `list_memory_stats` | Get space statistics and optimization suggestions |
|
|
173
|
+
| `search_sessions_by_keyword` | Search session history for a keyword |
|
|
174
|
+
|
|
175
|
+
## MCP Resources
|
|
176
|
+
|
|
177
|
+
| Resource | Description |
|
|
178
|
+
|----------|-------------|
|
|
179
|
+
| `memory://status` | Overall system status and configuration |
|
|
180
|
+
| `knowledge://files` | List all knowledge files with metadata |
|
|
181
|
+
|
|
182
|
+
## MCP Prompts
|
|
183
|
+
|
|
184
|
+
| Prompt | Description |
|
|
185
|
+
|--------|-------------|
|
|
186
|
+
| `knowledge_compression_prompt` | Template for AI-driven knowledge extraction from sessions |
|
|
187
|
+
|
|
188
|
+
## Architecture Deep Dive
|
|
189
|
+
|
|
190
|
+
### Why 4 Tiers?
|
|
191
|
+
|
|
192
|
+
| Tier | Cost | Capacity | Use Case |
|
|
193
|
+
|------|------|----------|----------|
|
|
194
|
+
| L0 (Index) | Tokens per turn | ~2KB | Quick lookup table |
|
|
195
|
+
| L1 (Knowledge) | 1 file read | Unlimited | Structured facts |
|
|
196
|
+
| L2 (Skills) | 1 skill load | Unlimited | Procedures |
|
|
197
|
+
| L3 (Sessions) | Full search | Unlimited | Historical recall |
|
|
198
|
+
|
|
199
|
+
### Relevance Scoring
|
|
200
|
+
|
|
201
|
+
When you call `recall_knowledge`, files are scored by:
|
|
202
|
+
|
|
203
|
+
1. **Filename match** (+10 points) — keyword appears in filename
|
|
204
|
+
2. **Heading match** (+3 points) — keyword appears in a `## heading`
|
|
205
|
+
3. **Content frequency** (+0.5 per occurrence, capped at 5) — how often keyword appears
|
|
206
|
+
|
|
207
|
+
Results are sorted by score, and only matching `## sections` are returned (not entire files).
|
|
208
|
+
|
|
209
|
+
### Session Compression
|
|
210
|
+
|
|
211
|
+
The `scan_recent_sessions` tool is designed for cron-job automation:
|
|
212
|
+
|
|
213
|
+
1. It scans session files from the past N days
|
|
214
|
+
2. Extracts user messages, assistant topics, and tool calls
|
|
215
|
+
3. Returns a structured JSON for an AI to analyze
|
|
216
|
+
4. The AI identifies stable knowledge and writes it to L1 files
|
|
217
|
+
|
|
218
|
+
This creates a **self-improving memory system** — the agent gets smarter over time as more knowledge is distilled from conversations.
|
|
219
|
+
|
|
220
|
+
## Agent Compatibility
|
|
221
|
+
|
|
222
|
+
Layered Memory is an MCP server — it works with any MCP-compatible agent.
|
|
223
|
+
|
|
224
|
+
| Agent | Config Method | Notes |
|
|
225
|
+
|-------|--------------|-------|
|
|
226
|
+
| **Hermes Agent** | `config.yaml` → `mcp_servers` | Native MCP client, zero config |
|
|
227
|
+
| **OpenClaw** | `openclaw mcp set` | Complements built-in vector memory |
|
|
228
|
+
| **Claude Desktop** | `claude_desktop_config.json` | Full MCP support |
|
|
229
|
+
| **Cursor** | Settings → MCP | Full MCP support |
|
|
230
|
+
| **Codex CLI** | Codex MCP config | Full MCP support |
|
|
231
|
+
| **Any MCP client** | stdio or HTTP transport | Standard MCP protocol |
|
|
232
|
+
|
|
233
|
+
### When to use Layered Memory vs. built-in memory
|
|
234
|
+
|
|
235
|
+
Most agents have **limited persistent memory** (2-4KB per turn). Layered Memory solves this by:
|
|
236
|
+
|
|
237
|
+
1. **Separating index from content** — L0 stays small (fits in agent memory), L1 holds unlimited knowledge
|
|
238
|
+
2. **On-demand loading** — the agent only reads what it needs, when it needs it
|
|
239
|
+
3. **Self-improving** — session compression automatically extracts new knowledge over time
|
|
240
|
+
|
|
241
|
+
### Integration patterns
|
|
242
|
+
|
|
243
|
+
```
|
|
244
|
+
Agent (2KB memory limit)
|
|
245
|
+
└── L0 index (injected every turn, ~500 bytes)
|
|
246
|
+
├── [L0] infrastructure: servers, DB → knowledge/infrastructure.md
|
|
247
|
+
├── [L0] api: REST conventions → knowledge/api-conventions.md
|
|
248
|
+
└── [L0] dev: code style, testing → knowledge/development.md
|
|
249
|
+
│
|
|
250
|
+
↓ (on demand via recall_knowledge)
|
|
251
|
+
L1 knowledge files (unlimited, loaded by keyword)
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Development
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
# Clone
|
|
258
|
+
git clone https://github.com/LAIguapi/layered-memory-mcp.git
|
|
259
|
+
cd layered-memory-mcp
|
|
260
|
+
|
|
261
|
+
# Install in dev mode
|
|
262
|
+
pip install -e ".[dev]"
|
|
263
|
+
|
|
264
|
+
# Run tests
|
|
265
|
+
pytest
|
|
266
|
+
|
|
267
|
+
# Run locally
|
|
268
|
+
python -m layered_memory_mcp.server
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT License — see [LICENSE](LICENSE) for details.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# Example Knowledge Files
|
|
2
|
+
|
|
3
|
+
This directory contains example L1 knowledge files that demonstrate
|
|
4
|
+
the layered memory system's structure.
|
|
5
|
+
|
|
6
|
+
## File Naming Convention
|
|
7
|
+
|
|
8
|
+
Use lowercase-hyphenated names that clearly indicate the domain:
|
|
9
|
+
- `infrastructure.md` — servers, databases, deployments
|
|
10
|
+
- `api-conventions.md` — REST patterns, auth, error handling
|
|
11
|
+
- `development.md` — coding standards, tools, workflows
|
|
12
|
+
- `user-preferences.md` — personal preferences, habits
|
|
13
|
+
|
|
14
|
+
## Structure
|
|
15
|
+
|
|
16
|
+
Each file should use `## Second-level Headings` to create searchable sections:
|
|
17
|
+
|
|
18
|
+
```markdown
|
|
19
|
+
## Section Title
|
|
20
|
+
- Fact 1
|
|
21
|
+
- Fact 2
|
|
22
|
+
- Configuration detail
|
|
23
|
+
|
|
24
|
+
## Another Section
|
|
25
|
+
- More facts...
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The `recall_knowledge` tool searches within `## sections`, so good
|
|
29
|
+
heading structure directly improves search accuracy.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
## Code Style
|
|
2
|
+
- Language: Python 3.12+
|
|
3
|
+
- Formatter: ruff format
|
|
4
|
+
- Type hints: required for all public functions
|
|
5
|
+
- Max line length: 120
|
|
6
|
+
|
|
7
|
+
## Testing
|
|
8
|
+
- Framework: pytest
|
|
9
|
+
- Run: `pytest tests/ -x --tb=short`
|
|
10
|
+
- Coverage target: 80%+
|
|
11
|
+
- Integration tests: marked with @pytest.mark.integration
|
|
12
|
+
|
|
13
|
+
## Git Workflow
|
|
14
|
+
- Branch naming: feat/<ticket>-<description>
|
|
15
|
+
- Commit format: conventional commits
|
|
16
|
+
- PR required for merge to main
|
|
17
|
+
- Squash merge preferred
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
## Server Configuration
|
|
2
|
+
- Production: app.example.com (SSH port 22)
|
|
3
|
+
- Staging: stage.example.com
|
|
4
|
+
- Deploy command: `./deploy.sh --env <stage|prod>`
|
|
5
|
+
|
|
6
|
+
## Database
|
|
7
|
+
- PostgreSQL 16 on db.example.com:5432
|
|
8
|
+
- Connection pool: 20 max connections
|
|
9
|
+
- Backup schedule: daily at 03:00 UTC
|
|
10
|
+
|
|
11
|
+
## Monitoring
|
|
12
|
+
- Grafana: monitoring.example.com:3000
|
|
13
|
+
- Alert channel: #alerts on Slack
|
|
14
|
+
- Log retention: 30 days
|